mirror of
https://github.com/anklimov/lighthub
synced 2025-12-08 12:49:50 +03:00
ccs811 & hdc1080 integration done
mqtt publish to abstrchin added http connection error threating for ESPx, DUE, STM fixed ESP8266 pin conflict between sensor WAK and DMX OUT fixed DHT begin added
This commit is contained in:
32
lighthub/abstractin.cpp
Normal file
32
lighthub/abstractin.cpp
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
|
||||||
|
#include "abstractin.h"
|
||||||
|
#include <PubSubClient.h>
|
||||||
|
#include "utils.h"
|
||||||
|
#include <aJSON.h>
|
||||||
|
#include "inputs.h"
|
||||||
|
|
||||||
|
extern PubSubClient mqttClient;
|
||||||
|
|
||||||
|
int abstractIn::publish(int value, char* subtopic)
|
||||||
|
{
|
||||||
|
char valstr[16];
|
||||||
|
printUlongValueToStr(valstr, value);
|
||||||
|
publish(valstr,subtopic);
|
||||||
|
};
|
||||||
|
|
||||||
|
int abstractIn::publish(float value, char* subtopic)
|
||||||
|
{
|
||||||
|
char valstr[16];
|
||||||
|
printFloatValueToStr(value, valstr);
|
||||||
|
publish(valstr,subtopic);
|
||||||
|
};
|
||||||
|
|
||||||
|
int abstractIn::publish(char * value, char* subtopic)
|
||||||
|
{
|
||||||
|
char addrstr[MQTT_TOPIC_LENGTH];
|
||||||
|
aJsonObject *emit = aJson.getObjectItem(in->inputObj, "emit");
|
||||||
|
strncpy(addrstr,emit->valuestring,sizeof(addrstr));
|
||||||
|
if (!strchr(addrstr,'/')) setTopic(addrstr,sizeof(addrstr),T_OUT,emit->valuestring);
|
||||||
|
strncat(addrstr,subtopic,sizeof(addrstr));
|
||||||
|
if (mqttClient.connected()) mqttClient.publish(addrstr, value, true);
|
||||||
|
};
|
||||||
@@ -1,3 +1,6 @@
|
|||||||
|
#pragma once
|
||||||
|
#include "Arduino.h"
|
||||||
|
|
||||||
class Input;
|
class Input;
|
||||||
class abstractIn {
|
class abstractIn {
|
||||||
public:
|
public:
|
||||||
@@ -7,5 +10,8 @@ public:
|
|||||||
|
|
||||||
protected:
|
protected:
|
||||||
Input * in;
|
Input * in;
|
||||||
|
int publish(int value, char* subtopic = NULL);
|
||||||
|
int publish(float value, char* subtopic = NULL );
|
||||||
|
int publish(char * value, char* subtopic = NULL);
|
||||||
friend Input;
|
friend Input;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -362,6 +362,7 @@ void Input::dht22Poll() {
|
|||||||
float humidity = roundf(dhtSensorData.humidity);
|
float humidity = roundf(dhtSensorData.humidity);
|
||||||
#else
|
#else
|
||||||
DHT dht(pin, DHT22);
|
DHT dht(pin, DHT22);
|
||||||
|
dht.begin();
|
||||||
float temp = dht.readTemperature();
|
float temp = dht.readTemperature();
|
||||||
float humidity = dht.readHumidity();
|
float humidity = dht.readHumidity();
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -1180,10 +1180,11 @@ lan_status loadConfigFromHttp(int arg_cnt, char **args)
|
|||||||
wdt_res();
|
wdt_res();
|
||||||
//debugSerial<<"making GET request");get
|
//debugSerial<<"making GET request");get
|
||||||
htclient.beginRequest();
|
htclient.beginRequest();
|
||||||
htclient.get(URI);
|
responseStatusCode = htclient.get(URI);
|
||||||
htclient.endRequest();
|
htclient.endRequest();
|
||||||
|
|
||||||
|
if (responseStatusCode == HTTP_SUCCESS)
|
||||||
|
{
|
||||||
// read the status code and body of the response
|
// read the status code and body of the response
|
||||||
responseStatusCode = htclient.responseStatusCode();
|
responseStatusCode = htclient.responseStatusCode();
|
||||||
response = htclient.responseBody();
|
response = htclient.responseBody();
|
||||||
@@ -1205,8 +1206,12 @@ lan_status loadConfigFromHttp(int arg_cnt, char **args)
|
|||||||
applyConfig();
|
applyConfig();
|
||||||
debugSerial<<F("Done.\n");
|
debugSerial<<F("Done.\n");
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
debugSerial<<F("Config retrieving failed\n");
|
||||||
|
return READ_RE_CONFIG;//-11; //Load from NVRAM
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
debugSerial<<F("Config retrieving failed\n");
|
debugSerial<<F("Connect failed\n");
|
||||||
return READ_RE_CONFIG;//-11; //Load from NVRAM
|
return READ_RE_CONFIG;//-11; //Load from NVRAM
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
#include "modules/in_ccs811_hdc1080.h"
|
#include "modules/in_ccs811_hdc1080.h"
|
||||||
|
#include "Arduino.h"
|
||||||
|
|
||||||
#ifndef CSSHDC_DISABLE
|
#ifndef CSSHDC_DISABLE
|
||||||
|
|
||||||
CCS811 ccs811(CCS811_ADDR);
|
CCS811 ccs811(CCS811_ADDR);
|
||||||
@@ -69,8 +71,10 @@ Serial.print(t=hdc1080.readTemperature());
|
|||||||
Serial.print("C, RH=");
|
Serial.print("C, RH=");
|
||||||
Serial.print(h=hdc1080.readHumidity());
|
Serial.print(h=hdc1080.readHumidity());
|
||||||
Serial.print("% Status=");
|
Serial.print("% Status=");
|
||||||
|
publish(t,"/T");
|
||||||
|
publish(h,"/H");
|
||||||
Serial.println(reg=hdc1080.readRegister().rawData,HEX);
|
Serial.println(reg=hdc1080.readRegister().rawData,HEX);
|
||||||
/////// TODO ccs811.setEnvironmentalData(h,t);
|
ccs811.setEnvironmentalData(h,t);
|
||||||
|
|
||||||
if (reg==0xff) //ESP I2C glitch
|
if (reg==0xff) //ESP I2C glitch
|
||||||
{
|
{
|
||||||
@@ -87,9 +91,9 @@ return 1;
|
|||||||
|
|
||||||
int in_ccs811::Poll()
|
int in_ccs811::Poll()
|
||||||
{
|
{
|
||||||
//#ifdef WAK_PIN
|
#ifdef WAK_PIN
|
||||||
// digitalWrite(WAK_PIN,LOW);
|
digitalWrite(WAK_PIN,LOW);
|
||||||
//#endif
|
#endif
|
||||||
//Check to see if data is ready with .dataAvailable()
|
//Check to see if data is ready with .dataAvailable()
|
||||||
if (ccs811.dataAvailable())
|
if (ccs811.dataAvailable())
|
||||||
{
|
{
|
||||||
@@ -97,16 +101,21 @@ int in_ccs811::Poll()
|
|||||||
//Get them later
|
//Get them later
|
||||||
CCS811Core::status returnCode = ccs811.readAlgorithmResults();
|
CCS811Core::status returnCode = ccs811.readAlgorithmResults();
|
||||||
printDriverError(returnCode);
|
printDriverError(returnCode);
|
||||||
|
float co2,tvoc;
|
||||||
Serial.print(" CO2[");
|
Serial.print(" CO2[");
|
||||||
//Returns calculated CO2 reading
|
//Returns calculated CO2 reading
|
||||||
Serial.print(ccs811.getCO2());
|
Serial.print(co2 = ccs811.getCO2());
|
||||||
Serial.print("] tVOC[");
|
Serial.print("] tVOC[");
|
||||||
//Returns calculated TVOC reading
|
//Returns calculated TVOC reading
|
||||||
|
|
||||||
Serial.print(ccs811.getTVOC());
|
Serial.print(tvoc = ccs811.getTVOC());
|
||||||
Serial.print("] baseline[");
|
Serial.print("] baseline[");
|
||||||
Serial.print(ccs811Baseline = ccs811.getBaseline());
|
Serial.print(ccs811Baseline = ccs811.getBaseline());
|
||||||
|
|
||||||
|
publish(co2,"/CO2");
|
||||||
|
publish(tvoc,"/TVOC");
|
||||||
|
publish(ccs811Baseline,"/base");
|
||||||
|
|
||||||
Serial.print("] millis[");
|
Serial.print("] millis[");
|
||||||
//Simply the time since program start
|
//Simply the time since program start
|
||||||
Serial.print(millis());
|
Serial.print(millis());
|
||||||
@@ -114,9 +123,9 @@ int in_ccs811::Poll()
|
|||||||
Serial.println();
|
Serial.println();
|
||||||
printSensorError();
|
printSensorError();
|
||||||
|
|
||||||
//#ifdef WAK_PIN
|
#ifdef WAK_PIN
|
||||||
// digitalWrite(WAK_PIN,HIGH); //Relax some time
|
digitalWrite(WAK_PIN,HIGH); //Relax some time
|
||||||
//#endif
|
#endif
|
||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,7 +12,7 @@
|
|||||||
|
|
||||||
#if defined (ARDUINO_ARCH_ESP8266)
|
#if defined (ARDUINO_ARCH_ESP8266)
|
||||||
#define twi_scl D1
|
#define twi_scl D1
|
||||||
#define WAK_PIN D4
|
#define WAK_PIN D3
|
||||||
|
|
||||||
#define SCL_LOW() (GPES = (1 << twi_scl))
|
#define SCL_LOW() (GPES = (1 << twi_scl))
|
||||||
#define SCL_HIGH() (GPEC = (1 << twi_scl))
|
#define SCL_HIGH() (GPEC = (1 << twi_scl))
|
||||||
|
|||||||
Reference in New Issue
Block a user