Browse Source

Display for OpenDoor on Arduino

master
Markus Becker 3 months ago
parent
commit
5e0d3ab8be
Signed by: mtib GPG Key ID: E9EC13667B0D8EAA
  1. 139
      code/hw/arduino/ida_display/ida_display.ino

139
code/hw/arduino/ida_display/ida_display.ino

@ -0,0 +1,139 @@
/*
* This sketch sends a message to a TCP server
*
*/
#include <WiFi.h>
#include <WiFiMulti.h>
#include <WiFiClientSecure.h>
#include <ArduinoJson.h>
#include <HTTPClient.h>
#include <LiquidCrystal.h>
WiFiMulti WiFiMulti;
LiquidCrystal lcd(4, 16, 19, 18, 5, 17);
int scroll_amount = 0;
#define DISPLAY_WIDTH 16
char top_line[256] = " Initializing ";
char bottom_line[256] = " Initializing ";
int loop_counter = 0;
#define UPDATE_INTERVAL 2
void setup()
{
lcd.begin(DISPLAY_WIDTH, 2);
lcd.print(" Starting ");
lcd.setCursor(0, 1);
lcd.print(" Serial ");
Serial.begin(115200);
delay(10);
lcd.setCursor(0, 1);
lcd.print(" WiFi ");
// We start by connecting to a WiFi network
WiFiMulti.addAP("MyWIFI Ida", "Lamppenkey1");
WiFiMulti.addAP("KDG Becker", "0aUAwEZGn2Ya");
Serial.print("Waiting for WiFi... ");
while(WiFiMulti.run() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
lcd.clear();
}
void loop()
{
if (loop_counter % UPDATE_INTERVAL == 0) {
// Use WiFiClient class to create TCP connections
WiFiClientSecure *client = new WiFiClientSecure;
client->setInsecure();
client->setTimeout(15000);
HTTPClient https;
if (https.begin(*client, "https://mtib.dev/opendoor/get")) {
int code = https.GET();
Serial.printf("code: %d\n", code);
String payload = https.getString();
Serial.println(payload);
DynamicJsonDocument doc(4096);
DeserializationError error = deserializeJson(doc, payload);
https.end();
if (error) {
return;
}
const char* msg = doc["msg"];
const char* timestamp = doc["set"];
bool isOpen = doc["open"];
if (msg == 0) {
strcpy(top_line, "No message left");
} else {
strcpy(top_line, msg);
}
Serial.printf("Message: %s\n", top_line);
Serial.printf("Timestamp: %s\n", timestamp);
Serial.printf("Status: %d\n", isOpen);
if (isOpen) {
strcpy(bottom_line, " Come over ");
} else {
strcpy(bottom_line, " Ask first ");
}
scroll_amount = max(0, (int)(strlen(top_line) - DISPLAY_WIDTH));
} else {
Serial.println("Connection failed.");
Serial.println("Waiting 5 seconds before retrying...");
delay(5000);
return;
}
Serial.println("Closing connection.");
client->stop();
loop_counter = 0;
lcd.clear();
}
lcd.setCursor(0, 0);
lcd.print(top_line);
lcd.setCursor(0, 1);
lcd.print(bottom_line);
for (int i = 0; i <= scroll_amount; i++) {
lcd.setCursor(0, 0);
lcd.print(top_line + i);
lcd.setCursor(0, 1);
lcd.print(bottom_line);
delay(800);
}
delay(2000);
for (int i = scroll_amount; i >= 0; i--) {
lcd.setCursor(0, 0);
lcd.print(top_line + i);
lcd.setCursor(0, 1);
lcd.print(bottom_line);
delay(800);
}
delay(5000);
loop_counter += 1;
}
Loading…
Cancel
Save