From 4c09dcbb2472aa29bd9376265996012a16c6dcf2 Mon Sep 17 00:00:00 2001 From: Mathieu Maret Date: Thu, 2 Jun 2016 01:31:57 +0200 Subject: [PATCH] Add the dry sensor --- WifiControlSensor/WifiControlSensor.ino | 10 ++++++++++ WifiControlSensor/config.h | 6 +++++- WifiControlSensor/config_device.h | 5 ++++- WifiControlSensor/dry_sensor.h | 11 +++++++++++ WifiControlSensor/dry_sensor.ino | 24 ++++++++++++++++++++++++ 5 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 WifiControlSensor/dry_sensor.h create mode 100644 WifiControlSensor/dry_sensor.ino diff --git a/WifiControlSensor/WifiControlSensor.ino b/WifiControlSensor/WifiControlSensor.ino index 3112558..902631c 100644 --- a/WifiControlSensor/WifiControlSensor.ino +++ b/WifiControlSensor/WifiControlSensor.ino @@ -42,6 +42,7 @@ #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" @@ -58,6 +59,7 @@ extern "C" { double temp, pressure; float dhtTemp, dhtHumidity; +int dryness; uint8_t mode; const char *hostName = ""; @@ -234,6 +236,9 @@ 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 @@ -266,6 +271,11 @@ void loop() { SKETCH_DEBUG_PRINTLN(dhtHumidity); MqttDhtPublish(dhtTemp, dhtHumidity); } + if (!DryGetMeasure(dryness)){ + SKETCH_DEBUG_PRINT("Current dryness "); + SKETCH_DEBUG_PRINT((dryness*100)/1024); + SKETCH_DEBUG_PRINTLN("%"); + } nbCycle = 0; } } diff --git a/WifiControlSensor/config.h b/WifiControlSensor/config.h index 8f612b0..99ee7e3 100644 --- a/WifiControlSensor/config.h +++ b/WifiControlSensor/config.h @@ -25,12 +25,16 @@ #define CONFIG_BMP180_SCL 0 #endif -#if defined(CONFIG_ENABLE_DHT) && !defined(CONFIG_DHT_PIN) +#if defined(CONFIG_ENABLE_DHT) && !defined(CONFIG_DHT_PIN) #error "When enabling DHT, you should configure SDA pin" #elif !defined(CONFIG_ENABLE_DHT) #define CONFIG_DHT_PIN 0 #endif +#ifndef CONFIG_DRY_POWER_PIN +#define CONFIG_DRY_POWER_PIN -1 +#endif + #ifndef CONFIG_SSID_NAME #define CONFIG_SSID_NAME "ESPConfigurator" #endif diff --git a/WifiControlSensor/config_device.h b/WifiControlSensor/config_device.h index d26800f..e3588a0 100644 --- a/WifiControlSensor/config_device.h +++ b/WifiControlSensor/config_device.h @@ -15,6 +15,10 @@ #define CONFIG_ENABLE_DHT #define CONFIG_DHT_PIN 5 +//#define CONFIG_ENABLE_DRY_SENSOR +//If the dry sensor is powered by a GPIO, this GPIO could be defined here +//#define CONFIG_DRY_POWER_PIN 13 + // Enable light sleep to save some power (http://bbs.espressif.com/viewtopic.php?f=6&t=133&p=485&hilit=sleep+modem#p485) //#define CONFIG_ENABLE_POWER_SAVE @@ -40,7 +44,6 @@ // Should have less value than MAXSUBSCRIPTIONS #define CONFIG_MQTT_CONTROLLED_GPIO {2,13} - // EEPROM SIZE // Max is 4096, but this amount will be allocated in RAM for reading its content //#CONFIG_EEPROM_SIZE 256 diff --git a/WifiControlSensor/dry_sensor.h b/WifiControlSensor/dry_sensor.h new file mode 100644 index 0000000..2d73a2f --- /dev/null +++ b/WifiControlSensor/dry_sensor.h @@ -0,0 +1,11 @@ +#pragma once + +#ifdef CONFIG_ENABLE_DRY_SENSOR +int DrySetup(int powerGPIO); +int DryGetMeasure(int &dry); +bool DryIsConnected(){return true;} +#else +int DrySetup(int powerGPIO){return 0;} +int DryGetMeasure(int){return -1;} +bool DryIsConnected(){return false;} +#endif diff --git a/WifiControlSensor/dry_sensor.ino b/WifiControlSensor/dry_sensor.ino new file mode 100644 index 0000000..d7ff47f --- /dev/null +++ b/WifiControlSensor/dry_sensor.ino @@ -0,0 +1,24 @@ +#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