2018年12月18日 星期二

ESP32 & Node-RED TCP IN Node


ESP32 is TCP Server - Node-RED is TCP Client
ESP32 is in server mode it will wait for connection, data (a “hello world” string) from client and then print this data to Terminal.
Tips: to get the IP address of your PC. Use the command below from Terminal:
- Windows OS: use ipconfig command

當ESP32處於伺服器模式時,它將等待來自客戶端的連接,數據(“hello world”字串),然後將此數據印到終端機Com Port。










 -  tcp in 節點:此節點可以充當客戶端或伺服器模式,但在我們的情況下,我們需要將其充當伺服器,接受ESP32客戶端連接,接收數據並轉發到調節點進行列印。



#include <WiFi.h>
//const char* ssid     = "<SSID>";
//const char* password = "<Passwort>";
const char* ssid     = "alex9ufo";
const char* password = "alex9981";
/* create a server and listen on port 8088 */
WiFiServer server(8088);
void setup()
{
    Serial.begin(115200);
    Serial.print("Connecting to ");
    Serial.println(ssid);
    /* connecting to 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());
    /* start Server */
    server.begin();
}
void loop(){
    /* listen for client */
    WiFiClient client = server.available();
    uint8_t data[30];
    if (client) {                 
      Serial.println("new client");       
      /* check client is connected */         
      while (client.connected()) {         
          if (client.available()) {
              int len = client.read(data, 30);
              if(len < 30){
                  data[len] = '\0'; 
              }else {
                  data[30] = '\0';
              }   
              Serial.print("client sent: ");           
              Serial.println((char *)data);
          }
      }
    }
}

=======================================================
Json檔案

[{"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","58db3572.a5559c"]]},{"id":"e8ea98d6.d2fd98","type":"tcp out","z":"dfe6f708.aa2998","host":"192.168.43.101","port":"8088","beserver":"client","base64":false,"end":false,"name":"","x":550,"y":200,"wires":[]},{"id":"58db3572.a5559c","type":"debug","z":"dfe6f708.aa2998","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":530,"y":300,"wires":[]}]

沒有留言:

張貼留言

2024產專班 作業2 (純模擬)

2024產專班 作業2  (純模擬) 1) LED ON,OFF,TIMER,FLASH 模擬 (switch 控制) 2)RFID卡號模擬 (buttom  模擬RFID UID(不從ESP32) Node-Red 程式 [{"id":"d8886...