要將esp32ap 連接到目前位置可以使用的WIFI
Arduino 程式 (Auto connected WIFI )
#include <WiFi.h>
#include <PubSubClient.h>
#include <SPI.h>
#include "MFRC522.h"
//=========================================
#include <AutoConnect.h>
#include <WebServer.h>
WebServer server;
AutoConnect Portal(server);
//=========================================
const int RST_PIN = 22; // Reset pin
const int SS_PIN = 21; // Slave select pin
//=========================================
//esp32 mfrc522
//21 SDA
//18 SCK
//23 MOSI
//21 MISO
//22 RST
//GND GND
//3.3v 3.3V
//==========================
#define BUILTIN_LED 2
// Update these with values suitable for your network.
#define MQTTid "" //id of this mqtt client
#define MQTTip "broker.mqtt-dashboard.com" //ip address or hostname of the mqtt broker
#define MQTTport 1883 //port of the mqtt broker
#define MQTTuser "alex9ufo" //username of this mqtt client
#define MQTTpsw "alex1234" //password of this mqtt client
//#define MQTTuser "your_username" //username of this mqtt client
//#define MQTTpsw "your_password" //password of this mqtt client
#define MQTTpubQos 2 //qos of publish (see README)
#define MQTTsubQos 1 //qos of subscribe
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance
//Variables
long lastMsg = 0;
String IDNo_buf=""; //client.publish("alex9ufo/outTopic/RFID/json"
char jsonChar1[100];
String json = ""; //client.publish("alex9ufo/led/led_status",
char jsonChar2[100];
bool Flash = false; //true
bool Timer = false; //true
bool Send = false; //true
int Count= 0;
//=============================================================================
boolean pendingDisconnect = false;
void mqttConnectedCb(); // on connect callback
void mqttDisconnectedCb(); // on disconnect callback
void mqttDataCb(char* topic, byte* payload, unsigned int length); // on new message callback
WiFiClient wclient;
PubSubClient client(MQTTip, MQTTport, mqttDataCb, wclient);
//======================================================
void rootPage() {
char content[] = "Hello, world";
server.send(200, "text/plain", content);
}
//=======================================================
String printHex(byte *buffer, byte bufferSize) {
String id = "";
for (byte i = 0; i < bufferSize; i++) {
id += buffer[i] < 0x10 ? "0" : "";
id += String(buffer[i], HEX);
}
return id;
}
//========================================================
void mqttConnectedCb() {
Serial.println("connected");
// Once connected, publish an announcement...
client.publish("alex9ufo/outTopic/RFID/json", jsonChar1, MQTTpubQos, true); // true means retain
// Once connected, publish an announcement...
client.publish("alex9ufo/led/led_status", jsonChar2, MQTTpubQos, true); // true means retain
// ... and resubscribe
client.subscribe("alex9ufo/inTopic/led/led_event", MQTTsubQos);
}
//=======================================================
void mqttDisconnectedCb() {
Serial.println("disconnected");
}
//=======================================================
void mqttDataCb(char* topic, byte* payload, unsigned int length) {
/*
you can convert payload to a C string appending a null terminator to it;
this is possible when the message (including protocol overhead) doesn't
exceeds the MQTT_MAX_PACKET_SIZE defined in the library header.
you can consider safe to do so when the length of topic plus the length of
message doesn't exceeds 115 characters
*/
char* message = (char *) payload;
message[length] = 0;
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
Serial.println(message);
String s = message;
s.trim();
// Switch on the LED if an 1 was received as first character
if (s == "OFF") {
digitalWrite(BUILTIN_LED, LOW); // Turn the LED on (Note that LOW is the voltage level
// but actually the LED is on; this is because
Serial.println("Received OFF , Send LOW TO BuildIn_LED");
Flash = false;
Timer = false;
json ="OFF";
Send = true ;
} // if (s == "OFF")
if (s == "ON") {
digitalWrite(BUILTIN_LED, HIGH); // Turn the LED off (Note that HIGH is the voltage level
// but actually the LED is on; this is because
Serial.println("Received ON , Send HIGH TO BuildIn_LED");
Flash = false;
Timer = false;
json ="ON";
Send = true ;
} //if (s == "ON")
if (s == "TOGGLE") {
digitalWrite(BUILTIN_LED, !digitalRead(BUILTIN_LED)); // Turn the LED toggle
// but actually the LED is on; this is because
Serial.println("Received TOGGLE , Send Toggle(H->L , L->H) TO BuildIn_LED");
Flash = false;
Timer = false;
json ="TOGGLE";
Send = true ;
} //if (s == "TOGGLE")
if (s == "FLASH") {
digitalWrite(BUILTIN_LED, HIGH); // Turn the LED off (Note that HIGH is the voltage level
// but actually the LED is on; this is because
Serial.println("Received FLASH , Flashing BuildIn_LED ");
Flash = true;
Timer = false;
json ="FLASH";
Send = true ;
} //if (message[0] == 'FLASH')
if (s == "TIMER") {
digitalWrite(BUILTIN_LED, HIGH); // Turn the LED off (Note that HIGH is the voltage level
// but actually the LED is on; this is because
Serial.println("Received TIMER , BuildIn_LED ON 5 SEC");
Flash = false;
Timer = true;
Count= 10;
json ="TIMER";
Send = true ;
} //if (message[0] == 'TIMER')
}
//======================================================
void setup() {
Serial.begin(115200);
Serial.println("Configuring ESP32...");
pinMode(BUILTIN_LED, OUTPUT);
Serial.println(F("Booting...."));
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522
//=================================================
Serial.println("Configuring your Mobile WiFi to esp32ap...");
Serial.println("Configuring another WiFi SSID,PWD...");
server.on("/", rootPage);
if (Portal.begin()) {
Serial.println("HTTP server:" + WiFi.localIP().toString());
}
//===================================================
Serial.println(F("Ready!"));
Serial.println(F("Control Build LED ON,OFF,FLASH,TOGGLE,TIMER...."));
Serial.println(F("======================================================"));
Serial.println(F("Scan for Card and print UID:"));
}
//======================================================
void process_mqtt() {
if (WiFi.status() == WL_CONNECTED) {
if (client.connected()) {
client.loop();
} else {
// client id, client username, client password, last will topic, last will qos, last will retain, last will message
if (client.connect(MQTTid, MQTTuser, MQTTpsw, MQTTid "/status", 2, true, "0")) {
pendingDisconnect = false;
mqttConnectedCb();
}
}
} else {
if (client.connected())
client.disconnect();
}
if (!client.connected() && !pendingDisconnect) {
pendingDisconnect = true;
mqttDisconnectedCb();
}
}
//======================================================
void loop() {
process_mqtt();
long now = millis();
if (Flash)
{
digitalWrite(BUILTIN_LED, !digitalRead(BUILTIN_LED));
delay(500);
}
if (Timer)
{
digitalWrite(BUILTIN_LED, HIGH);
delay(500);
Count=Count-1;
if (Count == 0 ){
Timer=false;
digitalWrite(BUILTIN_LED, LOW);
}
}
if (Send) {
// Convert JSON string to character array
json.toCharArray(jsonChar2, json.length()+1);
if (client.connected()) {
Serial.print("Publish message: ");
Serial.println(json);
// Publish JSON character array to MQTT topic
client.publish("alex9ufo/led/led_status",jsonChar2);
}
Send = false;
}
if (WiFi.status() == WL_CONNECTED) {
//========Auto Connect===============================
if (mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial()) { // 如果出現新卡片就讀取卡片資料
delay(100);
String IDNo = printHex(mfrc522.uid.uidByte, mfrc522.uid.size);
// Show some details of the PICC (that is: the tag/card)
if ((IDNo != IDNo_buf) || (now - lastMsg > 3000)) { //不同卡片 或是 等3秒
lastMsg = now;
Serial.print(F("Card UID:"));
Serial.println(IDNo);
//Serial.println(IDNo_buf);
IDNo_buf="";
IDNo_buf=IDNo;
// Convert data to JSON string
String json1 =
"{\"data\":{"
"\"RFID_No\": \"" + IDNo + "\"}"
"}";
// Convert JSON string to character array
json1.toCharArray(jsonChar1, json1.length()+1);
if (client.connected()) {
Serial.print("Publish message: ");
Serial.println(json1);
// Publish JSON character array to MQTT topic
client.publish("alex9ufo/outTopic/RFID/json",jsonChar1);
}
} // if ((IDNo != IDNo_buf) || (now - lastMsg > 5000))
} // if (mfrc522.PICC_IsNewCardPresent()
} //========Auto Connect===============================
Portal.handleClient();
} //Loop
//=========================================================
Node-RED 程式
[{"id":"c678fdd3.80d2e","type":"mqtt in","z":"5eeb4f72.c31c7","name":"","topic":"alex9ufo/led/led_status","qos":"1","datatype":"auto","broker":"841df58d.ee5e98","x":140,"y":100,"wires":[["40a66665.ea45b8","15c7548a.5a9ccb","a107873c.21d928"]]},{"id":"4638d138.51ec4","type":"ui_text","z":"5eeb4f72.c31c7","group":"ac0f1141.eb50e","order":0,"width":0,"height":0,"name":"","label":"ESP32發行到MQTT的資料","format":"{{msg.payload}}","layout":"col-center","x":540,"y":140,"wires":[]},{"id":"e89774d1.c59888","type":"ui_button","z":"5eeb4f72.c31c7","name":"","group":"ac0f1141.eb50e","order":0,"width":"4","height":"2","passthru":false,"label":"LED 開","tooltip":"","color":"white","bgcolor":"","icon":"fa-circle","payload":"ON","payloadType":"str","topic":"","x":100,"y":340,"wires":[["ed3e5d35.5b8e7","b77aa111.18563","b782b30b.315da"]]},{"id":"9f0d7463.a60058","type":"ui_button","z":"5eeb4f72.c31c7","name":"","group":"ac0f1141.eb50e","order":0,"width":"4","height":"2","passthru":false,"label":"LED 關","tooltip":"","color":"black","bgcolor":"","icon":"fa-circle-o","payload":"OFF","payloadType":"str","topic":"","x":100,"y":380,"wires":[["ed3e5d35.5b8e7","b77aa111.18563","b782b30b.315da"]]},{"id":"ed3e5d35.5b8e7","type":"mqtt out","z":"5eeb4f72.c31c7","name":"","topic":"alex9ufo/inTopic/led/led_event","qos":"1","retain":"true","broker":"841df58d.ee5e98","x":410,"y":440,"wires":[]},{"id":"40a66665.ea45b8","type":"function","z":"5eeb4f72.c31c7","name":"Format timestamp","func":"var date = new Date();\nvar h = date.getHours()+8;\nvar m = date.getMinutes();\nvar s = date.getSeconds();\nif(h<10){\n h = '0'+h;\n}\nif(m<10){\n m = '0' + m;\n}\nif(s<10){\n s = '0' + s;\n}\nmsg.payload = msg.payload + ' --> Time:(' + h + ':' + m + ':' + s + ')' ;\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":190,"y":220,"wires":[["bc155223.5e6a"]]},{"id":"bc155223.5e6a","type":"function","z":"5eeb4f72.c31c7","name":"Set Line API ","func":"msg.headers = {'content-type':'application/x-www-form-urlencoded','Authorization':'Bearer A4wwPNh2WqB7dlfeQyyIAwtggn1kfZSI5LkkCdia1gB'};\nmsg.payload = {\"message\":msg.payload};\nreturn msg;\n\n//oR7KdXvK1eobRr2sRRgsl4PMq23DjDlhfUs96SyUBZu","outputs":1,"noerr":0,"x":150,"y":280,"wires":[["b92a5ef.384d0a"]]},{"id":"b92a5ef.384d0a","type":"http request","z":"5eeb4f72.c31c7","name":"","method":"POST","ret":"txt","paytoqs":false,"url":"https://notify-api.line.me/api/notify","tls":"","persist":false,"proxy":"","authType":"","x":320,"y":280,"wires":[["5aedebdb.79a1b4"]]},{"id":"5aedebdb.79a1b4","type":"debug","z":"5eeb4f72.c31c7","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":470,"y":280,"wires":[]},{"id":"15c7548a.5a9ccb","type":"debug","z":"5eeb4f72.c31c7","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":390,"y":100,"wires":[]},{"id":"f472b776.bbeca8","type":"ui_audio","z":"5eeb4f72.c31c7","name":"","group":"ac0f1141.eb50e","voice":"zh-TW","always":true,"x":480,"y":180,"wires":[]},{"id":"c8945e52.7b063","type":"ui_button","z":"5eeb4f72.c31c7","name":"","group":"ac0f1141.eb50e","order":0,"width":"4","height":"2","passthru":false,"label":"LED 開關反向","tooltip":"","color":"blue","bgcolor":"","icon":"fa-circle-o","payload":"TOGGLE","payloadType":"str","topic":"","x":120,"y":420,"wires":[["ed3e5d35.5b8e7","b77aa111.18563","b782b30b.315da"]]},{"id":"a107873c.21d928","type":"function","z":"5eeb4f72.c31c7","name":"","func":"var st1;\nif (msg.payload === \"ON\") {\n st1=\"LED開\"; \n} \nelse if (msg.payload === \"OFF\") {\n st1=\"LED關\";\n}\nelse if (msg.payload === \"FLASH\") {\n st1=\"LED閃爍\";\n}\nelse if (msg.payload === \"TIMER\") {\n st1=\"LED開五秒鐘\";\n}\nelse if (msg.payload === \"TOGGLE\") {\n st1=\"LED ON OFF 交換\";\n}\n\nmsg.payload=st1;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":310,"y":180,"wires":[["f472b776.bbeca8","4638d138.51ec4","ee69d0c7.eac63"]]},{"id":"52e38277.8dfc6c","type":"ui_button","z":"5eeb4f72.c31c7","name":"","group":"ac0f1141.eb50e","order":0,"width":"4","height":"2","passthru":false,"label":"LED 開5秒鐘","tooltip":"","color":"purple","bgcolor":"","icon":"fa-circle-o","payload":"TIMER","payloadType":"str","topic":"","x":110,"y":500,"wires":[["ed3e5d35.5b8e7","b77aa111.18563","b782b30b.315da"]]},{"id":"ee69d0c7.eac63","type":"function","z":"5eeb4f72.c31c7","name":"INSERT","func":"msg.topic = \"INSERT INTO LED (time_led, led_status) VALUES (?,?)\";\n//msg.topic = \"INSERT INTO LED (id, time_led, led_status) VALUES (?,?,?)\";\n\n\nvar Today = new Date();\nvar yyyy = Today.getFullYear(); //年\nvar MM = Today.getMonth()+1; //月\nvar dd = Today.getDate(); //日\nvar h = Today.getHours(); //時\nvar m = Today.getMinutes(); //分\nvar s = Today.getSeconds(); //秒\n\nif(MM<10)\n{\n MM = '0'+MM;\n}\n\nif(dd<10)\n{\n dd = '0'+dd;\n}\n\nif(h<10)\n{\n h = '0'+h;\n}\n\nif(m<10)\n{\n m = '0' + m;\n}\n\nif(s<10)\n{\n s = '0' + s;\n}\n\nvar hms= yyyy + '/'+ MM + '/'+ dd + ' ' + h + ':' + m + ':' + s ;\n//var id= Date.now() ;\n//msg.payload = [id ,hms, msg.payload];\nmsg.payload = [hms, msg.payload];\n\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":480,"y":220,"wires":[["9353d7f7.b37348"]]},{"id":"9353d7f7.b37348","type":"sqlite","z":"5eeb4f72.c31c7","mydb":"19f59ce9.3edc23","sqlquery":"msg.topic","sql":"","name":"LED Status","x":630,"y":220,"wires":[["24cbdab4.befa96"]]},{"id":"24cbdab4.befa96","type":"debug","z":"5eeb4f72.c31c7","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":810,"y":220,"wires":[]},{"id":"3feb0d11.a6cc12","type":"comment","z":"5eeb4f72.c31c7","name":"Node-Red publish 到 HiveMQ Broker , ESP32訂閱","info":"將 alex9ufo/inTopic 到(publish)HiveMQ Broker \n給 Arduino 訂閱(Subscribe)","x":230,"y":540,"wires":[]},{"id":"13c54c12.b76704","type":"comment","z":"5eeb4f72.c31c7","name":"ESP32向 HiveMQ Broker發行 Node-Red 訂閱subscribe","info":"將 Arduino 發行到(publish)HiveMQ Broker alex9ufo/led/led_status \n給 Node-red 或 MQTTB-Box 訂閱(Subscribe)","x":240,"y":60,"wires":[]},{"id":"4fc1cc20.c72bf4","type":"ui_button","z":"5eeb4f72.c31c7","name":"","group":"ac0f1141.eb50e","order":0,"width":"4","height":"2","passthru":false,"label":"LED 閃爍","tooltip":"","color":"yellow","bgcolor":"","icon":"fa-circle-o","payload":"FLASH","payloadType":"str","topic":"","x":100,"y":460,"wires":[["ed3e5d35.5b8e7","b77aa111.18563","b782b30b.315da"]]},{"id":"b77aa111.18563","type":"ui_audio","z":"5eeb4f72.c31c7","name":"","group":"ac0f1141.eb50e","voice":"zh-TW","always":true,"x":360,"y":500,"wires":[]},{"id":"b782b30b.315da","type":"ui_text","z":"5eeb4f72.c31c7","group":"ac0f1141.eb50e","order":0,"width":0,"height":0,"name":"","label":"Node-RED發行到MQTT的資料","format":"{{msg.payload}}","layout":"row-center","x":410,"y":380,"wires":[]},{"id":"370820be.05e2b","type":"function","z":"5eeb4f72.c31c7","name":"產生資料庫TABLE","func":"msg.topic = ' CREATE TABLE \"LED\" (\t\"id\"\tINTEGER,\t\"time_led\"\tTEXT,\"led_status\"\tTEXT,PRIMARY KEY(\"id\" AUTOINCREMENT))';\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":930,"y":340,"wires":[["4e0306a4.572ee8"]]},{"id":"3f380fb7.f8cae","type":"inject","z":"5eeb4f72.c31c7","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":670,"y":360,"wires":[["370820be.05e2b"]]},{"id":"250c32d4.4b76ce","type":"ui_button","z":"5eeb4f72.c31c7","name":"建立LED資料庫(只能執行一次)","group":"ac0f1141.eb50e","order":66,"width":"4","height":"2","passthru":false,"label":"建立LED資料庫(只能執行一次)","tooltip":"","color":"","bgcolor":"","icon":"","payload":"","payloadType":"str","topic":"","x":690,"y":320,"wires":[["370820be.05e2b"]]},{"id":"4e0306a4.572ee8","type":"sqlite","z":"5eeb4f72.c31c7","mydb":"19f59ce9.3edc23","sqlquery":"msg.topic","sql":"","name":"LED Status","x":870,"y":420,"wires":[["381f91b3.49f44e","dbc22efa.ae609"]]},{"id":"381f91b3.49f44e","type":"debug","z":"5eeb4f72.c31c7","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1050,"y":420,"wires":[]},{"id":"dbc22efa.ae609","type":"ui_template","z":"5eeb4f72.c31c7","group":"ac0f1141.eb50e","name":"","order":0,"width":"12","height":"4","format":"<table style=\"width:100%\">\n <tr>\n <th>Index</th> \n <th>Time</th> \n <th>Status</th>\n </tr>\n <tr ng-repeat=\"x in msg.payload | limitTo:20\">\n <td>{{$index}}</td>\n <td>{{msg.payload[$index].time_led }}</td>\n <td>{{msg.payload[$index].led_status}}</td> \n </tr>\n</table>\n","storeOutMessages":true,"fwdInMessages":true,"resendOnRefresh":false,"templateScope":"local","x":960,"y":480,"wires":[[]]},{"id":"e207ffc6.9a995","type":"ui_button","z":"5eeb4f72.c31c7","name":"","group":"ac0f1141.eb50e","order":10,"width":"4","height":"2","passthru":false,"label":"檢視LED資料庫","tooltip":"","color":"","bgcolor":"","icon":"","payload":"檢視LED資料庫","payloadType":"str","topic":"SELECT * FROM LED ORDER BY id DESC LIMIT 100;","x":680,"y":420,"wires":[["4e0306a4.572ee8"]]},{"id":"841df58d.ee5e98","type":"mqtt-broker","name":"","broker":"broker.mqtt-dashboard.com","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"15","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"ac0f1141.eb50e","type":"ui_group","name":"LED讀取","tab":"e03ab9d0.1a8f08","order":1,"disp":true,"width":"15","collapse":false},{"id":"19f59ce9.3edc23","type":"sqlitedb","db":"MQTT_LED.db","mode":"RWC"},{"id":"e03ab9d0.1a8f08","type":"ui_tab","name":"LED","icon":"dashboard","order":1,"disabled":false,"hidden":false}]
沒有留言:
張貼留言