|
|
Line 1: |
Line 1: |
− | {{Project
| + | #REDIRECT [[Space_state#ESP-01_version_.282015-2021.29]] |
− | |Featured=No
| |
− | |State=Completed
| |
− | |Members=xopr
| |
− | |Description=Creating a wireless SpaceState switch
| |
− | |GitHub=esp8266-SpaceAPI
| |
− | |Picture=SpaceState.png
| |
− | }}
| |
− | {{Marked as outdated|The system-to-be-installed uses Tasmota firmware combined with an [MQTT] script}}
| |
− | | |
− | __TOC__
| |
− | | |
− | | |
− | == 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 ==
| |
− | <div class="mw-collapsible mw-collapsed" id="mw-customcollapsible-ESP8266-SpaceState_ino">
| |
− | ==== ESP8266-SpaceState.ino ====
| |
− | <div class="mw-customtoggle-ESP8266-SpaceState_ino mw-code">Click here to view the source code</div>
| |
− | <pre class="mw-collapsible-content">
| |
− | /*
| |
− | 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( ) );
| |
− | }
| |
− | </pre>
| |
− | </div>
| |
− | | |
− | == 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]]
| |
− | | |
− | [[Category:ESP8266]][[Category:SpaceAPI]][[Category:Network]]
| |