Spark Core + Plotly

A simple Spark Core and Plotly sample. I created this sample application using the official Plotly SDK from GitHub and the plotly_streaming_wifi source. This is a simple port, only 2 modifications are done. 1. Copied the method dtostrf from this location and included in Plotly class and 2. Commented the method void print_(const __FlashStringHelperd)* because this method was causing some compiler errors (I didn’t dig deep into it, just commented the method).

Hope this will help to get started.

I have tried hard to get this to work and it seems like it wants to but it just simply stalls after it gets the “All Streams Go!” response. Any Idea’s would be welcome. Here is the Serial output at full logging:

    ... Attempting to connect to plotly's REST servers
... Connected to plotly's REST servers
... Sending HTTP Post to plotly
POST /clientresp HTTP/1.1
Host: 107.21.214.199
User-Agent: Arduino/0.5.1
Content-Length: 243

version=2.2&origin=plot&platform=arduino&un=redacted&key=redacted&args=[{"y": [], "x": [], "type": "scatter", "stream": {"token": "redacted", "maxpoints": 30}}]&kwargs={"fileopt": "overwrite", "filename": "test2", "world_readable": true}
... Sent message, waiting for plotly's response...
HTTP/1.1 200 OK
Server: nginx/1.4.6 (Ubuntu)
Date: Fri, 27 Jun 2014 21:13:56 GMT
Content-Type: text/html; charset=utf-8
Transfer-Encoding: chunked
Vary: Accept-Encoding

ba
{"stream-host": "http://stream.plot.ly", "stream-status": "All Streams Go!", "url": "http://107.21.214.199/~redacted/4", "filename": "test2", "warning": "", "error": "", "message": ""}
0

Any one have an Idea on this?

@ccrumpacker, I sorry for the late reply, I somehow missed your post.

I think “All Streams Go!” is a normal status and the data point should be added there, can you please check the stream name and filename is correct?

I had a different problem, but since you’re here Krvarma, I thought I’d ask here. I added Plotly_SparkCore to my project, but after flashing from the web, I can no longer connect via serial. It seems the device is no longer detected by my OS at all. But it is not completely hosed. The led is breathing cyan and the spark Android app allows me to turn D7 on and off and see the LED react appropriately.

Has anyone seen anything like this?

Thanks.
-Casten

Hi @Casten

When things go very wrong on the core, the firmware defaults to running the last good app, which could be Tinker the default app that lets you interact with your core via your phone. If you are running Tinker, you are not running the Plotly app anymore.

You could try re-flashing it and watch the LED closely to see what happens. You might see flashing red SOS, a number of flashes and then SOS again if there is a big problem with the code you are trying to run.

Thanks @bko, thinking back, I do recall seeing some red flashing going on, but since it was doing something, I didn’t pay attention. Rookie move I guess. I’ll not ignore that in the future.

I’ll trim down the code until it works, but the new code I added isn’t active until I send a command, so I can’t imagine it is the actual execution that is causing the problem.

Is it possible that the code is too large, or would the web IDE warn about that before it sends down the binary? I’m just trying to figure out the most efficient next steps for diagnosing a problem with the code that is either a flash or init-time problem. Are there any debugging solutions for spark core?

Thanks again for your sage advice!

-Casten

Ok, I’m not sure what happened there, but it eventually started working. I’ve seen a little flakiness like this with some other sketches I’ve uploaded.

I’m seeing some other issues , but I’ll have a look around and see if I can find info on other threads.

Thanks again, I’m getting close to where I want to be!

-Casten

Does this stream using the SparkCore API, and Plot.ly listens to that stream? Just trying to wrap my head around how to publish a Stream from the SparkCore. Any advice would be helpful.

@Casten, I think I unnecessary declared szInfo as array of 64 character length, can you try reduce the size to 32 and try working?

@pzula, it opens a TCP connection to plot.ly on port 80 and post the JSON data.

I’ve double and triple checked. Still just hangs on the same place I mentioned in my debug copy above. I worked around this by continuing to use thingspeak (which is better for streaming in my opinion) to update air quality and temp/humidity data from the spark then I have have a few python scripts running from a RPi that mine the Spark data from thingspeak and combine it with data from my WiFi thermostat (that has a RESTful API Woot!) and also weather station for outside Temps.

1 Like

any news on this?
I too have the red sos.

@gerrit, can you reduce the szInfo to say 24 or something?

I had the same problem. During the initial response parsing code in plotly::init there is a loop on while((client.connected()) meaning the server will need to close the connection before the code will continue - but the plotly server keeps the connection open after sending the response.

I changed:

while(client.connected()){
    if(client.available()){

to:

while((client.connected()) && (client.available())){

(and removed the extra closing brace near the end of the parsing code) in order to make it work.

1 Like

Hi, I’m trying this method, but i’m taking error messages; i don’t have an idea.

In file included from ../inc/spark_wiring.h:29:0,
from ../inc/application.h:29,
from dht.h:1,
from dht.cpp:1:
../../core-common-lib/SPARK_Firmware_Driver/inc/config.h:12:2: warning: #warning "Defaulting to Release Build" [-Wcpp]
#warning "Defaulting to Release Build"
^
In file included from dht.cpp:1:0:
dht.h:14:0: warning: "NAN" redefined [enabled by default]
#define NAN 999999
^
In file included from dht.h:2:0,
from dht.cpp:1:
/opt/gcc_arm/arm-none-eabi/include/math.h:57:0: note: this is the location of the previous definition
# define NAN (__builtin_nanf(""))
^
In file included from ../inc/spark_wiring.h:29:0,
from ../inc/application.h:29,
from plotly.cpp:2:
../../core-common-lib/SPARK_Firmware_Driver/inc/config.h:12:2: warning: #warning "Defaulting to Release Build" [-Wcpp]
#warning "Defaulting to Release Build"
^
In file included from plotly.cpp:5:0:
dht.h:14:0: warning: "NAN" redefined [enabled by default]
#define NAN 999999
^
In file included from dht.h:2:0,
from plotly.cpp:5:
/opt/gcc_arm/arm-none-eabi/include/math.h:57:0: note: this is the location of the previous definition
# define NAN (__builtin_nanf(""))
^
In file included from plotly.cpp:11:0:
dht.h:16:7: error: redefinition of 'class DHT'
class DHT {
^
In file included from plotly.cpp:5:0:
dht.h:16:7: error: previous definition of 'class DHT'
class DHT {
^
plotly.cpp:22:51: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]

^
plotly.cpp:23:89: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
#define NUM_TRACES 1
^
plotly.cpp:23:89: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
plotly.cpp:23:89: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
make: *** [plotly.o] Error 1

@trshark, please add "application.h" and try.

OK - every time I write this to the core, it updates, then shows a happy breathing cyan. However: no output, a serial connection shows nothing, and unable to reflash it. I can only hard reset it and start over. Any pointers???

#include "myplotly.h"
#include "SHT1x/SHT1x.h"

// Specify data and clock connections and instantiate SHT1x object
#define dataPin  D0
#define clockPin D1
#define NUM_TRACES 1
SHT1x sht1x(dataPin, clockPin);
char szInfo[24];  //changed from 64

char *streaming_tokens[NUM_TRACES] = {"token"};
plotly graph = plotly("user_name", "another_token", streaming_tokens, "FileName", NUM_TRACES);

// Publush event
void Publish(char* szEventInfo){
    delay(20000); //delay of 20 seconds
    Serial.println("Entering Publish set");
    Spark.publish("plotlyinfo", szEventInfo);
}

void setup() {
    Serial.begin(9600); // Open serial connection to report values to host
    delay(60000);  //delay of 1 minute
    Serial.println("Entering initial setup");
    graph.init();
    Serial.println("Entering setup after graph.init");
    graph.openStream(); 
}

void loop() {
    float temp_f;    // int temp_f = 0;
    //float humidity;  // int humidity = 0
    //float t = dht.readTemperature();
    temp_f = sht1x.readTemperatureF();
    
    Serial.println("Entering void loop");
    Serial.println(temp_f, 0);
    sprintf(szInfo, "Temperature=%.2f F", temp_f);
    graph.plot(millis(), temp_f, streaming_tokens[0]);
    Publish(szInfo);
    delay(60000);  //delay of 1 minute
}

@h2ogood, are you seeing this serial output Serial.println(“Entering setup”);. Maybe somewhere it is stuck and not entering the loop, may be.

Can you put Serial Print between graph.init() and graph.openStream() and see it is displaying?

Thanks @krvarma! Originally, I was not seeing anything. I’ve updated the code above to reflect a few changes. Nothing major, and strangely the Core is connected and I can at least send it firmware updates… and now see the following output:

 Entering initial setup
... Attempting to connect to plotly's REST servers
... Connected to plotly's REST servers
... Sending HTTP Post to plotly

…and there it sits, locked up (again). It looks like it’s hung at graph.init();
I saw someone had adjusted a loop within one of the libraries. ?? I’ll chew on this post rest. Thanks for any thoughts/input/insight.