#include <SPI.h>
#include <WiFi.h>
#include <WiFiUdp.h>
#include <Servo.h>
int LEDpin = 8;
char ssid = “TP-LINK_POCKET_3020_986ECC”; // your network SSID (wifi name)
char pass = “88635569”;
char str;
int status = WL_IDLE_STATUS;
WiFiUDP Udp;
unsigned int localPort = 8080; // local port to listen on
byte packetBuffer[5]; // buffer to hold incoming packet
long last_sent = 0;
void setup (){
digitalWrite(LEDpin,LOW);
Serial.begin(9600);
if(WiFi.status() == WL_NO_SHIELD)
{
Serial.println(“WiFi shield not present”);
// dont continue
while(true);
}
String fv = WiFi.firmwareVersion();
if ( fv != “1.1.0”)
Serial.println(“Please upgrade the firmware”);
Serial.println( fv);
//attempt to connect to WiFi network:
while(status != WL_CONNECTED)
{
Serial.print(“Attempting to connect to: “);
Serial.println(ssid);
// connect to WPA/WPA2 network
status = WiFi.begin(ssid,pass);
// wait 8 seconds for connection
delay(1000);
}
Serial.println(“Connected to wifi”);
checkWifiStatus();
Serial.println(”\nStarting connection to server…”);
// if get a connection, begin to recieve signal
Udp.begin(localPort);
//Serial.print(“haha”);
}
void loop()
{
digitalWrite(LEDpin,HIGH);
int esc = packetBuffer[3];
int servo = packetBuffer[2];
int pan = packetBuffer[1];
int tilt = packetBuffer[0];
int packetSize = Udp.parsePacket();
if (packetSize)
{
IPAddress remoteIp = Udp.remoteIP();
// read the packet into packetBuffer
int len = Udp.read(packetBuffer,4);//len = maximum size of the buffer
if (len > 0) packetBuffer[len] = 0;
Serial.print("Data: “);
Serial.print(esc);
Serial.print(” - “);
Serial.print(servo);
Serial.print(” - “);
Serial.print(pan);
Serial.print(” - “);
Serial.print(tilt);
Serial.print(” - ");
Serial.print(“STATE:”);
Serial.print(“remoteIp:”);
Serial.println(remoteIp);
Udp.beginPacket(Udp.remoteIP(), 8081);
str = esc + servo + pan + tilt;
Udp.write(str);
Udp.endPacket();
}
}
void checkWifiStatus() {
// 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);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print(“signal strength (RSSI):”);
Serial.print(rssi);
Serial.println(" dBm");
}