include relay shield

This commit is contained in:
vitrinekast 2024-10-28 15:56:27 +01:00
parent 45ec7b159d
commit 4b7b8e2cdc

View File

@ -9,62 +9,33 @@
#define CERT mqtt_broker_cert
#define MSG_BUFFER_SIZE (50)
//--------------------------------------
// config (edit here before compiling)
//--------------------------------------
// #define MQTT_TLS // uncomment this define to enable TLS transport
// #define MQTT_TLS_VERIFY // uncomment this define to enable broker certificate verification
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* 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;
const char* mqttTopicIn = "esp-8266-in";
const char* mqttTopicOut = "esp-8266-out";
bool isOn = false;
#define MAIN_CHANNEL "main"
const char* rootCACertificate = R"(
-----BEGIN CERTIFICATE-----
MIIDvTCCAqWgAwIBAgIUIjMIFwrRWBr9z0lTWa7CQ0GcKV0wDQYJKoZIhvcNAQEL
BQAwdDELMAkGA1UEBhMCTkwxFTATBgNVBAgMDFp1aWQtSG9sbGFuZDESMBAGA1UE
BwwJUm90dGVyZGFtMRQwEgYDVQQKDAtLbGFua3NjaG9vbDENMAsGA1UECwwETVFU
VDEVMBMGA1UEAwwMa2xhbmsuc2Nob29sMB4XDTI0MTAyODExMjUwMFoXDTI1MTAy
ODExMjUwMFoweTELMAkGA1UEBhMCTkwxFTATBgNVBAgMDFp1aWQtSG9sbGFuZDES
MBAGA1UEBwwJUm90dGVyZGFtMRQwEgYDVQQKDAtLbGFua3NjaG9vbDENMAsGA1UE
CwwETVFUVDEaMBgGA1UEAwwRbXF0dC5rbGFuay5zY2hvb2wwggEiMA0GCSqGSIb3
DQEBAQUAA4IBDwAwggEKAoIBAQDHiV7tLQdZg9qztSXYujw1dqL9lpqy5nDCkm8/
Sd9wOh6SBMm65LvcJb3YBpoPfI6hwwOI+XcWsGiTTuOiCM3EemVYXkcc0VCjbgzn
FZ3Ld4s1jpXy+EgDaNhXXfFpREzStDOcRrmnIm5iHRiKdWAWeJTmYas915jzTCrk
ibhW9Qd9WZstxdccpjBWnB4X6uSMOeGdunAifTY8vVLQtaBlAqyoPbZk1ELbqKaz
zBso34euvKoPCPs8nkFR/RpLNStuOTVC8g+uCQ8nIgNlXpftvjMHvBsoq7ZdICRI
jx9uW4U0y2kEF7d3P9eLl07uJjxHhurB/J/kJHm5qc4tVcOXAgMBAAGjQjBAMB0G
A1UdDgQWBBRjQ4b5y5mEeidokSMz0pRetgNuRTAfBgNVHSMEGDAWgBR0e8d1l8aw
EGSdl1/vBUgQcmlV7jANBgkqhkiG9w0BAQsFAAOCAQEAaK8rrSeqLJIAr3v+GAdJ
yGqWK9qpJgXEbxYiuIj0pSejpDX3609qvyy7YEDUqLTu44ulZmrgGfrWAXg6Boms
CpBGybNNd7oYhcGJdSCURt2EgjkEP0eCVXo02l4JDrnaNvsBt9OhEVvNSFRxblyE
UmUTWeMh27T1/ioHeQpLIBf2BPOJZrMGeQGNfYujbtd8F4VQmGcOcfGsPhbvsaFR
dUut/VtKBnifKmZ/6afbmHHBUW6IjxTXBm8J0PrY9H5SR/Y7hEvbshlETFO5djMZ
6mmitx2Bxd2s9/nPWPXk5YZryCvWvEHof23dE81OfVLaGYGdzL4ys5UiEWd/X8jP
1Q==
-----END CERTIFICATE-----
)";
#define RELAY_1 1
//--------------------------------------
// globals
//--------------------------------------
#ifdef MQTT_TLS
WiFiClientSecure wifiClient;
WiFiClientSecure wifiClient;
#else
WiFiClient wifiClient;
WiFiClient wifiClient;
#endif
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP);
PubSubClient mqttClient(wifiClient);
//--------------------------------------
// function setup_wifi called once
//--------------------------------------
void setup_wifi() {
delay(10);
@ -73,31 +44,17 @@ void setup_wifi() {
Serial.println(ssid);
WiFi.begin(ssid, password);
Serial.println("start while");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
delay(50);
Serial.print(".");
}
Serial.println("i am here 1");
timeClient.begin();
#ifdef MQTT_TLS
#ifdef MQTT_TLS_VERIFY
// X509List *cert = new X509List(CERT);
// wifiClient.setTrustAnchors(cert);
wifiClient.setTrustAnchors(new BearSSL::X509List(rootCACertificate));
#else
wifiClient.setInsecure();
#endif
#endif
Serial.println("WiFi connected");
}
//--------------------------------------
// function callback called everytime
// if a mqtt message arrives from the broker
//--------------------------------------
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived on topic: '");
Serial.print(topic);
@ -105,29 +62,28 @@ void callback(char* topic, byte* payload, unsigned int length) {
for (unsigned int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
String myCurrentTime = timeClient.getFormattedTime();
mqttClient.publish(mqttTopicOut,("ESP8266: Cedalo Mosquitto is awesome. ESP8266-Time: " + myCurrentTime).c_str());
if (isOn) {
digitalWrite(RELAY_1, HIGH);
isOn = false;
} else {
digitalWrite(RELAY_1, LOW);
isOn = true;
}
Serial.println();
}
//--------------------------------------
// function connect called to (re)connect
// to the broker
//--------------------------------------
void connect() {
while (!mqttClient.connected()) {
// Serial.println("The last error was");
// Serial.println(wifiClient.getLastSSLError(NULL));
Serial.print("Attempting MQTT connection...");
String clientId = "ESP8266Client-";
clientId += String(random(0xffff), HEX);
if (mqttClient.connect(clientId.c_str(), mqttUser, mqttPassword)) {
Serial.println("connected");
mqttClient.subscribe(mqttTopicIn);
mqttClient.subscribe(MAIN_CHANNEL);
mqttClient.publish(MAIN_CHANNEL, ("Whaddup can i stay? "));
} else {
Serial.print("failed, rc=");
Serial.print(mqttClient.state());
@ -137,24 +93,24 @@ void connect() {
}
}
//--------------------------------------
// main arduino setup fuction called once
//--------------------------------------
void setup() {
Serial.begin(115200);
Serial.begin(9600);
Serial.println("run setup");
setup_wifi();
Serial.println("finish setup_wifi");
pinMode(RELAY_1, OUTPUT);
mqttClient.setServer(mqtt_server, mqtt_server_port);
mqttClient.setCallback(callback);
}
//--------------------------------------
// main arduino loop fuction called periodically
//--------------------------------------
void loop() {
if (!mqttClient.connected()) {
connect();
}
mqttClient.loop();
timeClient.update();
}