mirror of
https://gitee.com/beecue/fastbee.git
synced 2025-12-18 00:45:55 +08:00
更新esp8266的SDK
This commit is contained in:
@@ -1,3 +0,0 @@
|
||||
{
|
||||
"C_Cpp.errorSquiggles": "Disabled"
|
||||
}
|
||||
@@ -1,14 +1,13 @@
|
||||
/***********************************************************
|
||||
* author: LaoHuang
|
||||
* create: 2022-04-14
|
||||
* email:rememberyousaid@163.com
|
||||
* author: kerwincui [物美智能 wumei-smart]
|
||||
* create: 2022-02-20
|
||||
* email:164770707@qq.com
|
||||
* source:https://github.com/kerwincui/wumei-smart
|
||||
* board:esp32
|
||||
* board:esp8266 core for arduino v3.0.2
|
||||
***********************************************************/
|
||||
|
||||
#include "Helper.h"
|
||||
|
||||
String g_time;
|
||||
WiFiClient wifiClient;
|
||||
PubSubClient mqttClient;
|
||||
float rssi = 0;
|
||||
@@ -18,8 +17,8 @@ long monitorInterval = 1000;
|
||||
|
||||
//==================================== 这是需要配置的项 ===============================
|
||||
// Wifi配置
|
||||
char *wifiSsid = "wifi-ssid";
|
||||
char *wifiPwd = "wifi-password";
|
||||
char *wifiSsid = "wifi账号";
|
||||
char *wifiPwd = "wifi密码";
|
||||
|
||||
// 设备信息配置
|
||||
String deviceNum = "D6329VL54419L1Y0";
|
||||
@@ -60,8 +59,7 @@ void processProperty(String payload)
|
||||
{
|
||||
StaticJsonDocument<1024> doc;
|
||||
DeserializationError error = deserializeJson(doc, payload);
|
||||
if (error)
|
||||
{
|
||||
if (error) {
|
||||
Serial.print(F("deserializeJson() failed: "));
|
||||
Serial.println(error.f_str());
|
||||
return;
|
||||
@@ -82,8 +80,7 @@ void processFunction(String payload)
|
||||
{
|
||||
StaticJsonDocument<1024> doc;
|
||||
DeserializationError error = deserializeJson(doc, payload);
|
||||
if (error)
|
||||
{
|
||||
if (error) {
|
||||
Serial.print(F("deserializeJson() failed: "));
|
||||
Serial.println(error.f_str());
|
||||
return;
|
||||
@@ -108,9 +105,7 @@ void processFunction(String payload)
|
||||
else if (strcmp(id, "message") == 0)
|
||||
{
|
||||
printMsg("屏显消息 message:" + (String)value);
|
||||
}
|
||||
else if (strcmp(id, "report_monitor") == 0)
|
||||
{
|
||||
}else if(strcmp(id,"report_monitor")==0){
|
||||
String msg=randomPropertyData();
|
||||
printMsg("订阅到上报监测数据指令,上报数据:");
|
||||
printMsg(msg);
|
||||
@@ -148,7 +143,7 @@ void callback(char *topic, byte *payload, unsigned int length)
|
||||
Serial.println(error.f_str());
|
||||
return;
|
||||
}
|
||||
|
||||
// 计算设备当前时间:(${serverRecvTime} + ${serverSendTime} + ${deviceRecvTime} - ${deviceSendTime}) / 2
|
||||
float deviceSendTime = doc["deviceSendTime"];
|
||||
float serverSendTime = doc["serverSendTime"];
|
||||
float serverRecvTime = doc["serverRecvTime"];
|
||||
@@ -207,7 +202,6 @@ void connectMqtt()
|
||||
String password = generationPwd();
|
||||
String encryptPassword = encrypt(password, mqttSecret, wumei_iv);
|
||||
printMsg("密码(已加密):" + encryptPassword);
|
||||
|
||||
mqttClient.setClient(wifiClient);
|
||||
mqttClient.setServer(mqttHost, mqttPort);
|
||||
mqttClient.setCallback(callback);
|
||||
@@ -319,8 +313,7 @@ void publishMonitor()
|
||||
}
|
||||
|
||||
// 随机生成监测值
|
||||
String randomPropertyData()
|
||||
{
|
||||
String randomPropertyData(){
|
||||
// 匹配云端定义的监测数据,随机数代替监测结果
|
||||
float randFloat = 0;
|
||||
int randInt=0;
|
||||
@@ -360,8 +353,6 @@ String randomPropertyData()
|
||||
String generationPwd()
|
||||
{
|
||||
String jsonTime = getTime();
|
||||
printMsg("getTime()= " + jsonTime);
|
||||
|
||||
// 128字节内存池容量
|
||||
StaticJsonDocument<128> doc;
|
||||
// 解析JSON
|
||||
@@ -378,12 +369,10 @@ String generationPwd()
|
||||
float serverRecvTime = doc["serverRecvTime"];
|
||||
float deviceRecvTime = millis();
|
||||
float now = (serverSendTime + serverRecvTime + deviceRecvTime - deviceSendTime) / 2;
|
||||
|
||||
// 过期时间 = 当前时间 + 1小时
|
||||
float expireTime = now + 1 * 60 * 60 * 1000;
|
||||
String password = (String)mqttPwd + "&" + userId + "&" + String(expireTime, 0);
|
||||
printMsg("密码(未加密):" + password);
|
||||
|
||||
return password;
|
||||
}
|
||||
|
||||
@@ -394,7 +383,6 @@ String getTime()
|
||||
{
|
||||
HTTPClient http;
|
||||
printMsg("获取时间...");
|
||||
|
||||
if (http.begin(wifiClient, (ntpServer + (String)millis()).c_str()))
|
||||
{
|
||||
// 发送请求
|
||||
@@ -403,10 +391,9 @@ String getTime()
|
||||
{
|
||||
if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY)
|
||||
{
|
||||
g_time = http.getString();
|
||||
printMsg("获取时间成功,data:");
|
||||
Serial.print(g_time);
|
||||
return g_time;
|
||||
Serial.print(http.getString());
|
||||
return http.getString();
|
||||
}
|
||||
}
|
||||
else
|
||||
@@ -453,7 +440,6 @@ String encrypt(String plain_data, char *wumei_key, char *wumei_iv)
|
||||
int i;
|
||||
// pkcs7padding填充 Block Size : 16
|
||||
int len = plain_data.length();
|
||||
|
||||
int n_blocks = len / 16 + 1;
|
||||
uint8_t n_padding = n_blocks * 16 - len;
|
||||
uint8_t data[n_blocks * 16];
|
||||
@@ -463,24 +449,16 @@ String encrypt(String plain_data, char *wumei_key, char *wumei_iv)
|
||||
data[i] = n_padding;
|
||||
}
|
||||
uint8_t key[16], iv[16];
|
||||
uint8_t crypt_data[3 * 16] = {0};
|
||||
|
||||
memcpy(key, wumei_key, 16);
|
||||
memcpy(iv, wumei_iv, 16);
|
||||
|
||||
memset(crypt_data, 0, 48);
|
||||
|
||||
len = n_blocks * 16;
|
||||
// 加密
|
||||
mbedtls_aes_context aes_ctx;
|
||||
mbedtls_aes_init(&aes_ctx);
|
||||
mbedtls_aes_setkey_enc(&aes_ctx, key, 128);
|
||||
mbedtls_aes_crypt_cbc(&aes_ctx, MBEDTLS_AES_ENCRYPT, len, iv, data, crypt_data);
|
||||
|
||||
br_aes_big_cbcenc_keys encCtx;
|
||||
br_aes_big_cbcenc_init(&encCtx, key, 16);
|
||||
br_aes_big_cbcenc_run(&encCtx, iv, data, n_blocks * 16);
|
||||
// Base64编码
|
||||
len = n_blocks * 16;
|
||||
char encoded_data[base64_enc_len(len)];
|
||||
base64_encode(encoded_data, (char *)crypt_data, len);
|
||||
|
||||
base64_encode(encoded_data, (char *)data, len);
|
||||
return String(encoded_data);
|
||||
}
|
||||
|
||||
@@ -496,20 +474,14 @@ String decrypt(String encoded_data_str, char *wumei_key, char *wumei_iv)
|
||||
memcpy(key, wumei_key, 16);
|
||||
memcpy(iv, wumei_iv, 16);
|
||||
int n_blocks = len / 16;
|
||||
|
||||
br_aes_big_cbcdec_keys decCtx;
|
||||
br_aes_big_cbcdec_init(&decCtx, key, 16);
|
||||
br_aes_big_cbcdec_run(&decCtx, iv, data, n_blocks * 16);
|
||||
// PKCS#7 Padding 填充
|
||||
uint8_t n_padding = data[n_blocks * 16 - 1];
|
||||
len = n_blocks * 16 - n_padding;
|
||||
char plain_data[len + 1];
|
||||
|
||||
//密文空间
|
||||
mbedtls_aes_context aes_ctx;
|
||||
mbedtls_aes_init(&aes_ctx);
|
||||
|
||||
//设置解密密钥
|
||||
mbedtls_aes_setkey_dec(&aes_ctx, key, 128);
|
||||
mbedtls_aes_crypt_cbc(&aes_ctx, MBEDTLS_AES_DECRYPT, 48, iv, (unsigned char *)encoded_data, (unsigned char *)plain_data);
|
||||
mbedtls_aes_free(&aes_ctx);
|
||||
// PKCS#7 Padding 填充
|
||||
memcpy(plain_data, data, len);
|
||||
plain_data[len] = '\0';
|
||||
return String(plain_data);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user