#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <Ticker.h>
#include <WiFiUdp.h>
#define UDP_PORT 8888
#define LAMP2 2
Ticker tickerSet2;
bool zendHello = false;
WiFiUDP test_udp;
void ticker5s(){
zendHello = true;
}
String readUdp(){
int cb = test_udp.parsePacket();
String stroka ="";
if(cb){
for(int i=0;i<cb;++i) stroka += char(test_udp.read());
return stroka;
}
return stroka;
}
void sendUdp(String data){
IPAddress brobcastAddress(255,255,255,255);
int dataLendth = data.length();
char buff[dataLendth];
data.toCharArray(buff, dataLendth+1);
test_udp.beginPacket(brobcastAddress, 8888);
test_udp.write(buff, dataLendth);
test_udp.endPacket();
}
void test_udpHandle(){
String test_udpData = readUdp();
if(test_udpData != ""){
digitalWrite(LAMP2,!digitalRead(LAMP2));
sendUdp("echo");
}
}
void setup() {
// put your setup code here, to run once:
pinMode ( LAMP2, OUTPUT );
WiFi.mode(WIFI_STA);
WiFi.begin("VOID", "12345678");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
test_udp.begin(UDP_PORT);
tickerSet2.attach_ms(5000, ticker5s);
}
void loop() {
// put your main code here, to run repeatedly:
test_udpHandle();
if(zendHello){
String helloData = "hello";
sendUdp(helloData);
zendHello = false;
}
}