1970| 0
|
[用户分享] TinkerNode NB-IoT使用WIFI物联阿里云平台 |
TinkerNode NB-IoT使用WIFI物联阿里云平台 阿里云平台配置参考:https://mc.dfrobot.com.cn/thread-304131-1-1.html 代码: [mw_shl_code=java,false]/*! * @file SmartWatering.ino * * @brief Connect the soil sensor. * @brief Publish soil information to the platform. * @brief Can handle other things based on information * * @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-02-20 * @get from https://www.dfrobot.com */ #include <WiFi.h> #include <PubSubClient.h> #include <ArduinoJson.h> #include "DFRobot_Iot.h" #define SOIL_SENSOR_PIN A0 /*Set WIFI name and password*/ const char * WIFI_SSID = "**********"; const char * WIFI_PASSWORD = "**********"; /*Configure device certificate information*/ String ProductKey = "**********"; String ClientId = "12345"; String DeviceName = "**********"; String DeviceSecret = "**********"; /*Configure the domain name and port number*/ String ALIYUN_SERVER = "iot-as-mqtt.cn-shanghai.aliyuncs.com"; uint16_t PORT = 1883; /*Product identifier that needs to be operated*/ String Identifier = "**********"; /*TOPIC that need to be published and subscribed*/ const char * pubTopic = "/sys/a1zZW8epm4h/ahVRqIyGJPXi2s5wPbqO/thing/event/property/post";//******post 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"); }else{ Serial.print("failed, rc="); Serial.print(client.state()); Serial.println(" try again in 5 seconds"); delay(5000); } } } void setup(){ Serial.begin(115200); /*Connect to WIFI*/ connectWiFi(); /*Initialize the configuration of Aliyun*/ myIot.init(ALIYUN_SERVER,ProductKey,ClientId,DeviceName,DeviceSecret); 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(); } uint8_t tempTime = 0; void loop(){ if(!client.connected()){ ConnectCloud(); } /*Publish soil information twice a minute*/ if(tempTime > 10){ tempTime = 0; client.publish(pubTopic,("{\"id\":"+ClientId+",\"params\":{\""+Identifier+"\":"+random(9)+"},\"method\":\"thing.event.property.post\"}").c_str()); }else{ tempTime++; delay(300); } client.loop(); }[/mw_shl_code] |
© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed