资源简介
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
相关资源
- ADNS-3080光流传感器测试程序Arduino
- EESkill NRF24L01 无线模块用户手册
- Arduino nano 工程文件
- Arduino教程 Lesson 之--自制风扇
- 基于Arduino的智能环境监控系统设计
- 基于Arduino和Machtalk的温棚环境监测系
- esp8266 win10驱动 cp2102
- CSDN文件---ESP8266.rar
- arduino pca9685多舵机同时控制案例
- stm32103c8t6ESP8266串口转WIFI模块TCP服务器
- ESP8266ESP8266安卓控制端源代码
- arduino技术内幕
- Arduino电子设计实战指南.零基础篇_超
- 物联网智能家居平台DIY:ARDUINO 物联网
- 实验1.zip arduino跑马灯led灯实验,串口
- opencat所有资料.zip
- arduino主机,stm8从机。I2C测试 。每次
- nudemcu相关工具和固件测试稳定
- DS18B20_Serial_println.ino
- ps2手柄arduino库文件
- 基于手机蓝牙的arduino遥控小车
- 单片机通过ESP8266上传温湿度数据
- STM32F407ZG_ESP8266例程
- arduino中的can库函数
- 密码+指纹锁资料包.rar
- ESP8266最新稳定版固件
- ESP8266AP模式控制51单片机亮灯.docx
- ESP8266使用MQTT协议连接阿里云
- 贝壳物联arduino esp8266 demo版本
- ESP8266程序加上MQTT调试工具
评论
共有 条评论