2018年12月18日 星期二

ESP32 & Node-RED TCP OUT Node

ESP32 is TCP Client - Node-RED is TCP Server

1.3 TCP/IP ESP32
ESP32 supply a library that make ESP32 become a client or server. There are some classes that you need to know:
1.3.1 WiFi
We use this class to create a connection to WiFi. It is a static class and It has some important member functions:
- WiFi.begin(ssid, password): You should replace ssid and password with your WiFi ssid and password accordingly.
- WiFi.status(): return the current status of WiFi (connect or not)
- WiFi.localIP(): to get the current local IP address of ESP32.
1.3.2 WiFiClient
We use this class to create a TCP Client instance. It has some important member functions:
- connect(host, port): connect to a TCP server at host (IP address) and port
- print(data[]): send data to server
- stop(): stop connection
- available(): check whether data available for reading or not
- read(): read one byte of data
- read(uint8_t *buf, size_t size): read size bytes of data in to buf
- write(uint8_t data): write one byte data
- write(const uint8_t *buf, size_t size): write size bytes in buf
- connected(): is client connected
1.3.3 WiFiServer
We use this class to create a TCP Server instance. It has some important member functions:
- begin(): start Server
- available(): is there a client want to connect.
- accept(): accept the client connection
- write(uint8_t data): write one byte data
- write(const uint8_t *buf, size_t size): write size bytes in buf
For more information you can refer WiFiClient.h and WiFiServer.h








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


#include <WiFi.h>
/* change ssid and password according to yours WiFi*/
//const char* ssid     = "<SSID>";
//const char* password = "<Passwort>";
const char* ssid     = "alex9ufo";
const char* password = "alex9981";
/*
 * This is the IP address of your PC
 * [Wins: use ipconfig command, Linux: use ifconfig command]
*/
const char* host = "192.168.43.121";
const int port = 8088;
void setup()
{
    Serial.begin(115200);
    Serial.print("Connecting to ");
    Serial.println(ssid);
    /* connect to your WiFi */
    WiFi.begin(ssid, password);
    /* wait until ESP32 connect to WiFi*/
    while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
    }
    Serial.println("");
    Serial.println("WiFi connected with IP address: ");
    Serial.println(WiFi.localIP());
}
void loop()
{
    delay(5000);
    Serial.print("connecting to ");
    Serial.println(host);
    /* Use WiFiClient class to create TCP connections */
    WiFiClient client;
    
    if (!client.connect(host, port)) {
        Serial.println("connection failed");
        return;
    }
    /* This will send the data to the server */
    client.print("Hello world From Arduino ESP32");
    client.stop();
}


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":220,"y":60,"wires":[["c3dd673c.a35d18"]]},{"id":"c3dd673c.a35d18","type":"debug","z":"dfe6f708.aa2998","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":490,"y":60,"wires":[]},{"id":"e8ea98d6.d2fd98","type":"tcp out","z":"dfe6f708.aa2998","host":"192.168.43.121","port":"8088","beserver":"client","base64":false,"end":false,"name":"","x":550,"y":200,"wires":[]},{"id":"f7aeb8a7.499e88","type":"inject","z":"dfe6f708.aa2998","name":"","topic":"","payload":"Hello ESP32 & Node-RED","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":270,"y":200,"wires":[["e8ea98d6.d2fd98"]]}]





沒有留言:

張貼留言

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

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