Wifi Canary

Introduction to Physical Computing

15 Oct 2018

I made a wifi canary out of my nodemcu to test why I was having an unstable connection with my socket server running on my NodeMCU.

My previous project I controlled a servo motor with through my browser, however, I frequently would lose the ability to control the servo. I had a hypothesis that my servo would reset my server or lose connection to Wifi. A classmate of mine advised to upgrade the firmware and online research suggested that the ESP8266 module needed 5volts to maintain connection. I ameliorated this by reflashing the firmware and I used a phone battery charging pack but to no avail.

Out of ideas, I wanted to be able to check when exactly I was losing connection in order to find the cause of the drop. Roland suggested that I use serial print statements to debug..but no.. this is physical computing and I should make a physical interface.

I made a wifi canary to connect to wifi/ start a server and would indicate my connection status with leds. Let me step through my code.

First I wanted to indicate with a green light that my wifi was connected, a red light when my wifi was disconnected and lastly a white led when a client was connected to the server. I also wanted to make sure I could differentiate between a lost connection and when a connection was yet to be made.

Here I set my pins and write functions blinking functions.


int RED = 12;
const int GRE = 13;
const int WHI = 15;

// sets blink time
const int hightTime = 100;
const int lowTime = 100;

// set wifi indicator
int wifiStatus;
int connectSuccess = 0;

void red(){
    digitalWrite(RED, HIGH), delay(hightTime), digitalWrite(RED, LOW), delay(lowTime);
}


void green(){
    digitalWrite(GRE, HIGH), delay(hightTime), digitalWrite(GRE, LOW), delay(lowTime);
}

void white(){
    digitalWrite(WHI, HIGH), delay(hightTime), digitalWrite(WHI, LOW), delay(lowTime);
}


void blinkCycle(){
    red();
    green();
    white();
    delay(100);
}

I try to connect to wifi toin the setup. While it is connecting I cycle through my lights. I start a server after the wifi connects and print out the ip address for my purposes. At this point the wifi should be connected, the server is connected and waiting for a client to connect.

My loop function simply checks if the wifi is connected and lights green if its on wifi, and red if it is not connected. If my wifi is connected I also check if a client is connected to the server. If there is a connection light the white led. Also if there is a connection to the client print a line.

void loop() {

    wifiStatus = WiFi.status();    
    
    if(wifiStatus == WL_CONNECTED){
        // if connected blink green
        green();
        connectSuccess ++;

        // check if a client is connected
        checkClient();
    
    } else if (connectSuccess!=0){
        // if connect lost blink red
        red();        
        Serial.println("Womp Womp. I'm lost\nI was found");
    } 
    // print number of times it connected
    // Serial.println(connectSuccess); 
    delay(1000);    
}

void checkClient(){
    WiFiClient client = server.available();
    if (client){
        digitalWrite(12,HIGH);
        while(client.connected()){
            while(client.available()>0){
                char c = client.read();
                Serial.write(c);
                client.println("Welcome to my server. :) "); 
                delay(1);
            }
            delay(10);
        }
        digitalWrite(12,LOW);
        client.stop();
        Serial.println("Client disconnected");
    } 
}

:100:

With this setup I found out that my connection is more solid than I anticipated. There weren’t any drops while I was connected to the server or wifi wasn’t dropping either! I’m pretty stoked about this setup to find my connection status but now it means that there is a different problem with my servo. I’ll test again soon.

#include <ESP8266WiFi.h>

const char* ssid = ""; // insert wifi name
const char* password = ""; // insert wifi pw
WiFiServer server =(80);
// sets led pins
const int RED = 12;
const int GRE = 13;
const int WHI = 15;

// sets blink time
const int hightTime = 100;
const int lowTime = 100;

// set wifi indicator
int wifiStatus;
int connectSuccess = 0;


void setup() {
    Serial.begin(115200);
    delay(1000);
    Serial.println("Something started!");
    
    pinMode(RED, OUTPUT);
    pinMode(GRE, OUTPUT);
    pinMode(WHI, OUTPUT);


    Serial.println("\n");
    Serial.print("Connecting to: ");
    Serial.println(ssid);
    WiFi.begin(ssid, password);
    while(WiFi.status()!=WL_CONNECTED){
        // if never connected blink white.
        delay(1000);
        blinkCycle();
        Serial.println("Hold your horses! I'm still connecting!!!");
    }
    Serial.print("Connected to WiFi. IP: http://");
    Serial.print(WiFi.localIP());
    Serial.print("/\n");
    server.begin();
}

void loop() {

    wifiStatus = WiFi.status();    
    
    if(wifiStatus == WL_CONNECTED){
        // if connected blink green
        green();
        connectSuccess ++;

        checkClient();
        // Serial.println("I'm connected!!!");
    } else if(connectSuccess!=0){
        // if connect lost blink red
        red();        
        Serial.println("Womp Womp. I'm lost\nI was found");
    } 
    // print number of times it connected
    // Serial.println(connectSuccess); 
    delay(1000);    

}
void red(){
    digitalWrite(RED, HIGH), delay(hightTime), digitalWrite(RED, LOW), delay(lowTime);
}


void green(){
    digitalWrite(GRE, HIGH), delay(hightTime), digitalWrite(GRE, LOW), delay(lowTime);
}

void white(){
    digitalWrite(WHI, HIGH), delay(hightTime), digitalWrite(WHI, LOW), delay(lowTime);
}


void blinkCycle(){
    red();
    green();
    white();
    delay(100);
}

void checkClient(){
    WiFiClient client = server.available();
    if (client){
        digitalWrite(12,HIGH);
        while(client.connected()){
            while(client.available()>0){
                char c = client.read();
                Serial.write(c);
                client.println("Welcome to my server. :) ");
                
                delay(1);
            }
            delay(10);
        }
        digitalWrite(12,LOW);
        client.stop();
        Serial.println("Client disconnected");
    } 
}