HC-SR04 and DS18B20 help?

Discussion about Z-Uno product. Visit http://z-uno.z-wave.me for more details.
mdietinger@gmail.com
Posts: 39
Joined: 12 Aug 2016 12:08

Re: HC-SR04 and DS18B20 help?

Post by mdietinger@gmail.com »

You didn't put in:
zunoSendReport(1);
KristjanV
Posts: 17
Joined: 19 May 2018 10:54

Re: HC-SR04 and DS18B20 help?

Post by KristjanV »

mdietinger@gmail.com wrote:
19 Jun 2018 19:20
You didn't put in:
zunoSendReport(1);
Yep! Got it working now, only problem is, below 80% ( i set deviation to 10cm and tank height 110cm) in every 10cm which is also 10%, it goes off by 1%.
So in example if i set the object from the sensor to a distance of 30cm, it gives me the tank fillment of 80% which is correct, but if i set the object to a distance of 50cm which is 60%, i get a reading of 63%.
KristjanV
Posts: 17
Joined: 19 May 2018 10:54

Re: HC-SR04 and DS18B20 help?

Post by KristjanV »

Also now that i got this part working, i need to combine the distance sensor with temperature sensors.

So this code

Code: Select all

// HC-SR04 Ultrasonic Distance Sensor

ZUNO_SETUP_CHANNELS(
   ZUNO_SENSOR_MULTILEVEL_GENERAL_PURPOSE(getter)
);


int readPin = 9;
int triggerPin = 10;
byte controlState = 0;
byte percentage;

void setup() {
  Serial.begin();
  pinMode(readPin, INPUT);
  pinMode(triggerPin, OUTPUT);
  digitalWrite(triggerPin, LOW);
}

void loop(){
      byte distance, heightTank, deviation;
      int duration;
      
      //You'll probably want to change the next 2 lines.
      // The first one is the max. level of the water.
      // The next one is how high the sensor is above that max. level.
      heightTank=110;
      deviation=10;
  
      digitalWrite(triggerPin, LOW);
      delayMicroseconds(10);
      digitalWrite(triggerPin, HIGH);
      delayMicroseconds(10);
      digitalWrite(triggerPin, LOW);
     
      duration = pulseIn(readPin, HIGH, 100000);
      
      distance = (duration/2)/29.1;
      percentage = (byte)(100-(((distance-deviation)*100)/heightTank));
      Serial.println(percentage);
      zunoSendReport(1);
      
      delay(1000);
}

byte getter() {
  return percentage;
}
and this code

Code: Select all

// Multiple temperature sensors DS18B20

#include "ZUNO_DS18B20.h"

#define DS18B20_BUS_PIN 11                  // Pin to which 1-Wire bus is connected
#define MAX_SENSORS     10                  // Number of DS18B20 sensors supported (equals to maximum number of channels for Z-Uno) 

OneWire ow(DS18B20_BUS_PIN);                // Software 1-Wire BUS
DS18B20Sensor ds18b20(&ow);                 // connect DS18B20 class to it

#define ADDR_SIZE 8                         // Size of address of devices on 1-wire bus
byte addresses[ADDR_SIZE * MAX_SENSORS];    // Here we store all the scanned addresses
#define ADDR(i) (&addresses[i * ADDR_SIZE]) // Macro to simplify our life
byte number_of_sensors;                     // Number of sensors found
word temperature[MAX_SENSORS];              // Here we store temperatures

