commit 42b555b7a23bc3580a83d805e7b2278a2748991f Author: ReiMerc Date: Mon Nov 6 20:48:51 2023 +0100 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7fc9e3c --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +secrets.h + diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..a7f263c --- /dev/null +++ b/Makefile @@ -0,0 +1,7 @@ +compile: + arduino-cli compile --fqbn arduino:samd:mkrwifi1010 + +run: compile + sudo arduino-cli upload --fqbn arduino:samd:mkrwifi1010 --port /dev/ttyACM0 + sudo cat /dev/ttyACM0 + diff --git a/h3-arduino.ino b/h3-arduino.ino new file mode 100644 index 0000000..0690b50 --- /dev/null +++ b/h3-arduino.ino @@ -0,0 +1,78 @@ +#include +#include +#include + +#include "secrets.h" + +MKRIoTCarrier carrier; +WiFiSSLClient wifi_client; +MqttClient mqtt_client(wifi_client); + +extern const char *wifi_ssid; +extern const char *wifi_password; + +extern const char *mqtt_server; +extern const char *mqtt_username; +extern const char *mqtt_password; + +void setup(void) +{ + Serial.begin(9600); + + Serial.println("Started"); + + carrier.begin(); + carrier.display.init(240, 240); + + pinMode(TFT_BACKLIGHT, OUTPUT); + digitalWrite(TFT_BACKLIGHT, HIGH); + + Serial.println("Connecting to WiFi"); + + WiFi.begin(wifi_ssid, wifi_password); + while (WiFi.status() != WL_CONNECTED) { + Serial.print("."); + delay(1000); + } + + Serial.println("Connected to WiFi"); + + mqtt_client.setUsernamePassword(mqtt_username, mqtt_password); + if (!mqtt_client.connect(mqtt_server, 8883)) { + Serial.print("MQTT connection failed with error code: "); + Serial.println(mqtt_client.connectError()); + return; + } + + Serial.println("Connected to MQTT"); + + mqtt_client.subscribe("display"); + + carrier.display.setCursor(50, 20); + carrier.display.setTextSize(4); + carrier.display.print("Waiting"); +} + +void loop(void) +{ + int message_size = mqtt_client.parseMessage(); + if (message_size) { + char *message = (char *)malloc(message_size + 1); + + for (int i = 0; i < message_size && mqtt_client.available(); i++) { + message[i] = (char)mqtt_client.read(); + } + message[message_size] = '\0'; + + Serial.print("Got message: "); + Serial.println(message); + + carrier.display.fillScreen(0x000); + carrier.display.setCursor(50, 20); + carrier.display.setTextSize(4); + carrier.display.print(message); + + free(message); + } +} +