Skip to content

Commit

Permalink
Updated README and changed battery sensor values.
Browse files Browse the repository at this point in the history
Those were not matching with my devices.
  • Loading branch information
Christian Erhardt committed Apr 30, 2019
1 parent 5e9fa42 commit 8ce3591
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 14 deletions.
41 changes: 37 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,49 @@
# esp-soil-moisture-sensor
[![Latest Release](https://img.shields.io/badge/release-v1.0.0-yellow.svg?style=flat-square)](https://github.com/MoJo2600/esp-soil-moisture-sensor/releases) [![PlatformIO](https://img.shields.io/badge/Powered-PlatformIO-blue.png)](https://platformio.org/lib/show/555/Homie) [![Homie](https://img.shields.io/badge/Powered-Homie-red.svg)](https://github.com/homieiot/homie-esp8266)

This repository contains code to use the [Homie](https://homieiot.github.io/homie-esp8266/docs/develop-v3/quickstart/what-is-it/) framework and its IoT convention with a soil moisture sensor from [April Brothers](https://wiki.aprbrother.com/en/ESP_Soil_Moisture_Sensor.html). Source and schematic: https://github.com/AprilBrother/esp-soil-moisture-sensor
This repository contains code to use the [Homie](https://homieiot.github.io/homie-esp8266/docs/develop-v3/quickstart/what-is-it/) framework and its IoT convention with a soil moisture sensor from [April Brothers](https://wiki.aprbrother.com/en/ESP_Soil_Moisture_Sensor.html) (Source and schematic: https://github.com/AprilBrother/esp-soil-moisture-sensor). When this firmware is running, the sensor will publish it's sensor readings in the homie [convention format](https://homieiot.github.io/specification/spec-core-v2_0_1/).

## How to flash and configure
The following properties are published:

|Property | Description |
|--- |--- |
|moisture | Soil moisture in percent |
|moistureraw | Raw sensor reading (for debugging) |
|temperature | Temperature in °C |
|battery | Battery level in percent |
|batteryraw | Raw sensor reading (for debugging) |

## TL;DR

To use this firmware on your sensor follow the following steps:

* Grab the latest [release binary](https://github.com/MoJo2600/esp-soil-moisture-sensor/releases)
* Remove the jumper from the board
* [Open the configuration website](http://setup.homie-esp8266.marvinroger.fr/)
* Connect the flasher and write the firmware to your device

```
esptool -cd nodemcu -cb 115200 -cp "/dev/ttyUSB0" -cf firmware.bin
```
* Remove the flasher
* Insert batteries
* Connect to the new Wifi (homie-123456)
* Open the configuration website, the device is found and you can follow the configuration process
* Remove the batteries
* Add Jumper to enable deep sleep
* Insert batteries again
* You can then subscribe to the topic homie/# on your MQTT broker and you will see the incoming messages

## Developing

### How to flash and configure
This code uses [PlatformIO](https://platformio.org/) for easy compilation and uploading. Just open PlatformIO and open the source folder and click on build. PlatformIO will download all necessary libraries and installs them for you.

### Flashing
#### Flashing

1. Set the Jumper to the open position
2. Connect programmer
3. Build and upload
4. Power cycle the device

### Configuring

Expand Down
3 changes: 2 additions & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ framework = arduino
build_flags = -DPIO_FRAMEWORK_ARDUINO_LWIP2_LOW_MEMORY -Wl,-Teagle.flash.4m3m.ld
;-D PIO_FRAMEWORK_ARDUINO_LWIP2_LOW_MEMORY -DDEBUG_ESP_PORT=Serial -DDEBUG_ESP_SSL -DDEBUG_ESP_TLS_MEM -DDEBUG_ESP_HTTP_CLIENT -DDEBUG_ESP_CORE -DDEBUG_ESP_WIFI -DDEBUG_ESP_HTTP_UPDATE

; The order matters here. We're using 0.8.1 of AsyncMqttClient because of a bug https://github.com/homieiot/homie-esp8266/issues/529
; The order matters here. We're using 0.8.1 of AsyncMqttClient because of a bug
; see https://github.com/homieiot/homie-esp8266/issues/529
lib_deps =
[email protected],!=0.8.2
[email protected]
Expand Down
31 changes: 22 additions & 9 deletions src/esp-soil-moisture-sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ HomieSetting<bool> useLEDSetting("useLED", "Defines if the LED should be active"

// Pin settings
const int PIN_CLK = D5;
const int PIN_SENSOR = A0;
const int PIN_SENSOR = A0;
const int PIN_LED = D7;
const int PIN_SWITCH = D8;
const int PIN_BUTTON = D6;
Expand Down Expand Up @@ -51,7 +51,7 @@ float readSensor() {
float total = 0.0;
float rawVal = 0.0;
float ret = 0.0;
int sampleCount = 3;
float sampleCount = 3.0;

for(int i = 0; i < sampleCount; i++){
rawVal = analogRead(PIN_SENSOR);
Expand Down Expand Up @@ -89,6 +89,7 @@ void getSendMoisture(float batteryCharge) {

int moisture = 0;
float moist_raw = readSensor();
sensorNode.setProperty("moistureraw").send(String(moist_raw));

#ifdef DEBUG
Homie.getLogger() << "Moisture raw: " << moist_raw << endl;
Expand Down Expand Up @@ -128,9 +129,10 @@ float getSendBattery() {

int batteryCharge = 0;
float battery_raw = readSensor();
sensorNode.setProperty("batteryraw").send(String(battery_raw));

// 736 = 2.5v , 880 = 3.0v , esp dead at 2.3v
batteryCharge = map(battery_raw, 736, 880, 0, 100);
// 790 = 2.5v , 975 = 3.0v , esp dead at 2.3v
batteryCharge = map(battery_raw, 790, 975, 0, 100);

#ifdef DEBUG
Homie.getLogger() << "Battery charge after mapping: " << batteryCharge << endl;
Expand Down Expand Up @@ -164,7 +166,7 @@ void getSendTemperature() {
// End Transmission
Wire.endTransmission();

// Request 2 bytes , Msb first
// Request 2 bytes , Msb first to get temperature
Wire.requestFrom(TMP_ADDR, 2);
// Read temperature as Celsius (the default)
while(Wire.available()) {
Expand Down Expand Up @@ -226,9 +228,12 @@ void setup() {

Homie.setLoggingPrinter(&Serial);

// Set up default values for settings
temperatureIntervalSetting.setDefaultValue(DEFAULT_DEEP_SLEEP_MINUTES)
.setValidator([] (long candidate) {
return candidate > 0 && candidate < 72;
// 72 Minutes is the maximum sleep time supported
// by ESP8266 https://thingpulse.com/max-deep-sleep-for-esp8266/
return candidate > 0 && candidate <= 70;
});

useLEDSetting.setDefaultValue(DEFAULT_USE_LED);
Expand All @@ -245,7 +250,7 @@ void setup() {
#endif
}

Homie_setFirmware("esp-soil-moisture-sensor", "1.0.1");
Homie_setFirmware("esp-soil-moisture-sensor", "1.0.0");

// Configure homie to use the build in button for configuration reset
// Press and hold button for 2sec to reset the homie configuration
Expand All @@ -261,6 +266,10 @@ void setup() {
.setName("Moisture")
.setDatatype("integer")
.setUnit("%");
sensorNode.advertise("moistureraw")
.setName("Moisture RAW value")
.setDatatype("float")
.setUnit("");
sensorNode.advertise("temperature")
.setName("Temperature")
.setDatatype("float")
Expand All @@ -269,7 +278,11 @@ void setup() {
.setName("Battery")
.setDatatype("integer")
.setUnit("%");

sensorNode.advertise("batteryraw")
.setName("Battery RAW value")
.setDatatype("float")
.setUnit("");

// Define event handler
Homie.onEvent(onHomieEvent);
// Setup homie
Expand All @@ -287,7 +300,7 @@ void setup() {
pinMode(D0, WAKEUP_PULLUP);
// initialize pin states
digitalWrite(PIN_LED, HIGH);
digitalWrite(PIN_SWITCH, HIGH);
digitalWrite(PIN_SWITCH, LOW);
digitalWrite(PIN_BUTTON, HIGH);

// device address is specified in datasheet
Expand Down

0 comments on commit 8ce3591

Please sign in to comment.