Created
January 11, 2019 11:50
-
-
Save remisarrailh/29cd0ce5018ad693057645b89349c241 to your computer and use it in GitHub Desktop.
ESP32 MQTT SSL Weather sensor (BME280 / TSL2561)
This file contains hidden or 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
//Wifi | |
#include <WiFi.h> | |
#include <WiFiClientSecure.h> | |
#include <ESPmDNS.h> | |
const char* SSID = ""; | |
const char* PASSWORD = ""; | |
const char* NAME = "sensor1"; | |
//MQTT | |
#include "Adafruit_MQTT.h" | |
#include "Adafruit_MQTT_Client.h" | |
const char* MQTT_SERVER = ""; | |
const int MQTT_PORT = 8883; | |
const char* MQTT_USERNAME = ""; | |
const char* MQTT_KEY = ""; | |
//chain.pem | |
const char* CERT = \ | |
"-----BEGIN CERTIFICATE-----\n" \ | |
"-----END CERTIFICATE-----\n"; | |
const bool VERIFY_HOST = false; | |
const char* TEMP_NAME = "/sensors/temp/1"; | |
const char* BARO_NAME = "/sensors/baro/1"; | |
const char* LIGHT_NAME = "/sensors/light/1"; | |
const char* HUM_NAME = "/sensors/hum/1"; | |
WiFiClientSecure client; | |
Adafruit_MQTT_Client mqtt(&client, MQTT_SERVER, MQTT_PORT, MQTT_USERNAME, MQTT_KEY); | |
//Topic publish | |
Adafruit_MQTT_Publish temp_out = Adafruit_MQTT_Publish(&mqtt, TEMP_NAME); | |
Adafruit_MQTT_Publish baro_out = Adafruit_MQTT_Publish(&mqtt, BARO_NAME); | |
Adafruit_MQTT_Publish hum_out = Adafruit_MQTT_Publish(&mqtt, HUM_NAME); | |
Adafruit_MQTT_Publish light_out = Adafruit_MQTT_Publish(&mqtt, LIGHT_NAME); | |
//Serial | |
const int BAUDRATE = 115200; | |
//Sensors | |
#include <BME280I2C.h> | |
BME280I2C bme; | |
#include <Adafruit_Sensor.h> | |
#include <Adafruit_TSL2561_U.h> | |
Adafruit_TSL2561_Unified tsl = Adafruit_TSL2561_Unified(TSL2561_ADDR_FLOAT, 12345); | |
float temperature = NAN, humidity = NAN, pressure = NAN; | |
float light = NAN; | |
void setup() { | |
Serial.begin(BAUDRATE); | |
Serial.println("MQTT Server"); | |
//Wifi | |
WiFi.begin(SSID, PASSWORD); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
Serial.println(""); | |
Serial.print("Connected to "); | |
Serial.println(SSID); | |
Serial.print("IP address: "); | |
Serial.println(WiFi.localIP()); | |
MDNS.begin(NAME); | |
verifyFingerprint(); | |
//Sensors | |
Wire.begin(19,23); | |
//TSL | |
tsl.begin(&Wire); | |
tsl.enableAutoRange(true); | |
tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_13MS); | |
//BME280 | |
bme.begin(); | |
} | |
void loop() { | |
MQTT_connect(); | |
//TSL | |
light = NAN; | |
float temp(NAN), hum(NAN), pres(NAN); | |
tsl.enableAutoRange(true); | |
tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_13MS); | |
sensors_event_t event; | |
tsl.getEvent(&event); | |
light = event.light; | |
//BME28 | |
BME280::TempUnit tempUnit(BME280::TempUnit_Celsius); | |
BME280::PresUnit presUnit(BME280::PresUnit_Pa); | |
bme.read(pres, temp, hum, tempUnit, presUnit); | |
temperature = temp; | |
humidity = hum; | |
pressure = pres / 100; | |
//Serial | |
Serial.print("temperature:"); | |
Serial.println(temperature); | |
Serial.print("humidity:"); | |
Serial.println(humidity); | |
Serial.print("pressure:"); | |
Serial.println(pressure); | |
Serial.print("light:"); | |
Serial.println(light); | |
//MQTT | |
light_out.publish(light); | |
temp_out.publish(temperature); | |
baro_out.publish(pressure); | |
hum_out.publish(humidity); | |
delay(1000); | |
} | |
void verifyFingerprint() { | |
const char* host = MQTT_SERVER; | |
Serial.print("Connecting to "); | |
Serial.print(host); | |
Serial.print(":"); | |
Serial.println(MQTT_PORT); | |
if (VERIFY_HOST) { | |
client.setCACert(CERT); | |
} | |
if (! client.connect(host, MQTT_PORT)) { | |
Serial.println("Connection failed. Halting execution."); | |
char err[1024]; | |
client.lastError(err, 1024); | |
Serial.println(err); | |
delay(1000); | |
} | |
} | |
// Function to connect and reconnect as necessary to the MQTT server. | |
// Should be called in the loop function and it will take care if connecting. | |
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.println(mqtt.connectErrorString(ret)); | |
Serial.println("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!"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment