2020年12月15日 星期二

RFID 實驗2-2 Node-RED , MQTT , ESP8266 MFRC522 (For ESP8266 Auto connect WIFI 用)

 RFID 實驗2-2 Node-RED , MQTT , ESP8266  MFRC522  (For ESP8266 Auto connect WIFI 用)


修改Arduino程式成為auto connect WIFI 其他的參考

https://alex9ufoexploer.blogspot.com/2020/12/rfid-2-2-node-red-mqtt-esp8266-mfrc522.html


Arduino程式

#include <PubSubClient.h>

#include <SPI.h>

#include "MFRC522.h"

//=========================================

#include <ESP8266WiFi.h>

#include <ESP8266WebServer.h>

#include <AutoConnect.h>

ESP8266WebServer server;

AutoConnect  Portal(server);

//=========================================

#define RST_PIN  D3     // RST-PIN für RC522 - RFID - SPI - D3 

#define SS_PIN   D8     // SDA-PIN für RC522 - RFID - SPI - D8

//=========================================


/* Wiring RFID RC522 module

=============================================================================

GND     = GND   3.3V    = 3.3V

The following table shows the typical pin layout used:

Signal        MFRC522     WeMos D1 mini     NodeMcu     Generic

RST/Reset     RST         D3 [1]            D3 [1]      GPIO-0 [1]

SPI SS        SDA [3]     D8 [2]            D8 [2]      GPIO-15 [2]

SPI MOSI      MOSI        D7                D7          GPIO-13

SPI MISO      MISO        D6                D6          GPIO-12

SPI SCK       SCK         D5                D5          GPIO-14

[1] (1, 2) Configurable, typically defined as RST_PIN in sketch/program.

[2] (1, 2) Configurable, typically defined as SS_PIN in sketch/program.

[3] The SDA pin might be labeled SS on some/older MFRC522 boards

===========================================================================*/


//==========================

#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

     digitalWrite(BUILTIN_LED, HIGH);   // Turn the LED on ESP8266     

     // 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

     digitalWrite(BUILTIN_LED, LOW);      //ESP8266

     // 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

     digitalWrite(BUILTIN_LED, LOW);    //ESP8266

     // 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 ESP8266...");

  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 esp8266ap...");

  Serial.println("Configuring another WiFi SSID,PWD...");

 

  //===================================================  

  server.on("/", rootPage);

  if (Portal.begin()) {

    Serial.println("HTTP server:" + WiFi.localIP().toString());

  }

  else {

    Serial.println("Connection failed");

    while(true) {yield();}

  }

  //=================================================== 

  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, LOW);

    delay(500);

    Count=Count-1;

    if (Count == 0 ){

       Timer=false;

       // digitalWrite(BUILTIN_LED, LOW);  //ESP32

       digitalWrite(BUILTIN_LED, HIGH);   //ESP8266

    }

      

  }

  //=====================

  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 (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 > 2000)) {  //不同卡片 或是 等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 > 2000))

   }  // if (mfrc522.PICC_IsNewCardPresent()

  Portal.handleClient();

}   //Loop

//=========================================================


沒有留言:

張貼留言

WOKWI LED + MQTT Node-Red SQLite

WOKWI LED + MQTT Node-Red SQLite const char *mqtt_broker = "broker.mqtt-dashboard.com" ; const char *topic1 = "alex9ufo/e...