ESP32C3对接autMan机器人控制自带的LED灯状态demo,自己写着玩的,
Arduino写的,后期可以扩展开发控制一些电器之类的。
需要先创建autMan开发者开放接口权限
#include <WiFi.h>
#include <HTTPClient.h>
// WiFi 连接信息
const char* ssid = "wifi帐号";
const char* password = "wifi密码";
const int led0 = 12;
const int led1 = 13;
// API 接口信息
const String authUrl = "http://192.168.0.152:8080/open/auth/token?client_id=XXXXXX&client_secret=XXXXXX";
const String bucketUrl = "http://192.168.0.152:8080/open/buckets?bucket=otto&key=esp32c3";
String token;
unsigned long tokenExpiration = 0;
void setup() {
Serial.begin(115200);
pinMode(led0, OUTPUT);
pinMode(led1, OUTPUT);
WiFi.begin(ssid, password);
// 连接到WiFi
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nWiFi connected");
}
void loop() {
if (WiFi.status() == WL_CONNECTED) {
// 检查是否需要获取新的Token
if (millis() >= tokenExpiration || token.isEmpty()) {
getToken();
}
// 使用Token获取bucket数据
if (!token.isEmpty()) {
getBucketData();
}
} else {
Serial.println("WiFi not connected");
}
delay(1000); // 每秒执行一次
}
// 获取Token的函数
void getToken() {
HTTPClient http;
int retryCount = 0;
const int maxRetries = 5; // 最大重试次数
bool tokenObtained = false;
while (retryCount < maxRetries && !tokenObtained) {
http.begin(authUrl);
int httpResponseCode = http.GET();
if (httpResponseCode == 200) {
String response = http.getString();
Serial.println("Token response: " + response);
// 检查响应内容是否包含 code: 200
if (response.indexOf("\"code\":200") != -1) {
// 解析 JSON 数据
int tokenIndex = response.indexOf("\"token\":\"") + 9;
int tokenEndIndex = response.indexOf("\"", tokenIndex);
token = response.substring(tokenIndex, tokenEndIndex);
Serial.println(token);
int expirationIndex = response.indexOf("\"expiration\":") + 13;
int expirationEndIndex = response.indexOf(",", expirationIndex);
String expirationStr = response.substring(expirationIndex, expirationEndIndex);
tokenExpiration = millis() + (expirationStr.toInt() - 20 * 60) * 1000; // 设置为20分钟有效期
tokenObtained = true; // 标记为成功获取
Serial.println("Token retrieved successfully.");
} else {
Serial.println("Received error response, retrying...");
}
} else {
Serial.println("Failed to get token, retrying...");
}
http.end();
retryCount++;
delay(1000); // 等待1秒再重试
}
if (!tokenObtained) {
Serial.println("Failed to retrieve token after multiple attempts.");
}
}
// 获取桶数据的函数
void getBucketData() {
HTTPClient http;
http.begin(bucketUrl);
http.addHeader("Authorization", "Bearer " + token);
int httpResponseCode = http.GET();
if (httpResponseCode == 200) {
String response = http.getString();
Serial.println("Bucket response: " + response);
// 解析 JSON 数据中的value值
int valueIndex = response.indexOf("\"value\":\"") + 9;
int valueEndIndex = response.indexOf("\"", valueIndex);
String value = response.substring(valueIndex, valueEndIndex);
// 控制LED灯
if (value == "1") {
digitalWrite(led0, HIGH);
digitalWrite(led1, HIGH);
} else if (value == "2") {
digitalWrite(led0, LOW);
digitalWrite(led1, LOW);
} else if (value == "3") {
blinkLED();
} else if (value == "4") {
digitalWrite(led0, HIGH);
digitalWrite(led1, LOW);
} else if (value == "5") {
digitalWrite(led0, LOW);
digitalWrite(led1, HIGH);
}
} else {
Serial.println("Failed to get bucket data");
}
http.end();
}
// LED 闪烁函数
void blinkLED() {
digitalWrite(led0, HIGH);
digitalWrite(led1, LOW);
delay(500);
digitalWrite(led0, LOW);
digitalWrite(led1, HIGH);
delay(500);
}
autMan插件 es5插件源码
check();
function check() {
if (GetContent()=="开灯"){
bucketSet('otto', 'esp32c3', '1')
sendText("已开灯")
return;
}
if (GetContent()=="关灯"){
bucketSet('otto', 'esp32c3', '2')
sendText("已关灯")
return;
}
if (GetContent()=="闪灯"){
bucketSet('otto', 'esp32c3', '3')
sendText("已闪灯")
return;
}
if (GetContent()=="1开灯"){
bucketSet('otto', 'esp32c3', '4')
sendText("LED1已开灯")
return;
}
if (GetContent()=="2开灯"){
bucketSet('otto', 'esp32c3', '5')
sendText("LED2已开灯")
return;
}
}
测试结果:
评论列表:
发布于 4天前回复该评论
发布于 3天前回复该评论
发布于 3天前回复该评论
发布于 3天前回复该评论
发布于 3天前回复该评论