Change code to constant read sensors during wake time

Hello,

I have some DIY More ESP32 moisture sensors with light, temp, humidity and have installed the LilyGO/higrowopen/HiGrowEsp32.ino.

Everything works, but I'd like to have the sensors constantly read during wake time from deep sleep.

Could someone explain where I could adjust the code, please?

Ian

/*
  HiGrowESP32MQTT
  (c) Claus Kuehnel 2018-03-18 info@ckuehnel.ch

  The ESP32 reads data from HiGrow device and publishes these via MQTT

  based on https://github.com/LilyGO/higrowopen/tree/master/HiGrowEsp32
*/

#include <WiFi.h>
#include <PubSubClient.h>
#include "DHT.h"
#include "credentials.h"

/* create an instance of PubSubClient client */
WiFiClient espClient;
PubSubClient client(espClient);

uint64_t chipid;

const int dhtpin = 22;
const int soilpin = 32;
const int POWER_PIN = 34;
const int LIGHT_PIN = 33;

// Initialize DHT sensor.
DHT dht(dhtpin, DHTTYPE);

// Temporary variables
static char celsiusTemp[7];
static char humidityTemp[7];
char msg[20];

// Client variables 
char linebuf[80];
int charcount=0;

char deviceid[21];

void setup() 
{
  dht.begin();
  
  Serial.begin(115200);
  delay(2000); // wait for monitor

  esp_sleep_enable_timer_wakeup(DEEPSLEEP_SECONDS * uS_TO_S_FACTOR);

  pinMode(16, OUTPUT); // blue LED
  pinMode(POWER_PIN, INPUT);
  digitalWrite(16, LOW);  

  chipid = ESP.getEfuseMac();
  sprintf(deviceid, "%" PRIu64, chipid);
  Serial.print("DeviceId: ");
  Serial.println(deviceid);

  connectWiFi();
  configureMQTT();
}

void loop() 
{
  char body[1024];
  digitalWrite(16, LOW); //switched on

  /* if client was disconnected then try to reconnect again */
  if (!client.connected()) {
    mqttconnect();
  }

  sensorsData(body);
  delay(60000);
  WiFi.disconnect(true);
  Serial.println("Going to Deep Sleep..."); esp_deep_sleep_start();    // uncomment for deep sleep
  delay(5000);               // used for test
}

void sensorsData(char* body)
{
  //This section reads all sensors
  
  int waterlevel = analogRead(soilpin);
  int lightlevel = analogRead(LIGHT_PIN);
  
  waterlevel = map(waterlevel, 0, 4095, 1023, 0);
  waterlevel = constrain(waterlevel, 0, 1023);
  if (!isnan(waterlevel)) 
  {
    snprintf (msg, 20, "%d", waterlevel);
    /* publish the message */
    client.publish(SOIL_TOPIC, msg);
  }
  
  lightlevel = map(lightlevel, 0, 4095, 0, 1023);
  lightlevel = constrain(lightlevel, 0, 1023);
  if (!isnan(lightlevel)) 
  {
    snprintf (msg, 20, "%d", lightlevel);
    /* publish the message */
    client.publish(LIGHT_TOPIC, msg);
  }
  
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float humidity = dht.readHumidity();
  if (!isnan(humidity)) 
  {
    snprintf (msg, 20, "%5.1f", humidity);
    /* publish the message */
    client.publish(HUMI_TOPIC, msg);
  }
  
  // Read temperature as Celsius (the default)
  float temperature = dht.readTemperature();
  if (!isnan(temperature)) 
  {
    snprintf (msg, 20, "%5.1f", temperature);
    /* publish the message */
    client.publish(TEMP_TOPIC, msg);
  }
 
  Serial.print("DevideId: "); Serial.println(deviceid);
  Serial.print("Temperature: "); Serial.print(temperature); Serial.println(" *C");
  Serial.print("Humidity: "); Serial.print(humidity); Serial.println(" %rF");
  Serial.print("Soil: "); Serial.println(waterlevel);
  Serial.print("Light: "); Serial.println(lightlevel);
}

What do you mean, "constantly read"? Can you describe what they do now?

it just reads and sends MQTT once then no more before deep sleep.

Say the soil is dry and triggers a threshold to switch on irrigation, the sensor should then constantly resend MQTT data to know when the "wet" threshold has been reached. If the MQTT data is only sent once the irrigation will not know when to stop.

Ideally, a 5 sec delay then read again till deep sleep (5 min wake time / 5 sec = 60 sensor reads and send MQTT)

That sounds fairly straightforward. What is your specific obstacle in coding it? Did you not write it?

Na, I didn't write it and I only have basic programming skills and unfortunately don't know exactly where I have to change the code.

Oh, I see. I think it would take more than an "adjustment" to the code. It would involve some work, usually helpers here expect to see some effort on the part of the poster to complete the task themselves, as it is basically a self-help forum and the replies are usually in the form of guidance. There is another sub forum where you can offer payment to complete the work.

Aha sorry I didn't realise that, I'd gladly offer payment for help, would you know the title of the subforum?

