From 9e1e0606bf6939fb8ba66d11785fa4845f33b0a3 Mon Sep 17 00:00:00 2001 From: ReiMerc Date: Tue, 28 Nov 2023 22:48:48 +0100 Subject: [PATCH] Receive MQTT messages on arduino and display when it has to dispense --- arduino/.gitignore | 2 + arduino/Makefile | 7 ++++ arduino/arduino.ino | 78 +++++++++++++++++++++++++++++++++++++- arduino/secrets.template.h | 13 +++++++ 4 files changed, 98 insertions(+), 2 deletions(-) create mode 100644 arduino/.gitignore create mode 100644 arduino/Makefile create mode 100644 arduino/secrets.template.h diff --git a/arduino/.gitignore b/arduino/.gitignore new file mode 100644 index 0000000..7fc9e3c --- /dev/null +++ b/arduino/.gitignore @@ -0,0 +1,2 @@ +secrets.h + diff --git a/arduino/Makefile b/arduino/Makefile new file mode 100644 index 0000000..a7f263c --- /dev/null +++ b/arduino/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/arduino/arduino.ino b/arduino/arduino.ino index 74f966a..e7c90ef 100644 --- a/arduino/arduino.ino +++ b/arduino/arduino.ino @@ -1,6 +1,80 @@ +#include +#include +#include -void setup() { +#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); + + carrier.display.setCursor(20, 100); + carrier.display.setTextSize(3); + carrier.display.print("Connecting.."); + + Serial.println("Connecting to WiFi..."); + + if (wifi_enterprise) WiFi.beginEnterprise(wifi_ssid, wifi_username, wifi_password); + else WiFi.begin(wifi_ssid, wifi_password); + + while (WiFi.status() != WL_CONNECTED) delay(1000); + + Serial.println("Connected to WiFi, connecting to MQTT..."); + + 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("dispense"); + + carrier.display.fillScreen(0x000); } -void loop() { +void loop(void) +{ + int message_size = mqtt_client.parseMessage(); + if (message_size) { + Serial.print("Got message with topic: "); + Serial.println(mqtt_client.messageTopic()); + + if (mqtt_client.messageTopic() == "dispense") { + Serial.println("Dispensing..."); + + // TODO Connect to motor + + carrier.display.fillScreen(0x000); + carrier.display.setCursor(20, 100); + carrier.display.setTextSize(4); + carrier.display.print("Dispense"); + + delay(500); + + carrier.display.fillScreen(0x000); + } + } } + diff --git a/arduino/secrets.template.h b/arduino/secrets.template.h new file mode 100644 index 0000000..ba613da --- /dev/null +++ b/arduino/secrets.template.h @@ -0,0 +1,13 @@ +#include + +// Copy this file to secrets.h and fill out the values + +const bool wifi_enterprise = false; +const char *wifi_ssid = "Wifi-name"; +const char *wifi_username = ""; // Only used if wifi_enterprise = true +const char *wifi_password = "password"; + +const char *mqtt_server = "example.hivemq.cloud"; +const char *mqtt_username = "username"; +const char *mqtt_password = "password"; +