2018年12月18日 星期二

ESP32 & Node-RED TCP Send Data

ESP32 & Node-RED TCP Send Data 













/*
 * This is the IP address of your PC
 * [Wins: use ipconfig command, Linux: use ifconfig command]
*/

const char* server = "192.168.43.121"; // IP address of the Node RED server


#include <WiFi.h>
//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* server = "192.168.43.121"; // IP address of the Node RED server
const int port = 8088;  // Port
const int analogpin=35; //ADC Pin
unsigned long previousMillis = 0; // Last update timestamp
const long interval = 1000;       // Send interval in ms
//===================================================
void setup() {
  Serial.begin(115200);
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected to IP address: ");
  Serial.println(WiFi.localIP());
  pinMode(analogpin , INPUT);
  int analogValue = analogRead(analogpin);
  Serial.println(analogValue);
}
//===================================================
void loop() {
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;
    Serial.print("Connecting to ");
    Serial.println(server);
    WiFiClient client; // For TCP connection
     if (!client.connect(server, port)) {
        Serial.println("Connection failed");
        return;
    }
    int analogValue = analogRead(analogpin);
    Serial.println(analogValue);
    client.print(analogValue); // Send data to the server
    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":180,"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":490,"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 VR Data","label":"units","format":"{{value}}","min":0,"max":"4095","colors":["#00b500","#e6e600","#ca3838"],"seg1":"","seg2":"","x":510,"y":120,"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":510,"y":180,"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":250,"y":180,"wires":[["e8ea98d6.d2fd98"]]},{"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"}]

沒有留言:

張貼留言

Messaging API作為替代方案

  LINE超好用功能要沒了!LINE Notify明年3月底終止服務,有什麼替代方案? LINE Notify將於2025年3月31日結束服務,官方建議改用Messaging API作為替代方案。 //CHANNEL_ACCESS_TOKEN = 'Messaging ...