作者:Microblue | 更新時間:2020-05-12 | 瀏覽量:716
下面這個開關程序如何加上按鈕手動控制,按鈕接口 0。
手動按鈕程序
int led = 4;
int button = 0;
boolean buttonState = 1;
void setup() {
// put your setup code here, to run once:
pinMode(led, OUTPUT);
pinMode(button, INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
if (digitalRead(button) == HIGH) {
digitalWrite(led, buttonState);
buttonState = !buttonState;
while (digitalRead(button) == HIGH);
}
}
開關程序
#include <aJSON.h>
//============= 此處必須修該============
String DEVICEID = "1"; // 你的設備ID ==
String APIKEY = "493822592"; //設備密碼=
//=======================================
const int LED = 4;// LED正極連接針腳4
unsigned long lastCheckStatusTime = 0; //記錄上次報到時間
const unsigned long postingInterval = 40000; // 每隔40秒向服務器報到一次
unsigned long checkoutTime = 0;//登出時間
void setup() {
pinMode(LED, OUTPUT);
Serial.begin(115200);
delay(5000);//等一會兒ESP8266
}
void loop() {
//每一定時間查詢一次設備在線狀態,同時替代心跳
if (millis() - lastCheckStatusTime > postingInterval) {
checkStatus();
}
//checkout 50ms 后 checkin
if ( checkoutTime != 0 && millis() - checkoutTime > 50 ) {
checkIn();
checkoutTime = 0;
}
//讀取串口信息
while (Serial.available()) {
String inputString = Serial.readStringUntil('\n');
//檢測json數據是否完整
int jsonBeginAt = inputString.indexOf("{");
int jsonEndAt = inputString.lastIndexOf("}");
if (jsonBeginAt != -1 && jsonEndAt != -1) {
//凈化json數據
inputString = inputString.substring(jsonBeginAt, jsonEndAt + 1);
int len = inputString.length() + 1;
char jsonString[len];
inputString.toCharArray(jsonString, len);
aJsonObject *msg = aJson.parse(jsonString);
processMessage(msg);
aJson.deleteItem(msg);
}
}
}
//設備登錄
//{"M":"checkin","ID":"xx1","K":"xx2"}\n
void checkIn() {
Serial.print("{\"M\":\"checkin\",\"ID\":\"");
Serial.print(DEVICEID);
Serial.print("\",\"K\":\"");
Serial.print(APIKEY);
Serial.print("\"}\r\n");
}
//處理網絡接收到到指令,執行相關動作
void processMessage(aJsonObject *msg) {
aJsonObject* method = aJson.getObjectItem(msg, "M");
if (!method) {
return;
}
String M = method->valuestring;
if (M == "WELCOME TO BIGIOT") {
checkOut();
checkoutTime = millis();
return;
}
if (M == "connected") {
checkIn();
}
if (M == "say") {
aJsonObject* content = aJson.getObjectItem(msg, "C");
aJsonObject* client_id = aJson.getObjectItem(msg, "ID");
String C = content->valuestring;
String F_C_ID = client_id->valuestring;
if (C == "play") {
digitalWrite(LED, HIGH);
say(F_C_ID, "LED on!");
}
if (C == "stop") {
digitalWrite(LED, LOW);
say(F_C_ID, "LED off!");
}
}
}
//發送say指令,用于設備與用戶、設備與設備間通訊
//{"M":"say","ID":"xx1","C":"xx2","SIGN":"xx3"}\n
void say(String ID, String c) {
Serial.print("{\"M\":\"say\",\"ID\":\"");
Serial.print(ID);
Serial.print("\",\"C\":\"");
Serial.print(c);
Serial.print("\"}\r\n");
}
//強制設備下線,用消除設備掉線延時
//{"M":"checkout","ID":"xx1","K":"xx2"}\n
void checkOut() {
Serial.print("{\"M\":\"checkout\",\"ID\":\"");
Serial.print(DEVICEID);
Serial.print("\",\"K\":\"");
Serial.print(APIKEY);
Serial.print("\"}\n");
}
//查詢設備在線狀態
//{"M":"status"}\n
void checkStatus() {
Serial.print("{\"M\":\"status\"}\n");
lastCheckStatusTime = millis();
}