I can’t for the life of me figure this out… I can’t get this to compile.
I based my code on https://github.com/synox/Spark-Busstop-Light/blob/master/spark_core/application.cpp where @Coffee uses an enum. Even though he includes application.h at the top that I don’t have, when I paste the code into the Web IDE, it compiles just fine.
However, when I munged the code for my purposes, I get
../57de2889598ee51f77455b1d66f1ecf0703bb94ca54721b3998679a9f9ab/the_user_app.cpp:3:16: error: variable or field 'updateLED' declared void
../57de2889598ee51f77455b1d66f1ecf0703bb94ca54721b3998679a9f9ab/the_user_app.cpp:3:16: error: 'Status' was not declared in this scope
../57de2889598ee51f77455b1d66f1ecf0703bb94ca54721b3998679a9f9ab/the_user_app.cpp:4:1: error: 'Status' does not name a type
make: *** [../57de2889598ee51f77455b1d66f1ecf0703bb94ca54721b3998679a9f9ab/the_user_app.o] Error 1
I’m guessing this is related to the FAQ in http://playground.arduino.cc/Code/Enum. But why does it work for the code I based it on?
//#include "umbrella3.h"
//Originally based on https://github.com/synox/Spark-Busstop-Light/blob/master/spark_core/application.cpp
// ------------- configuration ------------
#define DEBUG true
// possible status list:
enum Status {
clear=0, rain=1, snow=2, thunder=3
};
const char* host = "myhiddenhost.no-ip.com"; //redacted
int port = 80;
const char* query = "/rain";
int led = D7;
long lastTsMilis = 0;
unsigned int nextTime = 0; // next time to contact the server
// Support functions
void updateLED(Status stat) {
switch(stat) {
case clear: RGB.color(0,0,10); break; // dimmed blue
case rain: RGB.color(255,0,0); break; // red
case snow: RGB.color(255,50,0); break; // orange
case thunder: RGB.color(255,150,0); break; // yellow
}
return;
}
Status parseIt(String jsonData) {
int offset = 0;
do {
offset = jsonData.indexOf("rain:", offset);
if(DEBUG) Serial.print("offset: ");
if(DEBUG) Serial.println(offset);
if (offset == -1) {
break;
}
offset += 6; // move to result value
String str = jsonData.substring(offset, offset + 1);
if(DEBUG) Serial.print("result: ");
if(DEBUG) Serial.println(str);
if(str.length() == 0) {
continue;
}
//return (Status) str.toInt();
switch (str.toInt()) {
case 0: return clear; break;
case 1: return rain; break;
default: return thunder; break;
}
} while (offset >= 0);
return thunder;
}
// ------------- HTTP functions --------------
//make http request and return body
TCPClient client;
char buffer[512];
String http_get(char const* hostname, int port, String path) {
if (client.connect(hostname, port)) {
client.print("GET ");
client.print(path);
client.print(" HTTP/1.0\n");
client.print("HOST: ");
client.println(hostname);
client.print("\n");
client.flush();
} else {
Serial.println("connection failed");
client.stop();
return NULL;
}
// Block until first byte is read.
client.read();
for (unsigned int i = 0; i < sizeof(buffer) && client.available(); i++) {
char c = client.read();
if (c == -1) {
break;
}
buffer[i] = c;
}
client.stop();
String response(buffer);
int bodyPos = response.indexOf("\r\n\r\n");
if (bodyPos == -1) {
Serial.println("can not find http reponse body");
return NULL;
}
return response.substring(bodyPos + 4);
}
// Setup
void setup() {
Serial.begin(9600);
pinMode(led, OUTPUT);
RGB.control(true);
RGB.color(0,0,10); // dimmed blue
}
//Looooooooooop forever
void loop() {
void updateLED(Status);
enum Status {
clear=0, rain=1, snow=2, thunder=3
};
if (nextTime > millis()) {
// keep the same color while waiting
return;
}
digitalWrite(led, HIGH);
delay(200);
digitalWrite(led, LOW);
// the timestamp is checked online every 5 min
if(lastTsMilis == 0 || millis() - lastTsMilis > 5 * 60 * 1000 ) {
Serial.println("local time is old, refreshing");
// refresh time online
lastTsMilis = millis();
}
String resp = http_get(host, port, query);
updateLED(parseIt(resp));
// check the color again in 5 seconds:
nextTime = millis() + 5000;
}
I haven’t cleaned this up any or even really thought about the logic, I’m just trying to get this to compile in the Web IDE so I can get to the next step.