So everything runs perfectly, but when I take it to work the gpio will flicker off when it's outputting high(non periodically, really random). The module isn't resetting, it still works fine, voltage rail isn't drooping. There are about 3000 people in my office so i'm guessing there's a heck of a lot of wifi traffic going around. Any ideas?
Here's the sketch I was using
/*
* This sketch demonstrates how to set up a simple HTTP-like server.
* The server will set a GPIO pin depending on the request
* http://server_ip/gpio/0 will set the GPIO2 low,
* http://server_ip/gpio/1 will set the GPIO2 high
* server_ip is the IP address of the ESP8266 module, will be
* printed to Serial when the module is connected.
*/
#include <ESP8266WiFi.h>
const char* ssid = "wifiSSID";
const char* password = "wifiPassword";
//http://www.quackit.com/html/online-html-editor/full/
//http://community.thingspeak.com/forum/arduino/static-ip-instead-of-dhcp/
IPAddress ip(192,168,1,110);
IPAddress gateway(192,168,1,1);
IPAddress subnet(255,255,255,0);
const char* APssid = "WiFi GPIO board";
const char* APpassword = "DiggerDog";
// Create an instance of the server
// specify the port to listen on as an argument
WiFiServer server(80);
unsigned long previousReconnectionAttempt = 0;
void setup() {
Serial.begin(115200);
delay(10);
// prepare GPIO2
pinMode(2, OUTPUT);
digitalWrite(2, 0);
pinMode(0, OUTPUT);
digitalWrite(0, 0);
pinMode(4, OUTPUT);
digitalWrite(4, 0);
pinMode(5, OUTPUT);
digitalWrite(5, 0);
pinMode(12, OUTPUT);
digitalWrite(12, 0);
pinMode(13, OUTPUT);
digitalWrite(13, 0);
pinMode(14, OUTPUT);
digitalWrite(14, 0);
pinMode(15, OUTPUT);
digitalWrite(15, 0);
pinMode(16, OUTPUT);
digitalWrite(16, 0);
// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print("creating ");
Serial.println(APssid);
//WiFi.softAP(APssid);
int channel = 2;
WiFi.softAP(APssid, APpassword, channel);
WiFi.begin(ssid, password);
WiFi.config( ip, gateway, subnet);
//if this times out, it would be wise to set a flag and periodically try to reconnect.
//this failure mode is if like power went out then came back on. This module would boot and timeout before
//main router/wifi would have booted. In this scenario this esp8266 program would never recover comms.
while (WiFi.status() != WL_CONNECTED && millis() < 15*1000) {
previousReconnectionAttempt = millis();
delay(500);
Serial.print(".");
}
if(millis() >= 15*1000){
Serial.println("");
Serial.println("could not connect to local WiFi, still creating AP");
}
else{
Serial.println("");
Serial.println("WiFi connected, also created AP");
}
// Start the server
server.begin();
Serial.println("Server started");
// Print the IP address
Serial.println(WiFi.localIP());
}
unsigned long prev = 0;
void loop() {
if(millis() > prev + 5000){
Serial.print("WL_CONNECTED:");
Serial.print(WL_CONNECTED);
Serial.print(",WiFi.status():");
Serial.println(WiFi.status());
prev = millis();
}
if (WiFi.status() != WL_CONNECTED && millis() > previousReconnectionAttempt + 5000){
Serial.println("attempting to reconnect to the access point");
WiFi.begin(ssid, password);
WiFi.config( ip, gateway, subnet);
previousReconnectionAttempt = millis();
delay(500);
}
// Check if a client has connected
WiFiClient client = server.available();
if (!client) {
return;
}
// Wait until the client sends some data
Serial.println("new client");
while(!client.available()){
delay(1);
}
// Read the first line of the request
String req = client.readStringUntil('\r');
Serial.println(req);
client.flush();
// Match the request
int val;
int gpio;
if (req.indexOf("/gpio00/0") != -1){
gpio = 0;
val = 0;
gpioOut(0, 0);
}
else if (req.indexOf("/gpio00/1") != -1){
gpio = 0;
val = 1;
gpioOut(gpio, val);
}
else if (req.indexOf("/gpio02/0") != -1){
gpio = 2;
val = 0;
gpioOut(gpio, val);
}
else if (req.indexOf("/gpio02/1") != -1){
gpio = 2;
val = 1;
gpioOut(gpio, val);
}
else if (req.indexOf("/gpio04/0") != -1){
gpio = 4;
val = 0;
gpioOut(gpio, val);
}
else if (req.indexOf("/gpio04/1") != -1){
gpio = 4;
val = 1;
gpioOut(gpio, val);
}
else if (req.indexOf("/gpio05/0") != -1){
gpio = 5;
val = 0;
gpioOut(gpio, val);
}
else if (req.indexOf("/gpio05/1") != -1){
gpio = 5;
val = 1;
gpioOut(gpio, val);
}
else if (req.indexOf("/gpio12/0") != -1){
gpio = 12;
val = 0;
gpioOut(gpio, val);
}
else if (req.indexOf("/gpio12/1") != -1){
gpio = 12;
val = 1;
gpioOut(gpio, val);
}
else if (req.indexOf("/gpio13/0") != -1){
gpio = 13;
val = 0;
gpioOut(gpio, val);
}
else if (req.indexOf("/gpio13/1") != -1){
gpio = 13;
val = 1;
gpioOut(gpio, val);
}
else if (req.indexOf("/gpio14/0") != -1){
gpio = 14;
val = 0;
gpioOut(gpio, val);
}
else if (req.indexOf("/gpio14/1") != -1){
gpio = 14;
val = 1;
gpioOut(gpio, val);
}
else if (req.indexOf("/gpio15/0") != -1){
gpio = 15;
val = 0;
gpioOut(gpio, val);
}
else if (req.indexOf("/gpio15/1") != -1){
gpio = 15;
val = 1;
gpioOut(gpio, val);
}
else if (req.indexOf("/gpio16/0") != -1){
gpio = 16;
val = 0;
gpioOut(gpio, val);
}
else if (req.indexOf("/gpio16/1") != -1){
gpio = 16;
val = 1;
gpioOut(gpio, val);
}
else {
Serial.println("invalid request, sending homepage");
client.flush();
delay(0);
String mainText = "";
mainText += "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\n";
mainText += "<head>\r\n";
mainText += " <title>esp8266 webserver</title>\r\n";
mainText += "</head>\r\n";
mainText += "<body>\r\n";
mainText += "<h1><a href=\"http://www.esp8266.com\"><span style=\"color:#000000;\">ESP8266 Command</span></a></h1>\r\n";
//since we're offline, there's no point in having an image
//mainText += "<h1><a href=\"192.168.4.1\"><img alt=\"\" src=\"http://g-lab.ca/wp-content/uploads/2014/11/esp8266ex-esp-1-mod.png\" style=\"width: 250px; height: 217px;\" /></a></h1>\r\n";
mainText += "<table border=\"1\" cellpadding=\"1\" cellspacing=\"1\" height=\"247\" width=\"376\">";
mainText += " <thead>\r\n";
mainText += " <tr>\r\n";
mainText += " <th scope=\"col\">GPIO pin</th>\r\n<th scope=\"col\">on</th>\r\n<th scope=\"col\">off</th>\r\n";
mainText += " </tr>\r\n";
mainText += " </thead>\r\n";
mainText += " <tbody>\r\n";
mainText += " <tr>\r\n";
mainText += " <td style=\"text-align: center;\">GPIO 00</td>\r\n";
mainText += " <td style=\"text-align: center;\"><a href=\"http://192.168.4.1/gpio00/1\">on</td>\r\n";
mainText += " <td style=\"text-align: center;\"><a href=\"http://192.168.4.1/gpio00/0\">off</td>\r\n";
mainText += " </tr>\r\n";
mainText += " <tr>\r\n";
mainText += " <td style=\"text-align: center;\">GPIO 02</td>\r\n";
mainText += " <td style=\"text-align: center;\"><a href=\"http://192.168.4.1/gpio02/1\">on</td>\r\n";
mainText += " <td style=\"text-align: center;\"><a href=\"http://192.168.4.1/gpio02/0\">off</td>\r\n";
mainText += " </tr>\r\n";
mainText += " <tr>\r\n";
mainText += " <td style=\"text-align: center;\">GPIO 04</td>\r\n";
mainText += " <td style=\"text-align: center;\"><a href=\"http://192.168.4.1/gpio04/1\">on</td>\r\n";
mainText += " <td style=\"text-align: center;\"><a href=\"http://192.168.4.1/gpio04/0\">off</td>\r\n";
mainText += " </tr>\r\n";
mainText += " <tr>\r\n";
delay(0);
client.print(mainText);
mainText = "";
mainText += " <td style=\"text-align: center;\">GPIO 05</td>\r\n";
mainText += " <td style=\"text-align: center;\"><a href=\"http://192.168.4.1/gpio05/1\">on</td>\r\n";
mainText += " <td style=\"text-align: center;\"><a href=\"http://192.168.4.1/gpio05/0\">off</td>\r\n";
mainText += " </tr>\r\n";
mainText += " <tr>\r\n";
mainText += " <td style=\"text-align: center;\">GPIO 12</td>\r\n";
mainText += " <td style=\"text-align: center;\"><a href=\"http://192.168.4.1/gpio12/1\">on</td>\r\n";
mainText += " <td style=\"text-align: center;\"><a href=\"http://192.168.4.1/gpio12/0\">off</td>\r\n";
mainText += " </tr>\r\n";
mainText += " <tr>\r\n";
mainText += " <td style=\"text-align: center;\">GPIO 13</td>\r\n";
mainText += " <td style=\"text-align: center;\"><a href=\"http://192.168.4.1/gpio13/1\">on</td>\r\n";
mainText += " <td style=\"text-align: center;\"><a href=\"http://192.168.4.1/gpio13/0\">off</td>\r\n";
mainText += " </tr>\r\n";
mainText += " <tr>\r\n";
mainText += " <td style=\"text-align: center;\">GPIO 14</td>\r\n";
mainText += " <td style=\"text-align: center;\"><a href=\"http://192.168.4.1/gpio14/1\">on</td>\r\n";
mainText += " <td style=\"text-align: center;\"><a href=\"http://192.168.4.1/gpio14/0\">off</td>\r\n";
mainText += " </tr>\r\n";
mainText += " <tr>\r\n";
mainText += " <td style=\"text-align: center;\">GPIO 15</td>\r\n";
mainText += " <td style=\"text-align: center;\"><a href=\"http://192.168.4.1/gpio15/1\">on</td>\r\n";
mainText += " <td style=\"text-align: center;\"><a href=\"http://192.168.4.1/gpio15/0\">off</td>\r\n";
mainText += " </tr>\r\n";
mainText += " <tr>\r\n";
mainText += " <td style=\"text-align: center;\">GPIO 16</td>\r\n";
mainText += " <td style=\"text-align: center;\"><a href=\"http://192.168.4.1/gpio16/1\">on</td>\r\n";
mainText += " <td style=\"text-align: center;\"><a href=\"http://192.168.4.1/gpio16/0\">off</td>\r\n";
mainText += " </tr>\r\n";
mainText += " </tbody>\r\n";
mainText += "</table>\r\n";
mainText += "<p>Uptime:" + (String)(millis()/(1000.*3600.)) + " (hours)</p>\r\n";
mainText += "<p>ADC:" + (String)(analogRead(0)) + "/1024*ARef (ARef = 1v, ADC @ 0v = 1)</p>\r\n";
mainText += "<p>possibly link to other esp modules here:</p>\r\n";
mainText += "<ol>\r\n";
mainText += " <li>basement</li>\r\n";
mainText += " <li>sadie's nose</li>\r\n";
mainText += "</ol>\r\n";
mainText += "<p> </p>\r\n";
mainText += "<p> </p>\r\n";
mainText += "<p> </p>\r\n";
mainText += "</body>\r\n";
mainText += "</html>\r\n";
delay(0);
client.print(mainText);
mainText = "";
Serial.println("homepage send complete");
//client.print(millis());
//client.print("<meta http-equiv=\"refresh\" content=\"1\">" );//content="5">");
//client.print("</html>\n");
//Serial.println("printed millis...");
client.stop();
return;
}
// Set GPIO2 according to the request
//digitalWrite(2, val);
client.flush();
// Prepare the response
String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\nGPIO ";
s += (String)gpio;
s += " is now ";
s += (val)?"high":"low";
s += "<meta http-equiv=\"refresh\" content=\"1; url=http://192.168.4.1/\" />";//content=\"1; means a 1 second delay
s += "</html>\n";
// Send the response to the client
client.print(s);
delay(1);
Serial.println("Client disonnected");
// The client will actually be disconnected
// when the function returns and 'client' object is detroyed
}
void gpioOut(int gpio, boolean state){
digitalWrite(gpio,state);
delay(500);
return;
}