Arduino goes offline.Server is ok

I create local Blynk server and he work perfect,but i have problem with arduino…He goes offline randomly.I search forum for similar topics but i can’t find solution.When i reset arduino he work randomly time.

I use iteaduino mega+enc28j60 Ethernet module+five DS18B20+k type max6675 thermocouple

• Blynk Library version 0.6.0
•CODE

#include <UIPEthernet.h>
#include <BlynkSimpleUIPEthernet.h> // This part is for Ethernet stuff
#include <OneWire.h>
#include <max6675.h>

// OneWire DS18S20, DS18B20, DS1822 Temperature Example
//
// http://www.pjrc.com/teensy/td_libs_OneWire.html
//
// The DallasTemperature library can do all this work for you!
// http://milesburton.com/Dallas_Temperature_Control_Library
int thermoDO = 4; // SO of  MAX6675 module to D4
int thermoCS = 5; // CS of MAX6675 module to D5
int thermoCLK = 6; // SCK of MAX6675 module to D6

MAX6675 thermocouple(thermoCLK, thermoCS, thermoDO);

OneWire  ds(7);  // on pin 7 (a 4.7K resistor is necessary) make sure you change this from the original pin 10 to an unused pin.
int adr;
float s1;
float s2;
float s3;
float s4;
float s5;
float s6;
float s7;

char auth[] = "7-y6TDfWCh0ZK7uGnAVORCvLkgOTTRGa"; // Put your Auth Token here. (see Step 3 above)

void setup()
{
  Serial.begin(9600); // See the connection status in Serial Monitor
    Blynk.begin(auth, IPAddress(192,168,1,7), 8080);  // Here your Arduino connects to the Blynk Cloud.
    

}
BLYNK_READ(V12){

  Blynk.virtualWrite(V12, thermocouple.readCelsius());
  }

