Ds18b20 + hcsr04 + nodemcu + cayenne


#1

Hello everyone,

I encounter an error on my code following the addition of DS18B20 sensor to my project CAYENNE + NODEMCU + 2 HCSR04 sensors.
Both sensors work perfectly however I need to add 2 temperature sensors to my initial project.

Here is the code added that gives errors to the compilation

// 2 HCSR04 Pour la mesure de hauteur d’eau dans des cuves de 1000 litres.

#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial
#include <CayenneMQTTESP8266.h>

#include <ESP8266WiFi.h>
#include "CayenneDefines.h"
#include "BlynkSimpleEsp8266.h"
#include "CayenneWiFiClient.h"

#include <OneWire.h>
#include <DallasTemperature.h>
#define VIRTUAL_PIN1 V1
#define VIRTUAL_PIN2 V2
const int tmpPin = 14;
OneWire oneWire(tmpPin);
DallasTemperature sensors(&oneWire);

//const int tmpPin1 = 1;
//OneWire oneWire1(tmpPin1);
//DallasTemperature sensors1(&oneWire1);

// WiFi network info.
char ssid[] = "XXX";
char wifiPassword[] = "XXX";

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = "XXX";
char password[] = "XXX";
char clientID[] = "XXX";

unsigned long lastMillis = 0;
unsigned long lastMillis2 = 0;

// defines pins numbers HCSR04 N°1
const int trigPin = 2;  //D4
const int echoPin = 0;  //D3

// defines pins numbers HCSR04 N°2
const int trigPin2 = 4;  //D2
const int echoPin2 = 5;  //D1


// defines variables HCSR04 N°1
long duration;
int distance;
int y;
// defines variables HCSR04 N°2
long duration2;
int distance2;
int y2;

void setup() {
  Serial.begin(9600); // Starts the serial communication
  Cayenne.begin(username, password, clientID, ssid, wifiPassword);
  sensors.begin();
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  pinMode(echoPin, INPUT); // Sets the echoPin as an Input
  pinMode(trigPin2, OUTPUT); // Sets the trigPin2 as an Output
  pinMode(echoPin2, INPUT); // Sets the echoPin2 as an Input
  
}

void loop() {
  Cayenne.loop();

//HCSR04 N°1

  // Clears the trigPin
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);

  // Sets the trigPin on HIGH state for 10 micro seconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  // Reads the echoPin, returns the sound wave travel time in microseconds
  duration = pulseIn(echoPin, HIGH);

  // Calculating the distance
  distance = ((duration * 0.034) / 2)-10;
  // Prints the distance on the Serial Monitor
  Serial.print("Distance: ");
  Serial.println(distance);
  y = map(distance, 95, 10, 0, 100);
  y = constrain(y, 0, 100);  
  delay(5000);
  if (millis() - lastMillis > 10000) {
    lastMillis = millis();
    //Cayenne.virtualWrite(10, 105-distance);
    //Cayenne.virtualWrite(11, y, "t1", "null");
    Cayenne.virtualWrite(1, y, "analog_sensor", "null");

//HCSR04 N°2

 // Clears the trigPin N°2
  digitalWrite(trigPin2, LOW);
  delayMicroseconds(2);

  // Sets the trigPin N°2 on HIGH state for 10 micro seconds
  digitalWrite(trigPin2, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin2, LOW);

  // Reads the echoPin N°2, returns the sound wave travel time in microseconds
  duration2 = pulseIn(echoPin2, HIGH);

  // Calculating the distance N°2
  distance2 = ((duration2 * 0.034) / 2)-10;
   // Prints the distance N°2 on the Serial Monitor
  Serial.print("Distance2: ");
  Serial.println(distance2);
  y2 = map(distance2, 95, 10, 0, 100);
  y2 = constrain(y2, 0, 100);  
  delay(5000);
  if (millis() - lastMillis2 > 10000) {
    lastMillis2 = millis();
    //Cayenne.virtualWrite(20, 105-distance2);
    //Cayenne.virtualWrite(21, y2, "t1", "null");
    Cayenne.virtualWrite(2, y2, "analog_sensor", "null");



  // do something
}        
  }
 }
}
// This function is called when the Cayenne widget requests data for the Virtual Pin.
CAYENNE_OUT(V1)
{
  // Send the command to get temperatures.
  sensors.requestTemperatures();
  // This command writes the temperature in Celsius to the Virtual Pin.
  //Cayenne.virtualWrite(VIRTUAL_PIN, sensors.getTempCByIndex(0));
  // To send the temperature in Fahrenheit use the corresponding code below.
  Cayenne.virtualWrite(VIRTUAL_PIN1, sensors.getTempFByIndex(0));
}

CAYENNE_OUT(V2)
{
  // Send the command to get temperatures.
  sensors.requestTemperatures();
  // This command writes the temperature in Celsius to the Virtual Pin.
  //Cayenne.virtualWrite(VIRTUAL_PIN, sensors.getTempCByIndex(0));
  // To send the temperature in Fahrenheit use the corresponding code below.
  Cayenne.virtualWrite(VIRTUAL_PIN2, sensors.getTempFByIndex(1));
}


