-
-
Save GameTec-live/3cf043a67a2cd2a241f17b7b23082778 to your computer and use it in GitHub Desktop.
Fork für ESP32 und platform io...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Arduino.h> | |
#include <PubSubClient.h> | |
#include <WiFi.h> | |
#include <mDNS.h> | |
#define LED 2 | |
#define CONFIG_MQTT_PAYLOAD_ON "ON" | |
#define CONFIG_MQTT_PAYLOAD_OFF "OFF" | |
const char* cfg_wifi_ssid = "freifunk-myk.de"; | |
const char* cfg_wifi_pwd = ""; | |
const char* mqtt_server = "deinserver.local"; | |
const unsigned int mqtt_port = 1883; | |
const char* mqtt_user = "admin"; | |
const char* mqtt_pass = "test123"; | |
//echo | openssl s_client -connect localhost:1883 | openssl x509 -fingerprint -noout | |
const char* mqtt_fprint = "D9:B2:A5:BB:E1:E5:4D:50:23:E1:84:24:29:44:BA:7C:54:01:BB:08"; | |
WiFiClientSecure espClient; | |
PubSubClient client(espClient); | |
void callback(char* topic, byte* payload, unsigned int length) { | |
Serial.print("Message arrived ["); | |
Serial.print(topic); | |
Serial.print("] "); | |
char message[length + 1]; | |
for (int i = 0; i < length; i++) { | |
message[i] = (char)payload[i]; | |
} | |
message[length] = '\0'; | |
Serial.println(message); | |
String strMsg = (String)message; | |
if((strMsg.startsWith(CONFIG_MQTT_PAYLOAD_ON))) { | |
digitalWrite(LED,HIGH); | |
} | |
if((strMsg.startsWith(CONFIG_MQTT_PAYLOAD_OFF))) { | |
digitalWrite(LED,LOW); | |
} | |
} | |
void setup() { | |
// put your setup code here, to run once: | |
Serial.begin(115200); | |
Serial.println("TestMQTT"); | |
pinMode(LED,OUTPUT); | |
digitalWrite(LED,HIGH); | |
delay(500); | |
digitalWrite(LED,LOW); | |
WiFi.mode(WIFI_STA); | |
WiFi.begin(cfg_wifi_ssid, cfg_wifi_pwd); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
Serial.println(""); | |
Serial.println("WiFi connected"); | |
Serial.println("IP address: "); | |
Serial.println(WiFi.localIP()); | |
espClient.setFingerprint(mqtt_fprint); | |
client.setServer(mqtt_server, mqtt_port); | |
client.setCallback(callback); | |
while (!client.connected()) { | |
Serial.print("Attempting MQTT connection..."); | |
verifyFingerprint(); | |
if (client.connect(WiFi.macAddress().c_str(), mqtt_user, mqtt_pass)) { | |
Serial.println("connected"); | |
client.subscribe("/foo"); | |
}else{ | |
Serial.print("failed, rc="); | |
Serial.print(client.state()); | |
Serial.println(" try again in 5 seconds"); | |
// Wait 5 seconds before retrying | |
delay(5000); | |
} | |
} | |
} | |
void verifyFingerprint() { | |
if(client.connected() || espClient.connected()) return; //Already connected | |
Serial.print("Checking TLS @ "); | |
Serial.print(mqtt_server); | |
Serial.print("..."); | |
if (!espClient.connect(mqtt_server, mqtt_port)) { | |
Serial.println("Connection failed. Rebooting."); | |
Serial.flush(); | |
ESP.restart(); | |
} | |
if (espClient.verify(mqtt_fprint, mqtt_server)) { | |
Serial.print("Connection secure -> ."); | |
} else { | |
Serial.println("Connection insecure! Rebooting."); | |
Serial.flush(); | |
ESP.restart(); | |
} | |
espClient.stop(); | |
delay(100); | |
} | |
unsigned int counter = 0; | |
void loop() { | |
// put your main code here, to run repeatedly: | |
client.loop(); | |
char buf[12]; | |
itoa(counter, buf, 11); | |
client.publish("/test", buf); | |
counter++; | |
delay(1000); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Arduino.h> | |
#include <PubSubClient.h> | |
#include <WiFi.h> | |
#include <mDNS.h> | |
#define LED 2 | |
#define CONFIG_MQTT_PAYLOAD_ON "ON" | |
#define CONFIG_MQTT_PAYLOAD_OFF "OFF" | |
const char* cfg_wifi_ssid = "ssid"; | |
const char* cfg_wifi_pwd = "password"; | |
const char* mqtt_server = "192.168.broaker"; | |
const unsigned int mqtt_port = 1883; | |
WiFiClient espClient; | |
PubSubClient client(espClient); | |
void callback(char* topic, byte* payload, unsigned int length) { | |
Serial.print("Message arrived ["); | |
Serial.print(topic); | |
Serial.print("] "); | |
char message[length + 1]; | |
for (int i = 0; i < length; i++) { | |
message[i] = (char)payload[i]; | |
} | |
message[length] = '\0'; | |
Serial.println(message); | |
String strMsg = (String)message; | |
if((strMsg.startsWith(CONFIG_MQTT_PAYLOAD_ON))) { | |
digitalWrite(LED,HIGH); | |
} | |
if((strMsg.startsWith(CONFIG_MQTT_PAYLOAD_OFF))) { | |
digitalWrite(LED,LOW); | |
} | |
} | |
void setup() { | |
// put your setup code here, to run once: | |
Serial.begin(115200); | |
Serial.println("TestMQTT"); | |
pinMode(LED,OUTPUT); | |
digitalWrite(LED,HIGH); | |
delay(500); | |
digitalWrite(LED,LOW); | |
WiFi.mode(WIFI_STA); | |
WiFi.begin(cfg_wifi_ssid, cfg_wifi_pwd); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
Serial.println(""); | |
Serial.println("WiFi connected"); | |
Serial.println("IP address: "); | |
Serial.println(WiFi.localIP()); | |
client.setServer(mqtt_server, mqtt_port); | |
client.setCallback(callback); | |
while (!client.connected()) { | |
Serial.print("Attempting MQTT connection..."); | |
if (client.connect(WiFi.macAddress().c_str())) { | |
Serial.println("connected"); | |
client.subscribe("/test"); | |
}else{ | |
Serial.print("failed, rc="); | |
Serial.print(client.state()); | |
Serial.println(" try again in 5 seconds"); | |
// Wait 5 seconds before retrying | |
delay(5000); | |
} | |
} | |
} | |
unsigned int counter = 0; | |
void loop() { | |
// put your main code here, to run repeatedly: | |
client.loop(); | |
char buf[12]; | |
itoa(counter, buf, 11); | |
client.publish("/foo", buf); | |
counter++; | |
delay(1000); | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; PlatformIO Project Configuration File | |
; | |
; Build options: build flags, source filter | |
; Upload options: custom upload port, speed and extra flags | |
; Library options: dependencies, extra library storages | |
; Advanced options: extra scripting | |
; | |
; Please visit documentation for the other options and examples | |
; https://docs.platformio.org/page/projectconf.html | |
[env:esp32doit-devkit-v1] | |
platform = espressif32 | |
board = esp32doit-devkit-v1 | |
framework = arduino | |
lib_deps = knolleary/PubSubClient@^2.8 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment