资源简介
esp8266连接手机热点,可通过阿里云实现led灯的控制和监视
esp8266作为客户端,连接手机的热点
在阿里云物联网平台可以上可以控制led的亮灭

代码片段和文件信息
/*
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“;
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 4693 2020-10-16 18:19 aliyun\aliyun.ino
文件 3544 2019-10-15 13:07 aliyun\aliyun_mqtt.cpp
文件 2512 2019-10-15 13:07 aliyun\aliyun_mqtt.h
目录 0 2020-10-16 18:19 aliyun
----------- --------- ---------- ----- ----
10749 4
- 上一篇:秒杀抢拍器
- 下一篇:Labview设计简易汽车仪表盘
相关资源
- 在各城市的窄带物联网中传感器起到
- 物联网家居系统中基于单片机的多路
- 物联网中无线传感器节点和RFID数据融
- 有关面向工业物联网的无线传感器网
- 物联网无线传感器网络的7大特点
- EMCP物联网云平台操作手册
- 电信物联网NB-lot上传编解码插件检测
- 物联网云平台设备管理和相关页面
- 中国移动NB-IoT安全白皮书.pdf
- 智能网联汽车信息安全白皮书
- 物联网行业新突破:UWB人员定位技术
- 开放充电协议标准2.0 OCPP 2.0
- 物联网智能家居平台DIY:ARDUINO 物联网
-
Symli
nk XR1020--工业物联网智能网关规 - 矿山物联网网络技术发展趋势与关键
- 矿山物联网云计算与平台技术
- FCS技术在矿山物联网感知层的应用
- 论感知矿山物联网与矿山综合自动化
- 论矿山物联网的结构性平台与服务性
- 物联网与感知矿山专题讲座之三&mda
- 感知矿山物联网云计算应用探索
- 07-5123-07-ZigbeeClusterLibrary_Revision_7.pdf
- 物联网行业深度分析报告:NB-IOT开启
- 仓储物联网管理系统开发项目流程中
- 单片机通过ESP8266上传温湿度数据
- 智能门锁安全分析报告.pdf
- LSM6DSL陀螺仪+加速度传感器带中断唤醒
- 贝壳物联arduino esp8266 demo版本
- 基于ACS712的直流电机电流监测系统的
- IOT_FLASH TOOL
评论
共有 条评论