void loop()
{
  Blynk.run(); // All the Blynk Magic happens here...

  // You can inject your own code or combine it with other sketches.
  // Check other examples on how to communicate with Blynk. Remember
  // to avoid delay() function!
    byte i;
  byte present = 0;
  byte type_s;
  byte data[12];
  byte addr[8];
  float celsius, fahrenheit;
  
  if ( !ds.search(addr)) {
    Serial.println("No more addresses.");
    Serial.println();
    ds.reset_search();
    delay(250);
    return;
  }
  
//  Serial.print("ROM =");
  for( i = 0; i < 8; i++) {           //we need to drop 8 bytes of data
  }
  adr = (addr[7]);

  if (OneWire::crc8(addr, 7) != addr[7]) {
      Serial.println("CRC is not valid!");
      return;
  }
  Serial.println();
 
  ds.reset();
  ds.select(addr);
  ds.write(0x44, 1);        // start conversion, with parasite power on at the end
  
  delay(1000);     // maybe 750ms is enough, maybe not
  // we might do a ds.depower() here, but the reset will take care of it.
  
  present = ds.reset();
  ds.select(addr);    
  ds.write(0xBE);         // Read Scratchpad


  for ( i = 0; i < 9; i++) {           // we need 9 bytes to drop off
    data[i] = ds.read();
  }
  
  Serial.println();

  // Convert the data to actual temperature
  // because the result is a 16 bit signed integer, it should
  // be stored to an "int16_t" type, which is always 16 bits
  // even when compiled on a 32 bit processor.
  int16_t raw = (data[1] << 8) | data[0];
  if (type_s) {
    raw = raw << 3; // 9 bit resolution default
    if (data[7] == 0x10) {
      // "count remain" gives full 12 bit resolution
      raw = (raw & 0xFFF0) + 12 - data[6];
    }
  } else {
    byte cfg = (data[4] & 0x60);
    // at lower res, the low bits are undefined, so let's zero them
    if (cfg == 0x00) raw = raw & ~7;  // 9 bit resolution, 93.75 ms
    else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
    else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
    //// default is 12 bit resolution, 750 ms conversion time
  }
  celsius = (float)raw / 16.0;
  fahrenheit = celsius * 1.8 + 32.0;
  if(adr == 242)  {         //replace ??? with value of sensor number 1    
    s1 = celsius;           //change celsius to fahrenheit if you prefer output in Fahrenheit
  }

  if(adr == 58)  {         //replace ??? with value of sensor number 2
    s2 = celsius;           //change celsius to fahrenheit if you prefer output in Fahrenheit
  }

  if(adr == 144)  {         //replace ??? with value of sensor number 3
    s3 = celsius;           //change celsius to fahrenheit if you prefer output in Fahrenheit
  }

  if(adr == 37)  {         //replace ??? with value of sensor number 4
    s4 = celsius;           //change celsius to fahrenheit if you prefer output in Fahrenheit
  }

  if(adr == 21)  {         //replace ??? with value of sensor number 5
    s5 = celsius;           //change celsius to fahrenheit if you prefer output in Fahrenheit
  }

  //if(adr == ???)  {         //replace ??? with value of sensor number 6
  //  s6 = celsius;           //change celsius to fahrenheit if you prefer output in Fahrenheit
 // }

 // if(adr == ???)  {         //replace ??? with value of sensor number 7. If you use more sensors just add them below this section. Delete unused sections, or you will get "expected primary-expression before '?' token"
 //  s7 = celsius;           //change celsius to fahrenheit if you prefer output in Fahrenheit
 // }

Serial.print(" Sensor 1 = ");
Serial.print(s1);
Serial.print(" Sensor 2 = ");
Serial.print(s2);
Serial.print(" Sensor 3 = ");
Serial.print(s3);
Serial.print(" Sensor 4 = ");
Serial.print(s4);
Serial.print(" Sensor 5 = ");
Serial.print(s5);
Serial.print(" Sensor 6 = ");
Serial.print(s6);
Serial.print(" Sensor 7 = ");   //add or delete if you need more or less
Serial.print(s7);
Serial.print("Deg C = ");
   Serial.println(thermocouple.readCelsius());
  
  Blynk.virtualWrite(V5, s1);
  Blynk.virtualWrite(V6, s2);
  Blynk.virtualWrite(V7, s3);
  Blynk.virtualWrite(V8, s4);
  Blynk.virtualWrite(V9, s5);
  Blynk.virtualWrite(V10, s6);
  Blynk.virtualWrite(V11, s7);//add or delete if you need more or less
  
 
}.```

The problem is your void loop. You should read this:

In addition, the largest Blynk library version is 0.6.1 and it’s important to have the latest versions of the library, the server and the app installed, as they need to be synchronised to work correctly together.

Pete.

I must delete all code in loop?Will it work without it?

I’d suggest you re-read the article until you understand the bit about calling functions with a timer and removing delays.

Pete.

1 Like

Ok i know how to do it with one sensor how to do it with multiple ds18b20 on one wire?

I’d suggest using multiple staggered timers.

Pete.

Now work with this code:

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <UIPEthernet.h>
#include <BlynkSimpleUIPEthernet.h> // This part is for Ethernet stuff
#include <OneWire.h>
#include <max6675.h>
#include <DallasTemperature.h> 
//MAX6675 module
int thermoDO = 4; // SO of  MAX6675 module to D4
int thermoCS = 5; // CS of MAX6675 module to D5
int thermoCLK = 6; // SCK of MAX6675 module to D6

MAX6675 thermocouple(thermoCLK, thermoCS, thermoDO);


#define ONE_WIRE_BUS 7        // This is the ESP8266 pin
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

DeviceAddress tempSensor1 = { 0x28, 0x20, 0x9C, 0x03, 0x00, 0x00, 0x80, 0x3A }; // Temperature probe #1
DeviceAddress tempSensor2 = { 0x28, 0xFC, 0x6C, 0x03, 0x00, 0x00, 0x80, 0x25 }; // Temperature probe #2
DeviceAddress tempSensor3 = { 0x28, 0xA0, 0x6E, 0x01, 0x00, 0x00, 0x80, 0xF2 }; // Temperature probe #3
DeviceAddress tempSensor4 = { 0x28, 0x08, 0x96, 0x03, 0x00, 0x00, 0x80, 0x90 }; // Temperature probe #4
DeviceAddress tempSensor5 = { 0x28, 0x09, 0x79, 0x01, 0x00, 0x00, 0x80, 0x15 }; // Temperature probe #5

char auth[] = "7-y6TDfWCh0ZK7uGnAVORCvLkgOTTRGa"; // Put your Auth Token here. (see Step 3 above)


SimpleTimer timer;

int temperature1, temperature2, temperature3, temperature4, temperature5;         // Variables for storing temperatures

void setup()
{
  Serial.begin(9600); // See the connection status in Serial Monitor
    Blynk.begin(auth, IPAddress(192,168,1,6), 8080);  // Here your Arduino connects to the Blynk Cloud.

  while (Blynk.connect() == false) {
    // Wait until connected
  }

  sensors.begin();
  sensors.setResolution(tempSensor1, 12);   // More on resolutions: http://www.homautomation.org/2015/11/17/ds18b20-how-to-change-resolution-9101112-bits/
  sensors.setResolution(tempSensor2, 12);
  sensors.setResolution(tempSensor3, 12);
  sensors.setResolution(tempSensor4, 12);
  sensors.setResolution(tempSensor5, 12);
  // These timers are used to keep the loop() nice and leak... keeps Blynk from getting flooded.
  timer.setInterval(5000L, sendSensor1);
  timer.setInterval(5000L, sendSensor2);
  timer.setInterval(5000L, sendSensor3);
  timer.setInterval(5000L, sendSensor4);
  timer.setInterval(5000L, sendSensor5);
}

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

void sendSensor1() {
  sensors.requestTemperatures();                  // Polls the sensors
  temperature1 = sensors.getTempC(tempSensor1);   // Stores temp in F. Change getTempF to getTempC for celcius.
  Blynk.virtualWrite(1, temperature1);            // Send temp to Blynk virtual pin 1
}

void sendSensor2() {
  sensors.requestTemperatures();
  temperature2 = sensors.getTempC(tempSensor2);
  Blynk.virtualWrite(2, temperature2);
}
void sendSensor3() {
  sensors.requestTemperatures();
  temperature3 = sensors.getTempC(tempSensor3);
  Blynk.virtualWrite(3, temperature3);
}
void sendSensor4() {
  sensors.requestTemperatures();
  temperature4 = sensors.getTempC(tempSensor4);
  Blynk.virtualWrite(4, temperature4);
}
void sendSensor5() {
  sensors.requestTemperatures();
  temperature5 = sensors.getTempC(tempSensor5);
  Blynk.virtualWrite(5, temperature5);
}
BLYNK_READ(V12){

  Blynk.virtualWrite(V12, thermocouple.readCelsius());
  }```

