Compare commits

...

3 Commits

Author SHA1 Message Date
Mathieu Maret
14bf724ea9 correct doc 2016-11-20 23:59:55 +01:00
Mathieu Maret
7bb86cc53a mqtt correct warning when disabled 2016-11-20 23:59:50 +01:00
Mathieu Maret
785c555c7e Generic way to use info from gpio 2016-11-20 23:58:45 +01:00
8 changed files with 111 additions and 75 deletions

View File

@ -0,0 +1,33 @@
#include "GPIOSensor.h"
GPIOSensor::GPIOSensor(char *sName, char *mName,
uint8_t gpioPin, bool analog, int8_t power):
sensorName(sName), measureName(mName),
gpio(gpioPin), isAnalog(analog), powerGpio(power), lastMeasure(0){
if(powerGpio >= 0){
pinMode(powerGpio, OUTPUT);
}
}
int GPIOSensor::getMeasure(int &level){
if(powerGpio >= 0){
digitalWrite(powerGpio, HIGH);
delay(50);
}
if(isAnalog)
level = analogRead(gpio);
else
level = digitalRead(gpio);
if(powerGpio >= 0){
digitalWrite(powerGpio, LOW);
delay(50);
}
lastMeasure = level;
return 0;
}
int GPIOSensor::getLastMeasure(){
return lastMeasure;
}

View File

@ -0,0 +1,17 @@
#pragma once
#include "Arduino.h"
class GPIOSensor{
public:
char *sensorName;
char *measureName;
uint8_t gpio;
bool isAnalog;
int8_t powerGpio;
int lastMeasure;
GPIOSensor(char *sensorName, char *measureName, uint8_t gpio, bool isAnalog=false, int8_t powerGpio = -1);
int getMeasure(int &level);
int getLastMeasure();
};

View File

@ -9,23 +9,23 @@ int MqttSetup(char *server, char *user, char *passwd, int port, char * hostname)
template<typename T> int MqttPublish(Adafruit_MQTT_Publish *publisher, T value);
int MqttPublishBMP180(double temp, double pressure);
int MqttPublishDHT(float temp, float humidity);
int MqttPublishDry(int dry);
int MqttPublishIp(const String &ip);
void MqttCheckSubscription();
void MqttChangeGpioValue(int gpio, int value);
void MqttChangePWMValue(int gpio, int value);
bool MqttIsConfigured();
int MqttPublishGPIO();
#else
int MqttConnect(){return 0;}
int MqttIsConnected(){return 0;}
int MqttSetup(char *server, char *user, char *passwd, int port, char * hostname){return 0;}
template<typename T> int MqttPublish(Adafruit_MQTT_Publish *publisher, T value){return 0;}
int MqttPublishBMP180(double temp, double pressure){return 0;}
int MqttPublishDHT(float temp, float humidity){return 0;}
int MqttPublishDry(int dry){return 0;}
int MqttPublishIP(const String &ip){return 0;}
int MqttSetup(char *, char *, char *, int, char *){return 0;}
template<typename T> int MqttPublish(void *, T){return 0;}
int MqttPublishBMP180(double, double){return 0;}
int MqttPublishDHT(float, float){return 0;}
int MqttPublishIP(const String &){return 0;}
void MqttCheckSubscription(){}
void MqttChangeGpioValue(int gpio, int value){}
void MqttChangePWMValue(int gpio, int value){}
bool MqttIsConfigured(){}
void MqttChangeGpioValue(int, int){}
void MqttChangePWMValue(int, int){}
bool MqttIsConfigured(){return false;}
int MqttPublishGPIO(){return 0;}
#endif

View File

