From 0a0284e616b5688a1e53c08f2edba1a51d575f8f Mon Sep 17 00:00:00 2001 From: 2EEEB <192235@vutbr.cz> Date: Fri, 14 Feb 2020 15:58:31 +0100 Subject: [PATCH] add basic esp fw --- fw_esp8266.ino | 154 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 154 insertions(+) create mode 100644 fw_esp8266.ino diff --git a/fw_esp8266.ino b/fw_esp8266.ino new file mode 100644 index 0000000..27db799 --- /dev/null +++ b/fw_esp8266.ino @@ -0,0 +1,154 @@ +#include +#include "Adafruit_MQTT.h" +#include "Adafruit_MQTT_Client.h" +#include +#include + +/************************* WiFi Settings *********************************/ + +#define WLAN_SSID +#define WLAN_PASS + +/************************* MQTT server settings *********************************/ + +#define MQTT_SERVER +// Using port 8883 for MQTTS +#define MQTT_SERVERPORT 8883 +#define MQTT_USERNAME +#define MQTT_PASSW + +/***********************NTP settings*********************************/ + +//UTC offset in seconds, 3600 for UTC+1 +#define UTC_OFFSET_S 3600 +//NTP server to use, pool is recommended +#define NTP_SERVER "europe.pool.ntp.org" + +/************************* 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 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 ***************************************/ + +Adafruit_MQTT_Publish test = Adafruit_MQTT_Publish(&mqtt, "test"); + +/***************************** NTP setup *********************************/ + + 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(); +} + +char incoming[40]; +bool commRecvd = false; + +void loop() { + char buf; + static char i=0; + + MQTT_connect(); //Keep mqtt connection alive + + 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(">"); //indicate time data + 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); + } + } + Serial.println("MQTT Connected!"); +}