I had everything running fine - had a small php script to get the spark variable from the device.
But off late, I get a lot of these errors when I try a GET request.
Status Code: 408 - Request Timeout
"error": "Timed out."
A note of interest is that I often get a “error: core not found - make sure it’s online” on the first flash - it works if I hit the flash button again right after.
The core is happily breathing cyan meanwhile.
1 Like
zach
January 5, 2014, 6:22pm
2
@kidrock can you please share both the code that you’ve flashed on the Core and the GET request that you’re making?
This is the code - Used the library port for the DHT sensor.
#define MAXTIMINGS 85
#define cli noInterrupts
#define sei interrupts
#define DHT11 11
#define DHT22 22
#define DHT21 21
#define AM2301 21
#define NAN 999999
class DHT {
private:
uint8_t data[6];
uint8_t _pin, _type, _count;
bool read(void);
unsigned long _lastreadtime;
bool firstreading;
public:
DHT(uint8_t pin, uint8_t type, uint8_t count=6);
void begin(void);
float readTemperature(bool S=false);
float convertCtoF(float);
float readHumidity(void);
};
DHT::DHT(uint8_t pin, uint8_t type, uint8_t count) {
_pin = pin;
_type = type;
_count = count;
firstreading = true;
}
void DHT::begin(void) {
// set up the pins!
pinMode(_pin, INPUT);
digitalWrite(_pin, HIGH);
_lastreadtime = 0;
}
//boolean S == Scale. True == Farenheit; False == Celcius
float DHT::readTemperature(bool S) {
float _f;
if (read()) {
switch (_type) {
case DHT11:
_f = data[2];
if(S)
_f = convertCtoF(_f);
return _f;
case DHT22:
case DHT21:
_f = data[2] & 0x7F;
_f *= 256;
_f += data[3];
_f /= 10;
if (data[2] & 0x80)
_f *= -1;
if(S)
_f = convertCtoF(_f);
return _f;
}
}
return NAN;
}
float DHT::convertCtoF(float c) {
return c * 9 / 5 + 32;
}
float DHT::readHumidity(void) {
float _f;
if (read()) {
switch (_type) {
case DHT11:
_f = data[0];
return _f;
case DHT22:
case DHT21:
_f = data[0];
_f *= 256;
_f += data[1];
_f /= 10;
return _f;
}
}
return NAN;
}
bool DHT::read(void) {
uint8_t laststate = HIGH;
uint8_t counter = 0;
uint8_t j = 0, i;
unsigned long currenttime;
// pull the pin high and wait 250 milliseconds
digitalWrite(_pin, HIGH);
delay(250);
currenttime = millis();
if (currenttime < _lastreadtime) {
// ie there was a rollover
_lastreadtime = 0;
}
if (!firstreading && ((currenttime - _lastreadtime) < 2000)) {
//delay(2000 - (currenttime - _lastreadtime));
return true; // return last correct measurement
}
firstreading = false;
Serial.print("Currtime: "); Serial.print(currenttime);
Serial.print(" Lasttime: "); Serial.print(_lastreadtime);
_lastreadtime = millis();
data[0] = data[1] = data[2] = data[3] = data[4] = 0;
// now pull it low for ~20 milliseconds
pinMode(_pin, OUTPUT);
digitalWrite(_pin, LOW);
delay(20);
cli();
digitalWrite(_pin, HIGH);
delayMicroseconds(40);
pinMode(_pin, INPUT);
// read in timings
for ( i=0; i< MAXTIMINGS; i++) {
counter = 0;
while (digitalRead(_pin) == laststate) {
counter++;
delayMicroseconds(1);
if (counter == 255)
break;
}
laststate = digitalRead(_pin);
if (counter == 255)
break;
// ignore first 3 transitions
if ((i >= 4) && (i%2 == 0)) {
// shove each bit into the storage bytes
data[j/8] <<= 1;
if (counter > _count)
data[j/8] |= 1;
j++;
}
}
sei();
// check we read 40 bits and that the checksum matches
if ((j >= 40) && (data[4] == ((data[0] + data[1] + data[2] + data[3]) & 0xFF)))
return true;
return false;
}
#define DHTPIN D2 // Digital pin D2
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
float h; // humidity
float t; // temperature
int f = 0; // failed?
char Temp[5];
char Humid[5];
//char out[20];
void setup() {
dht.begin();
Serial.begin(9600);
Serial.println("started!");
Temp[0]='\0';
Humid[0]='\0';
// out[0] ='\0';
Spark.variable("Temp", Temp, STRING);
Spark.variable("Humid", Humid, STRING);
// Spark.variable("out", &out, STRING);
pinMode(D7, OUTPUT);
}
void loop() {
f = 0;
h = dht.readHumidity();
Serial.print("Humidity: ");
Serial.print(h);
t = dht.readTemperature();
Serial.print("Temp: ");
Serial.print(t);
if (t==NAN || h==NAN)
f = 1;
else
f = 0;
sprintf(Temp, "%f", t);
sprintf(Humid, "%f",h);
// sprintf(out, "%f:%f",t,h);
// if (f==1)
// digitalWrite(D7, HIGH);
// else
// digitalWrite(D7, LOW);
}
The get request is https://api.spark.io/v1/devices/48ff72065067555026242287/Humid?access_token=xxxxx
I have sent you a PM as well.
emc2
January 5, 2014, 8:51pm
4
Interesting - I had this working for all of 10 minutes before it started giving timeouts. Several flashes and edits later and I am still having trouble! Also tried renewing the access token.
emc2
January 5, 2014, 8:58pm
5
Now getting an error 400 on flash attempt. Maybe the system is just having a hiccup.
emc2
January 5, 2014, 9:14pm
6
Alright… so I got it working again, and now it’s failing again . The difference was in my sprintf
statements.
Fails:
sprintf(output, "{ temperature:%.2f, humidity:%.2f, fail:%i }", t, h, f);
Works:
sprintf(output, " %.2f", t);
Try using only 1 variable, or removing the colons in the string… does that change anything? I’ll be back for testing in a few hours.
Yep, only one variable worked for 2 days.
But I needed to get both values with one GET call to add to a database. That is when the issues started.
I tried it with colons, no colons, just spaces letters, but I guess there could be some issue with using two variables with the sprintf.
emc2
January 6, 2014, 2:10am
8
Yeah, that’s pretty strange. As inconvenient as it might be, could we work around this by making 2 GET calls instead of 1? I’m having a fair bit of trouble getting strings to place nicely… https://community.spark.io/t/strings-variable-types-manipulation-and-printing-how/1296
BDub
January 6, 2014, 2:24am
9
I did a lot of playing around trying to make it possible to return multiple variables in one string… and at the end of it all I determined that any string larger than 9 characters will cause your request to time out and your core to drop off the cloud. See here:
I have determined that when using the Spark.variable() for string lengths larger than 9 characters results in a timeout error. The following code example will set the variable output to a 9 character string for 10 seconds and turn off the onboard D7...
Reading time: 4 mins 🕑
Likes: 30 ❤
2 Likes