WOKWI 模擬RFID UID產生 暨 4LED on,off,flash控制(1)
#include <ArduinoMqttClient.h>
#include <WiFi.h>
//MFRC522 程式庫 模擬mfrc522 送出卡號 PB可以控制
int LED1 = 23;
int LED2 = 22;
int LED3 = 21;
int LED4 = 19;
char ssid[]="Wokwi-GUEST";
char pass[]="";
WiFiClient wifiClient;
MqttClient mqttClient(wifiClient);
//const char broker[] = "test.mosquitto.org";
const char broker[] = "broker.mqttgo.io";
//const char broker[] = "broker.mqtt-dashboard.com";
int port = 1883;
const char *SubTopic1 = "alex9ufo/esp32/led";
const char *PubTopic2 = "alex9ufo/esp32/led_status";
const char *PubTopic3 = "alex9ufo/esp32/RFID";
const char willTopic[] = "alex9ufo/esp32/Starting";
int inPin = 12; // pushbutton connected to digital pin 7
int val = 0; // variable to store the read value
bool create=false;
bool Send = false; //true
bool Flash1 = false; //true
bool Flash2 = false; //true
bool Flash3 = false; //true
bool Flash4 = false; //true
String LEDjson = "";
//===========================================================
void onMqttMessage(int messageSize) {
// we received a message, print out the topic and contents
Serial.print("Received a message with topic '");
Serial.print(mqttClient.messageTopic());
String Topic= mqttClient.messageTopic();
Serial.print("', duplicate = ");
Serial.print(mqttClient.messageDup() ? "true" : "false");
Serial.print(", QoS = ");
Serial.print(mqttClient.messageQoS());
Serial.print(", retained = ");
Serial.print(mqttClient.messageRetain() ? "true" : "false");
Serial.print("', length ");
Serial.print(messageSize);
Serial.println(" bytes:");
String message="";
// use the Stream interface to print the contents
while (mqttClient.available()) {
//Serial.print((char)mqttClient.read());
message += (char)mqttClient.read();
}
Serial.println(message);
message.trim();
Topic.trim();
if (Topic=="alex9ufo/esp32/led") {
if (message == "led1on") {
digitalWrite(LED1, HIGH); // Turn on the LED
//ledState = true; //ledState = ture HIGH
//設定 各個 旗號
LEDjson ="LED1ON";
Flash1 = false;
Flash2 = false;
Flash3 = false;
Flash4 = false;
Send = true ;
Serial.println(LEDjson);
}
if (message == "led2on") {
digitalWrite(LED2, HIGH); // Turn on the LED
//ledState = true; //ledState = ture HIGH
//設定 各個 旗號
LEDjson ="LED2ON";
Flash1 = false;
Flash2 = false;
Flash3 = false;
Flash4 = false;
Send = true ;
Serial.println(LEDjson);
}
if (message == "led3on") {
digitalWrite(LED3, HIGH); // Turn on the LED
//ledState = true; //ledState = ture HIGH
//設定 各個 旗號
LEDjson ="LED3ON";
Flash1 = false;
Flash2 = false;
Flash3 = false;
Flash4 = false;
Send = true ;
Serial.println(LEDjson);
}
if (message == "led4on") {
digitalWrite(LED4, HIGH); // Turn on the LED
//ledState = true; //ledState = ture HIGH
//設定 各個 旗號
LEDjson ="LED4ON";
Flash1 = false;
Flash2 = false;
Flash3 = false;
Flash4 = false;
Send = true ;
Serial.println(LEDjson);
}
if (message == "led1off" ) {
digitalWrite(LED1, LOW); // Turn off the LED
//ledState = false; //ledState = false LOW
LEDjson ="LED1OFF";
Flash1 = false;
Flash2 = false;
Flash3 = false;
Flash4 = false;
Send = true ;
Serial.println(LEDjson);
}
if (message == "led2off" ) {
digitalWrite(LED2, LOW); // Turn off the LED
//ledState = false; //ledState = false LOW
LEDjson ="LED2OFF";
Flash1 = false;
Flash2 = false;
Flash3 = false;
Flash4 = false;
Send = true ;
Serial.println(LEDjson);
}
if (message == "led3off" ) {
digitalWrite(LED3, LOW); // Turn off the LED
//ledState = false; //ledState = false LOW
LEDjson ="LED3OFF";
Flash1 = false;
Flash2 = false;
Flash3 = false;
Flash4 = false;
Send = true ;
Serial.println(LEDjson);
}
if (message == "led4off" ) {
digitalWrite(LED4, LOW); // Turn off the LED
//ledState = false; //ledState = false LOW
LEDjson ="LED4OFF";
Flash1 = false;
Flash2 = false;
Flash3 = false;
Flash4 = false;
Send = true ;
Serial.println(LEDjson);
}
if (message == "led1flash" ) {
digitalWrite(LED1, HIGH); // Turn off the LED
Flash1 = true;
//Flash2 = false;
//Flash3 = false;
//Flash4 = false;
LEDjson ="LED1FLASH";
Send = true ;
Serial.println(LEDjson);
}
if (message == "led2flash" ) {
digitalWrite(LED2, HIGH); // Turn off the LED
Flash2 = true;
//Flash1 = false;
//Flash3 = false;
//Flash4 = false;
LEDjson ="LED2FLASH";
Send = true ;
Serial.println(LEDjson);
}
if (message == "led3flash" ) {
digitalWrite(LED3, HIGH); // Turn off the LED
Flash3 = true;
//Flash1 = false;
//Flash2 = false;
//Flash4 = false;
LEDjson ="LED3FLASH";
Send = true ;
Serial.println(LEDjson);
}
if (message == "led4flash" ) {
digitalWrite(LED4, HIGH); // Turn off the LED
Flash4 = true;
//Flash1 = false;
//Flash2 = false;
//Flash3 = false;
LEDjson ="LED4FLASH";
Send = true ;
Serial.println(LEDjson);
}
if (message == "ledalloff" ) {
digitalWrite(LED1, LOW); // Turn off the LED
digitalWrite(LED2, LOW); // Turn off the LED
digitalWrite(LED3, LOW); // Turn off the LED
digitalWrite(LED4, LOW); // Turn off the LED
//ledState = false; //ledState = false LOW
LEDjson ="LEDALLOFF";
Flash1 = false;
Flash2 = false;
Flash3 = false;
Flash4 = false;
Send = true ;
Serial.println(LEDjson);
}
if (message == "ledallon") {
digitalWrite(LED1, HIGH); // Turn on the LED
digitalWrite(LED2, HIGH); // Turn on the LED
digitalWrite(LED3, HIGH); // Turn on the LED
digitalWrite(LED4, HIGH); // Turn on the LED
//ledState = true; //ledState = ture HIGH
//設定 各個 旗號
LEDjson ="LEDALLON";
Flash1 = false;
Flash2 = false;
Flash3 = false;
Flash4 = false;
Send = true ;
Serial.println(LEDjson);
}
Serial.println();
Serial.println("-----------------------");
}
}
//===========================================================
//===========================================================
//判斷 旗號Flash , Timer 是否為真
void LED_Message() {
//判斷 旗號 Flash / timer 是否為真 ? 閃爍 定時
if (Flash1){
digitalWrite(LED1, !digitalRead(LED1));
delay(500);
} //(Flash1)
if (Flash2){
digitalWrite(LED2, !digitalRead(LED2));
delay(500);
} //(Flash2)
if (Flash3){
digitalWrite(LED3, !digitalRead(LED3));
delay(500);
} //(Flash3)
if (Flash4){
digitalWrite(LED4, !digitalRead(LED4));
delay(500);
} //(Flash4)
////判斷 旗號 Send 是否為真 回傳MQTT訊息到MQTT Broker
if (Send) {
// Convert JSON string to character array
Serial.print("Publish message: ");
Serial.println(LEDjson);
LEDjson.trim();
bool retained = false;
int qos = 1;
bool dup = false;
// Publish JSON character array to MQTT topic
mqttClient.beginMessage(PubTopic2, LEDjson.length(), retained, qos, dup); //LED Status
mqttClient.print(LEDjson);
mqttClient.endMessage();
Send = false; //處理過後 旗號 Send為假
}
}
//===========================================================
//===========================================================
//副程式 setup wifi
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid); //print ssid
WiFi.begin(ssid, pass); //初始化WiFi 函式庫並回傳目前的網路狀態
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
} //假設 wifi 未連接 show ………
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
//==================================================================
void setup() {
Serial.begin(115200); // 初始化序列埠
pinMode(LED1, OUTPUT);
digitalWrite(LED1, LOW); // Turn off the LED initially
pinMode(LED2, OUTPUT);
digitalWrite(LED2, LOW); // Turn off the LED initially
pinMode(LED3, OUTPUT);
digitalWrite(LED3, LOW); // Turn off the LED initially
pinMode(LED4, OUTPUT);
digitalWrite(LED4, LOW); // Turn off the LED initially
pinMode(inPin, INPUT); // sets the digital pin 12 as input
randomSeed(analogRead(0)); // 設定亂數種子,增加亂數的隨機性
//Initialize serial and wait for port to open:
Serial.begin(115200); // Initialize serial communications with the PC
while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
setup_wifi();
Serial.println("You're connected to the network");
Serial.println();
String willPayload = "ESP32 Start working....!";
bool willRetain = true;
int willQos = 1;
mqttClient.beginWill(willTopic, willPayload.length(), willRetain, willQos);
mqttClient.print(willPayload);
mqttClient.endWill();
Serial.print("Attempting to connect to the MQTT broker: ");
Serial.println(broker);
if (!mqttClient.connect(broker, port)) {
Serial.print("MQTT connection failed! Error code = ");
Serial.println(mqttClient.connectError());
while (1);
}
Serial.println("You're connected to the MQTT broker!");
Serial.println();
// set the message receive callback
mqttClient.onMessage(onMqttMessage);
Serial.print("Subscribing to topic: ");
Serial.println(SubTopic1);
// subscribe to a topic
// the second parameter sets the QoS of the subscription,
// the the library supports subscribing at QoS 0, 1, or 2
int subscribeQos = 1;
mqttClient.subscribe(SubTopic1, subscribeQos);
Serial.println();
/***
Init SPI bus
Init MFRC522
***/
}
//==================================================================
void loop() {
val = digitalRead(inPin); // read the input pin
if (val==LOW) create= false;
if (val==HIGH) create= true;
// call poll() regularly to allow the library to receive MQTT messages and
// send MQTT keep alives which avoids being disconnected by the broker
mqttClient.poll();
LED_Message();
if (create) {
// 模擬 RFID UID 結構
struct RFID_UID {
uint8_t uidByte[10]; // UID 位元組陣列,最大長度為 10
uint8_t size; // UID 長度
};
// 建立一個 RFID UID 物件
RFID_UID uid;
// 設定 UID 資料
uid.size = 4;
for (int i = 0; i < uid.size; i++) {
uid.uidByte[i] = random(256); // 生成 0-255 的亂數
}
// 顯示 UID
Serial.print("UID: ");
for (int i = 0; i < uid.size; i++) {
Serial.print(uid.uidByte[i] < 0x10 ? " 0" : " "); // 補零
Serial.print(uid.uidByte[i], HEX); // 以十六進位格式輸出
}
Serial.println();
String json = "{ \"uid\": \"";
for (int i = 0; i < uid.size; i++) {
json += String(uid.uidByte[i], HEX);
}
json += "\" }";
json.trim();
bool retained = false;
int qos = 1;
bool dup = false;
mqttClient.beginMessage(PubTopic3, json.length(), retained, qos, dup);
mqttClient.print(json);
mqttClient.endMessage();
Serial.println();
Serial.println(json);
}
}
沒有留言:
張貼留言