感谢主办方慷慨,这次申请到了Gravity:WIFI IoT模块进行测评,现在请您随着这篇文章一起来鸟瞰下这个模块的全景吧。
一、开箱:
,,
我们可以看到,秉承DF一贯的风格,模块封装比较好,有个明显的地方是有:UART/I2C字样的切换开关,也就是说,这样模块是支持两种连接方式的,
不过我看介绍里,还有一款只支持 UART连接的,价格是便宜的。不过要买肯定买豪华版
二、上手:
当我们拿到一个新的DF模块的时候,你首先会干什么?(当然是看看怎么用!)
这里稍微吐槽下: 我看包装上有二维码,以为是直达产品资料库的链接,不过很可惜它指向的是DF的国际版网址。
如果我们想查看使用文档,一般有两种方式:
1. 在DF商城搜这个产品名称: 可以是 TEL0126(包装外壳有带),或者 iot物联网模块,就可以找到模块。
,
点产品文档或者下拉到底,直达说明文档链接。
2. 直接在产品维库里搜TEL0126等也能找到访问链接:
,
https://wiki.dfrobot.com.cn/WiFi_IoT_Module_TEL0126 , 这里有这个模块非常详细的说明。
本文我们采用 Arduino来进行测试,为什么是Arduino有几个原因,一是它比较通用,有代码有真相,第二点主要是
当初使用Arduino的时候没有WIFI功能是一大遗憾,这里就用这个模块弥补遗憾。
按照模块说明文档里的信息,安装好相关库文件后,我们可以直接打开Arduino,进行连接使用,然后来到这里:
,
这里提供了几种不同物联的连接方法,不过Easy_IoT平台是我比较喜欢的一个平台,(这里赞一下)。
我们使用,这个平台进行连接。代码的配置信息很明显,按照参数要求填写几个这里不赘述。
,
不过有个问题要说明下:代码默认是 UART 连接的,如果使用I2C连接,需要把UART的代码注释,并开启I2C的代码,
如上图我的配置。完整代码如下:
- #include "DFRobot_WiFi_IoT_Module.h"
- #include <SoftwareSerial.h>
- //I2C mode
- DFRobot_WiFi_IoT_Module_I2C IOT;
- //UART mode
- //Use software serial port RX:10,TX:11
- //SoftwareSerial mySerial(10, 11);
- //DFRobot_WiFi_IoT_Module_UART IOT(&mySerial);
-
- //Configure WiFi name and password
- const char *WIFI_SSID = "";
- const char *WIFI_PASSWORD = "";
- //Easy IOT EN configuration
- const char *EASY_IOT_SERVER = "iot.dfrobot.com.cn";
- //Easy IOT EN configuration
- //const char *EASY_IOT_SERVER = "iot.dfrobot.com";
- const char *EASY_IOT_PORT = "1883";
- const char *EASY_IOT_ID = "";
- const char *EASY_IOT_PWD = "";
- const char *SUBSCRIBE_TOPIC = "";
- const char *PUBLISH_TOPIC = "";
- const char *EASY_IOT_SEND_MESSAGE = "Send_Message";
-
- //Set callback function
- void callback(const char*topic,const char*message){
- Serial.println("Receive [ " + (String)topic + "]," + "Message : " + (String)message);
- }
-
- void setup(void){
- //Use serial as print serial port
- Serial.begin(115200);
- //Use softwareserial myserial as communication serial port
- // mySerial.begin(9600);
-
- //Init communication port
- while(IOT.begin() != 0){
- Serial.println("init ERROR!!!!");
- delay(100);
- }
- Serial.println("init Success");
- //Connect WiFi
- while(IOT.connectWifi(WIFI_SSID, WIFI_PASSWORD) != 0){
- Serial.print(".");
- delay(100);
- }
- Serial.println("Wifi Connect Success");
- //Initialize MQTT and connect to platform
- while(IOT.MQTTBegin(EASY_IOT_SERVER, EASY_IOT_PORT, EASY_IOT_ID, EASY_IOT_PWD) != 0){
- Serial.print(".");
- delay(100);
- }
- Serial.println("MQTT Connect Success");
- //Set callback function
- IOT.setCallBack(callback);
- //Subscribe to topics
- while(IOT.subscribe(SUBSCRIBE_TOPIC) != 0){
- Serial.print(".");
- delay(100);
- }
- Serial.println("Subscribe Topic Success");
- }
-
- void loop(void){
-
- // Send data to the subscribed topic.
- if(IOT.publish(PUBLISH_TOPIC,EASY_IOT_SEND_MESSAGE) == 0){
- IOT.loop();
- }else{
- Serial.println("Data sending timeout");
- }
-
- delay(2000);
- }
复制代码
刷入程序后,打开串口,看到如下字样就表示程序成功,如果有其它问题,请参考输出对照代码解决。
模块的灯,也会有相应的变化,
不过中间,应该还有一种 粉红的灯,没有截图,就此略过。。。
第二吐槽:模块灯太亮,钛金眼受不了。。。,希望可以加个调整亮度的API。。。。
三、物联实现:
为了实现物联,我们这里使用 高精度、响应快、有防尘功能 的Gravity SHT31-F数字温湿度传感器 ,
进行室内温度读取。将 SHT31模块添加到Arduino扩展板的I2C接口上:
刷入以下代码:并观察串口监视器:
- #include "DFRobot_WiFi_IoT_Module.h"
- #include <DFRobot_SHT3x.h>
- #include <SoftwareSerial.h>
-
- DFRobot_SHT3x sht3x;
- //I2C mode
- DFRobot_WiFi_IoT_Module_I2C IOT;
- //UART mode
- //Use software serial port RX:10,TX:11
- //SoftwareSerial mySerial(10, 11);
- //DFRobot_WiFi_IoT_Module_UART IOT(&mySerial);
-
- //Configure WiFi name and password
- const char *WIFI_SSID = "";
- const char *WIFI_PASSWORD = "";
- //Easy IOT EN configuration
- const char *EASY_IOT_SERVER = "iot.dfrobot.com.cn";
- //Easy IOT EN configuration
- //const char *EASY_IOT_SERVER = "iot.dfrobot.com";
- const char *EASY_IOT_PORT = "1883";
- const char *EASY_IOT_ID = "";
- const char *EASY_IOT_PWD = "";
- const char *SUBSCRIBE_TOPIC = "";
- const char *PUBLISH_TOPIC = "";
- const char *EASY_IOT_SEND_MESSAGE = "Send_Message";
-
- //Set callback function
- void callback(const char*topic,const char*message){
- Serial.println("Receive [ " + (String)topic + "]," + "Message : " + (String)message);
- }
-
- void setup(void){
- //Use serial as print serial port
- Serial.begin(115200);
-
-
- // set sht3x
- while (sht3x.begin() != 0) {
- Serial.println("Failed to Initialize the chip, please confirm the wire connection");
- delay(1000);
- }
- Serial.print("Chip serial number:");
- Serial.println(sht3x.readSerialNumber());
-
- if(!sht3x.softReset()){
-
- Serial.println("Failed to Initialize the chip....");
-
- }
- Serial.println("------------------Read adta in single measurement mode-----------------------");
-
-
- //Use softwareserial myserial as communication serial port
- // mySerial.begin(9600);
-
- //Init communication port
- while(IOT.begin() != 0){
- Serial.println("init ERROR!!!!");
- delay(100);
- }
- Serial.println("init Success");
- //Connect WiFi
- while(IOT.connectWifi(WIFI_SSID, WIFI_PASSWORD) != 0){
- Serial.print(".");
- delay(100);
- }
- Serial.println("Wifi Connect Success");
- //Initialize MQTT and connect to platform
- while(IOT.MQTTBegin(EASY_IOT_SERVER, EASY_IOT_PORT, EASY_IOT_ID, EASY_IOT_PWD) != 0){
- Serial.print(".");
- delay(100);
- }
- Serial.println("MQTT Connect Success");
- //Set callback function
- IOT.setCallBack(callback);
- //Subscribe to topics
- while(IOT.subscribe(SUBSCRIBE_TOPIC) != 0){
- Serial.print(".");
- delay(100);
- }
- Serial.println("Subscribe Topic Success");
- }
-
- //void setup(void){
- // Serial.begin(115200);
- //while (sht3x.begin() != 0) {
- //
- // Serial.println("Failed to Initialize the chip, please confirm the wire connection");
- //
- // delay(1000);
- //
- //}
- //Serial.print("Chip serial number");
- //
- //Serial.println(sht3x.readSerialNumber());
- //
- // if(!sht3x.softReset()){
- //
- // Serial.println("Failed to Initialize the chip....");
- //
- // }
- //Serial.println("------------------Read adta in single measurement mode-----------------------");
- //
- //
- //}
-
-
- void loop(void){
- //IOT.loop();
-
- //Send data to the subscribed topic.
- // if(IOT.publish(PUBLISH_TOPIC,EASY_IOT_SEND_MESSAGE) == 0){
- // IOT.loop();
- // }else{
- // Serial.println("Data sending timeout");
- // }
-
- char Temp[10];
- dtostrf(sht3x.getTemperatureC(),1,2,Temp);
-
- Serial.print("Ambient Temperature(°C/F):");
- Serial.print(Temp);
- Serial.print(" C/");
- Serial.print("Relative Humidity(%RH):");
- Serial.print(sht3x.getHumidityRH());
- Serial.println(" %RH");
- //if(IOT.publish(PUBLISH_TOPIC,EASY_IOT_SEND_MESSAGE) == 0){
- if(IOT.publish(PUBLISH_TOPIC,Temp) == 0){
-
- IOT.loop();
- }
- delay(2000);
- }
复制代码
为了展示温度变化,我们可以用手指,轻触传感器,让温度升高,再放掉让温度降低。
,
基本的物联功能,已经实现了。你学废了吗?
下一文,准备用这个WIFI模块实现不一样的功能,尽情期待。
|