Created
September 22, 2022 14:35
-
-
Save H3wastooshort/340bd567728527909b2158d4438f42ce to your computer and use it in GitHub Desktop.
LED Thingie to show if your favorite Makerspace is open.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Hardware: one ESP01 Module, 2 LEDs, 1 resistor roughly 500Ohm | |
GPIO in RED_LED to red led anode (+) | |
GPIO in GREEN_LED to green led anode (+) | |
both LEDs cathodes to one side of resistor | |
other side of resistor to 3.3V | |
*/ | |
#include <ESP8266WiFi.h> | |
#include <WiFiClientSecureBearSSL.h> | |
#include <ESP8266HTTPClient.h> | |
#include <ArduinoJson.h> | |
#include <ArduinoOTA.h> | |
#define WIFI_SSID "Arvid's IoT" | |
#define WIFI_PASS "6gBB0738JbJ63HisSt1P" | |
#define SPACEAPI_ENDPOINT "https://vspace.one/spaceapi.json" //the URL to a spaces https://spaceapi.io/ endpoint | |
#define UPDATE_INTERVAL 10000 //in ms | |
#define RED_LED 0 | |
#define GREEN_LED 2 | |
#define LED_INVERTED true //true if common anode, false if common cathode | |
HTTPClient http; | |
auto last_millis = millis(); | |
void updateLED() { | |
Serial.println(F("==== Pulling new Data ====")); | |
std::unique_ptr<BearSSL::WiFiClientSecure> ssl_client(new BearSSL::WiFiClientSecure); | |
ssl_client->setInsecure(); //noone is going to do an MITM attack to change an led to green | |
http.begin(*ssl_client, SPACEAPI_ENDPOINT); | |
int16_t httpCode = http.GET(); | |
Serial.print(F("HTTP Code: ")); | |
Serial.println(httpCode); | |
String text = http.getString(); | |
Serial.println(F("---- Payload ----")); | |
Serial.println(text); | |
Serial.println(F("---- EOF ---")); | |
if (httpCode == 200) { | |
DynamicJsonDocument json(2048); | |
deserializeJson(json, text); | |
Serial.print(F("New State: ")); | |
if (json["state"]["open"].is<bool>()) { | |
if (json["state"]["open"]) { | |
digitalWrite(RED_LED, LED_INVERTED); | |
digitalWrite(GREEN_LED, !LED_INVERTED); | |
Serial.println(F("Open")); | |
} | |
else { | |
digitalWrite(RED_LED, !LED_INVERTED); | |
digitalWrite(GREEN_LED, LED_INVERTED); | |
Serial.println(F("Closed")); | |
} | |
} | |
else { | |
digitalWrite(RED_LED, LED_INVERTED); | |
digitalWrite(GREEN_LED, LED_INVERTED); | |
Serial.println(F("ERROR")); | |
} | |
} | |
last_millis = millis(); | |
Serial.println(F("==== Done ====")); | |
} | |
void setup() { | |
// put your setup code here, to run once: | |
Serial.begin(115200); | |
Serial.println(); | |
Serial.println(F("SpaceAPI Indicator")); | |
Serial.print(F("Endpoint: ")); | |
Serial.println(F(SPACEAPI_ENDPOINT)); | |
pinMode(RED_LED, OUTPUT); | |
pinMode(GREEN_LED, OUTPUT); | |
digitalWrite(RED_LED, !LED_INVERTED); | |
digitalWrite(GREEN_LED, !LED_INVERTED); | |
Serial.print(F("WiFi...")); | |
WiFi.begin(WIFI_SSID, WIFI_PASS); | |
while (WiFi.status() != WL_CONNECTED) { | |
Serial.print('.'); | |
delay(100); | |
} | |
Serial.println(F(" OK")); | |
Serial.println(F("OTA...")); | |
ArduinoOTA.begin(); | |
digitalWrite(RED_LED, LED_INVERTED); | |
digitalWrite(GREEN_LED, LED_INVERTED); | |
updateLED(); | |
} | |
void loop() { | |
bool sent_text = false; | |
while (WiFi.status() != WL_CONNECTED) { | |
if (!sent_text) Serial.println(F("Renonnecting WiFi...")); | |
sent_text = true; | |
Serial.print('.'); | |
delay(100); | |
} | |
if (millis() - last_millis > UPDATE_INTERVAL) updateLED(); | |
ArduinoOTA.handle(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment