listeningdaemon/arduino/MQTT/MQTT.ino

117 lines
2.5 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"
#include "secrets.h"
#define CERT mqtt_broker_cert
#define MSG_BUFFER_SIZE (50)
2024-10-28 14:56:27 +00:00
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-28 14:56:27 +00:00
bool isOn = false;
#define MAIN_CHANNEL "main"
#define RELAY_1 1
//--------------------------------------
// globals
//--------------------------------------
#ifdef MQTT_TLS
2024-10-28 14:56:27 +00:00
WiFiClientSecure wifiClient;
#else
2024-10-28 14:56:27 +00:00
WiFiClient wifiClient;
#endif
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP);
PubSubClient mqttClient(wifiClient);
2024-10-28 14:56:27 +00:00
void setup_wifi() {
delay(10);
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
2024-10-26 09:56:48 +00:00
WiFi.begin(ssid, password);
2024-10-28 14:56:27 +00:00
Serial.println("start while");
2024-10-26 09:56:48 +00:00
while (WiFi.status() != WL_CONNECTED) {
2024-10-28 14:56:27 +00:00
delay(50);
Serial.print(".");
2024-10-26 09:56:48 +00:00
}
2024-10-28 14:56:27 +00:00
Serial.println("i am here 1");
timeClient.begin();
2024-10-26 09:56:48 +00:00
Serial.println("WiFi connected");
}
2024-10-26 09:56:48 +00:00
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived on topic: '");
Serial.print(topic);
Serial.print("' with payload: ");
for (unsigned int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
2024-10-26 09:56:48 +00:00
2024-10-28 14:56:27 +00:00
if (isOn) {
digitalWrite(RELAY_1, HIGH);
isOn = false;
} else {
digitalWrite(RELAY_1, LOW);
isOn = true;
}
Serial.println();
}
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...");
String clientId = "ESP8266Client-";
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);
mqttClient.publish(MAIN_CHANNEL, ("Whaddup can i stay? "));
} 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
}
2024-10-28 14:56:27 +00:00
void setup() {
2024-10-28 14:56:27 +00:00
Serial.begin(9600);
Serial.println("run setup");
setup_wifi();
2024-10-28 14:56:27 +00:00
Serial.println("finish setup_wifi");
pinMode(RELAY_1, OUTPUT);
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();
}