listeningdaemon/arduino/MQTT.ino
2024-11-01 13:52:04 +01:00

194 lines
4.5 KiB
C++

#include "secret.h"
#include <Arduino.h>
#if isESP
#include <WiFi.h>
#else
#include <ESP8266WiFi.h>
#endif
#include <PubSubClient.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
#include "certificate.h"
#define CERT mqtt_broker_cert
#define MSG_BUFFER_SIZE (50)
const char* ssid = VITRINE_SSID;
const char* password = VITRINE_WIFI_PASS;
const char* mqtt_server = "mqtt.klank.school"; // eg. your-demo.cedalo.cloud or 192.168.1.11
const uint16_t mqtt_server_port = 7000; // or 8883 most common for tls transport
const char* mqttUser = MQTT_ARDUINO_USERNAME;
const char* mqttPassword = MQTT_ARDUINO_PASS;
#define MAIN_CHANNEL "main/#"
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP);
#if isESP
WiFiClient espClient;
PubSubClient mqttClient(espClient);
#else
WiFiClient wifiClient;
PubSubClient mqttClient(wifiClient);
#endif
#if hasMotorshield
#include <Adafruit_MotorShield.h>
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
Adafruit_DCMotor* motor1 = AFMS.getMotor(1);
Adafruit_DCMotor* motor2 = AFMS.getMotor(2);
Adafruit_DCMotor* motor3 = AFMS.getMotor(3);
Adafruit_DCMotor* motor4 = AFMS.getMotor(4);
void movePrinter(String topic, int payload) {
if (topic.indexOf("speed") > 0) {
if (topic.indexOf("printer/one") > 0) {
motor1->setSpeed(payload);
} else if (topic.indexOf("printer/two") > 0) {
motor2->setSpeed(payload);
} else if (topic.indexOf("printer/three") > 0) {
motor3->setSpeed(payload);
} else if (topic.indexOf("printer/four") > 0) {
motor4->setSpeed(payload);
}
} else {
if (payload == 0) {
payload = 4;
}
if (topic.indexOf("printer/one") > 0) {
motor1->run(payload);
} else if (topic.indexOf("printer/two") > 0) {
motor2->run(payload);
} else if (topic.indexOf("printer/three") > 0) {
motor3->run(payload);
} else if (topic.indexOf("printer/four") > 0) {
motor4->run(payload);
}
}
}
#endif
#if hasRelayShield
void togglePower(int pin, int payload) {
if (payload > 1) {
digitalWrite(pin, HIGH);
} else {
digitalWrite(pin, LOW);
}
}
#endif
void callback(char* topic, uint8_t* payload, unsigned int length) {
String t(topic);
Serial.println();
Serial.print("Message arrived on topic: '");
Serial.print(topic);
Serial.print("' with payload: ");
String message = "";
for (unsigned int i = 0; i < length; i++) {
message += (char)payload[i];
}
Serial.println(message);
#if hasMotorshield
if (t.indexOf("/printer/") > 0) {
movePrinter(t, message.toInt());
}
#endif
#if hasRelayShield
if (t.indexOf("/fan/one") > 0) {
togglePower(PIN_FAN1, message.toInt());
} else if (t.indexOf("/fan/two") > 0) {
togglePower(PIN_FAN2, message.toInt());
} else if (t.indexOf("/radio/one") > 0) {
togglePower(PIN_RADIO1, message.toInt());
} else if (t.indexOf("/lamp/one") > 0) {
togglePower(PIN_LAMP1, message.toInt());
} else if (t.indexOf("/lamp/two") > 0) {
togglePower(PIN_LAMP2, message.toInt());
}
#endif
}
void connect() {
while (!mqttClient.connected()) {
Serial.print("Attempting MQTT connection...");
String clientId = arduinoName;
clientId += String(random(0xffff), HEX);
if (mqttClient.connect(clientId.c_str(), mqttUser, mqttPassword)) {
Serial.println("connected");
mqttClient.subscribe(MAIN_CHANNEL);
mqttClient.publish("main/hello", ("A wild arduino appeared"));
} else {
Serial.print("failed, rc=");
Serial.print(mqttClient.state());
Serial.println(" will try again in 5 seconds");
delay(5000);
}
}
}
void setup() {
Serial.begin(115200);
Serial.println("run setup");
#if hasRelayShield
pinMode(PIN_FAN1, OUTPUT);
pinMode(PIN_FAN2, OUTPUT);
pinMode(PIN_RADIO1, OUTPUT);
pinMode(PIN_LAMP1, OUTPUT);
pinMode(PIN_LAMP2, OUTPUT);
#endif
#if hasMotorshield
if (!AFMS.begin()) {
Serial.println("Could not find Motor Shield. Check wiring.");
while (1)
;
};
motor1->setSpeed(160);
motor1->run(RELEASE);
motor2->setSpeed(160);
motor2->run(RELEASE);
motor3->setSpeed(160);
motor3->run(RELEASE);
motor4->setSpeed(160);
motor4->run(RELEASE);
#endif
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(50);
Serial.print(".");
}
Serial.println("WiFi connected");
timeClient.begin();
mqttClient.setServer(mqtt_server, mqtt_server_port);
mqttClient.setCallback(callback);
}
void loop() {
Serial.println("loop");
if (!mqttClient.connected()) {
connect();
}
mqttClient.loop();
timeClient.update();
}