OneWire, Ubidots, Breathing green

Hi everyone
I’m having trouble identifying what is making my photon “breathe green” shortly after flashing a project.
I’m trying to get two onewire temp sensors (on two different pins/busses) to post to ubidots. Was going fine with one sensor, but something about adding another request in the loop makes the photon breathe green shortly after it posts a single data point on one of the ubidots variables. Could someone please give me some help and look over what I’m doing wrong?

 // This #include statement was automatically added by the Spark IDE.
 #include "HttpClient/HttpClient.h"

 // This #include statement was automatically added by the Spark IDE.
 #include "OneWire/OneWire.h"

 // This #include statement was automatically added by the Spark IDE.
 #include "spark-dallas-temperature/spark-dallas-temperature.h"

 #define ONE_WIRE_BUSA D3
 #define ONE_WIRE_BUSB D4
 OneWire oneWireA(ONE_WIRE_BUSA);
 OneWire oneWireB(ONE_WIRE_BUSB);
 DallasTemperature sensorA(&oneWireA);
 DallasTemperature sensorB(&oneWireB);

 float temperaturefridge = 0.0;
 float temperaturebeer = 0.0;
 char resultstrfridge[64];
 char resultstrbeer[64];
 HttpClient http;
 unsigned int nextTime = 0;    // Next time to contact the server


 #define VARIABLE_ID "56767bcd7625426703e9b851"
 #define VARIABLE_ID2 "574983f87625427f0341f898"
 #define TOKEN "880hJv0HQWcgnwzUxxxxxxxxxxxxxxxxjWQgMmCqQjICD1lg28Q7"

 http_header_t headers[] = {
       { "Content-Type", "application/json" },
       { "X-Auth-Token" , TOKEN },
     { NULL, NULL } // NOTE: Always terminate headers will NULL
 };

 http_request_t request;
 http_response_t response;

 void setup() {

     Serial.begin(9600);
     sensorA.begin();
     sensorB.begin();
 }

 void loop() {
         if (nextTime > millis()) {
         return;
     }

  sensorA.requestTemperatures();
 // temperature=sensor.getTempCByIndex( 0 );
 float temp1 = sensorA.getTempCByIndex( 0 );
  while(temp1 == -127 || temp1 == 85){
         //Particle.process();
         temp1 = sensorA.getTempCByIndex( 0 );
     }
  sprintf(resultstrfridge, "{\"value\":%.4f}",temp1);
     request.port = 80;
     request.hostname = "things.ubidots.com";
     request.path = "/api/v1.6/variables/"VARIABLE_ID"/values";

  request.body = resultstrfridge;//Sending presence to Ubidots
  http.post(request, response, headers);
  Serial.println(response.status); //For debug only
  Serial.println(response.body);
  //delay(1000);
 //Second sensor
 float temp2 = sensorB.getTempCByIndex( 0 );
  while(temp2 == -127 || temp2 == 85){
         //Particle.process();
         temp2 = sensorB.getTempCByIndex( 0 );
     }
  sprintf(resultstrbeer, "{\"value\":%.4f}",temp2);
     request.port = 80;
     request.hostname = "things.ubidots.com";
     request.path = "/api/v1.6/variables/"VARIABLE_ID2"/values";

  request.body = resultstrbeer;//Sending presence to Ubidots
  http.post(request, response, headers);
  Serial.println(response.status); //For debug only
  Serial.println(response.body);


 nextTime = millis() + 1000;
  //delay(1000);
 }

Where these commented Particle.process() lines in your while()s a try to overcome the issue and didn’t work?

I’d leave them in.

Might be stuck here:

Add a couple of Serial.println messages for debug. Put one just before the ‘while’ and one just after it (after ‘}’, before the ‘sprintf(resultstrbeer’ line). If you see the first one but not the second then it’s looping at the while statement.

The Particle. process() lines keep the photon connected, breathing cyan, but prevent publishing data to ubidots for some reason.
The while loops are there to mitigate problem readings of - 127.

I wasn’t calling requestTemperatures() for sensor B. I don’t really know what I’m doing, so can’t explain exactly how that affects the execution of the hardware nor software. This is what works:

> // This #include statement was automatically added by the Spark IDE.
> #include "HttpClient/HttpClient.h"

> // This #include statement was automatically added by the Spark IDE.
> #include "OneWire/OneWire.h"

> // This #include statement was automatically added by the Spark IDE.
> #include "spark-dallas-temperature/spark-dallas-temperature.h"

> #define ONE_WIRE_BUSA D3
> #define ONE_WIRE_BUSB D4
> OneWire oneWireA(ONE_WIRE_BUSA);
> OneWire oneWireB(ONE_WIRE_BUSB);
> DallasTemperature sensorA(&oneWireA);
> DallasTemperature sensorB(&oneWireB);

> float temperaturefridge = 0.0;
> float temperaturebeer = 0.0;
> char resultstrfridge[64];
> char resultstrbeer[64];
> HttpClient http;
> unsigned int nextTime = 0;    // Next time to contact the server


> #define VARIABLE_ID "56767bcd7625426703e9b851"
> #define VARIABLE_ID2 "574983f87625427f0341f898"
> #define TOKEN "880hJv0HQWcgnwzUTqhrkQKq************lt49lzzjWQgMmCqQjICD1lg28Q7"