void setup() {
  // Scanning sensors on the bus every time we starting a sketch
  number_of_sensors = ds18b20.findAllSensors(addresses);

  if(number_of_sensors > MAX_SENSORS)
    number_of_sensors = MAX_SENSORS;

  // Setting up Z-Uno channels
  // We do id dynamically... 
  // You have to exclude/include your Z-Uno to this take effect on the controller side
  ZUNO_START_CONFIG();
  for (byte i = 0; i < number_of_sensors; i++) {
    // Each channel is temperature sensor 
    // with 2 decimals precision (ex. 25.45C)
    ZUNO_ADD_CHANNEL(ZUNO_SENSOR_MULTILEVEL_CHANNEL_NUMBER, 
                     ZUNO_SENSOR_MULTILEVEL_TYPE_TEMPERATURE, 
                     SENSOR_MULTILEVEL_PROPERTIES_COMBINER(
                            SENSOR_MULTILEVEL_SCALE_CELSIUS, 
                            SENSOR_MULTILEVEL_SIZE_TWO_BYTES, 
                            SENSOR_MULTILEVEL_PRECISION_TWO_DECIMALS));
  }
  // Commit configuration that we made...
  ZUNO_COMMIT_CONFIG();
}

void loop() {
  for (byte i = 0; i < number_of_sensors; i++) {
    // Read temperature
    float ct =  ds18b20.getTemperature(ADDR(i));
    temperature[i] = ct * 100; // Convert temperature to fixed point (d)+.dd
    // Sending report
    zunoSendReport(i + 1); // Channels start from 1
  }
  // We have to wait 30 seconds 
  // It's a requirement of Z-Wave protocol
  delay(30000);
}

// Universal handler for all the channels
void zunoCallback(void) {
    // See callback_data variable 
    // We use word params for all 
    // We use zero based index of the channel instead of typical 
    // Getter/Setter index of Z-Uno. 
    // See enum ZUNO_CHANNEL*_GETTER/ZUNO_CHANNEL*_SETTER in ZUNO_Definitions.h 
    byte index = callback_data->type;
    index >>= 1;
    index --;
    callback_data->param.wParam = temperature[index];
}
will need to work together. How do i do that? I combined two codes but cant seem to get them working properly. First i cant get the ultrasonic sensor reading and temp sensors show 0.91C. Also i have connected 2 temp sensord but the system shows me 3 temp sensors and same reading on all.
KristjanV
Posts: 17
Joined: 19 May 2018 10:54

Re: HC-SR04 and DS18B20 help?

Post by KristjanV »

I have combined two codes like this at the moment

Code: Select all

// Multiple temperature sensors DS18B20

#include "ZUNO_DS18B20.h"

#define DS18B20_BUS_PIN 11                  // Pin to which DS18B20 bus is connected
#define N_SENSOR 2                          // Number of DS18B20 sensors
// You need to adopt ZUNO_SETUP_CHANNELS and add more getters if tou need more sensors

OneWire ow(DS18B20_BUS_PIN);
DS18B20Sensor ds18b20(&ow);

#define ADDR_SIZE 8                         // Size of address of devices on 1-wire bus
byte addresses[ADDR_SIZE * N_SENSOR];        // Here we store all the scanned addresses
#define ADDR(i) (&addresses[i * ADDR_SIZE]) // Macro to simplify our life
byte number_of_sensors;                     // Number of sensors found
word temperature[N_SENSOR];                 // Here we store temperatures

//ultrasonic sensor
int readPin = 9;
int triggerPin = 10;
byte controlState = 0;
byte percentage;
//ultrasonic sensor

// Define Z-Wave channels. Add more if you need more sensors
ZUNO_SETUP_CHANNELS(
        ZUNO_SENSOR_MULTILEVEL(ZUNO_SENSOR_MULTILEVEL_TYPE_TEMPERATURE, SENSOR_MULTILEVEL_SCALE_CELSIUS, SENSOR_MULTILEVEL_SIZE_TWO_BYTES, SENSOR_MULTILEVEL_PRECISION_TWO_DECIMALS, getterTemp1),
        ZUNO_SENSOR_MULTILEVEL(ZUNO_SENSOR_MULTILEVEL_TYPE_TEMPERATURE, SENSOR_MULTILEVEL_SCALE_CELSIUS, SENSOR_MULTILEVEL_SIZE_TWO_BYTES, SENSOR_MULTILEVEL_PRECISION_TWO_DECIMALS, getterTemp2),

//ultrasonic sensor        
        ZUNO_SENSOR_MULTILEVEL_GENERAL_PURPOSE(getter)  
);

