Esp8266 With MQTT On AliIOT
本文最后更新于 1729 天前,其中的信息可能已经有所发展或是发生改变。

阿里云官方MQTT接入文档 使用MQTT.fx接入物联网平台

参考库 Arduino TopLevel Client for aliyun IoT Platform

// 引入 wifi 模块,并实例化,不同的芯片这里的依赖可能不同
#include <ESP8266WiFi.h>
static WiFiClient espClient;

//引入DHT11操作库,并实例化
#include <SimpleDHT.h>
#define DHTPIN 5
#define DHTTYPE DHT11
SimpleDHT11 dht11(DHTPIN);

// 引入阿里云 IoT SDK
#include <AliyunIoTSDK.h>

// 设置产品和设备的信息,从阿里云设备信息里查看
#define PRODUCT_KEY ""
#define DEVICE_NAME ""
#define DEVICE_SECRET ""
#define REGION_ID "cn-shanghai"
#define PWD ""

// 设置 wifi 信息
#define WIFI_SSID ""
#define WIFI_PASSWD ""

void setup()
{
    //串口波特率设定为115200
	Serial.begin(115200);
    
    // 初始化 wifi
    wifiInit(WIFI_SSID, WIFI_PASSWD);
    
    // 初始化 iot,需传入 wifi 的 client,和设备产品信息
	//由于找不到原库依赖的SHA256.h 这里进行了魔改,直接传参密码
    AliyunIoTSDK::begin(espClient, PRODUCT_KEY, DEVICE_NAME, DEVICE_SECRET, REGION_ID, PWD);
    
    // 绑定一个设备属性回调,当远程修改此属性,会触发 powerCallback
    // PowerSwitch 是在设备产品中定义的物联网模型的 id
    //AliyunIoTSDK::bindData("PowerSwitch", powerCallback);
    
    // 发送一个数据到云平台,CurrentTemperature 是在产品详情-功能定义中的对应标识符
    //AliyunIoTSDK::send("CurrentTemperature", 100);
}

void loop()
{
	//DHT11操作部分
    byte temperature = 0;
    byte humidity = 0;
    int err = SimpleDHTErrSuccess;
    if ((err = dht11.read(&temperature, &humidity, NULL)) != SimpleDHTErrSuccess) {
      Serial.print("Read DHT11 failed, err="); Serial.println(err);delay(1000);
      return;
    }
	//CurrentTemperature 是在产品详情-功能定义中的对应标识符
	//发送DHT11读取的温度到云平台
	AliyunIoTSDK::send("CurrentTemperature", (int)temperature);
	//发送DHT11读取的湿度到云平台
	AliyunIoTSDK::send("CurrentHumidity", (int)humidity);
	
	//该方法用来发送心跳包保持连接
    AliyunIoTSDK::loop();
}

// 初始化 wifi 连接
void wifiInit(const char *ssid, const char *passphrase)
{
    WiFi.mode(WIFI_STA);
    WiFi.begin(ssid, passphrase);
    while (WiFi.status() != WL_CONNECTED)
    {
        delay(1000);
        Serial.println("WiFi not Connect");
    }
    Serial.println("Connected to AP");
}

 

魔改说明

原库中的用户验证(User Credentials)是依照阿里云文档在类构造方法中计算得出,这里改成直接传参。其中密码计算工具阿里云已给出。PassWord生成工具

原SHA256函数全部注释

构造修改:

void AliyunIoTSDK::begin(Client &espClient,
                         const char *_productKey,
                         const char *_deviceName,
                         const char *_deviceSecret,
                         const char *_region,
						 const char *_pwd)
{

    client = new PubSubClient(espClient);
    productKey = _productKey;
    deviceName = _deviceName;
    deviceSecret = _deviceSecret;
    region = _region;
	passwd = _pwd;
    long times = millis();
    String timestamp = String(times);

    //sprintf(clientId, "%s|securemode=3,signmethod=hmacsha256,timestamp=%s|", deviceName, timestamp.c_str());
    //clientId = "1234567890|securemode=3,signmethod=hmacsha1|";
	String Id = "1234567890|securemode=3,signmethod=hmacsha1|";
	strcpy(clientId, Id.c_str());


    String signcontent = "clientId";
    signcontent += deviceName;
    signcontent += "deviceName";
    signcontent += deviceName;
    signcontent += "productKey";
    signcontent += productKey;
    signcontent += "timestamp";
    signcontent += timestamp;

    //String pwd = hmac256(signcontent, deviceSecret);
	String pwd = passwd;

    strcpy(mqttPwd, pwd.c_str());
	//mqttPwd = "";
	
    sprintf(mqttUsername, "%s&%s", deviceName, productKey);
    sprintf(ALINK_TOPIC_PROP_POST, "/sys/%s/%s/thing/event/property/post", productKey, deviceName);
    sprintf(ALINK_TOPIC_PROP_SET, "/sys/%s/%s/thing/service/property/set", productKey, deviceName);
    sprintf(ALINK_TOPIC_EVENT, "/sys/%s/%s/thing/event", productKey, deviceName);

    sprintf(domain, "%s.iot-as-mqtt.%s.aliyuncs.com", productKey, region);
    client->setServer(domain, MQTT_PORT); /* 连接WiFi之后,连接MQTT服务器 */
    client->setCallback(callback);

    mqttCheckConnect();
}

 

 

上一篇
下一篇