Arduino MKR 1010 & DS12B20

Hi

I have tried different codes and libraries, but somehow I don’t find one that fits

exists a Blynk code that can read the sensor DS18B20 using Arduino MKR 1010 ?

At the moment I’m trying with this code, but I can’t read it on Blynk

 #include <OneWire.h>
#include <DallasTemperature.h>
/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial

/* Fill-in your Template ID (only if using Blynk.Cloud) */
#define BLYNK_TEMPLATE_ID ""
#define BLYNK_DEVICE_NAME ""
#define RELAY1 = (V20)
#define ONE_WIRE_BUS 2 


#include <SPI.h>
#include <Arduino_MKRENV.h>
#include <WiFiNINA.h>
#include <BlynkSimpleWiFiNINA.h>
#include <math.h>
#include "RTClib.h"

RTC_Millis rtc;

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "";

OneWire oneWire(ONE_WIRE_BUS);  

DallasTemperature sensors(&oneWire);

char ssid[] = "";
char pass[] = "";


void setup()
  
  Blynk.begin(auth, ssid, pass);

//  timer.setInterval(1000L, sendSensor);
  
 Serial.begin(9600); 
 Serial.println("Dallas Temperature IC Control Library Demo");       

 // Start up the library 
 sensors.begin(); 
} 


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


 // call sensors.requestTemperatures() to issue a global temperature 
 // request to all devices on the bus 
/********************************************************************/
 Serial.print(" Requesting temperatures..."); 
 sensors.requestTemperatures(); // Send the command to get temperature readings 
 Serial.println("DONE"); 
/********************************************************************/
 Serial.print("Temperature is: "); 
 Serial.print(sensors.getTempCByIndex(0)); // Why "byIndex"?  
   // You can have more than one DS18B20 on the same bus.  
   // 0 refers to the first IC on the wire 
   delay(1000); 
   
}

void requestTemperature(){
  sensors.requestTemperatures();
  Serial.print(sensors.getTempCByIndex(0));
  Serial.print("°");
  Serial.println("C");
  Blynk.virtualWrite(V6, sensors.getTempCByIndex(0));
}

Hey there,
You must read this first
https://docs.blynk.io/en/legacy-platform/legacy-articles/keep-your-void-loop-clean

Needs to be at the top of your sketch

1 Like

OK, I managed to get a signal, but the values are not quite right

@Blynk_Coeur, i have removed them

 *************************************************************/
#include <OneWire.h>
#include <DallasTemperature.h>
#include <SimpleTimer.h> 
/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial

/* Fill-in your Template ID (only if using Blynk.Cloud) */
#define BLYNK_TEMPLATE_ID "TMPLrNGyjAuU"
#define BLYNK_DEVICE_NAME "ITSE21a"
#define RELAY1 = (V20)
#define ONE_WIRE_BUS 2 


#include <SPI.h>
#include <Arduino_MKRENV.h>
#include <WiFiNINA.h>
#include <BlynkSimpleWiFiNINA.h>
#include <math.h>
#include "RTClib.h"

RTC_Millis rtc;

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "";

OneWire oneWire(ONE_WIRE_BUS);  

DallasTemperature sensors(&oneWire);

BlynkTimer timer;// this timer is required to send data from shield to blynk app in intervals 


// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "";
char pass[] = "";
int relay_1 = 1;
int val = 1; 


int roomTemperature; 
// This function will be called every time Slider Widget
// in Blynk app writes values to the Virtual Pin 1



void setup()
{  
  Blynk.begin(auth, ssid, pass);


 // start serial port 
 Serial.begin(9600); 

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

  sensors.begin();                        // Starts the DS18B20 sensor(s).
  sensors.setResolution(10);              // More on resolution: http://www.homautomation.org/2015/11/17/ds18b20-how-to-change-resolution-9101112-bits/

  timer.setInterval(2000L, sendTemps);  
} 


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

float tempC = sensors.getTempCByIndex(0);


 // call sensors.requestTemperatures() to issue a global temperature 
 // request to all devices on the bus 
/********************************************************************/
 Serial.print(" Requesting temperatures..."); 
 sensors.requestTemperatures(); // Send the command to get temperature readings 
 Serial.println("DONE"); 
/********************************************************************/
 Serial.print("Temperature is: "); 
 Serial.print(sensors.getTempCByIndex(0)); // Why "byIndex"?  
   // You can have more than one DS18B20 on the same bus.  
   // 0 refers to the first IC on the wire 
   delay(1000); 
   
}