void setup() {
  number_of_sensors = ds18b20.findAllSensors(addresses);

//ultrasonic sensor
  Serial.begin();
  pinMode(readPin, INPUT);
  pinMode(triggerPin, OUTPUT);
  digitalWrite(triggerPin, LOW);
//ultrasonic sensor
}

void loop() {
  for (byte i = 0; i < number_of_sensors && i < N_SENSOR; i++) {
    // Read temperature
    temperature[i] = ds18b20.getTemperature(ADDR(i)) * 100;
    // Sending report
    zunoSendReport(i + 1); // Channels starts from 1
 
//ultrasonic sensor
      byte distance, heightTank, deviation;
      int duration;
      
      //You'll probably want to change the next 2 lines.
      // The first one is the max. level of the water.
      // The next one is how high the sensor is above that max. level.
      heightTank=110;
      deviation=10;
  
      digitalWrite(triggerPin, LOW);
      delayMicroseconds(10);
      digitalWrite(triggerPin, HIGH);
      delayMicroseconds(10);
      digitalWrite(triggerPin, LOW);
     
      duration = pulseIn(readPin, HIGH, 100000);
      
      distance = (duration/2)/29.1;
      percentage = (byte)(100-(((distance-deviation)*100)/heightTank));
      Serial.println(percentage);
      zunoSendReport(1);
//ultrasonic sensor 
  }
  delay(3000);
}

// Getters. Add more if you need more sensors
word getterTemp1() {
  return temperature[1];
}

word getterTemp2() {
  return temperature[2];
}
//ultrasonic sensor
byte getter() {
  return percentage;
//ultrasonic sensor
}
And Vera is showing temp values and distance, but.... distance is not changing, at all. It's the same value at all times. What am i missing here?

Image
Thomas Carew poems
mdietinger@gmail.com
Posts: 39
Joined: 12 Aug 2016 12:08

Re: HC-SR04 and DS18B20 help?

Post by mdietinger@gmail.com »

Your just updating the temperature sensors in code:
zunoSendReport(i + 1)
and zunoSendReport(1);

You need to add:
zunoSendReport(3);
KristjanV
Posts: 17
Joined: 19 May 2018 10:54

Re: HC-SR04 and DS18B20 help?

Post by KristjanV »

mdietinger@gmail.com wrote:
04 Jul 2018 11:09
Your just updating the temperature sensors in code:
zunoSendReport(i + 1)
and zunoSendReport(1);

You need to add:
zunoSendReport(3);
Thanks, will try it out right now. Silly question but, does it matter where i put zunoSendReports?

One here

Code: Select all