CAYENNE_CONNECTED()
{
  CAYENNE_LOG("Connection established");
}

The error :

erreur


#2

it’s smell like an acolade missing somewhere !!


#3

I have made the syntax changes to your code but not tested it. and you should have continued on the previous topic only instead of creating a new one.

#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial
#include <CayenneMQTTESP8266.h>

#include <OneWire.h>
#include <DallasTemperature.h>
#define VIRTUAL_PIN1 V1
#define VIRTUAL_PIN2 V2
const int tmpPin = 14;
OneWire oneWire(tmpPin);
DallasTemperature sensors(&oneWire);

//const int tmpPin1 = 1;
//OneWire oneWire1(tmpPin1);
//DallasTemperature sensors1(&oneWire1);

// WiFi network info.
char ssid[] = "XXX";
char wifiPassword[] = "XXX";

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = "XXX";
char password[] = "XXX";
char clientID[] = "XXX";

unsigned long lastMillis = 0;
unsigned long lastMillis2 = 0;

// defines pins numbers HCSR04 N°1
const int trigPin = 2;  //D4
const int echoPin = 0;  //D3

// defines pins numbers HCSR04 N°2
const int trigPin2 = 4;  //D2
const int echoPin2 = 5;  //D1


// defines variables HCSR04 N°1
long duration;
int distance;
int y;
// defines variables HCSR04 N°2
long duration2;
int distance2;
int y2;

void setup() {
  Serial.begin(9600); // Starts the serial communication
  Cayenne.begin(username, password, clientID, ssid, wifiPassword);
  sensors.begin();
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  pinMode(echoPin, INPUT); // Sets the echoPin as an Input
  pinMode(trigPin2, OUTPUT); // Sets the trigPin2 as an Output
  pinMode(echoPin2, INPUT); // Sets the echoPin2 as an Input

}

void loop() {
  Cayenne.loop();

  //HCSR04 N°1

  // Clears the trigPin
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);

  // Sets the trigPin on HIGH state for 10 micro seconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  // Reads the echoPin, returns the sound wave travel time in microseconds
  duration = pulseIn(echoPin, HIGH);

  // Calculating the distance
  distance = ((duration * 0.034) / 2) - 10;
  // Prints the distance on the Serial Monitor
  Serial.print("Distance: ");
  Serial.println(distance);
  y = map(distance, 95, 10, 0, 100);
  y = constrain(y, 0, 100);
  delay(5000);
  if (millis() - lastMillis > 10000) {
    lastMillis = millis();
    //Cayenne.virtualWrite(10, 105-distance);
    //Cayenne.virtualWrite(11, y, "t1", "null");
    Cayenne.virtualWrite(1, y, "analog_sensor", "null");
  }
  //HCSR04 N°2

  // Clears the trigPin N°2
  digitalWrite(trigPin2, LOW);
  delayMicroseconds(2);

  // Sets the trigPin N°2 on HIGH state for 10 micro seconds
  digitalWrite(trigPin2, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin2, LOW);

  // Reads the echoPin N°2, returns the sound wave travel time in microseconds
  duration2 = pulseIn(echoPin2, HIGH);

  // Calculating the distance N°2
  distance2 = ((duration2 * 0.034) / 2) - 10;
  // Prints the distance N°2 on the Serial Monitor
  Serial.print("Distance2: ");
  Serial.println(distance2);
  y2 = map(distance2, 95, 10, 0, 100);
  y2 = constrain(y2, 0, 100);
  delay(5000);
  if (millis() - lastMillis2 > 10000) {
    lastMillis2 = millis();
    //Cayenne.virtualWrite(20, 105-distance2);
    //Cayenne.virtualWrite(21, y2, "t1", "null");
    Cayenne.virtualWrite(2, y2, "analog_sensor", "null");
  }
}
// This function is called when the Cayenne widget requests data for the Virtual Pin.
CAYENNE_OUT(V1)
{
  // Send the command to get temperatures.
  sensors.requestTemperatures();
  // This command writes the temperature in Celsius to the Virtual Pin.
  //Cayenne.virtualWrite(VIRTUAL_PIN, sensors.getTempCByIndex(0));
  // To send the temperature in Fahrenheit use the corresponding code below.
  Cayenne.virtualWrite(VIRTUAL_PIN1, sensors.getTempFByIndex(0));
}

CAYENNE_OUT(V2)
{
  // Send the command to get temperatures.
  sensors.requestTemperatures();
  // This command writes the temperature in Celsius to the Virtual Pin.
  //Cayenne.virtualWrite(VIRTUAL_PIN, sensors.getTempCByIndex(0));
  // To send the temperature in Fahrenheit use the corresponding code below.
  Cayenne.virtualWrite(VIRTUAL_PIN2, sensors.getTempFByIndex(1));
}

#4

Thank’s for all. All is all right.


#5

everything is working fine?


#6

Hello shramiksalgaonkar,

Yes, everything is OK, I just added some delay (5000); because values were strange.
How can I close my two posts?

Thanks again for the time spent with me to help me.

I have to come back to the forum in a few days because I have problems with a raspberry PI3 and a motion sensor.

Vincent.


#7