void sendTemps()
{
  sensors.requestTemperatures();                  // Polls the sensors.
  roomTemperature = sensors.getTempFByIndex(0);   // Stores temperature. Change to getTempCByIndex(0) for celcius.
  Blynk.virtualWrite(6, roomTemperature);         // Send temperature to Blynk app virtual pin 1.
}

Please edit your code put the 3 backticks ‘’’’

1 Like

You can’t call sensor 1000 times per second
And your void loop must contain only
Blynk.run();
timer.run();

sorry, where do you see the 1000 times per second query?

thank you

This might help you

1 Like

cool, thanks you bothe, now it works !

 #include <OneWire.h>
#include <DallasTemperature.h>
#include <SimpleTimer.h> 
/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial

/* Fill-in your Template ID (only if using Blynk.Cloud) */
#define BLYNK_TEMPLATE_ID ""
#define BLYNK_DEVICE_NAME ""
#define RELAY1 = (V20)
#define ONE_WIRE_BUS 2 


#include <SPI.h>
#include <Arduino_MKRENV.h>
#include <WiFiNINA.h>
#include <BlynkSimpleWiFiNINA.h>
#include <math.h>
#include "RTClib.h"

RTC_Millis rtc;

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "";

OneWire oneWire(ONE_WIRE_BUS);  

DallasTemperature sensors(&oneWire);

BlynkTimer timer;// this timer is required to send data from shield to blynk app in intervals 


// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "";
char pass[] = "";
int relay_1 = 1;
int val = 1; 

float temp1;

void setup()

  {
     Serial.begin(115200);
     sensors.begin();
     Blynk.begin(auth, ssid, pass);
     timer.setInterval(30000L, sendTemps);
         
    }
 
void sendTemps()
  {
  sensors.requestTemperatures(); // Polls the sensors
  temp1  = sensors.getTempCByIndex(0); // Gets first pr
  Serial.println(temp1);
  Blynk.virtualWrite(V6, temp1); //вывод значений влажности в БЛИНК
 
  }

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

I have now tried to insert this section into my code, but it does not work on it

maybe someone sees the problem

/*************************************************************
  Download latest Blynk library:
https://github.com/blynkkk/blynk-library/releases/latest
 *************************************************************





/* Fill-in your Template ID (only if using Blynk.Cloud) */

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


#include <SPI.h>                       // Library: Serial-Peripheral-Interface - protocal & bus for the serial communication                     
#include <Arduino_MKRENV.h>            // Library: Allows you to read the temperature, humidity, pressure, light and UV sensors of your MKR ENV shield
#include <WiFiNINA.h>                  // Library: 
#include <BlynkSimpleWiFiNINA.h>       // Library:  Enables network connection (local and Internet) with the Arduino MKR Wifi 1010
#include <math.h>                      // LIbrary:  Defines various mathematical functions
#include "RTClib.h"                    // LIbrary:  Allows and enables SAMD board/architectures to control and use internal RTC (Real Time Clock) 


#define BLYNK_PRINT Serial

#define BLYNK_TEMPLATE_ID "TMPLrNGyjAuU"   // ID of the created TEmplate on BLYNK, whic is generated by BLYNK     
#define BLYNK_DEVICE_NAME "ITSE21a"        // Define BL

#define ONE_WIRE_BUS 2 
#define RELAY1 = (V20)                 // assign virtual pin 20 to Relay1 


RTC_Millis rtc;
OneWire oneWire(ONE_WIRE_BUS);  
DallasTemperature sensors(&oneWire);
BlynkTimer timer;// this timer is required to send data from shield to blynk app in intervals 


char auth[] = "XPCSpLcWsELvNRCeJL-dvt43-r4deHS8";      // put here the  Auth Token which you find in the Blynk App.
char ssid[] = "Fitim";                  // Your WiFi credentials.
char pass[] = "123456789";              // Set password to "" for open networks.

int relay_1 = 1;                        // Assigns relay_1 to pin 1. It is important that we assign it to pin 1, as the relay is internally wired to this pin.    

int minRain=0;                          //E ruajm sa minuta ka rene shi gjate nje dite, ne fillim i japim vleren 0 
int minDelay = 60000;                   //me kete variabel caktojme se sa shpesh marrim informata prej senzorit
int minOpen = 0;                        //Ketu ruhet informata seqe sa kohe u leshu relay (vintili)
int isOpen = 0;                          //tregon se a eshte i hapur apo i mbyllur vintili 1=i hapur, 0=i mbyllur

float temp1;








                                      // This function will be called every time Slider Widget
                                      // in Blynk app writes values to the Virtual Pin 1

