working soil sensor

This commit is contained in:
Stefan Ostermann 2021-04-10 23:17:56 +02:00
parent 42830e1200
commit 50dff40902
2 changed files with 29 additions and 5 deletions

View File

@ -14,7 +14,7 @@
#if defined(USE_ARDUINO)
#define RH_RX_PIN 11
#elif defined(USE_ESPRESSIF)
#define RH_RX_PIN 12
#define RH_RX_PIN D8
#endif
#define RH_BUF_LEN 5
@ -22,6 +22,9 @@ uint8_t rh_buf[RH_BUF_LEN];
#define RH_ASK_MAX_MESSAGE_LEN RH_BUF_LEN
const int AirValue = 800; //replace the value with value when placed in air
const int WaterValue = 345; //replace the value with value when placed in water
RH_ASK driver(RH_SPEED, RH_RX_PIN);
@ -29,6 +32,7 @@ RH_ASK driver(RH_SPEED, RH_RX_PIN);
float t = 0;
float h = 0;
long bat = 0;
int soil = 0;
/**
* Message identifier constants:
@ -37,6 +41,7 @@ long bat = 0;
#define MSG_TEMP 0x01
#define MSG_HUMID 0x02
#define MSG_BAT 0x03
#define MSG_SOIL 0x04
#define MSG_ERR 0xee
void setup() {
@ -71,6 +76,14 @@ void loop() {
Serial.print(t);
Serial.println(" %");
break;
case MSG_SOIL:
// Soil Humidity:
Serial.println("0x04 Soil Humidity");
memcpy(&soil, &rh_buf[1], 2);
Serial.println(soil);
Serial.println(map(soil, AirValue, WaterValue, 0, 100));
break;
case MSG_BAT:
// battery data
Serial.println("0x03 Battery");

View File

@ -19,6 +19,9 @@
// pin of the DHT22 sensor
#define DHT_PIN 1
// pin of the soil sensor
#define SOIL_PIN A1
// the own RadioHead address
#define RH_OWN_ADDR 0xca // 202
@ -29,9 +32,9 @@
#define RH_SPEED 1000
// pins for the radio hardware
#define RH_RX_PIN 5 // not used, set to a non-existent pin
#define RH_RX_PIN 6 // not used, set to a non-existent pin
#define RH_TX_PIN 3
#define RH_PTT_PIN 5 // not used, set to a non-existent pin
#define RH_PTT_PIN 6 // not used, set to a non-existent pin
#include <Arduino.h>
#include <avr/sleep.h>
@ -68,6 +71,7 @@ uint8_t rh_buf[RH_BUF_LEN] = { 0x00, 0x00, 0x00, 0x00, 0x00 };
#define MSG_TEMP 0x01
#define MSG_HUMID 0x02
#define MSG_BAT 0x03
#define MSG_SOIL 0x04
#define MSG_ERR 0xee
@ -95,6 +99,8 @@ void setup() {
// setup the LED pin
pinMode(LED_PIN, OUTPUT);
pinMode(SOIL_PIN, INPUT);
// init the DHT22
dht_init(&dht, DHT_PIN);
@ -132,6 +138,7 @@ void loop() {
// temperature and humidity
float t = 0;//float is 32bit = 1byte
float h = 0;
int soil = 0;
long battery = 0;
if (counter % 4 == 0) {
@ -140,12 +147,16 @@ void loop() {
memcpy(&rh_buf[1], &battery, 4);
rh_send(RH_BUF_LEN);
}else if (dht_read_data(&dht, &t, &h) == 1) {
if (counter % 2 == 0) {
if (counter % 3 == 0) {
rh_buf[0] = MSG_TEMP;
memcpy(&rh_buf[1], &t, 4);
} else if (counter % 2 == 1) {
} else if (counter % 3 == 1) {
rh_buf[0] = MSG_HUMID;
memcpy(&rh_buf[1], &h, 4);
} else if (counter % 3 == 2) {
rh_buf[0] = MSG_SOIL;
soil = analogRead(SOIL_PIN);
memcpy(&rh_buf[1], &soil, 2);
}
rh_send(RH_BUF_LEN);
} else {