I thought I would use UDP to control my star tracker from my phone to allow me to select different tracking speeds for planets, moon etc and to control fiddle, diddle and fudge factors to improve accuracy. I started a simple POC sketch.
But to my surprise when the sketch runs I see all my prints OK but the LED doesn't flash. Am I missing something obvious? This code doesn't flash:
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
WiFiServer server(80); //Initialize the server on Port 80
boolean Looping = false;
int ledPin = 13; // LED connected to digital pin 13
int status = WL_CONNECT_FAILED;
char ssid[] = "StarTracker"; // network SSID (name)
char pass[] = "RocketMan"; // network password for WPA
unsigned int localPort = 2390; // local port to listen on
char packetBuffer[255]; //buffer to hold incoming packet
char ReplyBuffer[] = "acknowledged"; // default string to send back
void flash (int flashes, int duration) { // Max 1 second flash
for(int x = 0; x < flashes; x++) {
digitalWrite(BUILTIN_LED, LOW); // sets the LED on
delay(duration); // waits for duration
digitalWrite(BUILTIN_LED, HIGH); // sets the LED off
delay(1000 - duration); // waits for rest of a second
}
}
WiFiUDP Udp;
void setup() {
Serial.begin(115200);
flash(2, 500);
WiFi.mode(WIFI_AP); //Set ESP8266-12E as an AccessPoint
boolean result = WiFi.softAP(ssid, pass);
if(result == true)
{
Serial.println("softAP Ready");
Serial.print("Soft-AP IP address = ");
Serial.println(WiFi.softAPIP());
Serial.printf("MAC address = %s\n", WiFi.softAPmacAddress().c_str());
//Got a connection, report back via UDP:
Udp.begin(localPort);
// send a message to say Wemos ready
Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
Udp.write("Wemos open for business");
Udp.endPacket();
}
else
{
Serial.print("softAP Failed! ");
Serial.println(result);
while (true) { // Send out SOS
flash(3, 250);
delay(250);
flash(3, 750);
delay(250);
flash(3, 250);
delay(1000);
}
}
}
void loop() {
if (!Looping) {
Serial.println("Loop starting");
flash(2,500);
//printWifiStatus();
Looping=true;
}
flash(2, 500);
Serial.printf("Stations connected to soft-AP = %d\n", WiFi.softAPgetStationNum());
// if there's data available, read a packet
int packetSize = Udp.parsePacket();
if (packetSize) {
Serial.print("Received packet of size ");
Serial.println(packetSize);
Serial.print("From ");
IPAddress remoteIp = Udp.remoteIP();
Serial.print(remoteIp);
Serial.print(", port ");
Serial.println(Udp.remotePort());
// read the packet into packetBufffer
int len = Udp.read(packetBuffer, 255);
if (len > 0) {
packetBuffer[len] = 0;
}
Serial.println("Contents:");
Serial.println(packetBuffer);
// send a reply, to the IP address and port that sent us the packet we received
Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
Udp.write(ReplyBuffer);
Udp.endPacket();
}
delay(5000);
}
void printWifiStatus() {
// print the SSID of the network you're attached to:
Serial.println(WiFi.SSID());
// print your WiFiIP 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");
}
Different libraries and a different approach to WiFi but this code does flash:
#include <WiFi.h>
#include <WiFiClient.h>
#include <WiFiServer.h>
#include <WiFiUdp.h>
WiFiServer server(80); //Initialize the server on Port 80
int status = WL_CONNECT_FAILED;
char ssid[] = "StarTracker"; // your network SSID (name)
char pass[] = "RocketMan"; // your network password (use for WPA, or use as key for WEP)
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
char ReplyBuffer[] = "acknowledged"; // a string to send back
void flash (int flashes, int duration) {
for(int x = 0; x < flashes; x++) {
digitalWrite(BUILTIN_LED, LOW); // sets the LED on
delay(duration); // waits for duration
digitalWrite(BUILTIN_LED, HIGH); // sets the LED off
delay(1000 - duration); // waits for rest of a second
}
}
void setup() {
pinMode(BUILTIN_LED, OUTPUT); // sets the ledPin pin as output
flash(2, 500);
Serial.println("Ready");
Serial.begin(115200);
Serial.println("Ready");
flash(20, 100);
//WiFi.mode(WIFI_AP); //Our ESP8266-12E is an AccessPoint
}
void loop() {
// put your main code here, to run repeatedly:
flash(2, 500);
delay(1000);
}
Why does the first sketch not work when the second does. Help would be much appreciated.