BLYNK_WRITE(V1)                          // Switch between automatic and manual mode by changing the value of virtual pin  
{
  if(param.asInt() == 1)                 // execute this code if the switch widget is now ON
  {
                                    
digitalWrite(relay_1,HIGH);          // Set relay_1 HIGH
  }
  else                                   // execute this code if the switch widget is now OFF
  {
   
digitalWrite(relay_1,LOW);           // Set relay_1 LOW
  }

}



BLYNK_WRITE(V20)                           // Switch for for manual irrigation
{
  if(param.asInt() == 1)                   // execute this code if the switch widget is now ON
  {
                                    
digitalWrite(relay_1,HIGH);             // Set relay_1 HIGH
Blynk.virtualWrite(V20,1);              // sending value to Blank app to set Virtual Switch ON  
delay (9000);                           // Wait 9 sekond (to fall back )
                                    
digitalWrite(relay_1,LOW);              // Set relay_ LOW
Blynk.virtualWrite(V20,0);              // sending value to Blynk app to set Blynk Switch OFF
}

}


void setup(){   
  
  pinMode(1, OUTPUT);                       // Initialise digital pin 1 as an output pin
digitalWrite(relay_1, LOW);             // Set relay:1 on LOW
  pinMode (3, INPUT_PULLUP);                // Initialise digital pin 2 as an input pin

   
 rtc.begin(DateTime(F(__DATE__), F(__TIME__)));             // Following line sets the RTC to the date & time this sketch was compiled
 //rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));                // This line sets the RTC with an explicit date & time, for example: / January 21, 2014 at 3am you would call:
 
// to initiale variables, pinmodes asf. for once


 Serial.begin(115200);
 sensors.begin();
 Blynk.begin(auth, ssid, pass);
 timer.setInterval(10000L, sendTemps);
     
}


void sendTemps()
  {
  sensors.requestTemperatures(); // Polls the sensors
  temp1  = sensors.getTempCByIndex(0); // Gets first pr
  Serial.println(temp1);
  Blynk.virtualWrite(V6, temp1); //вывод значений влажности в БЛИНК

 }
   



   


void openRelay()                              //Ky eeshte funksioni qe hap relayn dhe pergatit programin per llogaritje per diten e ardhshme
{ 
minRain = 0;                              //ketu kthehen minutat qe kan rene shi ne 0 per arsye qe duhet filloj numerimi i minutave per diten tjeter
isOpen = 1;                                // ketu ruajm informaten qe eshte hapur relay
minOpen = 0;                                //fillojme llogaritjen se sa kohe eshte hapur relay
digitalWrite(relay_1, HIGH);                      // hapim relayn

}
void closeRelay()
{
  digitalWrite(relay_1, LOW);  // Ketu duhet me e mbyll relay
}

void skipOpening(){  //nese ka rene shi se paku 30 minuta, ne nuk e hapim relayn por vetem minutat i kthejm ne zero qe me u pergadit per numrim te minutave per diten tjeter
minRain = 0;  
}


void loop()
{
  DateTime now = rtc.now();  //Marrim kohen nga rtc
  Blynk.run();                                                  // run Blank magic 
  timer.run();                                                  // use timer for runing interval
  if (now.hour() == 20 && now.minute()==0) //ketu e marrim kohen nga rtc dhe shikojme a eshte ora 20:00
    {
        if (minRain > 360) //kontrollojme a ka rene shi me shume se 360 min
        {
            skipOpening(); //Nuk e hapim relay
            delay(minDelay*60*24*2); //skip per dy dite, pastaj fillo mer informata per diten e trete
        }
        else if (minRain > 120) //kontrollojme a ka rene shi me shume se 120 min
        {
            skipOpening(); ///Nuk e hapim relayn
            delay(minDelay*60*24); //skip per 1 dite, pastaj fillo mer informata per diten e dyte
        }
        else if (minRain > 30)
        {
            skipOpening(); ///Nuk e hapim relayn
        }
        else
        {
            openRelay(); //hapet relay
        }
    }
  delay(minDelay);                        // Delay 1 min
  if(isOpen == 1){                        //Shikojme a eshte e hapur relay
minOpen++;                                   //rrisim minutat e hapjes per nga nje
if(minOpen>30){                    //Shikojme aka qendruar hapur relay 30 minuta
isOpen=0;
closeRelay();                // nese relay ka qendruar hapur me shume se 30 minuta e mbyrllim rellayn
}
  }
 
  if((digitalRead(3)) == LOW)    //ketu marrim informaten nga senzori se a eshte duke rene shi
  { 
minRain++;                  // nese eshte duke rene shi, rrisim variablen per nje
  }
}

Pete.

2 Likes