HELP! Additional 8 Relay without delay

Hello guys, can anyone help me about additional 8 relay without delay in my code. The relay activates alternately every few minutes.
This my code :

#include <Arduino.h>
#include <ModbusMaster.h>
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <ArduinoJson.h>
#include "Update.h"
#include <ThingsBoard.h>

#define PIN_RS485_TXEN 5 // D5
#define PIN_WATER_PUMP 4 // D4

#define TIMER_SEND_DATA_INTERVAL 2000
#define SOIL_MOISTURE_THRESHOLD_LOW 50.0f
#define SOIL_MOISTURE_THRESHOLD_HIGH 80.0f

/*const int relay1 = 34;       //Output Relay 1 ke Arnuino Pin D0
const int relay2 = 35;          //Output Relay 2 ke Arnuino Pin D1
const int relay3 = 32;          //Output Relay 3 ke Arnuino Pin D2
const int relay4 = 33;          //Output Relay 4 ke Arnuino Pin D3
const int relay5 = 25;          //Output Relay 5 ke Arnuino Pin D4
const int relay6 = 26;          //Output Relay 6 ke Arnuino Pin D5
const int relay7 = 27;          //Output Relay 7 ke Arnuino Pin D6
const int relay8 = 14;          //Output Relay 8 ke Arnuino Pin D7*/


constexpr uint16_t MAX_MESSAGE_SIZE = 128U;
constexpr char WIFI_SSID[] = "....";
constexpr char WIFI_PASSWORD[] = "....";
constexpr char THINGSBOARD_SERVER[] = "....";
constexpr char THINGSBOARD_TOKEN[] = "....";
constexpr uint16_t THINGSBOARD_PORT = 80U;
constexpr char TEMPERATURE_KEY[] = "temperature";
constexpr char HUMIDITY_KEY[] = "moisture";

ModbusMaster mbus;
WiFiClient espClient;
ThingsBoard tb(espClient);

void preTransmission()
{
  digitalWrite(PIN_RS485_TXEN, HIGH);
}

void postTransmission()
{
  digitalWrite(PIN_RS485_TXEN, LOW);
}

void OnReadSensor()
{
    uint8_t result = mbus.readHoldingRegisters(0x00, 8);
    if (result == mbus.ku8MBSuccess)
    {
      digitalWrite(LED_BUILTIN, HIGH);
        // printf("Data: ");
        // for (int i=0; i<8; i++)
        //   Serial.printf("%d ", mbus.getResponseBuffer(i));
        // Serial.println();  
      int nData0    = mbus.getResponseBuffer(0);
      int nData1    = mbus.getResponseBuffer(1);
      float fSoilMoisture = (float(mbus.getResponseBuffer(2))/10.0f);
      float fTemperature = (float(mbus.getResponseBuffer(3))/10.0f);

      Serial.printf("SoilSensor Data: %d %d Moisture: %2.1f %% Temp: %2.1f C\r\n",
      nData0, nData1, fSoilMoisture, fTemperature);    
      if (tb.connected())
      {
        Serial.println("Sending data to thingsboard");
        tb.sendTelemetryData("Temperature", fTemperature);
        tb.sendTelemetryData("SoilMoisture", fSoilMoisture);
      }
      else
      {
        Serial.println("Thingsboard not connected");
      }

      if (fSoilMoisture<=SOIL_MOISTURE_THRESHOLD_LOW)
      {
        Serial.println("Soil moisture is low, turn on water pump");
        digitalWrite(LED_BUILTIN, HIGH);
      }

      if (fSoilMoisture>=SOIL_MOISTURE_THRESHOLD_HIGH)
      {
        Serial.println("Soil moisture is high, turn off water pump");
        digitalWrite(LED_BUILTIN, LOW);
      }
        
      digitalWrite(LED_BUILTIN, LOW);
    }
    else
      Serial.printf("Read failed, result: 0x%02X\r\n", result);
}

void setup() {
  Serial.begin(115200);
  Serial2.begin(9600);
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, LOW);
  pinMode(PIN_RS485_TXEN, OUTPUT);
  digitalWrite(PIN_RS485_TXEN, LOW); 

  pinMode(PIN_WATER_PUMP, OUTPUT);
  digitalWrite(PIN_WATER_PUMP, LOW);

  mbus.begin(1, Serial2);

  // pakai ini jika modul RS585 bukan mode AutoDirection (memiliki pin DE dan RE)
  mbus.preTransmission(preTransmission);
  mbus.postTransmission(postTransmission);

  WiFi.mode(WIFI_STA);
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  digitalWrite(LED_BUILTIN, HIGH);
  while (WiFi.waitForConnectResult() != WL_CONNECTED) {
  Serial.println("WiFi Connection Failed! Rebooting...");
  delay(500);
  digitalWrite(LED_BUILTIN, LOW);
  delay(4500);
  ESP.restart();
  }
  digitalWrite(LED_BUILTIN, LOW);
  Serial.print("System connected with IP Address: ");
  Serial.println(WiFi.localIP());
    if (tb.connect(THINGSBOARD_SERVER, THINGSBOARD_TOKEN)){
    Serial.println("Connected to thingsboard.");
  }
  else
  {
      Serial.println("Failed to connect thingsBoard");
  }
}


unsigned long lastMillis = 0;
void loop() {
  unsigned long nNow = millis();
  if (nNow-lastMillis >= TIMER_SEND_DATA_INTERVAL)
  {
    lastMillis = nNow;
    OnReadSensor();
  }
  tb.loop();
}

I used platformIO to code and run it.

what doe it mean?
what's your arduino?
did you write that code? do you understand it?

Hello blink_101

Welcome to the world's best Arduino forum ever.

Solder together a relay object that has the corresponding services.

Have a nice day and enjoy coding in C++.

yes, my arduino. I'm not the only one writing, but I'm also helped. and in general I understand

im new in here, thanks

what type of arduino was the question. Uno, Mega, ESP32, MKR WiFi, ATTINY85, ... there are zillions possible hardware...

i use esp32 for my project

so can you answer all the questions or do we need to ask 36 times?

Please provide:

  • a link to the relay module you're using.
  • schematic of the system you're making
  • clear photos of the system as you have built it so far

Also explain which relays need to switch under which conditions; i.e. 'every few minutes': what does this mean exactly? Do each of the 8 relays need to switch after 2 minutes, precisely, all at the same time? Or do they switch in response to some kind of input from the outside world that occurs every few minutes? Or should they switch on in response to an external trigger, and then switch off again on their own accord after a few minutes?

Don't hesitate to use e.g. Google Translate to communicate on this forum; it often does a really good job these days and may help both you and us in understanding each other.

Ah, I just realized. what this means is how to place the correct coding for 8 relays alternately for several minutes, for example 1 minute without using delay. I tried several methods but failed. The program is already running without a relay, namely with sensors only and online monitoring using a thingsboard, I'm confused about adding a relay. because my friend said that if there is a delay it will interfere with the sensor reading time

thanks for the advice. I'll do it in more detail next time

1 Like

For extra information and examples look at

you might benefit from studying state machines. Here is a small introduction to the topic: Yet another Finite State Machine introduction

1 Like

that's very helpful. Thank you for your advice, I'll study that first I guess

oh and also do you have an example of 4 relays that are active alternately within a few minutes that don't use delay and also use sensors to activate the relays?

look at the tutorials

flashing a led

is like activating and deactivating a relay

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