listeningdaemon/arduino/MQTT.ino

194 lines
4.5 KiB
Arduino
Raw Permalink Normal View History

2024-10-29 14:48:07 +00:00
#include "secret.h"
#include <Arduino.h>
2024-10-29 14:48:07 +00:00
#if isESP
#include <WiFi.h>
#else
2024-10-26 09:56:48 +00:00
#include <ESP8266WiFi.h>
2024-10-29 14:48:07 +00:00
#endif
2024-10-26 09:56:48 +00:00
#include <PubSubClient.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
#include "certificate.h"
2024-10-29 14:48:07 +00:00
#define CERT mqtt_broker_cert
#define MSG_BUFFER_SIZE (50)
const char* ssid = VITRINE_SSID;
const char* password = VITRINE_WIFI_PASS;
2024-10-28 14:56:27 +00:00
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;
2024-10-29 13:46:41 +00:00
#define MAIN_CHANNEL "main/#"
2024-10-28 14:56:27 +00:00
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP);
2024-10-29 14:48:07 +00:00
#if isESP
WiFiClient espClient;
PubSubClient mqttClient(espClient);
#else
WiFiClient wifiClient;
PubSubClient mqttClient(wifiClient);
2024-10-29 13:46:41 +00:00
#endif
2024-10-28 14:56:27 +00:00
2024-10-29 13:46:41 +00:00
#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;
}
2024-10-29 13:46:41 +00:00
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
2024-10-26 09:56:48 +00:00
2024-10-29 13:46:41 +00:00
#if hasRelayShield
void togglePower(int pin, int payload) {
if (payload > 1) {
2024-10-29 14:48:07 +00:00
digitalWrite(pin, HIGH);
} else {
digitalWrite(pin, LOW);
2024-10-26 09:56:48 +00:00
}
}
2024-10-29 13:46:41 +00:00
#endif
2024-10-26 09:56:48 +00:00
2024-10-29 13:46:41 +00:00
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: ");
2024-10-29 13:46:41 +00:00
String message = "";
2024-10-29 14:48:07 +00:00
for (unsigned int i = 0; i < length; i++) {
2024-10-29 13:46:41 +00:00
message += (char)payload[i];
}
2024-10-29 13:46:41 +00:00
Serial.println(message);
2024-10-26 09:56:48 +00:00
2024-10-29 14:48:07 +00:00
2024-10-29 13:46:41 +00:00
#if hasMotorshield
if (t.indexOf("/printer/") > 0) {
movePrinter(t, message.toInt());
2024-10-28 14:56:27 +00:00
}
2024-10-29 13:46:41 +00:00
#endif
#if hasRelayShield
if (t.indexOf("/fan/one") > 0) {
2024-10-29 14:48:07 +00:00
togglePower(PIN_FAN1, message.toInt());
2024-10-29 13:46:41 +00:00
} else if (t.indexOf("/fan/two") > 0) {
2024-10-29 14:48:07 +00:00
togglePower(PIN_FAN2, message.toInt());
2024-10-29 13:46:41 +00:00
} else if (t.indexOf("/radio/one") > 0) {
2024-10-29 14:48:07 +00:00
togglePower(PIN_RADIO1, message.toInt());
2024-10-29 13:46:41 +00:00
} else if (t.indexOf("/lamp/one") > 0) {
2024-10-29 14:48:07 +00:00
togglePower(PIN_LAMP1, message.toInt());
2024-10-29 13:46:41 +00:00
} else if (t.indexOf("/lamp/two") > 0) {
2024-10-29 14:48:07 +00:00
togglePower(PIN_LAMP2, message.toInt());
2024-10-29 13:46:41 +00:00
}
#endif
}
2024-10-26 09:56:48 +00:00
void connect() {
while (!mqttClient.connected()) {
Serial.print("Attempting MQTT connection...");
2024-10-29 13:46:41 +00:00
String clientId = arduinoName;
clientId += String(random(0xffff), HEX);
2024-10-29 14:48:07 +00:00
if (mqttClient.connect(clientId.c_str(), mqttUser, mqttPassword)) {
Serial.println("connected");
2024-10-28 14:56:27 +00:00
mqttClient.subscribe(MAIN_CHANNEL);
2024-10-29 13:46:41 +00:00
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);
}
}
2024-10-26 09:56:48 +00:00
}
void setup() {
2024-10-29 13:46:41 +00:00
Serial.begin(115200);
2024-10-28 14:56:27 +00:00
Serial.println("run setup");
2024-10-29 13:46:41 +00:00
#if hasRelayShield
2024-10-29 14:48:07 +00:00
pinMode(PIN_FAN1, OUTPUT);
pinMode(PIN_FAN2, OUTPUT);
pinMode(PIN_RADIO1, OUTPUT);
pinMode(PIN_LAMP1, OUTPUT);
pinMode(PIN_LAMP2, OUTPUT);
2024-10-29 13:46:41 +00:00
#endif
2024-10-28 14:56:27 +00:00
2024-10-29 13:46:41 +00:00
#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);
2024-10-26 09:56:48 +00:00
}
void loop() {
2024-10-29 14:48:07 +00:00
Serial.println("loop");
if (!mqttClient.connected()) {
connect();
2024-10-26 09:56:48 +00:00
}
2024-10-28 14:56:27 +00:00
mqttClient.loop();
timeClient.update();
2024-10-29 14:48:07 +00:00
}