Compare commits

...

3 Commits

4 changed files with 160 additions and 10 deletions

View File

@ -0,0 +1,106 @@
/*
* 31 mar 2015
* This sketch display UDP packets coming from an UDP client.
* On a Mac the NC command can be used to send UDP. (nc -u 192.168.1.101 2390).
*
* Configuration : Enter the ssid and password of your Wifi AP. Enter the port number your server is listening on.
*
13 apr 2015. Partly working. Packets are received and displayed, but the confirmation packet is not received by the client.
Tested with Mac as client using netcat (nc -u 192.168.1.101 2390)
*/
#include <ESP8266WiFi.h>
#include <WiFiUDP.h>
int status = WL_IDLE_STATUS;
const char* ssid = "yourssid"; // your network SSID (name)
const char* pass = "yourpassword"; // your network password
unsigned int localPort = 2390; // local port to listen for UDP packets
byte packetBuffer[512]; //buffer to hold incoming and outgoing packets
// A UDP instance to let us send and receive packets over UDP
WiFiUDP Udp;
// Multicast declarations
IPAddress ipMulti(239, 0, 0, 57);
unsigned int portMulti = 12345; // local port to listen on
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(115200);
// setting up Station AP
WiFi.begin(ssid, pass);
// Wait for connect to AP
Serial.print("[Connecting]");
Serial.print(ssid);
int tries=0;
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
tries++;
if (tries > 30){
break;
}
}
Serial.println();
printWifiStatus();
Serial.println("Connected to wifi");
Serial.print("Udp Multicast server started at : ");
Serial.print(ipMulti);
Serial.print(":");
Serial.println(portMulti);
Udp.beginMulticast(WiFi.localIP(), ipMulti, portMulti);
}
void loop()
{
int noBytes = Udp.parsePacket();
if ( noBytes ) {
Serial.print(millis() / 1000);
Serial.print(":Packet of ");
Serial.print(noBytes);
Serial.print(" received from ");
Serial.print(Udp.remoteIP());
Serial.print(":");
Serial.println(Udp.remotePort());
// We've received a packet, read the data from it
Udp.read(packetBuffer,noBytes); // read the packet into the buffer
// display the packet contents in HEX
for (int i=1;i<=noBytes;i++){
Serial.print(packetBuffer[i-1],HEX);
if (i % 32 == 0){
Serial.println();
}
else Serial.print(' ');
} // end for
Serial.println();
// send a reply, to the IP address and port that sent us the packet we received
Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
Udp.write("UDP packet received");
Udp.endPacket();
} // end if
}
void printWifiStatus() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
}

View File

@ -7,22 +7,31 @@
#include <ESP8266WiFi.h>
char ssid[] = "AJ_HullFromHome_F1EAA6E5";
char password[] = "humberbridge";
char ssid[] = "ssid";
char password[] = "password";
// The address that the PixelHub beacon is broadcasting on.
IPAddress beaconAddress(239, 62, 148, 30);
// The port number that the PixelHub beacon is broadcasting on.
unsigned int beaconPort = 5050;
// A UDP Client to allow us to recieve UDP datagrams.
WiFiUdp UdpClient;
// A buffer to store recieved datagrams in.
int datagramBufferSize = 256;
byte datagramBuffer[datagramBufferSize];
void setup()
{
// put your setup code here, to run once:
// Setup the serial connection
Serial.begin(4800);
Serial.println("Hello, world!");
// Make sure that WiFi is supported
Serial.println("Beginning connection sequence.");
Serial.print("Attempting to connect to ");
Serial.print(" - ");
Serial.print(ssid);
Serial.print(" - ");
WiFi.begin(ssid, password);
@ -35,13 +44,13 @@ void setup()
}
Serial.println("success!");
printWiFiInfoLocal();
}
void loop()
{
delay(1000);
printWiFiInfoLocal();
delay(1000);
}
void printWiFiInfoLocal()
@ -49,3 +58,38 @@ void printWiFiInfoLocal()
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
}
void findServer()
{
Serial.print("Initialising PixelHub auto detection system - ");
Udp.beginMulticast(WiFi.localIP(), beaconAddress, beaconPort);
Serial.println("success!");
Serial.println("Listening for PixelHub beacon pings.");
while(true) {
int datagramSize = UdpClient.parsePacket();
if(!datagramSize) continue;
Serial.print("Received datagram #");
Serial.print(datagramSize);
Serial.print(" bytes in size from ");
Serial.print(Udp.remoteIP());
Serial.print(":");
Serial.print(Udp.remotePort());
if(datagramSize > datagramBufferSize) {
Serial.println(", but the message is larger than the datagram buffer size.");
continue;
}
Serial.println(".");
Udp.read(datagramBuffer, datagramSize);
Serial.print("Content: ");
for(int i = 0; i < datagramSize; i++) {
Serial.print(datagramBuffer[i], HEX);
Serial.print(" ");
}
Serial.print("\n");
}
}