WeMOS: D1 R1 mini Send A0 via UDP Server

From OnnoWiki
Revision as of 01:02, 24 March 2023 by Onnowpurbo (talk | contribs)
Jump to navigation Jump to search
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>

/* WiFi network name and password */
const char * ssid = "o3";
const char * pwd = "Dzaq1993!";

// IP address to send UDP data to.
// it can be ip address of the server or 
// a network broadcast address
// here is broadcast address
const char * udpAddress = "192.168.0.159";
const int udpPort = 8888;

//create UDP instance
WiFiUDP udp;

void setup(){
  Serial.begin(115200);
  
  //Connect to the WiFi network
  WiFi.begin(ssid, pwd);
  Serial.println("");  

  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
  //This initializes udp and transfer buffer
  udp.begin(udpPort);
} 

void loop() {
  int sensorReading = analogRead(A0); 
  udp.beginPacket(udpAddress, udpPort);
  Serial.println(sensorReading);
  udp.printf("%u \n", sensorReading);
  udp.endPacket();
  delay(1000);
}


Server

IP address 192.168.0.150 Cek menggunakan ifconfig Jalankan

nc -ul 8888

untuk menerima data yang dikirim