made MQTT connection routine non-blocking, added wifi info sending to arduino

This commit is contained in:
2EEEB 2020-04-11 19:17:33 +02:00
parent 29bf14e630
commit 8b75336e7d
Signed by: 2EEEB
GPG Key ID: 8F12BD6D9D435DB2

View File

@ -3,155 +3,150 @@
#include "Adafruit_MQTT_Client.h"
#include <NTPClient.h>
#include <WiFiUdp.h>
//#include "time.h"
/************************* WiFi Settings *********************************/
#define WLAN_SSID <SSID>
#define WLAN_PASS <PASSWD>
#define WLAN_SSID "<SSID>"
#define WLAN_PASS "<PASSWD"
/************************* MQTT server settings *********************************/
/********************** MQTT server settings ******************************/
#define MQTT_SERVER <SERVER>
#define MQTT_SERVER "<SERVER>"
// Using port 8883 for MQTTS
#define MQTT_SERVERPORT 8883
#define MQTT_USERNAME <USERNAME>
#define MQTT_PASSW <PASSWD>
#define MQTT_SERVERPORT 8883
#define MQTT_USERNAME "<USERNAME>"
#define MQTT_PASSW "<PASSWORD>"
/***********************NTP settings*********************************/
/************************** NTP settings **********************************/
//UTC offset in seconds, 3600 for UTC+1
#define UTC_OFFSET_S 3600
#define UTC_OFFSET_S 3600
//NTP server to use, pool is recommended
#define NTP_SERVER "europe.pool.ntp.org"
#define NTP_SERVER "europe.pool.ntp.org"
/************************* MQTT Setup ************************************/
/************************** MQTT Setup ************************************/
// WiFiFlientSecure for SSL/TLS support
WiFiClientSecure client;
// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
Adafruit_MQTT_Client mqtt(&client, MQTT_SERVER, MQTT_SERVERPORT, MQTT_USERNAME, MQTT_PASSW);
//mqtt ssl server cert SHA1 fingerprint
static const char *fingerprint PROGMEM = "<SSL_CERT_FPRINT>";
//mqtt server cert SHA1 fingerprint
static const char *fingerprint PROGMEM = "10 AF 12 90 BA CA 5D B9 10 57 4C 06 96 DD 25 3B A9 09 E6 70";
/**************************** MQTT Feeds ***********************************/
/****************************** MQTT Feeds ***************************************/
Adafruit_MQTT_Publish <TOPIC> = Adafruit_MQTT_Publish(&mqtt, "<TOPIC>");
Adafruit_MQTT_Publish test = Adafruit_MQTT_Publish(&mqtt, "test");
/***************************** NTP setup ***********************************/
/***************************** NTP setup *********************************/
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, NTP_SERVER, UTC_OFFSET_S);
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, NTP_SERVER, UTC_OFFSET_S);
void setup() {
Serial.begin(115200);
delay(10);
// Connect to WiFi access point.
Serial.println();
Serial.print("esp: Connecting to ");
Serial.println(WLAN_SSID);
delay(1000);
WiFi.begin(WLAN_SSID, WLAN_PASS);
delay(2000);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
Serial.println("esp: WiFi connected");
Serial.print("esp: IP address: ");
Serial.println(WiFi.localIP());
// check the fingerprint of SSL cert
client.setFingerprint(fingerprint);
//start NTP client
timeClient.begin();
}
/**************************** Global vars **********************************/
char incoming[40];
bool commRecvd = false;
uint32_t timest;
void setup() {
Serial.begin(115200);
// Connect to WiFi access point.
delay(500);
WiFi.begin(WLAN_SSID, WLAN_PASS);
delay(1000);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
//check the fingerprint of SSL cert
client.setFingerprint(fingerprint);
//start NTP client
timeClient.begin();
//send connection details over
Serial.println();
Serial.print("!");
Serial.print(WLAN_SSID);
Serial.print(",");
Serial.print(WiFi.localIP());
Serial.print("\n");
delay(500);
//take boot timestamp
timest = millis();
}
void loop() {
char buf;
static char i=0;
MQTT_connect(); //Keep mqtt connection alive
if ((millis() - timest) >= (1000UL * 10)) {
MQTT_connect(); //Retry connecting to mqtt broker every 10 seconds
//The way this is setup, we only connect to the broker about 10s after powerup
//this is to allow time sync to go through for the arduino
}
//monitor serial line
while(Serial.available() > 0) {
buf = Serial.read();
if(buf != '\n') {
incoming[i] = buf;
i++;
}
else {
incoming[i] = '\0';
commRecvd = true;
}
}
i = 0;
if(commRecvd == true){
if(strcmp("?ntp", incoming) == 0){ //if request for time update received, process time update
while(!timeClient.update()){ //update from ntp server
delay(10); //time sync is critical, wait for succesful update
Serial.print("\\");
};
Serial.print(">"); //indicate time data
delay(50);
Serial.print(timeClient.getYear());
Serial.print("/");
Serial.print(timeClient.getMonth());
Serial.print("/");
Serial.print(timeClient.getDate());
Serial.print("/");
Serial.print(timeClient.getHours());
Serial.print("/");
Serial.print(timeClient.getMinutes());
Serial.print("/");
Serial.print(timeClient.getSeconds());
Serial.print('\n');
delay(1000); //give time to process comm
} else {
Serial.print("esp: recvd: ");
Serial.println(incoming);
test.publish(incoming);
Serial.println("esp: sent data");
};
commRecvd = false;
}
delay(100);;
}
// Function to connect and reconnect as necessary to the MQTT server.
// Borrowed from Adafruit MQTT lib example.
void MQTT_connect() {
int8_t ret;
// Stop if already connected.
if (mqtt.connected()) {
return;
}
Serial.print("Connecting to MQTT... ");
uint8_t retries = 3;
while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
Serial.print("esp: ");
Serial.println(mqtt.connectErrorString(ret));
Serial.println("esp: Retrying MQTT connection in 5 seconds...");
mqtt.disconnect();
delay(5000); // wait 5 seconds
retries--;
if (retries == 0) {
// basically die and wait for WDT to reset me
while (1);
buf = Serial.read();
if(buf != '\n') {
incoming[i] = buf;
i++;
}
else {
incoming[i] = '\0';
commRecvd = true;
}
}
Serial.println("MQTT Connected!");
i = 0;
//process serial comms
if(commRecvd == true){
if(strcmp("?ntp", incoming) == 0){ //if request for time update received, process time update
while(!timeClient.update()){ //update from ntp server
delay(10); //time sync is critical, wait for succesful update
};
Serial.print(">"); //indicate time data
delay(50);
Serial.print(timeClient.getYear());
Serial.print("/");
Serial.print(timeClient.getMonth());
Serial.print("/");
Serial.print(timeClient.getDate());
Serial.print("/");
Serial.print(timeClient.getHours());
Serial.print("/");
Serial.print(timeClient.getMinutes());
Serial.print("/");
Serial.print(timeClient.getSeconds());
Serial.print('\n');
delay(1000); //give time to process comm
}
else {
<TOPIC>.publish(incoming); //publish whatever we received to mqtt topic
};
commRecvd = false;
}
delay(100);
}
//Connect to mqtt broker
bool MQTT_connect() {
if (mqtt.connected()) { //if alreaady connected take timestamp and return
timest = millis();
return(true);
}
if(mqtt.connect() != 0) { //if there are errors take timestamp and return
mqtt.disconnect();
timest = millis();
return(false);
}
else { //on successful connection take timestamp and return
timest = millis();
return(true);
}
}