Difference between revisions of "ESP8266-SpaceState"
(set project picture) |
(→ESP8266-SpaceState.ino) |
||
Line 64: | Line 64: | ||
bool bState = false; | bool bState = false; | ||
− | unsigned long | + | unsigned long Time = 0; |
void loop() | void loop() | ||
{ | { | ||
− | if ( ( millis() - | + | if ( ( millis() - Time > 19900 ) || bState != digitalRead( PIN_SWITCH ) ) |
{ | { | ||
delay( 100 ); | delay( 100 ); | ||
Line 76: | Line 76: | ||
updateSpaceState( bState ); | updateSpaceState( bState ); | ||
updateOldSpaceState( bState ); | updateOldSpaceState( bState ); | ||
− | + | Time = millis(); | |
} | } | ||
Revision as of 21:15, 8 December 2015
Project: ESP8266-SpaceState | |
---|---|
Featured: | |
State | Completed |
Members | xopr |
GitHub | No GitHub project defined. Add your project here. |
Description | Creating a wireless SpaceState switch |
Picture | |
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.
hardware
The new Raspberry PI replacement: fast boot times and no SD card corruption anymore, thanks to the ESP8266 The hardware is simple enough to put on a small prototyping board. It has a 3v3 regulator and the 4k7 and 10k pull-ups for onewire and the spacestate switch respectively.
code
ESP8266-SpaceState.ino
Click here to view the source 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 the new SpaceAPI