- Mon Apr 04, 2016 6:01 am
#44779
I use this page.
https://thearduinoandme.wordpress.com/t ... nary-data/From Arduino code.
1.Connect Wifi
2.Connect Udp
3.Can receive and send messages.
// connect to UDP – returns true if successful or false if not
boolean connectUDP()
{
boolean state = false;
Serial.println("");
Serial.println("Connecting to UDP");
if (UDP.begin(localPort) == 1){
Serial.println("Connection successful");
state = true;
}
else{
Serial.println("Connection failed");
}
return state;
}
// connect to wifi – returns true if successful or false if not
boolean connectWifi()
{
boolean state = true;
int i = 0;
WiFi.config(ip, gateway, subnet);
WiFi.begin(ssid, password);
// Wait for connection
Serial.print("Connecting");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
if (i > 10){
state = false;
break;
}
i++;
}
if (state){
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
else {
Serial.println("");
Serial.println("Connection failed.");
}
return state;
}
// receive udp messages
void udpReceive( void)
{
if(wifiConnected && udpConnected){
// if there’s data available, read a packet
int packetSize = UDP.parsePacket();
if(packetSize) {
// read the packet into packetBufffer
UDP.read(packetBuffer,UDP_TX_PACKET_MAX_SIZE);
Serial.println("Receive:");
Serial.println(packetBuffer);
}
}
}
unsigned char udpSend( char * msg)
{ UDP.beginPacket("192.168.73.1", 1111);
Serial.println(msg);
UDP.write(msg);
return UDP.endPacket();
}
Last edited by Robert Angyal on Mon Apr 04, 2016 1:53 pm, edited 1 time in total.