|
#pragma once |
|
#include "esphome.h" |
|
#include <PubSubClient.h> |
|
#include <WiFiClientSecure.h> |
|
#include <ArduinoJson.h> |
|
|
|
#define TAG "MqttGrid" |
|
|
|
class MqttGrid : public Component { |
|
|
|
public: |
|
TextSensor *incident_id_sensor = new TextSensor(); |
|
TextSensor *incident_team_sensor = new TextSensor(); |
|
TextSensor *incident_title_sensor = new TextSensor(); |
|
TextSensor *incident_role_sensor = new TextSensor(); |
|
TextSensor *incident_severity_sensor = new TextSensor(); |
|
|
|
long lastReconnectAttempt = 0; |
|
|
|
//TODO: put your values |
|
const char *mqtt_server = "<event_grid_name>.westus2-1.ts.eventgrid.azure.net"; |
|
const char *user_name = "<user_name>"; |
|
const char *client_id = "sample-device-esp"; |
|
const char *topic ="kiosk-incidents/#"; |
|
|
|
//note: mind indentation, should be no spaces in the beginning of the line. |
|
const char *certPem = R"( |
|
-----BEGIN PRIVATE KEY----- |
|
MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDc4Kveii5JY+lm |
|
.... |
|
RW74ULxFmhIG2aEmmZt3tq45 |
|
-----END PRIVATE KEY----- |
|
-----BEGIN CERTIFICATE----- |
|
MIIDTDCCAjSgAwIBAgIQfdEnhcKaRGqVc1s6mh0B2TANBgkqhkiG9w0BAQsFADAj |
|
... |
|
anWy6O29fetchPlKyT5EmpylYpfYFhYnUCRqe2ey63Z3zsFKtdr0D/8EU40Uc2Ar |
|
soGMhcN7+aNSE9MD/VjPTeWylxmvMFlsWhHuF45V2BY= |
|
-----END CERTIFICATE----- |
|
)"; |
|
|
|
WiFiClientSecure wifiClient; |
|
PubSubClient client; |
|
|
|
MqttGrid() : client(mqtt_server, 8883, wifiClient){ |
|
client.setCallback([this](char* topic, byte* message, unsigned int length) { this->on_icm(topic, message, length); }); |
|
} |
|
|
|
float get_setup_priority() const override { return esphome::setup_priority::AFTER_CONNECTION; } |
|
|
|
void on_icm(char* topic, byte* message, unsigned int length) { |
|
ESP_LOGD(TAG, "Message arrived on topic: '%s'", topic); |
|
|
|
DynamicJsonDocument json_buffer(1024); |
|
DeserializationError error = deserializeJson(json_buffer, message); |
|
|
|
if (error) { |
|
ESP_LOGE(TAG, "Json deserialization failed: '%s'", error.f_str()); |
|
return; |
|
} |
|
|
|
incident_id_sensor->publish_state(json_buffer["IncidentId"]); |
|
incident_team_sensor->publish_state(json_buffer["Team"]); |
|
incident_title_sensor->publish_state(json_buffer["Title"]); |
|
incident_role_sensor->publish_state(json_buffer["Role"]); |
|
incident_severity_sensor->publish_state(json_buffer["Severity"]); |
|
} |
|
|
|
// Load Certificates |
|
void loadcerts() { |
|
|
|
#if defined(ESP32) |
|
// note cannot use insecure mode in ESP32 - client certs won't be sent. Perhaps a bug in wifiClientSecure. |
|
wifiClient.setCACert(ca_pem); |
|
wifiClient.setCertificate(certPem); |
|
wifiClient.setPrivateKey(keyPem); |
|
#elif defined(ESP8266) |
|
wifiClient.allowSelfSignedCerts(); |
|
wifiClient.setInsecure(); |
|
BearSSL::X509List certList(certPem); |
|
BearSSL::PrivateKey privateKey(certPem); |
|
wifiClient.setClientRSACert(&certList, &privateKey); |
|
#else |
|
#error "This ain't a ESP8266 or ESP32 cannot support it!" |
|
#endif |
|
|
|
ESP_LOGD(TAG, "Checking TLS connection to '%s'", mqtt_server); |
|
if (!wifiClient.connect(mqtt_server, 8883)) { |
|
ESP_LOGE(TAG,"connection failed"); |
|
return; |
|
} |
|
ESP_LOGD(TAG, "Connection OK"); |
|
} |
|
|
|
boolean reconnect() |
|
{ |
|
if (client.connected()) return true; |
|
|
|
long now = millis(); |
|
if (now - lastReconnectAttempt < 2000) return false; |
|
|
|
lastReconnectAttempt = now; |
|
ESP_LOGD(TAG, "trying to connect to MQTT"); |
|
if (client.connect(client_id, user_name, "")) |
|
{ |
|
client.subscribe(topic); |
|
ESP_LOGI(TAG, "===> mqtt connected"); |
|
} |
|
else |
|
{ |
|
ESP_LOGE(TAG, "---> mqtt failed, rc='%d'", client.state()); |
|
} |
|
|
|
return client.connected(); |
|
} |
|
|
|
void setup() override |
|
{ |
|
loadcerts(); |
|
} |
|
|
|
void loop() override |
|
{ |
|
if (!client.connected()) |
|
{ |
|
reconnect(); |
|
} |
|
|
|
client.loop(); |
|
} |
|
|
|
}; |