资源简介
ESP8266(arduino)连接阿里云物联网平台,ESP8266作为主控,可上报固件版本
代码片段和文件信息
/*
Aliyun_mqtt.h - Library for connect to Aliyun MQTT server.
*/
#include “aliyun_mqtt.h“
#include
#define MQTT_PORT 1883
#define SHA256HMAC_SIZE 32
// Verify tool: http://tool.oschina.net/encrypt?type=2
static String hmac256(const String &signcontent const String &ds)
{
byte hashCode[SHA256HMAC_SIZE];
SHA256 sha256;
const char *key = ds.c_str();
size_t keySize = ds.length();
sha256.resetHMAC(key keySize);
sha256.update((const byte *)signcontent.c_str() signcontent.length());
sha256.finalizeHMAC(key keySize hashCode sizeof(hashCode));
String sign = ““;
for (byte i = 0; i < SHA256HMAC_SIZE; ++i)
{
sign += “0123456789ABCDEF“[hashCode[i] >> 4];
sign += “0123456789ABCDEF“[hashCode[i] & 0xf];
}
return sign;
}
static String mqttBroker;
static String mqttClientID;
static String mqttUserName;
static String mqttPassword;
// call this function once
void mqtt_prepare(const char *timestampconst char *productKey const char *deviceNameconst char *deviceSecretconst char *region)
{
mqttBroker = productKey;
mqttBroker += “.iot-as-mqtt.“;
mqttBroker += String(region);
mqttBroker += “.aliyuncs.com“;
// Serial.println(mqttBroker);
mqttUserName = deviceName;
mqttUserName += ‘&‘;
mqttUserName += productKey;
//Serial.println(mqttUserName);
mqttClientID = deviceName; // device name used as client ID
mqttClientID += “|securemode=3signmethod=hmacsha256timestamp=“;
mqttClientID += timestamp;
mqttClientID += ‘|‘;
//Serial.println(mqttClientID);
}
bool connect_aliyun_mqtt_With_password(PubSubClient &mqttClient const char *password)
{
mqttClient.setServer(mqttBroker.c_str() MQTT_PORT);
byte mqttConnectTryCnt = 5;
while (!mqttClient.connected() && mqttConnectTryCnt > 0)
{
Serial.println(“Connecting to MQTT Server ...“);
if (mqttClient.connect(mqttClientID.c_str() mqttUserName.c_str() password))
{
// Serial.println(“MQTT Connected!“);
return true;
}
else
{
byte errCode = mqttClient.state();
Serial.print(“MQTT connect failed error code:“);
Serial.println(errCode);
if (errCode == MQTT_CONNECT_BAD_PROTOCOL || errCode == MQTT_CONNECT_BAD_CLIENT_ID || errCode == MQTT_CONNECT_BAD_CREDENTIALS || errCode == MQTT_CONNECT_UNAUTHORIZED)
{
Serial.println(“No need to try again.“);
break; // No need to try again for these situation
}
delay(3000);
}
mqttConnectTryCnt -= 1;
}
return false;
}
bool connect_aliyun_mqtt(
PubSubClient &mqttClient
const char *productKey
const char *deviceName
const char *deviceSecret
const char *region)
{
String timestamp = String(millis());
mqtt_prepare(timestamp.c_str() productKey deviceName deviceSecret region);
// Generate MQTT Password use deviceName as clientID
String signcontent = “clientId“;
signcontent += deviceName;
signcontent += “deviceName“;
signcontent += deviceName;
signcontent +=
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2019-03-21 12:11 esp8266alinyun\
目录 0 2019-03-21 12:11 esp8266alinyun\_temp_by_dltool\
目录 0 2019-03-21 12:11 esp8266alinyun\_temp_by_dltool\downloadPanel1\
文件 303072 2019-03-20 20:42 esp8266alinyun\_temp_by_dltool\downloadPanel1\esp8266.ino.generic.bin_rep
文件 302832 2019-03-18 21:57 esp8266alinyun\_temp_by_dltool\downloadPanel1\esp8266.ino.nodemcu.bin_rep
文件 3422 2019-03-20 20:01 esp8266alinyun\aliyun_mqtt.cpp
文件 2444 2019-03-20 17:38 esp8266alinyun\aliyun_mqtt.h
文件 4278 2019-03-21 12:11 esp8266alinyun\esp8266.ino
文件 303072 2019-03-20 20:42 esp8266alinyun\esp8266.ino.generic.bin
文件 302832 2019-03-19 19:49 esp8266alinyun\esp8266.ino.nodemcu.bin
相关资源
- TCS3200颜色传感器Arduino优化代码快速识
- avrdude-GUI 1.0.5
- arduino程序,用两个红外线传感器来判
- 乐为物联环境监测
- PID巡线,arduino
- ArduinoUSBKeyboard库文件
- arduino DS3231库
- Arduino串口转发源代码
- Esp8266_Wifi原理图
- arduino+rc522读写卡
- arduino_code.rar
- arduino和ds1302的基于lcd12864闹钟,按键
- Arduino-TMC2130Stepper.zip
- Arduino-smartcar_shield.zip
- 基于arduino的温度检测和显示
- TFmini_Plus I²C版在Arduino上的例程及参考
- nodemcu固件2018-06-25
- ESP8266通信源码+继电器控制
- pid算法-arduino
- HidEmuKbd.bin
- arduino编程库函数合集
- arduino JSON数据格式库
- proteus arduino仿真库
- arduino 多个程序一起工作程序
- arduino蜂鸣器和光控灯实验报告
- esp8266连接手机热点,可通过阿里云实
- Arduino自动浇花系统_一路继电器控制水
- ESP8266串口输入字符串,同时用0.96寸
- Arduino小车控制程序含遥控+寻迹+避障
- TM1637显示器arduino库文件
评论
共有 条评论