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:
2019-03-30 20:20:32 +03:00
parent b30b9346fb
commit 9b1c2e5889
6 changed files with 67 additions and 14 deletions

32
lighthub/abstractin.cpp Normal file
View 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);
};

View File

@@ -1,3 +1,6 @@
#pragma once
#include "Arduino.h"
class Input;
class abstractIn {
public:
@@ -7,5 +10,8 @@ public:
protected:
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;
};

View File

@@ -362,6 +362,7 @@ void Input::dht22Poll() {
float humidity = roundf(dhtSensorData.humidity);
#else
DHT dht(pin, DHT22);
dht.begin();
float temp = dht.readTemperature();
float humidity = dht.readHumidity();
#endif

View File

@@ -1180,10 +1180,11 @@ lan_status loadConfigFromHttp(int arg_cnt, char **args)
wdt_res();
//debugSerial<<"making GET request");get
htclient.beginRequest();
htclient.get(URI);
responseStatusCode = htclient.get(URI);
htclient.endRequest();
if (responseStatusCode == HTTP_SUCCESS)
{
// read the status code and body of the response
responseStatusCode = htclient.responseStatusCode();
response = htclient.responseBody();
@@ -1209,6 +1210,10 @@ lan_status loadConfigFromHttp(int arg_cnt, char **args)
debugSerial<<F("Config retrieving failed\n");
return READ_RE_CONFIG;//-11; //Load from NVRAM
}
} else {
debugSerial<<F("Connect failed\n");
return READ_RE_CONFIG;//-11; //Load from NVRAM
}
#endif
#if defined(ARDUINO_ARCH_ESP8266)
HTTPClient httpClient;

View File

@@ -1,4 +1,6 @@
#include "modules/in_ccs811_hdc1080.h"
#include "Arduino.h"
#ifndef CSSHDC_DISABLE
CCS811 ccs811(CCS811_ADDR);
@@ -69,8 +71,10 @@ Serial.print(t=hdc1080.readTemperature());
Serial.print("C, RH=");
Serial.print(h=hdc1080.readHumidity());
Serial.print("% Status=");
publish(t,"/T");
publish(h,"/H");
Serial.println(reg=hdc1080.readRegister().rawData,HEX);
/////// TODO ccs811.setEnvironmentalData(h,t);
ccs811.setEnvironmentalData(h,t);
if (reg==0xff) //ESP I2C glitch
{
@@ -87,9 +91,9 @@ return 1;
int in_ccs811::Poll()
{
//#ifdef WAK_PIN
// digitalWrite(WAK_PIN,LOW);
//#endif
#ifdef WAK_PIN
digitalWrite(WAK_PIN,LOW);
#endif
//Check to see if data is ready with .dataAvailable()
if (ccs811.dataAvailable())
{
@@ -97,16 +101,21 @@ int in_ccs811::Poll()
//Get them later
CCS811Core::status returnCode = ccs811.readAlgorithmResults();
printDriverError(returnCode);
float co2,tvoc;
Serial.print(" CO2[");
//Returns calculated CO2 reading
Serial.print(ccs811.getCO2());
Serial.print(co2 = ccs811.getCO2());
Serial.print("] tVOC[");
//Returns calculated TVOC reading
Serial.print(ccs811.getTVOC());
Serial.print(tvoc = ccs811.getTVOC());
Serial.print("] baseline[");
Serial.print(ccs811Baseline = ccs811.getBaseline());
publish(co2,"/CO2");
publish(tvoc,"/TVOC");
publish(ccs811Baseline,"/base");
Serial.print("] millis[");
//Simply the time since program start
Serial.print(millis());
@@ -114,9 +123,9 @@ int in_ccs811::Poll()
Serial.println();
printSensorError();
//#ifdef WAK_PIN
// digitalWrite(WAK_PIN,HIGH); //Relax some time
//#endif
#ifdef WAK_PIN
digitalWrite(WAK_PIN,HIGH); //Relax some time
#endif
}
return 1;
}

View File

@@ -12,7 +12,7 @@
#if defined (ARDUINO_ARCH_ESP8266)
#define twi_scl D1
#define WAK_PIN D4
#define WAK_PIN D3
#define SCL_LOW() (GPES = (1 << twi_scl))
#define SCL_HIGH() (GPEC = (1 << twi_scl))