DHT: get temp and humidity
And save them with mqtt
This commit is contained in:
parent
0a2f18b8ee
commit
bdbcfd25d4
@ -17,5 +17,5 @@ bool BMP180IsConnected();
|
||||
int BMP180GetTemperature(double &t){return 0;};
|
||||
int BMP180GetTempAndPressure(double &t, double &p){return 0;};
|
||||
int BMP180Setup(int , int ){SKETCH_DEBUG_PRINTLN("BMP180 is disabled at build time"); return 0;};
|
||||
bool BMP180IsConnected(){return 0;};
|
||||
bool BMP180IsConnected(){return false;};
|
||||
#endif
|
||||
|
@ -38,8 +38,8 @@ int MqttSetup(char *server, char *user, char *passwd, int port, char * hostname)
|
||||
mqttId = hostname;
|
||||
snprintf(temperatureFeed, FEED_MAX_SIZE, TEMPERATURE_FEED_FORMAT, mqttId);
|
||||
snprintf(pressureFeed, FEED_MAX_SIZE, PRESSURE_FEED_FORMAT, mqttId);
|
||||
snprintf(temperatureDhtFeed, FEED_MAX_SIZE, TEMPERATURE_FEED_FORMAT, mqttId);
|
||||
snprintf(humidityDhtFeed, FEED_MAX_SIZE, PRESSURE_FEED_FORMAT, mqttId);
|
||||
snprintf(temperatureDhtFeed, FEED_MAX_SIZE, TEMPERATURE_DHT_FEED_FORMAT, mqttId);
|
||||
snprintf(humidityDhtFeed, FEED_MAX_SIZE, HUMIDITY_DHT_FEED_FORMAT, mqttId);
|
||||
|
||||
mqtt = new Adafruit_MQTT_Client(&client, server, port, user, passwd);
|
||||
mqtt_temp = new Adafruit_MQTT_Publish(mqtt, temperatureFeed);
|
||||
@ -102,7 +102,7 @@ int MqttPublish(double temp, double pressure) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int MqttDhtPublish(double temp, double humidity) {
|
||||
int MqttDhtPublish(float temp, float humidity) {
|
||||
if (MqttConnect() == 0) {
|
||||
SKETCH_DEBUG_PRINTLN("publishing DHT!");
|
||||
mqtt_dht_temp->publish(temp);
|
||||
|
@ -40,6 +40,7 @@
|
||||
|
||||
#include "debug_sketch.h"
|
||||
#include "BMP180.h"
|
||||
#include "sensor_DHT.h"
|
||||
#include "Adafruit_MQTT.h"
|
||||
#include "Adafruit_MQTT_Client.h"
|
||||
|
||||
@ -54,6 +55,7 @@ char eeprom[CONFIG_EEPROM_SIZE];
|
||||
#define BOOTMODE_OTA 2
|
||||
|
||||
double temp, pressure;
|
||||
float dhtTemp, dhtHumidity;
|
||||
uint8_t mode;
|
||||
char *hostName = "";
|
||||
|
||||
@ -87,6 +89,7 @@ int MqttConnect();
|
||||
int MqttIsConnected();
|
||||
int MqttSetup(char *server, char *user, char *passwd, int port, char * hostname);
|
||||
int MqttPublish(double temp, double pressure);
|
||||
int MqttDhtPublish(float temp, float humidity);
|
||||
void MqttCheckSubscription();
|
||||
void MqttChangeGpioValue(int gpio, int value);
|
||||
bool MqttIsConfigured();
|
||||
@ -238,8 +241,12 @@ void setup() {
|
||||
if (mode == BOOTMODE_OTA) {
|
||||
OTASetup();
|
||||
} else {
|
||||
if (BMP180Setup(CONFIG_BMP180_SDA, CONFIG_BMP180_SCL))
|
||||
if (BMP180Setup(CONFIG_BMP180_SDA, CONFIG_BMP180_SCL)){
|
||||
SKETCH_DEBUG_PRINTLN("BMP180 init success");
|
||||
}
|
||||
if (DHTSetup(CONFIG_DHT_PIN)){
|
||||
SKETCH_DEBUG_PRINTLN("DHT init success");
|
||||
}
|
||||
WebSetupServer(mode);
|
||||
}
|
||||
#ifdef CONFIG_ENABLE_POWER_SAVE
|
||||
@ -259,9 +266,9 @@ void loop() {
|
||||
MqttCheckSubscription();
|
||||
delay(CONFIG_WEB_DELAY_MS);
|
||||
|
||||
#ifdef CONFIG_ENABLE_BMP180
|
||||
nbCycle++;
|
||||
if (nbCycle > CONFIG_SAMPLING_PERIODE_MS / CONFIG_WEB_DELAY_MS) {
|
||||
#ifdef CONFIG_ENABLE_BMP180
|
||||
if (BMP180IsConnected() && BMP180GetTempAndPressure(temp, pressure) == 0) {
|
||||
SKETCH_DEBUG_PRINT("Current T°C ");
|
||||
SKETCH_DEBUG_PRINT(temp);
|
||||
@ -272,9 +279,18 @@ void loop() {
|
||||
} else {
|
||||
SKETCH_DEBUG_PRINTLN("Cannot get T°C");
|
||||
}
|
||||
nbCycle = 0;
|
||||
|
||||
#endif
|
||||
#ifdef CONFIG_ENABLE_DHT
|
||||
if (DHTIsConnected() && DHTGetTempAndHumidity(dhtTemp, dhtHumidity) == 0) {
|
||||
SKETCH_DEBUG_PRINT("Current T°C ");
|
||||
SKETCH_DEBUG_PRINT(dhtTemp);
|
||||
SKETCH_DEBUG_PRINT(" Humidity ");
|
||||
SKETCH_DEBUG_PRINTLN(dhtHumidity);
|
||||
if (MqttIsConfigured())
|
||||
MqttDhtPublish(dhtTemp, dhtHumidity);
|
||||
}
|
||||
#endif
|
||||
nbCycle = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -13,13 +13,15 @@
|
||||
#define CONFIG_SAMPLING_PERIODE_MS 60000
|
||||
#endif
|
||||
|
||||
#ifdef CONFIG_ENABLE_BMP180
|
||||
#ifndef CONFIG_BMP180_SDA
|
||||
#define CONFIG_BMP180_SDA 12
|
||||
#endif
|
||||
#ifndef CONFIG_BMP180_SLC
|
||||
#define CONFIG_BMP180_SLC 14
|
||||
#endif
|
||||
|
||||
#ifndef CONFIG_DHT_PIN
|
||||
#define CONFIG_DHT_PIN 2
|
||||
#endif
|
||||
|
||||
#ifndef CONFIG_SSID_NAME
|
||||
|
@ -12,6 +12,9 @@
|
||||
#define CONFIG_BMP180_SDA 12
|
||||
#define CONFIG_BMP180_SCL 14
|
||||
|
||||
#define CONFIG_ENABLE_DHT
|
||||
#define CONFIG_DHT_PIN 2
|
||||
|
||||
// Enable light sleep to save some power (http://bbs.espressif.com/viewtopic.php?f=6&t=133&p=485&hilit=sleep+modem#p485)
|
||||
// Switch off internal LED
|
||||
// Disable mDNS
|
||||
|
14
WifiControlSensor/sensor_DHT.h
Normal file
14
WifiControlSensor/sensor_DHT.h
Normal file
@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
#ifdef CONFIG_ENABLE_DHT
|
||||
#include <DHT.h>
|
||||
#define DHTTYPE DHT11
|
||||
DHT *dht = NULL;
|
||||
int DHTSetup(int pin);
|
||||
int DHTGetTempAndHumidity(float &t, float &h);
|
||||
bool DHTIsConnected();
|
||||
|
||||
#else //CONFIG_ENABLE_DHT
|
||||
int DHTSetup(int pin){SKETCH_DEBUG_PRINTLN("DHT is disabled at build time"); return 0};
|
||||
int DHTGetTempAndHumidity(float &t, float &h){return 0;};
|
||||
bool DHTIsConnected(){return false;};
|
||||
#endif
|
29
WifiControlSensor/sensor_DHT.ino
Normal file
29
WifiControlSensor/sensor_DHT.ino
Normal file
@ -0,0 +1,29 @@
|
||||
#ifdef CONFIG_ENABLE_DHT
|
||||
#include "sensor_DHT.h"
|
||||
int DHTSetup(int pin){
|
||||
dht = new DHT(pin, DHTTYPE);
|
||||
dht->begin();
|
||||
return 0;
|
||||
}
|
||||
|
||||
int DHTGetTempAndHumidity(float &t, float &h){
|
||||
int ret = 0;
|
||||
t = dht->readTemperature();
|
||||
h = dht->readHumidity();
|
||||
if(isnan(t)){
|
||||
t = 0;
|
||||
ret = -1;
|
||||
}
|
||||
if(isnan(h)){
|
||||
h = 0;
|
||||
ret = -1;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool DHTIsConnected(){
|
||||
//No way to know if connected
|
||||
//Check at least if initialized
|
||||
return dht != NULL;
|
||||
}
|
||||
#endif
|
Loading…
Reference in New Issue
Block a user