Tag: Maple Bacon

  • Making Stephen Wright’s Light Switch with the Adafruit Huzzah!

    23439940833_31be1e9c50_o

     

    Things are Different

    In 2012 I was struggling to put together a stm32 based wireless datalogger using leaflabs libmaple and a $20 wifi module. I had to write most of the code myself because libmaple was based on arduino-0022 and most of the wifi libraries made extensive use of the new classes available after arduino 1.xx (String for instance). It was painful and in the end the project was a complete failure except that I had a premade platform to make an art piece called Stephen Wrights Light Switch.

     

    8346730548_dede0fa72e_n img_0935_23439975343_o

    Nowadays you can get an Arduino capable wifi module for less than 10 bucks.

    Huzzah!

    (look it up on the internets!)


    /*-----------------------------------StephenWrightsLightSwitch.ino
     This is a hack of the provided WifiClient example.
     This is free software (BSD License)
     (C) Donald Delmar Davis , SuspectDevices.
    
     ---------------------------------------------------------------*/
    
    
    #include <ESP8266WiFi.h>
    
    
    #define SWITCH_OFF 1
    #define SWITCH_ON 0
    #define LED_OFF 0
    #define LED_ON 1
    
    
    const char* ssid = "MYWIFI";
    const char* password = "MYSECRET";
    const char* host = "www.suspectdevices.com";
    const int httpPort = 80;
    
    const char* offstate = "OFF";
    const char* onstate = "ON";
    
    #ifdef ESP8266_DEV_BOARD
    const int switchPin = 4;
    const int ledPin = 5;
    #else
    const int switchPin = 4;
    const int ledPin = 5;
    #endif 
    /*
     global variables
    */
    
    bool newSwitchValueFlag = false;
    bool oldSwitchValue = SWITCH_OFF;
    bool lightValue = SWITCH_OFF;
    
    
    /*---------------------------------------------------setup()
    
    */
    void setup() {
     bool flipFlop = SWITCH_OFF;
    
     Serial.begin(115200);
     pinMode(switchPin, INPUT_PULLUP);
     pinMode(ledPin, OUTPUT);
     digitalWrite(ledPin, LED_ON);
     delay(10);
    
     // We start by connecting to a WiFi network
    
     Serial.println();
     Serial.println();
     Serial.print("Connecting to ");
     Serial.println(ssid);
     WiFi.begin(ssid, password);
    
     while (WiFi.status() != WL_CONNECTED) {
     flipFlop = !flipFlop;
     digitalWrite(switchPin, flipFlop);
     delay(500);
     Serial.print(".");
     }
    
     digitalWrite(ledPin, LED_OFF);
    
     Serial.println("");
     Serial.println("WiFi connected");
     Serial.println("IP address: ");
     Serial.println(WiFi.localIP());
    
    }
    
    /*-------------------------------------------------------------------------loop()
    
    */
    void loop() {
     
     bool newSwitchValue;
     
     newSwitchValue = (digitalRead(switchPin)==SWITCH_ON);
     
     Serial.print("Switch="); Serial.println(newSwitchValue?"ON":"OFF");
     
     if (newSwitchValue != oldSwitchValue) {
     newSwitchValueFlag = true;
     } else {
     newSwitchValueFlag = false;
     }
     
     Serial.print("connecting to ");
     Serial.println(host);
    
     // Use WiFiClient class to create TCP connections
     WiFiClient client;
     if (!client.connect(host, httpPort)) {
     Serial.println("connection failed");
     return;
     }
    
     // We now create a URI for the request
     String url = "/art2013/settheswitch/";
     if (newSwitchValueFlag) {
     url += "?state=";
     url += newSwitchValue ? onstate : offstate;
     oldSwitchValue=newSwitchValue;
     }
    
     Serial.print("Requesting URL: ");
     Serial.println(url);
    
     // This will send the request to the server
     client.print(String("GET ") + url + " HTTP/1.1\r\n" +
     "Host: " + host + "\r\n" +
     "Connection: close\r\n\r\n");
     
     for (int t = 8; t > 0 && !client.available(); t--)
     delay(100);
    
     // Read all the lines of the reply from server and print them to Serial
     while (client.available()) {
     String line = client.readStringUntil('\r');
     if (line.lastIndexOf("ON") >= 0) {
     Serial.println("FOUND AN ON!!!");
     lightValue = SWITCH_ON;
     } else {
     lightValue = SWITCH_OFF;
     }
    
     Serial.print(line);
     }
    
     digitalWrite(ledPin, lightValue ? LED_ON : LED_OFF);
    
     Serial.println();
     Serial.println("closing connection");
     // client.close();
    
    }