开发板又换成了ESP8266,mpy代码要改成C++。
[C++] 纯文本查看 复制代码 #include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <ArduinoJson.h>
#include <Arduino.h>
#include <Crypto.h>
#include <SHA256.h>
#include <WiFiUdp.h>
#include <NTPClient.h>
const char base64_chars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/";
#define WIFI_SSID "F1"
#define WIFI_PASSWORD "123456789"
const unsigned long connectInterval = 60000;
unsigned long lastConnectAttemptTime = 0;
const int maxConnectAttempts = 15;
unsigned long epochTime = 0;
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "ntp7.aliyun.com", 60*60*8, 30*60*1000);
void setup() {
Serial.begin(2400);
connectWiFi();
timeClient.begin();
}
void loop() {
checkWiFiConnection();
timeClient.update();
epochTime = timeClient.getEpochTime() - 28800;
sendDingMSG("Hello World From ESP8266!");
delay(10000);
}
void connectWiFi() {
int attempt = 0;
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED && attempt < maxConnectAttempts) {
delay(1000);
Serial.print(".");
attempt++;
}
if (WiFi.status() == WL_CONNECTED) {
Serial.println("WiFi Connected");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
} else {
Serial.println("WiFi Connection failed after max attempts");
}
}
void checkWiFiConnection() {
unsigned long currentTime = millis();
if (currentTime - lastConnectAttemptTime > connectInterval) {
lastConnectAttemptTime = currentTime;
if (WiFi.status() != WL_CONNECTED) {
WiFi.disconnect(true);
delay(1000);
connectWiFi();
} else {
Serial.print(WiFi.localIP());
}
}
}
void sendDingMSG(const char* msgAlert) {
String webhook = "https://oapi.dingtalk.com/robot/send?access_token=3ae63bd51de7053486b32d4e2d2eeb176e4f52b8798fdbd680e4be61f78cec87";
String secret_key = "SEC1306372b3167575940247b5fdc840fabda0fe28408b0e8c8dd232d29c70f2e9e";
String stringToSign = String(epochTime) + "000\n" + secret_key;
String sign = hmac_sha256_base64(secret_key, stringToSign);
String url = webhook + "×tamp=" + String(epochTime) + "000&sign=" + sign;
if (WiFi.status() == WL_CONNECTED) {
WiFiClientSecure client;
client.setInsecure();
HTTPClient http;
http.begin(client,url);
http.addHeader("Content-Type", "application/json");
DynamicJsonDocument doc(2048);
doc["msgtype"] = "text";
JsonObject text = doc.createNestedObject("text");
text["content"] = msgAlert;
String httpRequestData;
serializeJson(doc, httpRequestData);
int httpResponseCode = http.POST(httpRequestData);
if (httpResponseCode > 0) {
String response = http.getString();
Serial.println(httpResponseCode);
Serial.println(response);
} else {
Serial.print("Error on sending POST: ");
Serial.println(httpResponseCode);
}
http.end();
} else {
Serial.println("Error in WiFi connection");
}
}
String base64_encode(byte* input, int length) {
String encoded;
int i = 0;
while (i < length) {
int octet_a = i < length ? input[i++] : 0;
int octet_b = i < length ? input[i++] : 0;
int octet_c = i < length ? input[i++] : 0;
int triple = (octet_a << 0x10) + (octet_b << 0x08) + octet_c;
encoded += base64_chars[(triple >> 3 * 6) & 0x3F];
encoded += base64_chars[(triple >> 2 * 6) & 0x3F];
encoded += base64_chars[(triple >> 1 * 6) & 0x3F];
encoded += base64_chars[(triple >> 0 * 6) & 0x3F];
}
for (int j = 0; j < (3 - length % 3) % 3; j++) {
encoded.setCharAt(encoded.length() - 1 - j, '=');
}
return encoded;
}
String hmac_sha256_base64(const String& key, const String& message) {
int block_size = 64;
byte key_copy[block_size];
memset(key_copy, 0, block_size);
if (key.length() > block_size) {
SHA256 sha256;
sha256.update((const byte*)key.c_str(), key.length());
sha256.finalize(key_copy, block_size);
} else {
memcpy(key_copy, key.c_str(), key.length());
}
byte o_key_pad[block_size];
byte i_key_pad[block_size];
for (int i = 0; i < block_size; i++) {
o_key_pad[i] = 0x5c ^ key_copy[i];
i_key_pad[i] = 0x36 ^ key_copy[i];
}
SHA256 inner_sha256;
inner_sha256.update(i_key_pad, block_size);
inner_sha256.update((const byte*)message.c_str(), message.length());
byte inner_hash[32];
inner_sha256.finalize(inner_hash, 32);
SHA256 outer_sha256;
outer_sha256.update(o_key_pad, block_size);
outer_sha256.update(inner_hash, 32);
byte hmac_code[32];
outer_sha256.finalize(hmac_code, 32);
return base64_encode(hmac_code, 32);
}
|