@ -2,25 +2,26 @@
#include <stdarg.h>
#include "utils.h"
#include "MQTT.h"
#include "sensors.h"
Adafruit_MQTT_Client *mqtt;
Adafruit_MQTT_Publish *mqtt_temp;
Adafruit_MQTT_Publish *mqtt_pressure;
Adafruit_MQTT_Publish *mqtt_dht_temp;
Adafruit_MQTT_Publish *mqtt_dht_humidity;
Adafruit_MQTT_Publish *mqtt_dry;
Adafruit_MQTT_Publish *mqtt_ip;
Adafruit_MQTT_Publish *mqttGpio[MAXSUBSCRIPTIONS] = {};
Adafruit_MQTT_Publish *mqttPwm[MAXSUBSCRIPTIONS] = {};
Adafruit_MQTT_Publish **gpioSensorsMqtt;
#define FEED_MAX_SIZE 96
//FEED have the following formats /feeds/USER/DEVICE_NAME/....
#define GPIO_FEED_FORMAT "/feeds/%s/%s/%s/%s"
#define TEMPERATURE_FEED_FORMAT "/feeds/%s/%s/temperature"
#define PRESSURE_FEED_FORMAT "/feeds/%s/%s/pressure"
#define TEMPERATURE_DHT_FEED_FORMAT "/feeds/%s/%s/dht/temperature"
#define HUMIDITY_DHT_FEED_FORMAT "/feeds/%s/%s/dht/humidity"
#define DRY_FEED_FORMAT "/feeds/%s/%s/dry"
#define GPIO_FEED_FORMAT "/feeds/%s/%s/gpio/%d"
#define GPIO_SET_FEED_FORMAT "/feeds/%s/%s/gpio/%d/set"
#define PWM_FEED_FORMAT "/feeds/%s/%s/gpio/%d"
@ -37,6 +38,14 @@ char *mqttId;
bool isMqttConfigured = false;
bool useMqtts = false;
void initGpioSensorsMqtt(char *user, char *hostname){
uint i;
gpioSensorsMqtt = new Adafruit_MQTT_Publish *[NB_ELEMENTS(gpioSensors)];
for(i = 0; i < NB_ELEMENTS(gpioSensors); i++){
gpioSensorsMqtt[i] = MqttCreatePublisher(GPIO_FEED_FORMAT, user, hostname,
gpioSensors[i]->sensorName, gpioSensors[i]->measureName);
}
}
int MqttSetup(char *server, char *user, char *passwd, int port, char *hostname) {
mqttId = hostname;
@ -57,7 +66,6 @@ int MqttSetup(char *server, char *user, char *passwd, int port, char *hostname)
mqtt_dht_humidity = MqttCreatePublisher(HUMIDITY_DHT_FEED_FORMAT, user, mqttId);
mqtt_temp = MqttCreatePublisher(TEMPERATURE_FEED_FORMAT, user, mqttId);
mqtt_pressure = MqttCreatePublisher(PRESSURE_FEED_FORMAT, user, mqttId);
mqtt_dry = MqttCreatePublisher(DRY_FEED_FORMAT, user, mqttId);
mqtt_ip = MqttCreatePublisher(IP_FEED_FORMAT, user, mqttId);
if( NB_ELEMENTS(gpioWatched) + NB_ELEMENTS(pwmWatched) > MAXSUBSCRIPTIONS){
@ -75,6 +83,7 @@ int MqttSetup(char *server, char *user, char *passwd, int port, char *hostname)
mqtt->subscribe(MqttCreateSubscribe(PWM_SET_FEED_FORMAT, user, mqttId, pwmWatched[i]));
mqttPwm[i] = MqttCreatePublisher(PWM_FEED_FORMAT, user, mqttId, pwmWatched[i]);
}
initGpioSensorsMqtt(user, hostname);
return 0;
}
@ -139,10 +148,6 @@ int MqttPublishBMP180(double temp, double pressure) {
return MqttPublish(mqtt_temp, temp) + MqttPublish(mqtt_pressure, pressure);
}
int MqttPublishDry(int dry) {
return MqttPublish(mqtt_dry, (dry*100)/1024);
}
int MqttPublishIP(const String &ip) {
return MqttPublish(mqtt_ip, ip.c_str());
}
@ -151,6 +156,17 @@ int MqttPublishDHT(float temp, float humidity) {
return MqttPublish(mqtt_dht_temp, temp) + MqttPublish(mqtt_dht_humidity, humidity);
}
int MqttPublishGPIO(){
if (MqttConnect() == 0){
uint i;
for (i = 0; i < NB_ELEMENTS(gpioSensors); i++) {
gpioSensorsMqtt[i]->publish(gpioSensors[i]->lastMeasure);
}
return 0;
}
return -1;
}
int getGpioFromSubscription(Adafruit_MQTT_Subscribe *subscription, const char *pattern) {
char *temp = strstr(subscription->topic, pattern);
if (!temp)

View File

@ -5,24 +5,31 @@ const int pwmWebConf[] = CONFIG_CONTROLLED_PWM;
String gpioControlHTML = "";
String pwmControlHTML = "";
void WebBuildGpio(String &desc){
uint i;
for (i = 0; i < NB_ELEMENTS(gpioSensors); i++){
desc += "<li>" + String(gpioSensors[i]->sensorName) + ": " + String(gpioSensors[i]->lastMeasure) + "</li>";
}
}
void WebHandleRoot() {
String gpioDesc = "";
WebBuildGpio(gpioDesc);
server.send(200, "text/html",
"<head><meta http-equiv=\"refresh\" content=\"" + String(CONFIG_SAMPLING_PERIODE_MS / 1000) + "\" ></head>"
"<h1>You are connected to " + String(conf.host) + "</h1>"
"<fieldset>"
"<legend>Sensors</legend>"
"<legend>Sensors</legend><ul>"
#ifdef CONFIG_ENABLE_BMP180
"Temperature " + String(temp, 2) + "C<br/>"
"<li>BMP180</li>Temperature " + String(temp, 2) + "C<br/>"
"Pressure " + String(pressure, 2) + "mB<br/>"
#endif
#ifdef CONFIG_ENABLE_DHT
"Temperature DHT " + String(dhtTemp, 0) + "C<br/>"
"Humidity DHT " + String(dhtHumidity, 0) + "%<br/>"
#endif
#ifdef CONFIG_ENABLE_DRY_SENSOR
"Dryness " + String((dryness*100)/1024) + "%<br/>"
"<li>DHT</li>Temperature " + String(dhtTemp, 0) + "C<br/>"
"Humidity " + String(dhtHumidity, 0) + "%<br/>"
#endif
""+gpioDesc+"</ul>"
"</fieldset>" + gpioControlHTML + pwmControlHTML + "<fieldset>"
"<legend>Settings</legend>"
"<a href=\"/setup\">Enter Setup</a><br/>"

View File

@ -20,11 +20,12 @@
/* --------------------- MQTT ------------------------------------ */
/* Send information to mqtt server configured in the setup mode */
/* GPIO value configured in config_device.h can be get by */
/* subscribing to /feeds/MQTTUSER/[HOSTNAME]/gpio/[GPIO] and */
/* subscribing to /feeds/[MQTTUSER]/[HOSTNAME]/gpio/[GPIO] and */
/* modified by publishin to */
/* /feeds/MQTTUSER/[HOSTNAME]/gpio/[GPIO]/set */
/* BMP180 will be published to /feeds/[HOSTNAME]/temperature and */
/* /feeds/[HOSTNAME]/pressure */
/* /feeds/[MQTTUSER]/[HOSTNAME]/gpio/[GPIO]/set */
/* BMP180 will be published to */
/* /feeds/[MQTTUSER]/[HOSTNAME]/temperature and */
/* /feeds/[MQTTUSER]/[HOSTNAME]/pressure */
@ -44,11 +45,11 @@
#include "debug_sketch.h"
#include "BMP180.h"
#include "sensor_DHT.h"
#include "dry_sensor.h"
#include "MQTT.h"
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
#include "EEPROM.h"
#include "sensors.h"
extern "C" {
#include <user_interface.h>
@ -61,7 +62,6 @@ extern "C" {
double temp, pressure;
float dhtTemp, dhtHumidity;
int dryness;
uint8_t mode;
productConfig conf = {BOOTMODE_SETUP, "", "", "", "", "", "", 0, 0, 0, 0, 0, 0, 0};
@ -172,7 +172,7 @@ void OTASetup() {
});
ArduinoOTA.begin();
SKETCH_DEBUG_PRINTLN("Ready");
SKETCH_DEBUG_PRINTF("IP address: ");
SKETCH_DEBUG_PRINT("IP address: ");
SKETCH_DEBUG_PRINTLN(WiFi.localIP());
SKETCH_DEBUG_PRINTF("Free Space: %d\n", ESP.getFreeSketchSpace());
#endif
@ -232,9 +232,6 @@ void setup() {
if (!DHTSetup(CONFIG_DHT_PIN)){
SKETCH_DEBUG_PRINTLN("DHT init success");
}
if (!DrySetup(CONFIG_DRY_POWER_PIN)){
SKETCH_DEBUG_PRINTLN("DRY init success");
}
WebSetupServer(mode);
}
#ifdef CONFIG_ENABLE_POWER_SAVE
@ -257,6 +254,7 @@ void loop() {
nbCycle++;
if (nbCycle > CONFIG_SAMPLING_PERIODE_MS / CONFIG_WEB_DELAY_MS) {
uint i;
if (!BMP180GetTempAndPressure(temp, pressure)) {
SKETCH_DEBUG_PRINT("Current T°C ");
SKETCH_DEBUG_PRINT(temp);
@ -273,13 +271,13 @@ void loop() {
if (mode == BOOTMODE_NORMAL)
MqttPublishDHT(dhtTemp, dhtHumidity);
}
if (!DryGetMeasure(dryness)){
SKETCH_DEBUG_PRINT("Current dryness ");
SKETCH_DEBUG_PRINT((dryness*100)/1024);
SKETCH_DEBUG_PRINTLN("%");
if (mode == BOOTMODE_NORMAL)
MqttPublishDry(dryness);
for (i = 0; i < NB_ELEMENTS(gpioSensors); i++){
int val;
gpioSensors[i]->getMeasure(val);
}
if (mode == BOOTMODE_NORMAL)
MqttPublishGPIO();
nbCycle = 0;
}
}

View File

@ -1,11 +0,0 @@
#pragma once
#ifdef CONFIG_ENABLE_DRY_SENSOR
int DrySetup(int powerGPIO);
int DryGetMeasure(int &dry);
bool DryIsConnected(){return true;}
#else
int DrySetup(int){return -1;}
int DryGetMeasure(int){return -1;}
bool DryIsConnected(){return false;}
#endif

View File

@ -1,24 +0,0 @@
#ifdef CONFIG_ENABLE_DRY_SENSOR
#include "dry_sensor.h"
int dryGPIO;
int DrySetup(int powerGPIO){
dryGPIO = powerGPIO;
if(dryGPIO >= 0){
pinMode(dryGPIO, OUTPUT);
}
return 0;
}
int DryGetMeasure(int &dry){
if(dryGPIO >= 0){
digitalWrite(dryGPIO,1);
delay(50);
}
dry = analogRead(A0);
if(dryGPIO >= 0){
digitalWrite(dryGPIO,0);
}
return 0;
}
#endif