listeningdaemon/arduino/MQTT/MQTT.ino
2024-10-29 14:46:41 +01:00

197 lines
4.9 KiB
C++

#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
#include "certificate.h"
#include "secret.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/#"
WiFiClient wifiClient;
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP);
PubSubClient mqttClient(wifiClient);
#if hasRelayShield
const int RELAY_PIN_FAN1 = 1;
const int RELAY_PIN_FAN2 = 2;
const int RELAY_PIN_RADIO1 = 3;
const int RELAY_PIN_LAMP1 = 4;
const int RELAY_PIN_LAMP2 = 5;
// const int RELAY_PIN = 6;
// const int RELAY_PIN = 7;
// const int RELAY_PIN = 8;
#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) {
bool state = false;
if (payload > 1) {
state = true;
}
digitalWrite(pin, state);
delay(500);
}
#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(RELAY_PIN_FAN1, message.toInt());
} else if (t.indexOf("/fan/two") > 0) {
togglePower(RELAY_PIN_FAN2, message.toInt());
} else if (t.indexOf("/radio/one") > 0) {
togglePower(RELAY_PIN_RADIO1, message.toInt());
} else if (t.indexOf("/lamp/one") > 0) {
togglePower(RELAY_PIN_LAMP1, message.toInt());
} else if (t.indexOf("/lamp/two") > 0) {
togglePower(RELAY_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");
Serial.print("Connecting to ");
Serial.println(ssid);
#if hasRelayShield
pinMode(RELAY_PIN_FAN1, OUTPUT);
pinMode(RELAY_PIN_FAN2, OUTPUT);
pinMode(RELAY_PIN_RADIO1, OUTPUT);
pinMode(RELAY_PIN_LAMP1, OUTPUT);
pinMode(RELAY_PIN_LAMP2, OUTPUT);
#endif
#if hasMotorshield
Serial.println("i have a motorshield");
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("----");
Serial.println("WiFi connected");
Serial.println("----");
timeClient.begin();
Serial.println("finish setup_wifi");
mqttClient.setServer(mqtt_server, mqtt_server_port);
mqttClient.setCallback(callback);
}
void loop() {
if (!mqttClient.connected()) {
connect();
}
mqttClient.loop();
timeClient.update();
}