listeningdaemon/arduino/MQTT/MQTT.ino

197 lines
4.9 KiB
Arduino
Raw Normal View History

#include <Arduino.h>
2024-10-26 09:56:48 +00:00
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
#include "certificate.h"
2024-10-29 13:46:41 +00:00
#include "secret.h"
#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
WiFiClient wifiClient;
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP);
PubSubClient mqttClient(wifiClient);
2024-10-29 13:46:41 +00:00
#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
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) {
bool state = false;
if (payload > 1) {
state = true;
2024-10-26 09:56:48 +00:00
}
2024-10-29 13:46:41 +00:00
digitalWrite(pin, state);
delay(500);
}
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 = "";
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 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) {
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
}
2024-10-26 09:56:48 +00:00
2024-10-28 14:56:27 +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);
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
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
2024-10-28 14:56:27 +00:00
2024-10-29 13:46:41 +00:00
#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);
2024-10-26 09:56:48 +00:00
}
void 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();
}