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);
}
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: