Problem with non-stop calls using Arduino Uno and Blynk

Can anyone help me with this problem? Why are calls made one by one after the threshold is exceeded if one call was supposed to make and then another if the temperature (after 10 minutes) is not above the threshold? Any advice on what to change in the code so that it makes another call after 10 minutes if the temperature is below the set temperature?

#include <OneWire.h>
#include <DallasTemperature.h>



#define BLYNK_TEMPLATE_ID "TMPL44_6bflrE"
#define BLYNK_TEMPLATE_NAME "Pomiar temperatury"
#define BLYNK_AUTH_TOKEN "XXXX"
#define BLYNK_PRINT Serial
#define TINY_GSM_MODEM_SIM800
#include <TinyGsmClient.h>
#include <BlynkSimpleTinyGSM.h>
char apn[] = "internet";
char user[] = "internet";
char pass[] = "internet";
#include <SoftwareSerial.h>
SoftwareSerial SerialAT(3, 2); // RX, TX
TinyGsm modem(SerialAT);

#define ONE_WIRE_BUS_1 4  
#define ONE_WIRE_BUS_2 5  
OneWire oneWire1(ONE_WIRE_BUS_1);
OneWire oneWire2(ONE_WIRE_BUS_2);
DallasTemperature sensors1(&oneWire1);
DallasTemperature sensors2(&oneWire2);

float temperatureThreshold = 15.0;
bool callInitiated = false;
bool callInProgress = false;  
int ledPin = 7;
bool ledState = LOW;
int virtualLedPin = V2;

unsigned long callCooldown = 600000; // Czas oczekiwania między kolejnymi połączeniami (10 minut w milisekundach)
unsigned long lastTempReadTime = 0;
const long tempReadInterval = 5000;  // Odczyt temperatury co 5 sekund
unsigned long lastLedToggleTime = 0;
const long ledToggleInterval = 500;  // Migotanie diody co 0.5 sekundy
unsigned long lastCallTime = 0;  

void setup()
{
  Serial.begin(9600);
  delay(10);

  SerialAT.begin(9600);
  delay(3000);

  Serial.println("Inicjalizacja modemu...");
  modem.restart();

  Blynk.begin(BLYNK_AUTH_TOKEN, modem, apn, user, pass);

  sensors1.begin();
  sensors2.begin();

  pinMode(ledPin, OUTPUT);

  Blynk.virtualWrite(virtualLedPin, LOW);
}

void initiateCall()
{
  Serial.println("Inicjacja połączenia...");
  SerialAT.println("ATDxxxxxxx;");
  delay(10000);
  SerialAT.println("ATH");
  lastCallTime = millis();  
}

void loop()
{
  Blynk.run();

  unsigned long currentMillis = millis();

  sensors1.requestTemperatures();
  float temperature1 = sensors1.getTempCByIndex(0);

  sensors2.requestTemperatures();
  float temperature2 = sensors2.getTempCByIndex(0);

  Serial.print("Temperatura 1: ");
  Serial.println(temperature1);
  Serial.print("Temperatura 2: ");
  Serial.println(temperature2);

  bool temperatureBelowThreshold = (temperature1 < temperatureThreshold || temperature2 < temperatureThreshold);

  if (temperatureBelowThreshold && !callInitiated && !callInProgress)
  {
    Serial.println("Temperatura poniżej progu! Wykonywanie akcji...");
    callInitiated = true;
    callInProgress = true; 
    initiateCall();
  }
  else if (callInProgress && (currentMillis - lastLedToggleTime >= ledToggleInterval))
  {
    Serial.println("Migotanie diody: warunek spełniony");

    lastLedToggleTime = currentMillis;
    ledState = !ledState;
    digitalWrite(ledPin, ledState);

    Blynk.virtualWrite(virtualLedPin, ledState);

    Serial.println("Migotanie diody: wykonano");
  }

  if (currentMillis - lastTempReadTime >= tempReadInterval)
  {
    lastTempReadTime = currentMillis;

    Blynk.virtualWrite(V0, temperature1); 
    Blynk.virtualWrite(V1, temperature2); 
  }

  if (temperatureBelowThreshold && (currentMillis - lastCallTime >= callCooldown) && (currentMillis - lastLedToggleTime >= ledToggleInterval))
  {
    callInitiated = false;
    callInProgress = false;  
    Serial.println("Resetowanie flagi połączenia...");
  }
}

I moved your topic to an appropriate forum category @jayjay_1208.

In the future, please take some time to pick the forum category that best suits the subject of your topic. There is an "About the _____ category" topic at the top of each category that explains its purpose.

This is an important part of responsible forum usage, as explained in the "How to get the best out of this forum" guide. The guide contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

1 Like

If you are intending on using Blynk… you are not doing it correct at this moment. i think you need to maybe start with basics, get your idea and code working and then try to add in the Blynk code.

  • [quote="jayjay_1208, post:1, topic:1224768"]
    Blynk.virtualWrite(virtualLedPin, LOW);
    [/quote]

You cannot use this function like this.

  1. Blynk.run() needs to be called often and very fast. There should be little to nothing in your loop() when using Blynk. Also, if you lose connection to you AP or Blynk servers, Blynk.run(),is blocking and will halt your loop :scream::grimacing:. You should really use connection management logic. Blynk’s documentation explains this pretty well over at their site. Or their forum people.

Im not a wizard coder as some are here and not able to quickly read other peoples code but im pretty sure there are a ton of mistakes and flaws in logic in your code. Im sorry but i really do think you need to get to basics first.

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