Hello,
My task is to send UDP messages from my pc to the arduino uno (connected to the wifi shield) and back. I'm not sure how I'm suppose to do this. I connected my arduino board to wifi using the shield, but I don't know what to do next. Please help me find a solution.
This sketch wait an UDP packet on localPort using a WiFi shield.
When a packet is received an Acknowledge packet is sent to the client on port remotePort
int status = WL_IDLE_STATUS;
char ssid[] = "GTvisitor"; // your network SSID (name)
int keyIndex = 0; // your network key Index number (needed only for WEP)
unsigned int localPort = 2390; // local port to listen on
char packetBuffer[255]; //buffer to hold incoming packet
WiFiUDP Udp;
void setup() {
//Initialize serial and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
// check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
// don't continue:
while(true);
}
// attempt to connect to Wifi network:
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid);
// wait 10 seconds for connection:
delay(10000);
}
Serial.println("Connected to wifi");
printWifiStatus();
Serial.println("\nStarting connection to server...");
// if you get a connection, report back via serial:
Udp.begin(localPort);
}
Hello,
My task is to send UDP messages from my pc to the arduino uno (connected to the wifi shield) and back. I'm not sure how I'm suppose to do this. I connected my arduino board to wifi using the shield, but I don't know what to do next. Please help me find a solution.
It should go something like this if the UDP listener is 192.168.0.2 on port 8888.
IPAddress remoteIP(192,168,0,2);
int remotePort = 8888;
strcpy(packetBuffer,"This is a test");
Udp.beginPacket(remoteIP,remotePort);
Udp.write(packetBuffer,strlen(packetBuffer));
Udp.endPacket();
You did not include a loop() function, so there was really nowhere in your code to put it. But for a test, this should do. It sends a udp packet once a second. It does not listen for a response.
void loop() {
IPAddress remoteIP(192,168,0,2);
int remotePort = 8888;
strcpy(packetBuffer,"This is a test");
Udp.beginPacket(remoteIP,remotePort);
Udp.write(packetBuffer,strlen(packetBuffer));
Udp.endPacket();
delay(1000);
}
WiFiUdpSendReceiveString.ino: In function 'void loop()':
WiFiUdpSendReceiveString:71: error: invalid conversion from 'char*' to 'const uint8_t*'
WiFiUdpSendReceiveString:71: error: initializing argument 1 of 'virtual size_t WiFiUDP::write(const uint8_t*, size_t)'