arduino连接阿里云IOT平台 并上传数据点模型

Posted TOPthemaster

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了arduino连接阿里云IOT平台 并上传数据点模型相关的知识,希望对你有一定的参考价值。

很久没更新了,写个esp连接阿里云平台吧,主要实现云平台的连接和物模型的数据点上传,相比Onenet,阿里云平台的功能更加丰富,产业链更长远(虽然我更喜欢用自己的服务器,没那么约束,但安全系数也没那么高)。效果图就不放了,原理都差不多。

#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <ArduinoJson.h>
/* 连接您的WIFI SSID和密码 */
#define WIFI_SSID         "TP-LINK_3BF2"
#define WIFI_PASSWD       "13219991021"
/* 设备的三元组信息*/
#define PRODUCT_KEY       "a1XBVOA1XGE"
#define DEVICE_NAME       "card"
#define DEVICE_SECRET     "489bc1de15cea782d0bbcf1b4ec041d6"
#define REGION_ID         "cn-shanghai"
/* 线上环境域名和端口号,不需要改 */
#define MQTT_SERVER       PRODUCT_KEY ".iot-as-mqtt." REGION_ID ".aliyuncs.com"
#define MQTT_PORT         1883
#define MQTT_USRNAME      DEVICE_NAME "&" PRODUCT_KEY
#define CLIENT_ID         "pw123|securemode=3,signmethod=hmacsha1|"
// 算法工具: http://iot-face.oss-cn-shanghai.aliyuncs.com/tools.htm 进行加密生成password
// password教程 https://www.yuque.com/cloud-dev/iot-tech/mebm5g
#define MQTT_PASSWD       "83B667A9E9BBD4F59479BA7F81BC9F79A599758E"
//"id":"temp","version":"1.0","params":"CurrentHumidity":10,"method":"thing.event.property.post"
#define ALINK_BODY_FORMAT         "\\"id\\":\\"card\\",\\"version\\":\\"1.0\\",\\"method\\":\\"thing.event.property.post\\",\\"params\\":%s"
#define ALINK_TOPIC_PROP_POST     "/sys/a1XBVOA1XGE/card/thing/event/property/post"
unsigned long lastMs = 0;
String get_id="adviceA:";
String serial_input="";
long temp_id=0;
unsigned char count=0;
WiFiClient espClient;
PubSubClient  client(espClient);
void callback(char *topic, byte *payload, unsigned int length)

    Serial.print("Message arrived [");
    Serial.print(topic);
    Serial.print("] ");
    payload[length] = '\\0';
    Serial.println((char *)payload);

void wifiInit()

    WiFi.mode(WIFI_STA);
    WiFi.begin(WIFI_SSID, WIFI_PASSWD);   //连接WiFi
    while (WiFi.status() != WL_CONNECTED)
    
        delay(1000);
        Serial.println("WiFi not Connect");
    
    Serial.println("Connected to AP");
    Serial.println("IP address: ");
    Serial.println(WiFi.localIP());    
    Serial.print("espClient [");
    client.setServer(MQTT_SERVER, MQTT_PORT);   /* 连接WiFi之后,连接MQTT服务器 */
    client.setCallback(callback);

void mqttCheckConnect()

    while (!client.connected())
    
        Serial.println("Connecting to MQTT Server ...");
        if (client.connect(CLIENT_ID, MQTT_USRNAME, MQTT_PASSWD))
        
            Serial.println("MQTT Connected!");
        
        else
        
            Serial.print("MQTT Connect err:");
            Serial.println(client.state());
            delay(5000);
        
    


void mqttIntervalPost()

    char param[40];
    char jsonBuf[128];
    String buf="\\"Display\\":\\""+get_id+"\\"";
    
    strcpy(param, buf.c_str());
    sprintf(jsonBuf, ALINK_BODY_FORMAT, param);
    Serial.println(jsonBuf);
    boolean d = client.publish(ALINK_TOPIC_PROP_POST, jsonBuf);
    if(d)
      Serial.println("publish  success"); 
    else
      Serial.println("publish  fail"); 
    
    get_id="adviceA:";

void setup() 
   
    Serial.begin(115200);
    Serial.println("Demo Start");
    wifiInit();
    unsigned char i=0;
    // mqttCheckConnect(); 

void loop()

  mqttCheckConnect(); 
    /* 上报 */
   while(Serial.available())
      serial_input+=(char)Serial.read();
      delay(2);
   
 
     //读取电脑传来的数据
  //Serial.println(serial_input);
  if(serial_input!="")
  //Serial.println(serial_input);
  get_id+=serial_input;
  mqttIntervalPost();
  serial_input="";
  Serial.println(get_id);
  mqttIntervalPost();
  
  
  
  client.loop();

以上是关于arduino连接阿里云IOT平台 并上传数据点模型的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 NB-Iot + Arduino 通过MQTT协议上传数据至阿里云平台

阿里云iot平台缺陷

NodeMCU-ESP8266连接阿里云Iot平台进行数据监测

Esp8266 Arduino PubSubClient连接阿里云老是返回rc=2错误的解决方案

.NET Core 跨平台物联网开发:连接阿里云IOT

服务器连接阿里云Iot平台进行指令下发