Project



Name: ButtonSpace
People: dreamer
Description: An open/closed sign connecting to an API

ButtonSpace

Moar buttons needed!

Currently the button workstm^

Relay-control of the open-sign and temperature-measurement are on the ToDo.

When you change the knob, it sends a signal to the local http://state.lag virtual button, which sends a state-change to https://state.laglab.org, which is read by the esp8266, which then sets the RGB-led to red (closed) or green (open) and turns on the OPEN-sign in the window with a relay.

Parts

  • Button (switch)
  • esp8266
  • RGB-led
  • Open-sign
  • Relay
  • DS18B20
  • ..

Code

Current code:

/**
 * lagspacebutton.ino
 *
 *  Created on: 29.01.2016
 *
 */

#include <Arduino.h>

#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>

#include <ESP8266HTTPClient.h>

#define USE_SERIAL Serial

ESP8266WiFiMulti WiFiMulti;

const int ledr = 14;
const int ledg = 13;
const int ledb = 12;

const int button = 5;
int state;
int old_state = 0;

void setup() {

    USE_SERIAL.begin(115200);
   // USE_SERIAL.setDebugOutput(true);

    USE_SERIAL.println();
    USE_SERIAL.println();
    USE_SERIAL.println();

    for(uint8_t t = 4; t > 0; t--) {
        USE_SERIAL.printf("[SETUP] WAIT %d...\n", t);
        USE_SERIAL.flush();
        delay(1000);
    }

    WiFiMulti.addAP("LAG", "somethingeasytoremember");

    pinMode(ledr, OUTPUT);
    pinMode(ledg, OUTPUT);
    pinMode(ledb, OUTPUT);
    pinMode(button, INPUT_PULLUP);

}

void loop() {
    // wait for WiFi connection
    if((WiFiMulti.run() == WL_CONNECTED)) {
        old_state = state;
        state = digitalRead(button);
        HTTPClient http2;

        if(old_state != state){
          if(state == 1){
              http2.begin("state.lag", 80, "/?spacebutton=closespace");
              http2.GET();
              USE_SERIAL.print("New button state and closing\n");
            }
          else if(state == 0){
              http2.begin("state.lag", 80, "/?spacebutton=openspace");
              http2.GET();
              USE_SERIAL.print("New button state and opening\n");
            }
        } else{
            USE_SERIAL.print("Nothing changed\n");
        }

        HTTPClient http;

        USE_SERIAL.print("[HTTP] begin...\n");
        // configure traged server and url
        http.begin("state.laglab.org", 80, "/botstate"); //HTTP

        USE_SERIAL.print("[HTTP] GET...\n");
        // start connection and send HTTP header
        int httpCode = http.GET();
        if(httpCode) {
            // HTTP header has been send and Server response header has been handled
            USE_SERIAL.printf("[HTTP] GET... code: %d\n", httpCode);

            // file found at server
            if(httpCode == 200) {
                String payload = http.getString();
                USE_SERIAL.println(payload);
                if(payload == "closed"){
                  analogWrite(ledr, 255);
                  analogWrite(ledg, 0);
                  analogWrite(ledb, 0);
                  }
                else if(payload == "open"){
                  analogWrite(ledr, 0);
                  analogWrite(ledg, 255);
                  analogWrite(ledb, 100);
                  }
            }
        } else {
            USE_SERIAL.print("[HTTP] GET... failed, no connection or no HTTP server\n");
        }
    }

    delay(3000);
}

Stuff

The switch, it twiches. ..