2073| 0
|
[用户分享] TinkerNode NB-IoT使用WIFI订阅、发布Easy Iot主题 |
为了让你的开发板能够在 Arduino IDE中跑起来,首先请参考下文: https://mc.dfrobot.com.cn/thread-303693-1-1.html 配置: 演示: 代码: [mw_shl_code=java,false]/*! * @file Publish_Topic.ino * * @brief After the program download is complete. * @brief You can use the BC20 module to connect to DFRobot Easy IOT cloud platform, * @brief and publish data to your Topic * * @N Easy-IoT - the simplest solution of IoT cloud platform * @n Website(CN):https://iot.dfrobot.com.cn * @n Website(EN):http://iot.dfrobot.com * * @copyright Copyright (c) 2010 DFRobot Co.Ltd (http://www.dfrobot.com) * @licence The MIT License (MIT) * @author [Wuxiao](xiao.wu@dfrobot.com) * @version V1.0 * @date 2019-10-29 * @get from https://www.dfrobot.com */ #include <WiFi.h> #include "DFRobot_Iot.h" #include <PubSubClient.h> const char* WIFI_SSID = "******"; const char* WIFI_PASSWORD = "******"; /*Configure device certificate information*/ String Iot_id = "******"; String Client_ID = "******";//自定义 String Iot_pwd = "******"; /*Configure the domain name and port number*/ String EasyIot_SERVER = "182.254.130.180"; uint16_t PORT = 1883; /*Set the Topic you need to publish to*/ const char * pubTopic = "******"; const char * subTopic = "******"; DFRobot_Iot myIot; WiFiClient espClient; PubSubClient client(espClient); void connectWiFi(){ Serial.print("Connecting to "); Serial.println(WIFI_SSID); WiFi.begin(WIFI_SSID,WIFI_PASSWORD); while(WiFi.status() != WL_CONNECTED){ delay(500); Serial.print("."); } Serial.println(); Serial.println("WiFi connected"); Serial.print("IP Adderss: "); Serial.println(WiFi.localIP()); } void callback(char * topic, byte * payload, unsigned int len){ Serial.print("Recevice ["); Serial.print(topic); Serial.print("] "); for (int i = 0; i < len; i++){ Serial.print((char)payload); } Serial.println(); } void ConnectCloud(){ while(!client.connected()){ Serial.print("Attempting MQTT connection..."); /*A device connected to the cloud platform based on an automatically calculated username and password*/ if(client.connect(myIot._clientId, myIot._username, myIot._password)){ Serial.println("connected"); client.subscribe(subTopic); }else{ Serial.print("failed, rc="); Serial.print(client.state()); Serial.println(" try again in 5 seconds"); delay(5000); } } } void setup(){ Serial.begin(115200); delay(10); connectWiFi(); /*Initialize the configuration of OneNet*/ myIot.init(EasyIot_SERVER, Iot_id, Client_ID, Iot_pwd); client.setServer(myIot._mqttServer,PORT); /*Set the callback function to execute the callback function when receiving the subscription information*/ client.setCallback(callback); /*Connect to the cloud platform*/ ConnectCloud(); } void loop(){ if(!client.connected()){ ConnectCloud(); } client.loop(); }[/mw_shl_code] 发布数据: [mw_shl_code=java,false]/*! * @file Publish_Topic.ino * * @brief After the program download is complete. * @brief You can use the BC20 module to connect to DFRobot Easy IOT cloud platform, * @brief and publish data to your Topic * * @n Easy-IoT - the simplest solution of IoT cloud platform * @n Website(CN):https://iot.dfrobot.com.cn * @n Website(EN):http://iot.dfrobot.com * * @copyright Copyright (c) 2010 DFRobot Co.Ltd (http://www.dfrobot.com) * @licence The MIT License (MIT) * @author [Wuxiao](xiao.wu@dfrobot.com) * @version V1.0 * @date 2019-10-29 * @get from https://www.dfrobot.com */ #include "WiFi.h" #include "DFRobot_Iot.h" #include <PubSubClient.h> const char* WIFI_SSID = "sxs"; const char* WIFI_PASSWORD = "**************"; /*Configure device certificate information*/ String Iot_id = "RNZYsQIWg"; String Client_ID = "sxs12345678"; String Iot_pwd = "gHWLswIWgz"; /*Configure the domain name and port number*/ String EasyIot_SERVER = "182.254.130.180"; uint16_t PORT = 1883; /*Set the Topic you need to publish to*/ const char * pubTopic = "12IZ3JpWR"; const char * subTopic = "E1RcL1tZg"; DFRobot_Iot myIot; WiFiClient espClient; PubSubClient client(espClient); void connectWiFi(){ Serial.print("Connecting to "); Serial.println(WIFI_SSID); WiFi.begin(WIFI_SSID,WIFI_PASSWORD); while(WiFi.status() != WL_CONNECTED){ delay(500); Serial.print("."); } Serial.println(); Serial.println("WiFi connected"); Serial.print("IP Adderss: "); Serial.println(WiFi.localIP()); } void callback(char * topic, byte * payload, unsigned int len){ Serial.print("Recevice ["); Serial.print(topic); Serial.print("] "); for (int i = 0; i < len; i++){ Serial.print((char)payload); } Serial.println(); } void ConnectCloud(){ while(!client.connected()){ Serial.print("Attempting MQTT connection..."); /*A device connected to the cloud platform based on an automatically calculated username and password*/ if(client.connect(myIot._clientId, myIot._username, myIot._password)){ Serial.println("connected"); client.subscribe(subTopic); }else{ Serial.print("failed, rc="); Serial.print(client.state()); Serial.println(" try again in 5 seconds"); delay(5000); } } } void setup(){ Serial.begin(115200); delay(10); connectWiFi(); /*Initialize the configuration of OneNet*/ myIot.init(EasyIot_SERVER, Iot_id, Client_ID, Iot_pwd); client.setServer(myIot._mqttServer,PORT); /*Set the callback function to execute the callback function when receiving the subscription information*/ client.setCallback(callback); /*Connect to the cloud platform*/ ConnectCloud(); } void loop(){ if(!client.connected()){ ConnectCloud(); } client.loop(); client.publish(pubTopic,"data"); delay(5000); }[/mw_shl_code] |
© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed