Spark code. The "Spark.variable" code is used to push these variables up into the cloud so they can be monitored using any available dashboard service. I'm sending three different types of packets from the Moteino to the Spark: 1) THP - temp, humidity, pressure, 2) - WND - windspeed in MPH, 3) ELC - electrical, voltage and current for the LIPO, Solar panel and the Moteino. I'm using a C union on the sending side so I only send one data type and the Spark end figures it out by the first token in the string. Not my most elegant coding but it seems to work OK.
// Global variables
char buf[64]; // buffer for incoming data
double temp=0,
humidity=0,
pressure=0,
windspeed=0,
lipoVoltage=0,
lipoCurrent=0,
solarVoltage=0,
solarCurrent=0,
loadVoltage=0,
loadCurrent=0;
void setup()
{
Serial1.begin(115200);
Spark.variable("temp", &temp, DOUBLE);
Spark.variable("humidity", &humidity, DOUBLE);
Spark.variable("pressure", &pressure, DOUBLE);
Spark.variable("windspeed", &windspeed, DOUBLE);
Spark.variable("lipoVoltage", &lipoVoltage, DOUBLE);
Spark.variable("lipoCurrent", &lipoCurrent, DOUBLE);
Spark.variable("solarVoltage", &solarVoltage, DOUBLE);
Spark.variable("solarCurrent", &solarCurrent, DOUBLE);
Spark.variable("loadVoltage", &loadVoltage, DOUBLE);
Spark.variable("loadCurrent", &loadCurrent, DOUBLE);
}
void loop()
{
int x, avail;
char *token;
const char sep[2] = ",";
if ((avail = Serial1.available()) >= 20) // wait until we get a full buffer
{
for (x=0; x < avail; x++)
buf[x] = Serial1.read();
buf[x] = '\0'; // string must be null terminated
token = strtok(buf, sep); // pick off first token = packet type
if (token != NULL)
{
if (strncmp(token, "WND", 3) == 0) // windspeed packet
{
token = strtok(NULL, sep);
if (token != NULL)
windspeed = (float) atof(token); // extract windspeed
Serial1.print("wind");
}
else if (strncmp(token, "THP", 3) == 0) // temperature, humidity, pressure packet
{
token = strtok(NULL, sep);
if (token != NULL)
temp = (float) atof(token); // extract temperature
token = strtok(NULL, sep);
if (token != NULL)
humidity = (float) atof(token); // extract humidity
token = strtok(NULL, sep);
if (token != NULL)
pressure = (float) atof(token); // extract pressure
}
else if (strncmp(token, "ELC", 3) == 0) // electrical packet
{
token = strtok(NULL, sep);
if (token != NULL)
lipoVoltage = (float) atof(token); // extract lipoVoltage
token = strtok(NULL, sep);
if (token != NULL)
lipoCurrent = (float) atof(token); // extract lipoCurrent
token = strtok(NULL, sep);
if (token != NULL)
solarVoltage = (float) atof(token); // extract solarVoltage
token = strtok(NULL, sep);
if (token != NULL)
solarCurrent = (float) atof(token); // extract solarCurrent
token = strtok(NULL, sep);
if (token != NULL)
loadVoltage = (float) atof(token); // extract loadVoltage
token = strtok(NULL, sep);
if (token != NULL)
loadCurrent = (float) atof(token); // extract loadCurrent
}
}
}
}