> http_header_t headers[] = {
>       { "Content-Type", "application/json" },
>       { "X-Auth-Token" , TOKEN },
>     { NULL, NULL } // NOTE: Always terminate headers will NULL
> };

> http_request_t request;
> http_response_t response;

> void setup() {

>     Serial.begin(9600);
>     sensorA.begin();
>     sensorB.begin();
> }

> void loop() {
>         if (nextTime > millis()) {
>         return;
>     }

> sensorA.requestTemperatures();

> float temp1 = sensorA.getTempCByIndex( 0 );
>  while(temp1 == -127 || temp1 == 85){

>         temp1 = sensorA.getTempCByIndex( 0 );
>         Particle.process();
>     }
>  sprintf(resultstrfridge, "{\"value\":%.4f}",temp1);
>     request.port = 80;
>     request.hostname = "things.ubidots.com";
>     request.path = "/api/v1.6/variables/"VARIABLE_ID"/values";

>  request.body = resultstrfridge;//Sending presence to Ubidots
>  http.post(request, response, headers);
>  Serial.println(response.status); //For debug only
>  Serial.println(response.body);

> //Second sensor


> sensorB.requestTemperatures();

> float temp2 = sensorB.getTempCByIndex( 0 );
>  while(temp2 == -127 || temp2 == 85){

>         temp2 = sensorB.getTempCByIndex( 0 );
>         Particle.process();
>     }
>  sprintf(resultstrbeer, "{\"value\":%.4f}",temp2);
>     request.port = 80;
>     request.hostname = "things.ubidots.com";
>     request.path = "/api/v1.6/variables/"VARIABLE_ID2"/values";

>  request.body = resultstrbeer;//Sending presence to Ubidots
>  http.post(request, response, headers);
>  Serial.println(response.status); //For debug only
>  Serial.println(response.body);

> nextTime = millis() + 1000;



> }

I suppose there’s a way to be less annoying when posting code that I’m not aware of. Sorry about that.

Yes, there is :wink:
Wrap your code in a set of this

 ```cpp
 // your code here

These a *grave accents* amd not *single quotes*.

---

Obviously you code gets stuck inside these loops for some reason.
You should maybe add a timeout in the loops.

BTW: I'm using this and hardly ever have a false reading (also with two sensors)
```cpp
//SYSTEM_MODE(MANUAL)
STARTUP(System.enableFeature(FEATURE_RETAINED_MEMORY))

#include "DS18B20/Particle-OneWire.h"
#include "DS18B20/DS18B20.h"
#include "Ubidots/Ubidots.h"

#define TOKEN "Put here your Ubidots TOKEN"
#define DATA_SOURCE_NAME "Particle" 
#define DATA_SOURCE_TAG  "Temps"  

const uint32_t PUBLISH_MINUTES  = 15; 
const uint32_t SAMPLE_INTERVAL  = 10000;
const uint32_t PUBLISH_INTERVAL = PUBLISH_MINUTES + 60000;
const int led = D7;

DS18B20 ds18b20(A0); 
Ubidots ubidots(TOKEN);

char szInfo[64];
retained double   tempWater    =  0;
retained int      lastHour     = 24;

void setup() 
{
    #if (PLATFORM_ID == PLATFORM_ELECTRON_PRODUCTION)
      Cellular.on();
      Cellular.connect();
    #else
      WiFi.on();
      WiFi.connect();
    #endif

    if (Time.hour() < lastHour)
    {
        Particle.connect();
        Particle.syncTime();
    }
    Serial.begin(115200);
    Time.zone(+2);

    delay(1000);
    ubidots.setDatasourceName(DATA_SOURCE_NAME);
    ubidots.setDatasourceTag(DATA_SOURCE_TAG);
}

void loop() 
{
  getTemp();
  publishData();
}

void publishData()
{
  if (!ds18b20.crcCheck()) return;
  
  ubidots.add("WaterTemp"  , tempWater  );
  ubidots.sendAll();

  sprintf(szInfo, "Water %.1f °C", tempWater);
  Serial.println(szInfo);

  for(uint32_t ms = millis(); millis() - ms < 10000;)
    Particle.process();

  int dt = (PUBLISH_MINUTES - Time.minute() % PUBLISH_MINUTES) * 60 - Time.second();  // wake at next time boundary
  lastHour = Time.hour();
  System.sleep(SLEEP_MODE_DEEP, dt, SLEEP_NETWORK_STANDBY);   
}

void getTemp()
{
  if(!ds18b20.search())
  {
    int dsAttempts = 0;
    double _tempWater;
    ds18b20.resetsearch();
    _tempWater = ds18b20.getTemperature();
    while (!ds18b20.crcCheck() && dsAttempts < 4)
    {
      Serial.printf("Bad value (%d)", dsAttempts++);
      if (dsAttempts == 3)
        delay(1000);
      ds18b20.resetsearch();
      _tempWater = ds18b20.getTemperature();
    }
    Serial.println(_tempWater);

    if(dsAttempts < 4)
    { // we have a valid reading
      tempWater = (_tempWater + tempWater) / (tempWater ? 2.0 : 1.0); // avg if required
    }
  }
}