2024年4月24日 星期三

Wokwi ESP32 Simulator : ESP32 + LED +MQTT+ Node-RED Dashboard

 Wokwi ESP32 Simulator : ESP32 +  LED +MQTT+ Node-RED Dashboard 









WOKWI ESP32程式

// Create an MQTT Connection
// TCP Connection
// import the WiFi and PubSubClient libraries
//The WiFi library allows ESP32 to establish connections with Wi-Fi networks
//The PubSubClient library enables ESP32 to connect to an MQTT broker for publishing messages and subscribing to topics.
#include <WiFi.h>
#include "PubSubClient.h"
// WiFi
char ssid[]="Wokwi-GUEST";
char pass[]="";
// MQTT Broker
const char *mqtt_broker = "broker.mqtt-dashboard.com";//"broker.emqx.io";//Public Broker
const char *mqtt_username = "hivemqtt";
const char *mqtt_password = "public";
const int mqtt_port = 1883;
// App Protcol ---> Port
const char *topic_publish = "alex9ufo/esp32/p1";//Topic ESP32 Pusblish
const char *topic_subsrcibe = "alex9ufo/esp32/s1";//Topic ESp32 Subs
const char *topic_subsrcibe2 = "alex9ufo/esp32/s2";//Topic ESp32 Subs

WiFiClient hamada;//WIFI Opject
PubSubClient client(hamada);//OOP
//int y ;
//void function(int x);
//call_function(y);
void callback(char *topic, byte *payload, unsigned int length);
#define LED 2
bool ledState=false;

void setup(){
   // Setting LED pin as output
    pinMode(LED, OUTPUT);
    digitalWrite(LED, LOW);  // Turn off the LED initially
    //Open a serial connection to display program results and establish a connection to the Wi-Fi network.
    // Set software serial baud to 115200;
    Serial.begin(115200);
    // Connecting to a Wi-Fi network
    WiFi.begin(ssid, pass);
    while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.println("Connecting to WiFi..");
    }
        Serial.println("Connected to the Wi-Fi network");
    // Utilize PubSubClient to establish a connection with the MQTT broker.
    //connecting to a mqtt broker
    Serial.println("Before Connect MQTT");
    //Broker Configuration
    client.setServer(mqtt_broker, mqtt_port);
   
   while (!client.connected()) {
        String client_id = "esp32-client-";
        client_id =client_id+ String(WiFi.macAddress());
        //consloe.log("x"+"y");//xy

        //ESP32-ID -----> esp32-client-macaddress
        //variable.c_str()--->char [] , String
        Serial.printf("The client %s connects to the public MQTT broker\n", client_id.c_str());
        if (client.connect(client_id.c_str(), mqtt_username, mqtt_password)) {
            Serial.println("Public MQTT broker connected");
        } else {
            Serial.print("failed with state ");
            Serial.print(client.state());//1 , 2 , 5
            delay(2000);
        }
    }
    Serial.println("After Connect MQTT");
    client.publish(topic_publish, "Hi, I'm ESP32 ^^");
    client.subscribe(topic_subsrcibe);
    client.subscribe(topic_subsrcibe2);
    client.setCallback(callback);
}

void loop() {
    client.loop();
}



void callback(char* topic, byte* message, unsigned int length) {
  printPayload(topic, message, length);  
}

void printPayload(char* topic, byte* message, unsigned int length) {
  // Printing Received Message
  Serial.print("Message received on topic: ");
  Serial.println(topic);

  String messageTemp;
  for(int i=0; i<length; i+=1) {
    messageTemp += (char)message[i];
  }
  if(String(topic) == "alex9ufo/esp32/s1"){
      Serial.println("OK1-ON,OFF");
      if(messageTemp=="ON"){
        //Serial.println("Led ON");
        digitalWrite(2,HIGH);
      }  
      else{
        digitalWrite(2,LOW);
      }
  }
  else if(String(topic) == "alex9ufo/esp32/s2"){
    Serial.print("OK2-test messsage ");  
  }
  else{
    Serial.print("Any Thing");  
  }
  Serial.println(messageTemp);
}

WOKWI diagram.json 程式

