Help Combining Sketch File with DS18B20

Hi all,
Sorry,I am a beginner,and this job is a first for me.
I am using temperature with DS18B20 8 sensors sketch file.
Sketch with DS18B20 X8 working fine,in next step I add in dashboard two signaling light-
(add new-sensors- generic-digital input-connectivity digital-pin D5 and second pin D8, 2 state.I will use it how door open alarm swtch, when door is open swich is on and 5v through 10k resistor going in arduino uno digital pins 5 or 8,and then in dashboard lights green,and I know that door is open).Widget set in Integrated GPIO channel 5 second 8.
Also I add in dashboard two light switch (add new-actuators-light-light swtch-digital pin D3 and D4- widget Button.Widget set in Integrated GPIO chanel 3 second 4.When I push button on dashboard I get 5v output in it (3 or 4 pin).
With one sketch file from Cayenne all this working good:

#define CAYENNE_PRINT Serial  // Comment this out to disable prints and save space
   // If you're not using the Ethernet W5100 shield, change this to match your connection type. See Communications examples.
    #include <CayenneEthernet.h>

    // Cayenne authentication token. This should be obtained from the Cayenne Dashboard.
    char token[] = "xxxxxj167";

    void setup()
    {
    	Serial.begin(9600);
    	Cayenne.begin(token);
    }

    void loop()
    {
    	Cayenne.run();
    } 

But when I tried use this sketch and ds18b20 x8 both I get error from arduino- temperature working ok, signaling light ok, but lamp switch not working.
In IDE I am doing : new-sketch-add file-add ds18b20 x8 sketch-add file-add sketch which is above-verify-upload`.
Why I can upload these two sketchs in arduino,that all (temperature,signaling light and light switch) will work correctly?
ds18b20 x8 sketch:
#include <DallasTemperature.h>
#include <OneWire.h>
#include <CayenneEthernet.h>

// Virtual Pin of the DS18B20 widget.

#define VIRTUAL_PIN1 V1
#define VIRTUAL_PIN2 V2
#define VIRTUAL_PIN3 V3
#define VIRTUAL_PIN4 V4
#define VIRTUAL_PIN5 V5
#define VIRTUAL_PIN6 V6
#define VIRTUAL_PIN7 V7
#define VIRTUAL_PIN8 V8
const int tmpPin1 = 2;

OneWire oneWire1(tmpPin1);
OneWire oneWire2(tmpPin1);
OneWire oneWire3(tmpPin1);
OneWire oneWire4(tmpPin1);
OneWire oneWire5(tmpPin1);
OneWire oneWire6(tmpPin1);
OneWire oneWire7(tmpPin1);
OneWire oneWire8(tmpPin1);

DallasTemperature sensors1(&oneWire1);
DallasTemperature sensors2(&oneWire2);
DallasTemperature sensors3(&oneWire3);
DallasTemperature sensors4(&oneWire4);
DallasTemperature sensors5(&oneWire5);
DallasTemperature sensors6(&oneWire6);
DallasTemperature sensors7(&oneWire7);
DallasTemperature sensors8(&oneWire8);

char token[] = "xxxx167";

void setup()
{
Serial.begin(9600);

Cayenne.begin(token);
sensors1.begin();
sensors2.begin();
sensors3.begin();
sensors4.begin();
sensors5.begin();
sensors6.begin();
sensors7.begin();
sensors8.begin();
}

void loop()
{
Cayenne.run();
sensors1.requestTemperatures();
float temperatura1 = sensors1.getTempCByIndex(0);
sensors2.requestTemperatures();
float temperatura2 = sensors2.getTempCByIndex(1);
sensors4.requestTemperatures();
float temperatura3 = sensors3.getTempCByIndex(2);
sensors4.requestTemperatures();
float temperatura4 = sensors4.getTempCByIndex(3);
sensors5.requestTemperatures();
float temperatura5 = sensors5.getTempCByIndex(4);
sensors6.requestTemperatures();
float temperatura6 = sensors6.getTempCByIndex(5);
sensors7.requestTemperatures();
float temperatura7 = sensors7.getTempCByIndex(6);
sensors8.requestTemperatures();
float temperatura8 = sensors8.getTempCByIndex(7);
}

// This function is called when the Cayenne widget requests data for the Virtual Pin.
CAYENNE_OUT(VIRTUAL_PIN1)
{
// Send the command to get temperatures.
sensors1.requestTemperatures();
// This command writes the temperature in Celsius to the Virtual Pin.
Cayenne.celsiusWrite(VIRTUAL_PIN1, sensors1.getTempCByIndex(0));
// To send the temperature in Fahrenheit use the corresponding code below.
//Cayenne.fahrenheitWrite(VIRTUAL_PIN, sensors.getTempFByIndex(0));
}
CAYENNE_OUT(VIRTUAL_PIN2)
{
// Send the command to get temperatures.
sensors2.requestTemperatures();
// This command writes the temperature in Celsius to the Virtual Pin.
Cayenne.celsiusWrite(VIRTUAL_PIN2, sensors2.getTempCByIndex(1));
// To send the temperature in Fahrenheit use the corresponding code below.
//Cayenne.fahrenheitWrite(VIRTUAL_PIN, sensors.getTempFByIndex(0));
}
CAYENNE_OUT(VIRTUAL_PIN3)
{
// Send the command to get temperatures.
sensors3.requestTemperatures();
// This command writes the temperature in Celsius to the Virtual Pin.
Cayenne.celsiusWrite(VIRTUAL_PIN3, sensors3.getTempCByIndex(2));
// To send the temperature in Fahrenheit use the corresponding code below.
//Cayenne.fahrenheitWrite(VIRTUAL_PIN, sensors.getTempFByIndex(0));
}
// This function is called when the Cayenne widget requests data for the Virtual Pin.
CAYENNE_OUT(VIRTUAL_PIN4)
{
// Send the command to get temperatures.
sensors4.requestTemperatures();
// This command writes the temperature in Celsius to the Virtual Pin.
Cayenne.celsiusWrite(VIRTUAL_PIN4, sensors4.getTempCByIndex(3));
// To send the temperature in Fahrenheit use the corresponding code below.
//Cayenne.fahrenheitWrite(VIRTUAL_PIN, sensors.getTempFByIndex(0));
}
CAYENNE_OUT(VIRTUAL_PIN5)
{
// Send the command to get temperatures.
sensors5.requestTemperatures();
// This command writes the temperature in Celsius to the Virtual Pin.
Cayenne.celsiusWrite(VIRTUAL_PIN5, sensors5.getTempCByIndex(4));
// To send the temperature in Fahrenheit use the corresponding code below.
//Cayenne.fahrenheitWrite(VIRTUAL_PIN, sensors.getTempFByIndex(0));
}
CAYENNE_OUT(VIRTUAL_PIN6)
{
// Send the command to get temperatures.
sensors6.requestTemperatures();
// This command writes the temperature in Celsius to the Virtual Pin.
Cayenne.celsiusWrite(VIRTUAL_PIN6, sensors6.getTempCByIndex(5));
// To send the temperature in Fahrenheit use the corresponding code below.
//Cayenne.fahrenheitWrite(VIRTUAL_PIN, sensors.getTempFByIndex(0));
}
// This function is called when the Cayenne widget requests data for the Virtual Pin.
CAYENNE_OUT(VIRTUAL_PIN7)
{
// Send the command to get temperatures.
sensors7.requestTemperatures();
// This command writes the temperature in Celsius to the Virtual Pin.
Cayenne.celsiusWrite(VIRTUAL_PIN7, sensors7.getTempCByIndex(6));
// To send the temperature in Fahrenheit use the corresponding code below.
//Cayenne.fahrenheitWrite(VIRTUAL_PIN, sensors.getTempFByIndex(0));
}
CAYENNE_OUT(VIRTUAL_PIN8)
{
// Send the command to get temperatures.
sensors8.requestTemperatures();
// This command writes the temperature in Celsius to the Virtual Pin.
Cayenne.celsiusWrite(VIRTUAL_PIN8, sensors8.getTempCByIndex(7));
// To send the temperature in Fahrenheit use the corresponding code below.
//Cayenne.fahrenheitWrite(VIRTUAL_PIN, sensors.getTempFByIndex(0));
}

Thanks,

Try removing everything but Cayenne.run() in the loop(). Doesn’t look like you need them for anything, and it could be just so busy that it can’t respond to your request for I/O change.

I tried a variety of options, but no result.I try as described in Docs Combining Sketch Files ,with widget lamp switch or relay and I get the same problem.I see that there are many professionals users,and knows how to do it.
All why I need use temperature with 8 ds18b20 sensors(sketch is above) and control two relay or light switch with one arduino uno.Can anyone advise for beginner how to do this.
Thank You,

So an experienced programmer would cut the program down to nothing, and test. Then add another feature, and test, and so on.

So looking at your posts, I know the standard Cayenne sketch is working for you and you can toggle a digital output right?

I would add one ds18b20 sensor and see if you are getting your temp reading and outputs, and go from there.

Are you running all 8 sensors off the Arduino supply? You might also want to measure the voltage and make sure you aren’t loading down too much.

Start with this:

#include <CayenneEthernet.h>  

#define CAYENNE_PRINT Serial

const int tmpPin1 = 2;

OneWire oneWire1(tmpPin1);
DallasTemperature sensors1(&oneWire1);

char token[] = "xxxx167";

void setup()
{
	Serial.begin(9600);

	Cayenne.begin(token);
	sensors1.begin();
}

void loop()
{
	Cayenne.run();
}

CAYENNE_OUT(V1)
{
	sensors1.requestTemperatures();
	Cayenne.celsiusWrite(V1, sensors1.getTempCByIndex(0));
}

Cheers,

Craig

1 Like

HI kreggly,
Thank You for answer.
Your suggestion sketch with one ds18b20 working good all functions (temperature,output,input). After this I tried remove something from ds18b20 x8 sketch.And I got a good result when I remove from ds18b20 x8 sketch from Cayenne.run();

sensors1.requestTemperatures();
float temperatura1 = sensors1.getTempCByIndex(0);
sensors2.requestTemperatures();
float temperatura2 = sensors2.getTempCByIndex(1);
sensors4.requestTemperatures();
3 = sensors3.getTempCByIndex(2);
sensors4.requestTemperatures();
float temperatura4 = sensors4.getTempCByIndex(3);
sensors5.requestTemperatures();
float temperatura5 = sensors5.getTempCByIndex(4);
sensors6.requestTemperatures();
float temperatura6 = sensors6.getTempCByIndex(5);
sensors7.requestTemperatures();
float temperatura7 = sensors7.getTempCByIndex(6);
sensors8.requestTemperatures();
float temperatura8 = sensors8.getTempCByIndex(7);

After this cut all start working good. Let me know I lose anything in temperature measured results if I will use sketch of temperature Cayenne.run() without
> float temperatura comands.
Thank You,

1 Like

Excellent.

The Arduinos are not multi-threaded microprocessors. They are single microcontrollers, thus if Cayenne.run() is not called often, it cannot respond to commands.

Rule of thumb for everyone should be to minimize wait states in the main loop or function calls that take a lot of time. Also, be careful of functions that may get hung up or have retries in them.

This is a good way to do something periodically without using delay(). This could also be in a function call, and you could cycle through and print temps one at a time if needed.

unsigned long lastMillis = 0;

void loop() 
{
     //Needs to run every second or so to keep from missing things
    Cayenne.run();

    //Publish data every 10 seconds (10000 milliseconds). 
    if (millis() - lastMillis > 10000) 
    {
	lastMillis = millis();
	
        //do something
    }
}

Cheers,

Craig

2 Likes