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

View File

@ -2,25 +2,26 @@
#include <stdarg.h> #include <stdarg.h>
#include "utils.h" #include "utils.h"
#include "MQTT.h" #include "MQTT.h"
#include "sensors.h"
Adafruit_MQTT_Client *mqtt; Adafruit_MQTT_Client *mqtt;
Adafruit_MQTT_Publish *mqtt_temp; Adafruit_MQTT_Publish *mqtt_temp;
Adafruit_MQTT_Publish *mqtt_pressure; Adafruit_MQTT_Publish *mqtt_pressure;
Adafruit_MQTT_Publish *mqtt_dht_temp; Adafruit_MQTT_Publish *mqtt_dht_temp;
Adafruit_MQTT_Publish *mqtt_dht_humidity; Adafruit_MQTT_Publish *mqtt_dht_humidity;
Adafruit_MQTT_Publish *mqtt_dry;
Adafruit_MQTT_Publish *mqtt_ip; Adafruit_MQTT_Publish *mqtt_ip;
Adafruit_MQTT_Publish *mqttGpio[MAXSUBSCRIPTIONS] = {}; Adafruit_MQTT_Publish *mqttGpio[MAXSUBSCRIPTIONS] = {};
Adafruit_MQTT_Publish *mqttPwm[MAXSUBSCRIPTIONS] = {}; Adafruit_MQTT_Publish *mqttPwm[MAXSUBSCRIPTIONS] = {};
Adafruit_MQTT_Publish **gpioSensorsMqtt;
#define FEED_MAX_SIZE 96 #define FEED_MAX_SIZE 96
//FEED have the following formats /feeds/USER/DEVICE_NAME/.... //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 TEMPERATURE_FEED_FORMAT "/feeds/%s/%s/temperature"
#define PRESSURE_FEED_FORMAT "/feeds/%s/%s/pressure" #define PRESSURE_FEED_FORMAT "/feeds/%s/%s/pressure"
#define TEMPERATURE_DHT_FEED_FORMAT "/feeds/%s/%s/dht/temperature" #define TEMPERATURE_DHT_FEED_FORMAT "/feeds/%s/%s/dht/temperature"
#define HUMIDITY_DHT_FEED_FORMAT "/feeds/%s/%s/dht/humidity" #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_FEED_FORMAT "/feeds/%s/%s/gpio/%d"
#define GPIO_SET_FEED_FORMAT "/feeds/%s/%s/gpio/%d/set" #define GPIO_SET_FEED_FORMAT "/feeds/%s/%s/gpio/%d/set"
#define PWM_FEED_FORMAT "/feeds/%s/%s/gpio/%d" #define PWM_FEED_FORMAT "/feeds/%s/%s/gpio/%d"
@ -37,6 +38,14 @@ char *mqttId;
bool isMqttConfigured = false; bool isMqttConfigured = false;
bool useMqtts = 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) { int MqttSetup(char *server, char *user, char *passwd, int port, char *hostname) {
mqttId = 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_dht_humidity = MqttCreatePublisher(HUMIDITY_DHT_FEED_FORMAT, user, mqttId);
mqtt_temp = MqttCreatePublisher(TEMPERATURE_FEED_FORMAT, user, mqttId); mqtt_temp = MqttCreatePublisher(TEMPERATURE_FEED_FORMAT, user, mqttId);
mqtt_pressure = MqttCreatePublisher(PRESSURE_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); mqtt_ip = MqttCreatePublisher(IP_FEED_FORMAT, user, mqttId);
if( NB_ELEMENTS(gpioWatched) + NB_ELEMENTS(pwmWatched) > MAXSUBSCRIPTIONS){ 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])); mqtt->subscribe(MqttCreateSubscribe(PWM_SET_FEED_FORMAT, user, mqttId, pwmWatched[i]));
mqttPwm[i] = MqttCreatePublisher(PWM_FEED_FORMAT, user, mqttId, pwmWatched[i]); mqttPwm[i] = MqttCreatePublisher(PWM_FEED_FORMAT, user, mqttId, pwmWatched[i]);
} }
initGpioSensorsMqtt(user, hostname);
return 0; return 0;
} }
@ -139,10 +148,6 @@ int MqttPublishBMP180(double temp, double pressure) {
return MqttPublish(mqtt_temp, temp) + MqttPublish(mqtt_pressure, 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) { int MqttPublishIP(const String &ip) {
return MqttPublish(mqtt_ip, ip.c_str()); 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); 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) { int getGpioFromSubscription(Adafruit_MQTT_Subscribe *subscription, const char *pattern) {
char *temp = strstr(subscription->topic, pattern); char *temp = strstr(subscription->topic, pattern);
if (!temp) if (!temp)

View File

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

View File

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