99 lines
2.3 KiB
C++
99 lines
2.3 KiB
C++
/*
|
|
* Connecting to a WiFi network with WPA
|
|
************************************************
|
|
* Originally adapted from https://www.arduino.cc/en/Tutorial/ConnectWithWPA
|
|
* by Starbeamrainbowlabs
|
|
*/
|
|
|
|
#include <ESP8266WiFi.h>
|
|
#include <WiFiUdp.h>
|
|
|
|
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;
|
|
// The size of the datagram buffer that is used to buffer incoming messages.
|
|
int datagramBufferSize = 256;
|
|
|
|
void setup()
|
|
{
|
|
// Setup the serial connection
|
|
Serial.begin(4800);
|
|
|
|
Serial.println("Hello, world!");
|
|
|
|
Serial.println("Beginning connection sequence.");
|
|
Serial.print("Attempting to connect to ");
|
|
Serial.print(ssid);
|
|
Serial.print(" - ");
|
|
|
|
WiFi.begin(ssid, password);
|
|
|
|
while(WiFi.status() != WL_CONNECTED)
|
|
{
|
|
|
|
|
|
// Wait 10 seconds for the connection to start
|
|
delay(10000);
|
|
}
|
|
|
|
Serial.println("success!");
|
|
|
|
printWiFiInfoLocal();
|
|
}
|
|
|
|
void loop()
|
|
{
|
|
|
|
}
|
|
|
|
void printWiFiInfoLocal()
|
|
{
|
|
Serial.print("IP Address: ");
|
|
Serial.println(WiFi.localIP());
|
|
}
|
|
|
|
void findServer()
|
|
{
|
|
Serial.print("Initialising PixelHub auto detection system - ");
|
|
byte datagramBuffer[datagramBufferSize];
|
|
UdpClient.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(UdpClient.remoteIP());
|
|
Serial.print(":");
|
|
Serial.print(UdpClient.remotePort());
|
|
if(datagramSize > datagramBufferSize) {
|
|
Serial.println(", but the message is larger than the datagram buffer size.");
|
|
continue;
|
|
}
|
|
Serial.println(".");
|
|
|
|
UdpClient.read(datagramBuffer, datagramSize);
|
|
|
|
Serial.print("Content as hex: ");
|
|
for(int i = 0; i < datagramSize; i++) {
|
|
Serial.print(datagramBuffer[i], HEX);
|
|
Serial.print(" ");
|
|
}
|
|
Serial.println();
|
|
Serial.print("Raw content: ");
|
|
Serial.write(datagramBuffer, datagramSize);
|
|
Serial.println();
|
|
}
|
|
}
|
|
|