Connection problem with blynk

hello, could you tell me why I lose the connection to blynk after 15 seconds,
I use arduino nano 33 IOT

cpp
#include <WiFiNINA.h>
#include <BlynkSimpleWiFiNINA.h>
#define BLYNK_PRINT Serial
#include <OneWire.h>

// Code blynk + WIFI
char auth[]= "XXXXXX-XXXXX";
char ssid[] = "XXXXX";
char pass[]=  "XXXXX";

// Branché sur pin de l4  rduino :
const int relais1 = 3;
const int btnPin1 = 5;
const int relais2 = 4;
const int btnPin2 = 6;
const int ledV = 7;
const int ledR = 8;
const int BROCHE_ONEWIRE =2;

BlynkTimer timer;
void checkPhysicalButton1();
void checkPhysicalButton2();

int relais1State = LOW;
int btnState1 = HIGH;

int relais2State = LOW;
int btnState2 = HIGH;

// blink
BLYNK_CONNECTED() {
  // synchronisation des pin Virtuel
  Blynk.syncVirtual(V2);
  Blynk.syncVirtual(V3);
}

// quand le boutton de application est appuye
BLYNK_WRITE(V2) {
  relais1State = param.asInt();
  digitalWrite(relais1, relais1State);
}
BLYNK_WRITE(V3) {
  relais2State = param.asInt();
  digitalWrite(relais2, relais2State);
}

void checkPhysicalButton1()
{
  if (digitalRead(btnPin1) == LOW) {
   
    if (btnState1 != LOW) {

      // position relais
      relais1State = !relais1State;
      digitalWrite(relais1, relais1State);

      // mise a jour du boutton sur application
      Blynk.virtualWrite(V2, relais1State);
    }
    btnState1 = LOW;
  } else {
    btnState1 = HIGH;
  }
}

void checkPhysicalButton2()
{
  if (digitalRead(btnPin2) == LOW) {
    if (btnState2 != LOW) {

      // position relais
      relais2State = !relais2State;
      digitalWrite(relais2, relais2State);
      digitalWrite(ledV, HIGH);digitalWrite(ledR, LOW);

      // mise a jour du boutton sur application
      Blynk.virtualWrite(V3, relais2State);
    }
    btnState2 = LOW;
    
  } else {
    btnState2 = HIGH;
    digitalWrite(ledR, HIGH);digitalWrite(ledV, LOW);
    
  }
}

// ******************************temperature
unsigned long periode_lecture_temp = 0;
/* Code de retour de la fonction getTemperature() */
enum DS18B20_RCODES
{
  READ_OK,
  NO_SENSOR_FOUND,
  INVALID_ADDRESS,
  INVALID_SENSOR
};
/* Création de l'objet OneWire pour manipuler le bus 1-Wire */
OneWire ds(BROCHE_ONEWIRE);
byte getTemperature(float *temperature, byte reset_search)
{
  byte data[9], addr[8];
  // data[] : Données lues depuis le scratchpad
  // addr[] : Adresse du module 1-Wire détecté
  /* Reset le bus 1-Wire ci nécessaire (requis pour la lecture du premier capteur) */
  if (reset_search) {
    ds.reset_search();
  }
  /* Recherche le prochain capteur 1-Wire disponible */
  if (!ds.search(addr)) {
    // Pas de capteur
    return NO_SENSOR_FOUND;
  }
  /* Vérifie que l'adresse a été correctement reçue */
  if (OneWire::crc8(addr, 7) != addr[7]) {Serial.println("mauvaise adresse ");
    // Adresse invalide
    return INVALID_ADDRESS;
  }
  /* VĂ©rifie qu'il s'agit bien d'un DS18B20 */
  if (addr[0] != 0x28) {
    // Mauvais type de capteur
    return INVALID_SENSOR;
  }
  /* Reset le bus 1-Wire et sélectionne le capteur */
  ds.reset();
  ds.select(addr);
  /* Lance une prise de mesure de température et attend la fin de la mesure */
  ds.write(0x44, 1);
  //delay(800);
  /* Reset le bus 1-Wire, sélectionne le capteur et envoie une demande de lecture du scratchpad */
  ds.reset();
  ds.select(addr);
  ds.write(0xBE);
  /* Lecture du scratchpad */
  for (byte i = 0; i < 9; i++) {
    data[i] = ds.read();
  }
  /* Calcul de la température en degré Celsius */
  *temperature = (int16_t) ((data[1] << 8) | data[0]) * 0.0625;
  
  return READ_OK;
}

