2018年12月19日 星期三

ESP32 & Node-RED ---Test TCP client to send a random number

The Arduino module can be a simple TCP client that can talk to either a Python or a Node-Red TCP server. Below is an example that sends a random integer to a TCP server every 5 seconds.







/*
Test TCP client to send a random number

The Arduino module can be a simple TCP client that can talk 
to either a Node-Red TCP server. Below is an example 
that sends a random integer to a TCP server every 5 seconds.
 */
//#include <ESP8266WiFi.h>
//#include <ESP8266WiFiMulti.h>
//ESP8266WiFiMulti WiFiMulti;
#include <WiFi.h>

void setup() {
    Serial.begin(115200);

    // We start by connecting to a WiFi network
    //const char * ssid = "your_ssid";       // your WLAN ssid
    //const char * password = "your_password"; // your WLAN password
    const char * ssid = "alex9ufo";       // your WLAN ssid
    const char * password = "alex9981"; // your WLAN password
    Serial.print("Connecting to ");
    Serial.println(ssid);
    WiFi.begin(ssid, password);
    Serial.println();
    Serial.print("Wait for WiFi...");
    while (WiFi.status() != WL_CONNECTED) {
        Serial.print(".");
        delay(500);
    }
    Serial.println("WiFi connected");
    Serial.println("IP address: ");
    Serial.println(WiFi.localIP());
    delay(500);
}

void loop() {
    const uint16_t port = 8088;          // port to use
    /* create a server and listen on port 8088 */
   
    /*
    * This is the IP address of your PC
    * [Wins: use ipconfig command, Linux: use ifconfig command]
    */
    const char* host = "192.168.43.121"; // IP address of  server

    String msg;

    // Use WiFiClient class to create TCP connections
    WiFiClient client;

    if (!client.connect(host, port)) {
        Serial.println("connection failed");
        Serial.println("wait 5 sec...");
        delay(5000);
        return;
    }
   
    // Send a random number to the TCP server
    msg = String(random(0,100));
    client.print(msg);
    Serial.print("Send a random number by ESP32 : ");
    Serial.println(msg);
    client.stop();    
    delay(5000);

}

Json檔案

[{"id":"cdf905ab.fb0718","type":"tcp in","z":"dfe6f708.aa2998","name":"","server":"server","host":"","port":"8088","datamode":"stream","datatype":"utf8","newline":"","topic":"","base64":false,"x":80,"y":60,"wires":[["c3dd673c.a35d18","3405034f.cd33ac"]]},{"id":"c3dd673c.a35d18","type":"debug","z":"dfe6f708.aa2998","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":390,"y":60,"wires":[]},{"id":"3405034f.cd33ac","type":"ui_gauge","z":"dfe6f708.aa2998","name":"","group":"ec8c3389.f982a","order":0,"width":0,"height":0,"gtype":"gage","title":"Reading random number Frmo ESP32","label":"units","format":"{{value}}","min":0,"max":"100","colors":["#00b500","#e6e600","#ca3838"],"seg1":"","seg2":"","x":470,"y":120,"wires":[]},{"id":"ec8c3389.f982a","type":"ui_group","z":"","name":"IO Control","tab":"61198cca.c0fec4","disp":true,"width":"6","collapse":false},{"id":"61198cca.c0fec4","type":"ui_tab","z":"","name":"Home","icon":"dashboard"}]

沒有留言:

張貼留言

2024產專班 作業2

 2024產專班 作業2   1. 系統圖       ESP32+MFRC522 組成RFID Reader 可以將RFID卡片的UID 透過 MQTT協定    上傳(發行 主題 (:topic) alex9ufo/2024/RFID/RFID_UID  ,, Payload...