Send temperature information into MQTT

This commit is contained in:
Mathieu Maret 2016-03-11 01:31:03 +01:00
parent f03299cc64
commit f0b47e162c
4 changed files with 64 additions and 6 deletions

View File

@ -23,3 +23,6 @@ Device can also be put in OTA mode and will wait for OTA from the espota tool.
Provide previous WiFiAccessPointConfigurator features and can also measure temperature from a BMP180.
To interface with BMP180, the following library should be installed into Arduino environment: https://github.com/mmaret/BMP180_Breakout_Arduino_Library/archive/master.zip
To use mqtt, the Adafruit Mqtt library should be installed.
This have been tested against a mosquitto server without username or password

View File

@ -0,0 +1,23 @@
// 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.
int MQTT_connect() {
int8_t ret;
// Stop if already connected.
if (mqtt.connected()) {
return 0;
}
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 ...");
mqtt.disconnect();
delay(100); // wait
retries--;
if (retries == 0) {
return -1;
}
}
return 0;
}

View File

@ -1,5 +1,6 @@
void handleRoot() {
server.send(200, "text/html", "<h1>You are connected</h1><br/>"
"Current temperature "+String(temp,2)+"C<br/>"
"<a href=\"/setup\">Setup</a><br/>"
"<a href=\"/otamode\">OTA mode</a><br/>"
"<a href=\"/gpio?gpio=2&amp;value=1\">ON</a><br/>"

View File

@ -23,13 +23,21 @@
#include <SFE_BMP180.h>
#include <Wire.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
#define AIO_SERVER "192.168.0.250"
#define AIO_SERVERPORT 1883 // use 8883 for SSL
#define AIO_USERNAME ""
#define AIO_PASSWORD ""
//#define ENABLE_EXTRA_GPIO
#define EEPROM_SIZE 512
char eeprom[EEPROM_SIZE];
#define WEB_DELAY_MS 100
#define SAMPLING_PERIODE_MS 5000
#define SAMPLING_PERIODE_MS 60000
/* I2C pin used*/
#define SDA 2
@ -39,8 +47,24 @@ char eeprom[EEPROM_SIZE];
#define BOOTMODE_NORMAL 1
#define BOOTMODE_OTA 2
double temp;
int mode;
// Store the MQTT server, username, and password in flash memory.
// This is required for using the Adafruit MQTT library.
const char MQTT_SERVER[] PROGMEM = AIO_SERVER;
const char MQTT_USERNAME[] PROGMEM = AIO_USERNAME;
const char MQTT_PASSWORD[] PROGMEM = AIO_PASSWORD;
// Create an ESP8266 WiFiClient class to connect to the MQTT server.
WiFiClient 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, AIO_SERVERPORT, MQTT_USERNAME, MQTT_PASSWORD);
const char TEMPERATURE_FEED[] PROGMEM = "/feeds/temperature";
Adafruit_MQTT_Publish mqtt_temp = Adafruit_MQTT_Publish(&mqtt, TEMPERATURE_FEED);
/* Set these to your desired credentials. */
const char *ssid = "ESPConfigurator";
@ -64,6 +88,9 @@ void readEEPROM(int &bootMode, char **ssid, char **password, char **host);
/* BMP180 decl */
int getTemperature(double &t);
/* MQTT decl */
int MQTT_connect();
void setupWifi(int bootmode, int forceSetup, char *confSsid, char *confPassword, char *confHost) {
IPAddress myIP;
if (bootmode == BOOTMODE_SETUP || forceSetup) {
@ -167,7 +194,7 @@ void setup() {
}
Serial.print("Force Setup Mode ? :");
Serial.print(txStatus?"No":"Yes");
Serial.print(txStatus ? "No" : "Yes");
Serial.println();
setupWifi(mode, txStatus == 0, confSsid, confPassword, confHost);
@ -182,7 +209,6 @@ void setup() {
}
uint nbCycle = 0;
double temp;
void loop() {
if (mode == BOOTMODE_OTA) {
ArduinoOTA.handle();
@ -190,14 +216,19 @@ void loop() {
server.handleClient();
delay(WEB_DELAY_MS);
nbCycle++;
if (nbCycle > SAMPLING_PERIODE_MS/WEB_DELAY_MS){
if(getTemperature(temp) == 0){
if (nbCycle > SAMPLING_PERIODE_MS / WEB_DELAY_MS) {
if (getTemperature(temp) == 0) {
Serial.print("Sampling :");
Serial.println(temp);
}else{
if (MQTT_connect() == 0){
Serial.println("publishing !");
mqtt_temp.publish(temp);
}
} else {
Serial.println("Cannot get T°C");
}
nbCycle = 0;
}
}
}