void setup()
{
  // Debug console
  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass);

  pinMode(relais1, OUTPUT);
  pinMode(btnPin1, INPUT_PULLUP);
  digitalWrite(relais1, relais1State);

   pinMode(relais2, OUTPUT);
  pinMode(btnPin2, INPUT_PULLUP);
  digitalWrite(relais2, relais2State);

  
  pinMode(ledV, OUTPUT);
  pinMode(ledR, OUTPUT);

  // Setup a function to be called every 100 ms
  timer.setInterval(100L, checkPhysicalButton1);
  timer.setInterval(100L, checkPhysicalButton2);
}

void loop()
{
  Blynk.run();
  timer.run();
  SM_temperature ();
}
void SM_temperature ()
{
  float temperature[3];
  if (getTemperature(&temperature[0], true) != READ_OK)
  {
    Serial.println(F("Erreur de lecture du capteur 1"));
    return;
  }
  if (getTemperature(&temperature[1], false) != READ_OK)
  {
    Serial.println(F("Erreur de lecture du capteur 2"));
    return;
  }
  if (getTemperature(&temperature[2], false) != READ_OK)
  {
    Serial.println(F("Erreur de lecture du capteur 3"));
    return;
  }

  /* Affiche les températures */
  Serial.print(F("Temperatures : "));
  Serial.print(temperature[0], 2);
  Blynk.virtualWrite(V4, temperature[0], 2);

  Serial.print(temperature[1], 2);
  Blynk.virtualWrite(V5, temperature[1], 2);
  
  Serial.print(temperature[2], 2);
  Blynk.virtualWrite(V6, temperature[2], 2);
 
  
}
``

Blockquote

