ESP8266-SpaceState

From Hackerspace ACKspace
Revision as of 14:46, 10 July 2015 by Xopr (talk | contribs) (projectified page: added some info)
Jump to: navigation, search
Project: ESP8266-SpaceState
Featured:
State Active
Members xopr
GitHub No GitHub project defined. Add your project here.
Description Creating a wireless SpaceState switch
Picture
No project picture! Fill in form Picture or Upload a jpeg here


synopsis

This sketch is a web client and will update the SpaceState

It reads the GPIO2 (pulled up) pin where the space state switch is connected to via ground. GPIO0 is reserved for onewire devices.

code

/*
  2015-07-05: Created by xopr
  
  Code based on http://iot-playground.com/2-uncategorised/41-esp8266-ds18b20-temperature-sensor-arduino-ide by Igor Jarc
 
  External libraries:
  - https://github.com/milesburton/Arduino-Temperature-Control-Library
 
  This program is free software; you can redistribute it and/or
  modify it under the terms of the GNU General Public License
  version 2 as published by the Free Software Foundation.
*/

#include <ESP8266WiFi.h>
#include <OneWire.h>
#include <DallasTemperature.h>

//AP definitions
#define AP_SSID     "ACKspaceWifi"
#define AP_PASSWORD "nospacenet"

#define API_SERVER  "ackspace.nl"
#define API_KEY     "<YOUR_KEY>"

#define PIN_SWITCH  2

void updateSpaceState( bool _bState );

void setup()
{
  Serial.begin(115200);

  pinMode( PIN_SWITCH, INPUT_PULLUP );
  
  wifiConnect( );

}


bool bState = false;
unsigned long time = 0;

void loop()
{
  if ( ( millis() - time > 19900 ) || bState != digitalRead( PIN_SWITCH ) )
  {
    delay( 100 );
    
    bState = digitalRead( PIN_SWITCH );

    updateSpaceState( bState );
    updateOldSpaceState( bState );
    time = millis();
  }

  delay( 1 );
}

void updateSpaceState( bool _bState )
{
    // Use WiFiClient class to create TCP connections
  WiFiClient client;
  if (!client.connect( API_SERVER, 80 ) )
  {
    Serial.println( "connection failed" );
    return;
  }

  // We now create a URI for the request
  String url = "/spaceAPI/?update&state=";

  if ( _bState )
    url += 0;
  else
    url += 1;

  url += "&key=";
  url += API_KEY;

  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: " + API_SERVER + "\r\n" + 
                "Connection: close\r\n\r\n");
  delay( 10 );

  // Read all the lines of the reply from server and print them to Serial
  while( client.available( ) )
  {
    String line = client.readStringUntil( '\r' );
    Serial.print( line );
  }

  Serial.println( );
  Serial.println( "closing connection" );
}

void updateOldSpaceState( bool _bState )
{
    // Use WiFiClient class to create TCP connections
  WiFiClient client;
  if (!client.connect( API_SERVER, 80 ) )
  {
    Serial.println( "connection failed" );
    return;
  }

  // We now create a URI for the request
  String url = "/FORGOT_THE_URI";

  if ( _bState )
    url += "KEY_TO_SET_THE_SPACE_CLOSED";
  else
    url += "KEY_TO_SET_THE_SPACE_OPEN";

  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: " + API_SERVER + "\r\n" + 
                "Connection: close\r\n\r\n");
  delay( 10 );

  // Read all the lines of the reply from server and print them to Serial
  while( client.available( ) )
  {
    String line = client.readStringUntil( '\r' );
    Serial.print( line );
  }

  Serial.println( );
  Serial.println( "closing connection" );
}

void wifiConnect()
{
    Serial.print( "Connecting to " );
    Serial.print( AP_SSID );
    WiFi.begin( AP_SSID, AP_PASSWORD );
    while ( WiFi.status() != WL_CONNECTED )
    {
        delay( 1000 );
        Serial.print( "." );
    }
  
    Serial.println( "" );
    Serial.println( "WiFi connected" );

    // Print the IP address
    Serial.println( WiFi.localIP( ) );
}

todo

  • upload the php and database backend
  • switch over to the new API
  • remove the old SpaceAPI implementation

Note: this will be done in ESP8266-SpaceAPI