{
  "version": 1,
  "author": "Anonymous maker",
  "editor": "wokwi",
  "parts": [
    { "type": "wokwi-esp32-devkit-v1", "id": "esp", "top": -321.7, "left": 14.2, "attrs": {} },
    {
      "type": "wokwi-resistor",
      "id": "r1",
      "top": -236.79,
      "left": 182.53,
      "rotate": 90,
      "attrs": { "value": "1000" }
    },
    {
      "type": "wokwi-led",
      "id": "led1",
      "top": -310.28,
      "left": 155.35,
      "attrs": { "color": "red" }
    }
  ],
  "connections": [
    [ "esp:TX0", "$serialMonitor:RX", "", [] ],
    [ "esp:RX0", "$serialMonitor:TX", "", [] ],
    [ "led1:C", "esp:GND.1", "black", [ "v0" ] ],
    [ "r1:1", "led1:A", "red", [ "h0" ] ],
    [ "esp:D2", "r1:2", "red", [ "h0" ] ]
  ],
  "dependencies": {}
}

NODE-RED 程式

[
    {
        "id": "8ad0b62ca9d0f3bd",
        "type": "mqtt in",
        "z": "2ded2ffb8cb68caa",
        "name": "WOKWI--out",
        "topic": "alex9ufo/esp32/p1",
        "qos": "1",
        "datatype": "auto-detect",
        "broker": "841df58d.ee5e98",
        "nl": false,
        "rap": true,
        "rh": 0,
        "inputs": 0,
        "x": 710,
        "y": 340,
        "wires": [
            [
                "274dced93fde72ef",
                "3d40f264b73266ac"
            ]
        ]
    },
    {
        "id": "dc2407b3a8a088eb",
        "type": "ui_button",
        "z": "2ded2ffb8cb68caa",
        "name": "",
        "group": "a717a00c22deda21",
        "order": 3,
        "width": 3,
        "height": 1,
        "passthru": false,
        "label": "ON",
        "tooltip": "",
        "color": "",
        "bgcolor": "",
        "className": "",
        "icon": "",
        "payload": "ON",
        "payloadType": "str",
        "topic": "topic",
        "topicType": "msg",
        "x": 690,
        "y": 120,
        "wires": [
            [
                "af354a115bab8bb7",
                "a53f8fca6ea739e5"
            ]
        ]
    },
    {
        "id": "ad4d88a419f60961",
        "type": "ui_button",
        "z": "2ded2ffb8cb68caa",
        "name": "",
        "group": "a717a00c22deda21",
        "order": 5,
        "width": 3,
        "height": 1,
        "passthru": false,
        "label": "OFF",
        "tooltip": "",
        "color": "",
        "bgcolor": "",
        "className": "",
        "icon": "",
        "payload": "OFF",
        "payloadType": "str",
        "topic": "topic",
        "topicType": "msg",
        "x": 690,
        "y": 160,
        "wires": [
            [
                "a53f8fca6ea739e5",
                "af354a115bab8bb7"
            ]
        ]
    },
    {
        "id": "8956f4dd3a6211e1",
        "type": "mqtt out",
        "z": "2ded2ffb8cb68caa",
        "name": "Test",
        "topic": "alex9ufo/esp32/s2",
        "qos": "1",
        "retain": "true",
        "respTopic": "",
        "contentType": "",
        "userProps": "",
        "correl": "",
        "expiry": "",
        "broker": "841df58d.ee5e98",
        "x": 950,
        "y": 200,
        "wires": []
    },
    {
        "id": "3d40f264b73266ac",
        "type": "ui_text",
        "z": "2ded2ffb8cb68caa",
        "group": "a717a00c22deda21",
        "order": 9,
        "width": 0,
        "height": 0,
        "name": "",
        "label": "訂閱主題 : ",
        "format": "{{msg.payload}}",
        "layout": "row-left",
        "className": "",
        "x": 930,
        "y": 300,
        "wires": []
    },
    {
        "id": "274dced93fde72ef",
        "type": "ui_audio",
        "z": "2ded2ffb8cb68caa",
        "name": "",
        "group": "a717a00c22deda21",
        "voice": "Microsoft Yating - Chinese (Traditional, Taiwan)",
        "always": "",
        "x": 920,
        "y": 340,
        "wires": []
    },
    {
        "id": "af354a115bab8bb7",
        "type": "mqtt out",
        "z": "2ded2ffb8cb68caa",
        "name": "ON/OFF",
        "topic": "alex9ufo/esp32/s1",
        "qos": "1",
        "retain": "true",
        "respTopic": "",
        "contentType": "",
        "userProps": "",
        "correl": "",
        "expiry": "",
        "broker": "841df58d.ee5e98",
        "x": 960,
        "y": 140,
        "wires": []
    },
    {
        "id": "a53f8fca6ea739e5",
        "type": "debug",
        "z": "2ded2ffb8cb68caa",
        "name": "debug 298",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 990,
        "y": 100,
        "wires": []
    },
    {
        "id": "3b366f277e55e09d",
        "type": "ui_button",
        "z": "2ded2ffb8cb68caa",
        "name": "",
        "group": "a717a00c22deda21",
        "order": 5,
        "width": 3,
        "height": 1,
        "passthru": false,
        "label": "Test ",
        "tooltip": "",
        "color": "",
        "bgcolor": "",
        "className": "",
        "icon": "",
        "payload": "Test....",
        "payloadType": "str",
        "topic": "topic",
        "topicType": "msg",
        "x": 690,
        "y": 200,
        "wires": [
            [
                "8956f4dd3a6211e1"
            ]
        ]
    },
    {
        "id": "84d4cce17c19fe1d",
        "type": "ui_button",
        "z": "2ded2ffb8cb68caa",
        "name": "",
        "group": "a717a00c22deda21",
        "order": 5,
        "width": 3,
        "height": 1,
        "passthru": false,
        "label": "Any Thing",
        "tooltip": "",
        "color": "",
        "bgcolor": "",
        "className": "",
        "icon": "",
        "payload": "Any Thing",
        "payloadType": "str",
        "topic": "topic",
        "topicType": "msg",
        "x": 700,
        "y": 240,
        "wires": [
            [
                "638c9840c086dedd"
            ]
        ]
    },
    {
        "id": "638c9840c086dedd",
        "type": "mqtt out",
        "z": "2ded2ffb8cb68caa",
        "name": "Any Thing",
        "topic": "alex9ufo/esp32/s3",
        "qos": "1",
        "retain": "true",
        "respTopic": "",
        "contentType": "",
        "userProps": "",
        "correl": "",
        "expiry": "",
        "broker": "841df58d.ee5e98",
        "x": 960,
        "y": 240,
        "wires": []
    },
    {
        "id": "841df58d.ee5e98",
        "type": "mqtt-broker",
        "name": "",
        "broker": "broker.hivemq.com",
        "port": "1883",
        "clientid": "",
        "autoConnect": true,
        "usetls": false,
        "compatmode": false,
        "protocolVersion": "4",
        "keepalive": "15",
        "cleansession": true,
        "birthTopic": "",
        "birthQos": "0",
        "birthPayload": "",
        "birthMsg": {},
        "closeTopic": "",
        "closePayload": "",
        "closeMsg": {},
        "willTopic": "",
        "willQos": "0",
        "willPayload": "",
        "willMsg": {},
        "userProps": "",
        "sessionExpiry": ""
    },
    {
        "id": "a717a00c22deda21",
        "type": "ui_group",
        "name": "LED",
        "tab": "b83c5c221c74411b",
        "order": 3,
        "disp": true,
        "width": "6",
        "collapse": false,
        "className": ""
    },
    {
        "id": "b83c5c221c74411b",
        "type": "ui_tab",
        "name": "Wokwi",
        "icon": "dashboard",
        "order": 121,
        "disabled": false,
        "hidden": false
    }
]


沒有留言:

張貼留言

113 學年度第 1 學期 RFID應用課程 Arduino程式

113 學年度第 1 學期 RFID應用課程 Arduino程式 https://www.mediafire.com/file/zr0h0p3iosq12jw/MFRC522+(2).7z/file 內含修改過後的 MFRC522 程式庫 (原程式有錯誤) //定義MFRC522...