LAN DS18B20 WEB Temperature Monitor 1 meter Cable
www.WEB-Relays.com
www.Facebook.com/KMtronic

ESP8266: UDP broadcast receiver

Spread the love

 

ESP8266 UDP broadcast receiver

 

 

/*
Wireless Serial using UDP ESP8266
Hardware: NodeMCU
Circuits4you.com
2018
UDP Broadcast multi esp to esp communication
*/

#include <Wire.h> // Enable this line if using Arduino Uno, Mega, etc.

#include "Adafruit_LEDBackpack.h"
#include "Adafruit_GFX.h"

Adafruit_7segment matrix = Adafruit_7segment();

#include <ESP8266WiFi.h>
#include <WiFiUdp.h>

const char *ssid = "KMTRONIC";
const char *pass = "KMTRONIC";

unsigned int localPort = 12345; // local port to listen for UDP packets

IPAddress SendIP(192,168,1,255); //UDP Broadcast IP data sent to all devicess on same network

// A UDP instance to let us send and receive packets over UDP
WiFiUDP udp;

char packetBuffer[9]; //Where we get the UDP data
float temp;
//======================================================================
// Setup
//=======================================================================
void setup()
{
matrix.begin(0x70);

Serial.begin(115200);
Serial.println();

WiFi.begin(ssid, pass); //Connect to access point

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());

//Start UDP
Serial.println("Starting UDP");
udp.begin(localPort);
Serial.print("Local port: ");
Serial.println(udp.localPort());
}
//======================================================================
// MAIN LOOP
//======================================================================
void loop()
{
int cb = udp.parsePacket();
if (!cb) 
{
//If serial data is recived send it to UDP
if(Serial.available()>0)
{
udp.beginPacket(SendIP, 2000); //Send Data to Master unit
//Send UDP requests are to port 2000

char a[1];
a[0]=char(Serial.read()); //Serial Byte Read
udp.write(a,1); //Send one byte to ESP8266 
udp.endPacket();
}
}
else {
// We've received a UDP packet, send it to serial
memset(packetBuffer, 0, sizeof(packetBuffer));
udp.read(packetBuffer, 10); // read the packet into the buffer, we are reading only one byte
Serial.println(packetBuffer);
delay(20);
temp = atof (packetBuffer);
matrix.print(temp);
matrix.writeDisplay();
}
}
//=======================================================================

 

 

PHP broadcast

<?php

while(1) {
// Loading the XML file
$xml = simplexml_load_file("http://192.168.1.199/status.xml");

foreach($xml->children() as $sensor)
{
if ($sensor->name == "Office 1" )
{
$msg = $sensor->temp;
//echo $msg . "\r\n";
}
}


$sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); 

$len = strlen($msg); 

echo $msg . "\r\n";

socket_set_option($sock, SOL_SOCKET, SO_BROADCAST, 1);

socket_sendto($sock, $msg, $len, 0 ,'192.168.1.255', 12345);

socket_close($sock);
usleep(500000);

}

?>

 

 

 

KMtronic LAN WEB 8 Channel Relay board