Time to send data

Asked

Viewed 64 times

3

I’m developing a small meteorological station to apply on a farmland, which is to have a temperature sensor, a rain sensor and a humidity sensor to be uncooked later because I don’t have it yet. However I wanted to send the temperature data with a delay of 600000ms and that the sensor of rain had a delay of 1000. I don’t see the way to do it. What I’ve done in code so far is this::

#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <Wire.h>
#include <farmerkeith_BMP280.h>

int rainSensor = A0;
int rainLED = 2;
int dryLED = 3;
int sensorVal;
int sensortrigger = 350;
/*
   Conexao ao AP
   Credenciais de acesso
   SSID - Nome do AP de acesso
   Password - Passsword de acesso
*/
#define wifi_ssid "xxxxxxxx"
#define wifi_password "xxxxxxxxx"
/*
   Endereço IP do MQTT Broker (Mosquitto)
*/
#define mqtt_server "192.168.1.10"
/*
   Definição dos tópicos dos dados dos sensores
*/
#define bmp_temperature_topic "temperaturebmp"
#define bmp_pressure_topic "pressurebmp"
#define rain_sensor_topic "rainstate"

/*
   Inicialização das classes
*/
WiFiClient espClient;
PubSubClient client(espClient);
bmp280 bmp0;

void setup() {
  Serial.begin(115200);
  pinMode(rainSensor, INPUT);
  pinMode(rainLED, OUTPUT);
  pinMode(dryLED, OUTPUT);
  digitalWrite(rainLED, LOW);
  digitalWrite(dryLED, LOW);
  setup_wifi();
  client.setServer(mqtt_server, 1883);
  bmp0.begin();
}
void setup_wifi() {
  delay(10);
  /*
     Inicio de conexão á rede Wi-Fi
  */
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(wifi_ssid);
  WiFi.begin(wifi_ssid, wifi_password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void reconnect() {
  /*
   *Loop de reconexão até que se conecte na rede Wi-Fi e no MQTT Broker
   */
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    // If you do not want to use a username and password, change next line to
    // if (client.connect("ESP8266Client")) {
    if (client.connect("changeMe")) {
      Serial.println("connected");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}
void loop() {
  if (!client.connected()) {
    reconnect();
  }
  client.loop();
  // Segundos de espera entre medidas 
      int sensorVal = analogRead(rainSensor);
      if (sensorVal < sensortrigger) {
        Serial.println("Rain");
        digitalWrite(dryLED, LOW);
        digitalWrite(rainLED, HIGH);
        client.publish(rain_sensor_topic, String("Raining").c_str(), true);
      }
      else {
        Serial.println("Not Rain");
        digitalWrite(rainLED, LOW);
        digitalWrite(dryLED, HIGH);
        client.publish(rain_sensor_topic, String("Not Raining").c_str(), true);
      }


    // Leitura da temperatura e pressão
      double temperature = bmp0.readTemperature (); // measure temperature
      Serial.print("Temperature = ");
      Serial.print(temperature, 3); // print with 3 decimal places
      Serial.println( " degrees Celsius");
      client.publish(bmp_temperature_topic, String(temperature).c_str(), true);

      double pressure = bmp0.readPressure (); // measure pressure
      Serial.print("Atmospheric pressure = ");
      Serial.print(pressure, 4); // print with 4 decimal places
      Serial.println( " mbar");
      client.publish(bmp_pressure_topic, String(pressure).c_str(), true);
      delay(2000);
}

1 answer

0

The way it has is to use the delay(), only that the problem is that using it the program is stopped, so the only way is to use the timers, start a timer for 10s, when the timer ends (pass the 10s) then send the data, reset the timer again.

Just search a library you like and only use, you can use this Or one you like. You can also make your own library by making code at a lower level, activating the logging and using interruptions

Example code using this library that I used in my project:

#include millisDelay.h 

millisDelay ledDelay;

void setup()
{
 ledDelay.start(2000);
}
void loop(){
if(ledDelay.justFinished() )
   //faz algo (envia dados)
   ledDelay.start(2000); //dá reset no tempo
}

.c e . h of the library

Browser other questions tagged

You are not signed in. Login or sign up in order to post.