Hi everyone I need your help.
I have to combine two articles I created.
- Ultrasonic distance sensor
- Reading temperature sensor DS18B20
Code Project 1
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#define triggerPort D1 //pin NodeMCU
#define echoPort D2 //pin NodeMCU
long r ; //variabile distanza
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "xxxx";
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "xxxxx";
char pass[] = "xxxxx";
BlynkTimer timer;
void myTimerEvent()
{
// You can send any value at any time.
// Please don't send more that 10 values per second.
Blynk.virtualWrite(V0, 82-r); //Virtual Pin Label (82-r per avere il valore invertito)
Blynk.virtualWrite(V1, r); //Virtual Pin Level H
}
void setup()
{
// Debug console
Serial.begin(9600);
pinMode( triggerPort, OUTPUT );
pinMode( echoPort, INPUT );
Blynk.begin(auth, ssid, pass);
// You can also specify server:
//Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 8442);
//Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8442);
timer.setInterval(10000L, myTimerEvent); //Ritardo di lettura in millisecondi
}
void loop() {
digitalWrite( triggerPort, LOW ); //porta bassa l'uscita del trigger
digitalWrite( triggerPort, HIGH ); //invia un impulso di 10microsec su trigger
delayMicroseconds( 10 );
digitalWrite( triggerPort, LOW );
long duration = pulseIn( echoPort, HIGH , 10000 ); // 10000 µS = max circa 1,7 metri
r = 0.034 * duration / 2;
/*
Adessso sono disattivate
//Notiche allerta
if ( r > 10) {
Blynk.notify("ALLERTA COMBUSTIBILE CALDAIA!!! IL LIVELLO DI COMBUSTIBILE DELLA CALDAIA E' BASSO");
Blynk.email("xxxxxxxxx","ALLERTA COMBUSTIBILE CALDAIA","IL LIVELLO DI COMBUSTIBILE DELLA CALDAIA E' BASSO");
}*/
Blynk.run();
timer.run(); // Initiates BlynkTimer
}
code project 2
#include <OneWire.h>
#include <DallasTemperature.h>
#define BLYNK_PRINT Serial
#include <SPI.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#define ONE_WIRE_BUS 2 //pin connected to DS18B20 Sensor(s) D4 on NodeMCU
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
SimpleTimer timer;
char auth[] = "xxxxx";
char ssid[] = "xxxxx";
char pass[] = "xxxxx";
void sendSensor(){
Serial.print("Requesting temperatures...");
sensors.requestTemperatures(); // Send the command to get temperatures
Serial.println("DONE");
float temp1 = sensors.getTempCByIndex(0);
Serial.print("Temperature for the device 0 (index 0) is: ");
Serial.println(temp1);
Blynk.virtualWrite(V10, temp1); //BLYNK Value Display set to Virtual Pin 10
}
void setup()
{
Serial.begin(115200);
sensors.begin();
Blynk.begin(auth, ssid, pass);
timer.setInterval(1000L, sendSensor); // Temp Sensor Function every 1 seconds
}
void loop()
{
Blynk.run();
timer.run();
}
I tried joining my two projects but only the ulltrasuoni sensor works but the temperature is not detected. Where is wrong ???
Unified Code
#include <OneWire.h>
#include <DallasTemperature.h>
#define BLYNK_PRINT Serial
#include <SPI.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#define triggerPort D1 //pin NodeMCU
#define echoPort D2 //pin NodeMCU
#define ONE_WIRE_BUS 2 //pin connected to DS18B20 Sensor(s) D4 on NodeMCU
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
void sendSensor(){
Serial.print("Requesting temperatures...");
sensors.requestTemperatures(); // Send the command to get temperatures
float temp1 = sensors.getTempCByIndex(0);
Serial.print("Temperature for the device 0 (index 0) is: ");
Serial.println(temp1);
Blynk.virtualWrite(V10, temp1); //BLYNK Value Display set to Virtual Pin 10
}
long r ; //variabile distanza
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "xxxxxx";
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "xxxxxx";
char pass[] = "xxxxx";
BlynkTimer timer;
void myTimerEvent()
{
// You can send any value at any time.
// Please don't send more that 10 values per second.
Blynk.virtualWrite(V0, 82-r); //Virtual Pin Label (82-r per avere il valore invertito)
Blynk.virtualWrite(V1, r); //Virtual Pin Level H
}
void setup()
{
// Debug console
Serial.begin(115200);
pinMode( triggerPort, OUTPUT );
pinMode( echoPort, INPUT );
sensors.begin();
Blynk.begin(auth, ssid, pass);
// You can also specify server:
//Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 8442);
//Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8442);
timer.setInterval(10000L, myTimerEvent); //Ritardo di lettura in millisecondi
}
void loop() {
digitalWrite( triggerPort, LOW ); //porta bassa l'uscita del trigger
digitalWrite( triggerPort, HIGH ); //invia un impulso di 10microsec su trigger
delayMicroseconds( 10 );
digitalWrite( triggerPort, LOW );
long duration = pulseIn( echoPort, HIGH , 10000 ); // 10000 µS = max circa 1,7 metri
r = 0.034 * duration / 2;
/*
Adessso sono disattivate
//Notiche allerta
if ( r > 10) {
Blynk.notify("ALLERTA COMBUSTIBILE CALDAIA!!! IL LIVELLO DI COMBUSTIBILE DELLA CALDAIA E' BASSO");
Blynk.email("xxxxxxx","ALLERTA COMBUSTIBILE CALDAIA","IL LIVELLO DI COMBUSTIBILE DELLA CALDAIA E' BASSO");
}*/
Blynk.run();
timer.run(); // Initiates BlynkTimer
}