`

Because you’re calling SM_temperature from your void loop rather than via a timer. Read this…

Pete.

Thank you very much, it works perfectly.
For my three temperature sensors, on Bkynk application I would like to put SuperChart, the values are not displayed. But it appears in the labeled Value. Do you have any idea why this doesn’t work with SurperChart?

cpp
#include <WiFiNINA.h>
#include <BlynkSimpleWiFiNINA.h>
#define BLYNK_PRINT Serial
#include <OneWire.h>

// Code blynk + WIFI
char auth[]= "XXXXXXX-XXXXXX";
char ssid[] = "XXXXXXX";
char pass[]=  "XXXXX";

// Branché sur pin de l4  rduino :
const int relais1 = 3;
const int btnPin1 = 5;
const int relais2 = 4;
const int btnPin2 = 6;
const int ledV = 7;
const int ledR = 8;
const int BROCHE_ONEWIRE =2;

BlynkTimer timer;
void checkPhysicalButton1();
void checkPhysicalButton2();

int relais1State = LOW;
int btnState1 = HIGH;

int relais2State = LOW;
int btnState2 = HIGH;

// blink
BLYNK_CONNECTED() {
  // synchronisation des pin Virtuel
  Blynk.syncVirtual(V2);
  Blynk.syncVirtual(V3);
}

// quand le boutton de application est appuye
BLYNK_WRITE(V2) {
  relais1State = param.asInt();
  digitalWrite(relais1, relais1State);
}
BLYNK_WRITE(V3) {
  relais2State = param.asInt();
  digitalWrite(relais2, relais2State);
}

void checkPhysicalButton1()
{
  if (digitalRead(btnPin1) == LOW) {
   
    if (btnState1 != LOW) {

      // position relais
      relais1State = !relais1State;
      digitalWrite(relais1, relais1State);

      // mise a jour du boutton sur application
      Blynk.virtualWrite(V2, relais1State);
    }
    btnState1 = LOW;
  } else {
    btnState1 = HIGH;
  }
}

void checkPhysicalButton2()
{
  if (digitalRead(btnPin2) == LOW) {
    if (btnState2 != LOW) {

      // position relais
      relais2State = !relais2State;
      digitalWrite(relais2, relais2State);
      digitalWrite(ledV, HIGH);digitalWrite(ledR, LOW);

      // mise a jour du boutton sur application
      Blynk.virtualWrite(V3, relais2State);
    }
    btnState2 = LOW;
    
  } else {
    btnState2 = HIGH;
    digitalWrite(ledR, HIGH);digitalWrite(ledV, LOW);
    
  }
}

// ******************************temperature
unsigned long periode_lecture_temp = 0;
/* Code de retour de la fonction getTemperature() */
enum DS18B20_RCODES
{
  READ_OK,
  NO_SENSOR_FOUND,
  INVALID_ADDRESS,
  INVALID_SENSOR
};
/* Création de l'objet OneWire pour manipuler le bus 1-Wire */
OneWire ds(BROCHE_ONEWIRE);
byte getTemperature(float *temperature, byte reset_search)
{
  byte data[9], addr[8];
  // data[] : Données lues depuis le scratchpad
  // addr[] : Adresse du module 1-Wire détecté
  /* Reset le bus 1-Wire ci nécessaire (requis pour la lecture du premier capteur) */
  if (reset_search) {
    ds.reset_search();
  }
  /* Recherche le prochain capteur 1-Wire disponible */
  if (!ds.search(addr)) {
    // Pas de capteur
    return NO_SENSOR_FOUND;
  }
  /* Vérifie que l'adresse a été correctement reçue */
  if (OneWire::crc8(addr, 7) != addr[7]) {Serial.println("mauvaise adresse ");
    // Adresse invalide
    return INVALID_ADDRESS;
  }
  /* VĂ©rifie qu'il s'agit bien d'un DS18B20 */
  if (addr[0] != 0x28) {
    // Mauvais type de capteur
    return INVALID_SENSOR;
  }
  /* Reset le bus 1-Wire et sélectionne le capteur */
  ds.reset();
  ds.select(addr);
  /* Lance une prise de mesure de température et attend la fin de la mesure */
  ds.write(0x44, 1);
  //delay(800);
  /* Reset le bus 1-Wire, sélectionne le capteur et envoie une demande de lecture du scratchpad */
  ds.reset();
  ds.select(addr);
  ds.write(0xBE);
  /* Lecture du scratchpad */
  for (byte i = 0; i < 9; i++) {
    data[i] = ds.read();
  }
  /* Calcul de la température en degré Celsius */
  *temperature = (int16_t) ((data[1] << 8) | data[0]) * 0.0625;
  
  return READ_OK;
}

void setup()
{
  // Debug console
  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass);

  pinMode(relais1, OUTPUT);
  pinMode(btnPin1, INPUT_PULLUP);
  digitalWrite(relais1, relais1State);

   pinMode(relais2, OUTPUT);
  pinMode(btnPin2, INPUT_PULLUP);
  digitalWrite(relais2, relais2State);

  
  pinMode(ledV, OUTPUT);
  pinMode(ledR, OUTPUT);

  // Setup a function to be called every 100 ms
  timer.setInterval(100L, checkPhysicalButton1);
  timer.setInterval(100L, checkPhysicalButton2);
  timer.setInterval(1000L, sensorDataSend); //timer will run every sec 
}

void loop()
{
  Blynk.run();
  timer.run();
  
}
void sensorDataSend() 
{
  float temperature[3];
  if (getTemperature(&temperature[0], true) != READ_OK)
  {
    Serial.println(F("Erreur de lecture du capteur 1"));
    return;
  }
  if (getTemperature(&temperature[1], false) != READ_OK)
  {
    Serial.println(F("Erreur de lecture du capteur 2"));
    return;
  }
  if (getTemperature(&temperature[2], false) != READ_OK)
  {
    Serial.println(F("Erreur de lecture du capteur 3"));
    return;
  }

  /* Affiche les températures */
  Serial.print(F("Temperatures : "));
  Serial.print(temperature[0], 2);
  Blynk.virtualWrite(V4, temperature[0], 2);

  Serial.print(temperature[1], 2);
  Blynk.virtualWrite(V5, temperature[1], 2);
  
  Serial.print(temperature[2], 2);
  Blynk.virtualWrite(V6, temperature[2], 2);
 
  
}

The correct syntax for Blynk.virtualWrite is Blynk.virtualWrite(vPin, value)

It will also depend on how you’ve configured your data streams in the SuperChart.

Pete.

my connection is really not stable, do you have any idea why? bkynk disconnect every 5 mins or so

So why has it gone from…

to…

What has changed?

Pete.

I don’t know, I spoke too quickly.

what do you think of my code?

cpp
#include <WiFiNINA.h>
#include <BlynkSimpleWiFiNINA.h>
#define BLYNK_PRINT Serial
#include <OneWire.h>

// Code blynk + WIFI
char auth[]= "XXXXXX-XXXXXXX";
char ssid[] = "XXXXXXX";
char pass[]=  "XXXXXXXX";

// Branché sur pin de l4  rduino :
const int relais1 = 3;
const int btnPin1 = 5;
const int relais2 = 4;
const int btnPin2 = 6;
const int ledV = 7;
const int ledR = 8;
const int BROCHE_ONEWIRE =2;

BlynkTimer timer;
void checkPhysicalButton1();
void checkPhysicalButton2();

int relais1State = LOW;
int btnState1 = HIGH;

int relais2State = LOW;
int btnState2 = HIGH;

// blink
BLYNK_CONNECTED() {
  // synchronisation des pin Virtuel
  Blynk.syncVirtual(V2);
  Blynk.syncVirtual(V3);
  Blynk.syncVirtual(V4);
}

// quand le boutton de application est appuye
BLYNK_WRITE(V2) {
  relais1State = param.asInt();
  digitalWrite(relais1, relais1State);
}
BLYNK_WRITE(V3) {
  relais2State = param.asInt();
  digitalWrite(relais2, relais2State);
}

void checkPhysicalButton1()
{
  if (digitalRead(btnPin1) == LOW) {
   
    if (btnState1 != LOW) {

      // position relais
      relais1State = !relais1State;
      digitalWrite(relais1, relais1State);

      // mise a jour du boutton sur application
      Blynk.virtualWrite(V2, relais1State);
    }
    btnState1 = LOW;
  } else {
    btnState1 = HIGH;
  }
}

void checkPhysicalButton2()
{
  if (digitalRead(btnPin2) == LOW) {
    if (btnState2 != LOW) {

      // position relais
      relais2State = !relais2State;
      digitalWrite(relais2, relais2State);
      digitalWrite(ledV, HIGH);digitalWrite(ledR, LOW);

      // mise a jour du boutton sur application
      Blynk.virtualWrite(V3, relais2State);
    }
    btnState2 = LOW;
    
  } else {
    btnState2 = HIGH;
    digitalWrite(ledR, HIGH);digitalWrite(ledV, LOW);
    
  }
}

// ******************************temperature
unsigned long periode_lecture_temp = 0;
/* Code de retour de la fonction getTemperature() */
enum DS18B20_RCODES
{
  READ_OK,
  NO_SENSOR_FOUND,
  INVALID_ADDRESS,
  INVALID_SENSOR
};
/* Création de l'objet OneWire pour manipuler le bus 1-Wire */
OneWire ds(BROCHE_ONEWIRE);
byte getTemperature(float *temperature, byte reset_search)
{
  byte data[9], addr[8];
  // data[] : Données lues depuis le scratchpad
  // addr[] : Adresse du module 1-Wire détecté
  /* Reset le bus 1-Wire ci nécessaire (requis pour la lecture du premier capteur) */
  if (reset_search) {
    ds.reset_search();
  }
  /* Recherche le prochain capteur 1-Wire disponible */
  if (!ds.search(addr)) {
    // Pas de capteur
    return NO_SENSOR_FOUND;
  }
  /* Vérifie que l'adresse a été correctement reçue */
  if (OneWire::crc8(addr, 7) != addr[7]) {Serial.println("mauvaise adresse ");
    // Adresse invalide
    return INVALID_ADDRESS;
  }
  /* VĂ©rifie qu'il s'agit bien d'un DS18B20 */
  if (addr[0] != 0x28) {
    // Mauvais type de capteur
    return INVALID_SENSOR;
  }
  /* Reset le bus 1-Wire et sélectionne le capteur */
  ds.reset();
  ds.select(addr);
  /* Lance une prise de mesure de température et attend la fin de la mesure */
  ds.write(0x44, 1);
  //delay(800);
  /* Reset le bus 1-Wire, sélectionne le capteur et envoie une demande de lecture du scratchpad */
  ds.reset();
  ds.select(addr);
  ds.write(0xBE);
  /* Lecture du scratchpad */
  for (byte i = 0; i < 9; i++) {
    data[i] = ds.read();
  }
  /* Calcul de la température en degré Celsius */
  *temperature = (int16_t) ((data[1] << 8) | data[0]) * 0.0625;
  
  return READ_OK;
}

void setup()
{
  // Debug console
  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass);

  pinMode(relais1, OUTPUT);
  pinMode(btnPin1, INPUT_PULLUP);
  digitalWrite(relais1, relais1State);

   pinMode(relais2, OUTPUT);
  pinMode(btnPin2, INPUT_PULLUP);
  digitalWrite(relais2, relais2State);

  
  pinMode(ledV, OUTPUT);
  pinMode(ledR, OUTPUT);

  // Setup a function to be called every 100 ms
  timer.setInterval(100L, checkPhysicalButton1);
  timer.setInterval(100L, checkPhysicalButton2);
  timer.setInterval(5000L, sensorDataSend); //timer will run every sec 
}
void sensorDataSend() 
{
  float temperature[3];
  if (getTemperature(&temperature[0], true) != READ_OK)
  {
    Serial.println(F("Erreur de lecture du capteur 1"));
    return;
  }
  if (getTemperature(&temperature[1], false) != READ_OK)
  {
    Serial.println(F("Erreur de lecture du capteur 2"));
    return;
  }
  if (getTemperature(&temperature[2], false) != READ_OK)
  {
    Serial.println(F("Erreur de lecture du capteur 3"));
    return;
  }

  /* Affiche les températures */
  Serial.print(F("Temperatures : "));
  Serial.print(temperature[0], 2);
  Blynk.virtualWrite(V4, temperature[0],2);

  Serial.print(temperature[1], 2);
  Blynk.virtualWrite(V5, temperature[1], 2);
  
  Serial.print(temperature[2], 2);
  Blynk.virtualWrite(V6, temperature[2], 2);

 
  
}
void loop()
{
  Blynk.run();
  timer.run();
  
}

You’re still using the wrong syntax for your virtualWrites.

How are you powering your relays? Do they have a separate power supply, or are you drawing the 5v that actuated the coils from the MCU?

Pete.

100x50=5000
every 5 sec, you call checkPhysicalButton1 checkPhysicalButton2 and call sensorDataSend together :stuck_out_tongue_winking_eye:

You have to stage as this

  timer.setInterval(100L, checkPhysicalButton1);
  timer.setInterval(120L, checkPhysicalButton2);
  timer.setInterval(5050L, sensorDataSend); //timer will run every sec 

could you show me an example with the correct syntax

I use an external power supply

I already did, in post #4…

It’s also in the documentation…

If you want to display the data to two decimal places in the app, which I think is what you’re trying to do with the ,2 in the virtualWrite command, then this is done in the widget using /pin.##/

Pete.

written like that, is it better ?

cpp
/* Affiche les températures */
  Serial.print(F("Temperatures : "));
  Serial.print(temperature[0]);
  Blynk.virtualWrite(V4, temperature[0]);

  Serial.print(temperature[1]);
  Blynk.virtualWrite(V5, temperature[1]);
  
  Serial.print(temperature[2]);
  Blynk.virtualWrite(V6, temperature[2]);

Yes, that’s the correct syntax for the Blynk.virtualWrite commands.

There is nothing wrong with including the ,2 in your Serial.print statements if you want to limit the serial output to two decimal places, but that syntax can’t be used for the Blynk.virtualWrite commands.

Pete.