Difference between revisions of "SpaceStateIndicator"
Coolepascal (talk | contribs) |
Coolepascal (talk | contribs) |
||
Line 48: | Line 48: | ||
<syntaxhighlight lang="cpp"> | <syntaxhighlight lang="cpp"> | ||
+ | <pre> | ||
// Title: SpaceStateIndicator.ini | // Title: SpaceStateIndicator.ini | ||
// Function: Arduino Space State indicator for home | // Function: Arduino Space State indicator for home | ||
Line 245: | Line 246: | ||
delay(ACQUIRE_REPEAT_DELAY); | delay(ACQUIRE_REPEAT_DELAY); | ||
} | } | ||
+ | </pre> | ||
</syntaxhighlight> | </syntaxhighlight> |
Revision as of 12:22, 21 January 2022
My verry fancy space state indicator broke down, so i tried to recreate it using stuff i had laying around, which was a Wemos D1 R1 arduino clown to which i added a shield with a RGB led. There is also an output to drive a beeper that sounds if the space state changens to 'Open'
Pin Numbering on this board is a bit confusing but this how i did set it up.
Led-Red | pin 7 |
Led-Green | pin 6 |
Led-Blue | pin 5 |
(In this version i did not hookup the beeper anymore, although the software still supports it) The RGB Led is hooked up to the pins using 100 Ohm resistors which offers adequate current to give a clear indication.
In the code provided below the login credentials for your personal wifi router have to be setup. The flashy light sequence "Das blinken das lichtes| shows during startup if everything goes well. The following startup stages are implemented for diagnostics
During startup | All colors will flash once |
During WiFi login | Blue Color flashing Red until login was sucsesfull |
Running | Red, Green or Yellow depending on the SpaceState or Dark on server error. |
Eventualy the Space indicator should show eighter Green for OPEN or Red for closed. If there is a problem in requesting the actual space state, the indicator shows Yellow. And it will remain dark if the server could not be connected.
The SpaceState is requested each 15 seconds, indicated by a show blue flash on the indicator
You can copy the code into a Arduino sketch Select for the board LOLIN(WeMos) D1 R1
If you can't find this board in your Arduino IDE most likely your setup has no support for ESP32 yet. In this case click on File->Preferences And add the following line to 'Additional Boards Manager' https://arduino.esp8266.com/stable/package_esp8266com_index.json
Then tools->Board->Board manager And perform a update. Afther this, you'd select the WeMos board and compile the sketch
<syntaxhighlight lang="cpp">
// Title: SpaceStateIndicator.ini // Function: Arduino Space State indicator for home // Author: CoolePascal (C) 2022 Common Licence dinges // Board: WeMos D1 R1 // Port: .. // CPU: ESP-8266 (Warning, Select WeMos Board !) // Programmer: Arduino Gemma or WeMos D1 const char *ssid = "YourWifiRouter"; const char *passwd = "YourWifiPassword"; const char *host = "ackspace.nl"; const int port = 443; const char* streamId = "...................."; const char* privateKey = "...................."; #define ACQUIRE_REPEAT_DELAY 15000 #define ACQUIRE_FLASH_DELAY 100 #define LEDR D13 #define LEDG D12 #define LEDB D7 #define BELL D8 #define OFF 0 #define LED_RED 1 #define LED_GREEN 2 #define LED_BLUE 4 #define LED_YELLOW 3 #define LED_BELL 8 #define SPACE_ACQUIRE LED_BLUE #define SPACE_OPEN LED_GREEN #define SPACE_CLOSED LED_RED #define SPACE_UNKNOWN LED_YELLOW #include <ESP8266WiFi.h> // Set multi color led void set_RGB(int rgb) { digitalWrite(LEDR, (rgb&LED_RED)==LED_RED); digitalWrite(LEDG, (rgb&LED_GREEN)==LED_GREEN); digitalWrite(LEDB, (rgb&LED_BLUE)==LED_BLUE); digitalWrite(BELL, (rgb&LED_BELL)==LED_BELL); } // Dim the LED, flash once BLUE, then flash RED n times void Signal(int n) { int t=250; set_RGB(OFF); delay(t); set_RGB(LED_BLUE); delay(t); for(int i=0;i<n;i++) { set_RGB(OFF); delay(t); set_RGB(LED_RED); delay(t); } set_RGB(OFF); } // Hardware test void hw_test() { int t=750; set_RGB(LED_RED); delay(t); set_RGB(LED_GREEN); delay(t); set_RGB(LED_BLUE); delay(t); set_RGB(LED_YELLOW); delay(t); set_RGB(LED_BELL); delay(t); set_RGB(OFF); delay(t); } // Initialize ports and Wifi connection void setup() { int l=1; delay(1000); pinMode(LEDR, OUTPUT); pinMode(LEDG, OUTPUT); pinMode(LEDB, OUTPUT); pinMode(BELL, OUTPUT); hw_test(); set_RGB(LED_YELLOW); WiFi.mode(WIFI_STA); WiFi.begin(ssid, passwd); while(WiFi.status() != WL_CONNECTED) { if(l) set_RGB(LED_RED); else set_RGB(LED_BLUE); l=l==0; delay(300); } set_RGB(LED_YELLOW); } // Main loop void loop() { static int spacestate; static int spaceopen=0; int chime=0; WiFiClientSecure client; int clientresult=0; // Startup, try to connect if(client.connect(host, port)) { unsigned long timeout = millis(); String url = "/spaceAPI/"; // Connected to server set_RGB(SPACE_ACQUIRE | spacestate); delay(ACQUIRE_FLASH_DELAY); client.print(String("GET ") + url + " " + "HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n"); while(!clientresult) { if(client.available() == 0) { if(millis() - timeout > 5000) { clientresult=-1; // Server connect timeout Signal(8); } } else { clientresult=1; spacestate=SPACE_UNKNOWN; } } // If connected to server if(clientresult==1) { // Read lines from page until done while(client.available()>0) { String line = client.readStringUntil('\n'); int l = line.indexOf("\"state\":"); if(l!=-1) { if(line.indexOf("\"open\":true")>l) { spacestate = SPACE_OPEN; if(!spaceopen) chime=1; spaceopen = 1; } else if(line.indexOf("\"open\":false")>l) { spacestate = SPACE_CLOSED; spaceopen = 0; } } } } client.stop(); } else { spacestate=OFF; BlinkError(); } if(chime) { set_RGB(spacestate|LED_BELL); delay(100); } set_RGB(spacestate); delay(ACQUIRE_REPEAT_DELAY); }
</syntaxhighlight>