Send a message to the moderators via the flag icon below the post, request a move. They will know where to place it.

Ok Thanks

Hi

Got this sorted yet?

sensorsData(body);
  delay(60000);

You need to change the delay(60000) part to a loop in which a delay is a suitable value like 2000 to get 30 readings in the 60000 seconds before it goes to sleep. You could also do away with delay and use a timer( millis()).

Of course the reading the sensors (sensorsData()) should be within the loop.

If you need further help just reply to my PM

Happy coding in the almost new year

@mugabi, not sure what you PM'ed, but if it was technical advice, please don't do that. It deprives other helpers of information and may create a lot of duplication and wasted time. Additionally, you may actually be wrong, and you are missing the opportunity to vet your advice with a lot of experts.

There is no rule about that, but it's better to be considerate and keep things in the open.

@anon57585045
My take on a solution (it probably can be solved in several ways) is outlined above. It is in the public domains, improvements corrections and contradictions are all welcome.

Okay, no harm, no foul. What you posted needs no discussion.

not tested

/*
  HiGrowESP32MQTT
  (c) Claus Kuehnel 2018-03-18 info@ckuehnel.ch

  The ESP32 reads data from HiGrow device and publishes these via MQTT

  based on https://github.com/LilyGO/higrowopen/tree/master/HiGrowEsp32
*/

#include <WiFi.h>
#include <PubSubClient.h>
#include "DHT.h"
#include "credentials.h"

/* create an instance of PubSubClient client */
WiFiClient espClient;
PubSubClient client(espClient);

uint64_t chipid;

const int dhtpin = 22;
const int soilpin = 32;
const int POWER_PIN = 34;
const int LIGHT_PIN = 33;

// Initialize DHT sensor.
DHT dht(dhtpin, DHTTYPE);

// Temporary variables
static char celsiusTemp[7];
static char humidityTemp[7];
char msg[20];

// Client variables 
char linebuf[80];
int charcount=0;

char deviceid[21];

void setup() 
{
  dht.begin();
  
  Serial.begin(115200);
  delay(2000); // wait for monitor

  esp_sleep_enable_timer_wakeup(DEEPSLEEP_SECONDS * uS_TO_S_FACTOR);

  pinMode(16, OUTPUT); // blue LED
  pinMode(POWER_PIN, INPUT);
  digitalWrite(16, LOW);  

  chipid = ESP.getEfuseMac();
  sprintf(deviceid, "%" PRIu64, chipid);
  Serial.print("DeviceId: ");
  Serial.println(deviceid);

  connectWiFi();
  configureMQTT();
}

unsigned long sleepInSix = 6000;
unsigned long pastTime = millis();
void loop() 
{
  char body[1024];
  digitalWrite(16, LOW); //switched on

  /* if client was disconnected then try to reconnect again */
  if (!client.connected()) {
    mqttconnect();
  }

  sensorsData(body);
  //delay(60000);
  if( (millis() - pastTime) >= sleepInSix )
  {
  WiFi.disconnect(true);
  Serial.println("Going to Deep Sleep..."); 
  esp_deep_sleep_start();    // uncomment for deep sleep
  }
  //delay(5000);               // used for test
}

void sensorsData(char* body)
{
  //This section reads all sensors
  
  int waterlevel = analogRead(soilpin);
  int lightlevel = analogRead(LIGHT_PIN);
  
  waterlevel = map(waterlevel, 0, 4095, 1023, 0);
  waterlevel = constrain(waterlevel, 0, 1023);
  if (!isnan(waterlevel)) 
  {
    snprintf (msg, 20, "%d", waterlevel);
    /* publish the message */
    client.publish(SOIL_TOPIC, msg);
  }
  
  lightlevel = map(lightlevel, 0, 4095, 0, 1023);
  lightlevel = constrain(lightlevel, 0, 1023);
  if (!isnan(lightlevel)) 
  {
    snprintf (msg, 20, "%d", lightlevel);
    /* publish the message */
    client.publish(LIGHT_TOPIC, msg);
  }
  
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float humidity = dht.readHumidity();
  if (!isnan(humidity)) 
  {
    snprintf (msg, 20, "%5.1f", humidity);
    /* publish the message */
    client.publish(HUMI_TOPIC, msg);
  }
  
  // Read temperature as Celsius (the default)
  float temperature = dht.readTemperature();
  if (!isnan(temperature)) 
  {
    snprintf (msg, 20, "%5.1f", temperature);
    /* publish the message */
    client.publish(TEMP_TOPIC, msg);
  }
 
  Serial.print("DevideId: "); Serial.println(deviceid);
  Serial.print("Temperature: "); Serial.print(temperature); Serial.println(" *C");
  Serial.print("Humidity: "); Serial.print(humidity); Serial.println(" %rF");
  Serial.print("Soil: "); Serial.println(waterlevel);
  Serial.print("Light: "); Serial.println(lightlevel);
}

Thanks, in the meantime I'm also trying to install ESPeasy on the ESP32 and I shall also try out your sketch and post the results here.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.