void loop() {
  for (byte i = 0; i < number_of_sensors && i < N_SENSOR; i++) {
    // Read temperature
    temperature[i] = ds18b20.getTemperature(ADDR(i)) * 100;
    // Sending report
    zunoSendReport(i + 1); // Channels starts from 1
Seccond here

Code: Select all

//ultrasonic sensor
      byte distance, heightTank, deviation;
      int duration;
      
      //You'll probably want to change the next 2 lines.
      // The first one is the max. level of the water.
      // The next one is how high the sensor is above that max. level.
      heightTank=110;
      deviation=10;
  
      digitalWrite(triggerPin, LOW);
      delayMicroseconds(10);
      digitalWrite(triggerPin, HIGH);
      delayMicroseconds(10);
      digitalWrite(triggerPin, LOW);
     
      duration = pulseIn(readPin, HIGH, 100000);
      
      distance = (duration/2)/29.1;
      percentage = (byte)(100-(((distance-deviation)*100)/heightTank));
      Serial.println(percentage);
      zunoSendReport(3);
//ultrasonic sensor 
mdietinger@gmail.com
Posts: 39
Joined: 12 Aug 2016 12:08

Re: HC-SR04 and DS18B20 help?

Post by mdietinger@gmail.com »

Doesn't matter where in the loop you put it, as long the value is calculated before the send report.
The above example will just work fine.
KristjanV
Posts: 17
Joined: 19 May 2018 10:54

Re: HC-SR04 and DS18B20 help?

Post by KristjanV »

Cheers! Got it working now. Only thing is, i added 2 more temp sensors so now it's 4 temps sensors and one ultrasonic sensor.
In the code i wrote 4 temp sensors and added lines as needed, for ultrasonic i wrote zunoSendReport(5);
first 3 temp sensors work as they should, ultrasonic reads distance in percent as it should but the fourth temp is stuck on 0,09C.
I can't figure out whats the problem.

Here's the code

Code: Select all

// Multiple temperature sensors DS18B20

#include "ZUNO_DS18B20.h"

#define DS18B20_BUS_PIN 11                  // Pin to which DS18B20 bus is connected
#define N_SENSOR 4                          // Number of DS18B20 sensors
// You need to adopt ZUNO_SETUP_CHANNELS and add more getters if tou need more sensors

OneWire ow(DS18B20_BUS_PIN);
DS18B20Sensor ds18b20(&ow);

#define ADDR_SIZE 8                         // Size of address of devices on 1-wire bus
byte addresses[ADDR_SIZE * N_SENSOR];        // Here we store all the scanned addresses
#define ADDR(i) (&addresses[i * ADDR_SIZE]) // Macro to simplify our life
byte number_of_sensors;                     // Number of sensors found
word temperature[N_SENSOR];                 // Here we store temperatures

//ultrasonic sensor
int readPin = 9;
int triggerPin = 10;
byte controlState = 0;
byte percentage;
//ultrasonic sensor

// Define Z-Wave channels. Add more if you need more sensors
ZUNO_SETUP_CHANNELS(
        ZUNO_SENSOR_MULTILEVEL(ZUNO_SENSOR_MULTILEVEL_TYPE_TEMPERATURE, SENSOR_MULTILEVEL_SCALE_CELSIUS, SENSOR_MULTILEVEL_SIZE_TWO_BYTES, SENSOR_MULTILEVEL_PRECISION_TWO_DECIMALS, getterTemp1),
        ZUNO_SENSOR_MULTILEVEL(ZUNO_SENSOR_MULTILEVEL_TYPE_TEMPERATURE, SENSOR_MULTILEVEL_SCALE_CELSIUS, SENSOR_MULTILEVEL_SIZE_TWO_BYTES, SENSOR_MULTILEVEL_PRECISION_TWO_DECIMALS, getterTemp2),
        ZUNO_SENSOR_MULTILEVEL(ZUNO_SENSOR_MULTILEVEL_TYPE_TEMPERATURE, SENSOR_MULTILEVEL_SCALE_CELSIUS, SENSOR_MULTILEVEL_SIZE_TWO_BYTES, SENSOR_MULTILEVEL_PRECISION_TWO_DECIMALS, getterTemp3),
        ZUNO_SENSOR_MULTILEVEL(ZUNO_SENSOR_MULTILEVEL_TYPE_TEMPERATURE, SENSOR_MULTILEVEL_SCALE_CELSIUS, SENSOR_MULTILEVEL_SIZE_TWO_BYTES, SENSOR_MULTILEVEL_PRECISION_TWO_DECIMALS, getterTemp4),


//ultrasonic sensor        
        ZUNO_SENSOR_MULTILEVEL_GENERAL_PURPOSE(getter)  
);

void setup() {
  number_of_sensors = ds18b20.findAllSensors(addresses);

//ultrasonic sensor
  Serial.begin();
  pinMode(readPin, INPUT);
  pinMode(triggerPin, OUTPUT);
  digitalWrite(triggerPin, LOW);
//ultrasonic sensor
}

void loop() {
  for (byte i = 0; i < number_of_sensors && i < N_SENSOR; i++) {
    // Read temperature
    temperature[i] = ds18b20.getTemperature(ADDR(i)) * 100;
    // Sending report
    zunoSendReport(i + 1); // Channels starts from 1
    

//ultrasonic sensor
      byte distance, heightTank, deviation;
      int duration;
      
      //You'll probably want to change the next 2 lines.
      // The first one is the max. level of the water.
      // The next one is how high the sensor is above that max. level.
      heightTank=110;
      deviation=10;
  
      digitalWrite(triggerPin, LOW);
      delayMicroseconds(10);
      digitalWrite(triggerPin, HIGH);
      delayMicroseconds(10);
      digitalWrite(triggerPin, LOW);
     
      duration = pulseIn(readPin, HIGH, 100000);
      
      distance = (duration/2)/29.1;
      percentage = (byte)(100-(((distance-deviation)*100)/heightTank));
      Serial.println(percentage);
      zunoSendReport(5);
//ultrasonic sensor 
  }
  delay(3000);
}

// Getters. Add more if you need more sensors
word getterTemp1() {
  return temperature[1];
}

word getterTemp2() {
  return temperature[2];
}

word getterTemp3() {
  return temperature[3];
}

word getterTemp4() {
  return temperature[4];
}

//ultrasonic sensor
byte getter() {
  return percentage;
//ultrasonic sensor
}
KristjanV
Posts: 17
Joined: 19 May 2018 10:54

Re: HC-SR04 and DS18B20 help?

Post by KristjanV »

Sorry, my bad, got it working now. Got some numbers wrong in getter's

Changed that part and now its working!

Code: Select all

// Getters. Add more if you need more sensors
word getterTemp1() {
  return temperature[0];
}

word getterTemp2() {
  return temperature[1];
}

word getterTemp3() {
  return temperature[2];
}

word getterTemp4() {
  return temperature[3];
}
p0lyg0n1
Posts: 242
Joined: 04 Aug 2016 07:14

Re: HC-SR04 and DS18B20 help?

Post by p0lyg0n1 »

Hi,
try to use

Code: Select all

temperature[i] = ds18b20.getTempC100(ADDR(i)); 
instead of

Code: Select all

 temperature[i] = ds18b20.getTemperature(ADDR(i)) * 100;
Another good practice is to send reports at the and of all calculation just before the delay()
try this "loop" code instead (it's just an illustration, go give you an idea):

Code: Select all

void loop() {

  for (byte i = 0; i < number_of_sensors; i++) {
    // Read temperature 
   // It already encoded for format VVV.vv and doesn't use float
    temperature[i] = ds18b20.getTempC100(ADDR(i));
    }

//ultrasonic sensor
      byte distance, heightTank, deviation;
      int duration;
      // WE HAVE TO DO IT JUST ONCE, NOT INSIDE  DS18B20 LOOP^
      //You'll probably want to change the next 2 lines.
      // The first one is the max. level of the water.
      // The next one is how high the sensor is above that max. level.
      heightTank=110;
      deviation=10;
   
      digitalWrite(triggerPin, LOW);
      delayMicroseconds(10);
      digitalWrite(triggerPin, HIGH);
      delayMicroseconds(10);
      digitalWrite(triggerPin, LOW);
     // this code loads the uC hard, so lets report all values after it ends
      duration = pulseIn(readPin, HIGH, 100000);
      // This uses float thats not good for this uS, it eats lots of code
      // distance = (duration/2)/29.1;
      // percentage = (byte)(100-(((distance-deviation)*100)/heightTank));
     // alternative will be: (RISC style)
      duration >>= 1; // /2
      duration *= 100;
      duration /= 291;
      duration -= deviation;
      // To ensure there is no overflow
      duration = max(duration, heightTank);
      duration *= 100;
      duration /= heightTank;
      percentage = 100 - duration;
      Serial.println(percentage);
     // Report all the sensors at the end of the loop
     for(byte i=0;i<5;i++){
       zunoSendReport(i+1);
     }
     // delay while the OS could do something with  reports & radio
     delay(3000); 
  }
Post Reply