@gogo2409 please edit your post, using the pencil icon at the bottom, and add triple backticks at the beginning and end of your code so that it displays correctly.
Triple backticks look like this:
```

Pete.

Arduino goes offline after 5h why?Now code is ok i mean.Afrer i reset arduino he work ok…

So, instead of staggering the timers you’ve set them up so that they all get called at exactly the same time.

Also, what is the function of this bit of code?

Pete.

How change value etc 5000,6000,7000,8000,9000? or

BLYNK_READ(V12)
{
Blynk.virtualWrite(V12, thermocouple.readCelsius());
}

This is code for max6675.

No, if you search the forum for topics about staggered timers then you’ll see that delays between each one in void setup is the best way.

So why are you relying on a widget in push mode to take these reading, rather than using an BlynkTimer to take these readings? This way you can fully control the timing of when these readings and ensure that it doesn’t clash with any of your other timed functions.

Pete.

Ouch… First mistake! :stuck_out_tongue_winking_eye:

Nobody needs 0,0625 degree increments on their temperature readings! The Temperature Conversion Time (the time it takes to get a reading) when using 12 bit resolution is 750 ms per sensor. During this period everything else in your program is pretty much halted (including network). With some creative coding you could circumvent most of the delays, check the code from my “433” project:

https://community.blynk.cc/t/cheap-est-rf-433-mhz-bridge-for-home-automation-with-both-tx-and-rx/

enc28j60 Ethernet module

I would suggest a different Ethernet shield. ENC28J60 is basically crap! :neutral_face:

Some links:

https://community.blynk.cc/search