Example Stock Ticker Http In/Out

Attached are device and agent code which demonstrate calling out to a web service, handling an incoming http request, and updating the device. The application will use the Emma to display stock values retrieved from the Yahoo! Finance site.

The http.onrequest input handler will parse incoming requests, which send Stock Symbols to persist and check each cycle (60 seconds). In addition, sending a 0 will clear the symbols, and 1 will request a set of quotes.

A timer will call http.get to get stock quotes every 60 seconds. The agent will call device.send() to send the Emma the stock symbols and quotes to display.

The code shows the usage of http.onrequest, http.get, device.send.

I am still looking for an example to get some data from a website onto the IMP (I am new)
I would like to know if I can use the above example with my sparkfun impee.
Also, what I dont understand, is the two code parts.
So the first one is the code as in one of my planner codes associated to my imp node?
Do I need to have additional nodes (like http in and out) set up on the planner for this code to work?
And what is the second (agent?) code? DO I need that for the first piece of code to work?

This code is made for the beta firmware
First listing is the agent code, second is device code.
I have modified the code, so you can run it without the hardware
You don’t use nodes anymore

Agent:

tickers <- ["AAPL", "YHOO", "GOOG", "CHRM"]; //tickers <- ["GOOG"]; //tickers <- []; CanUpdate <- true http.onrequest(function(request,res){ server.log("http.onrequest called (in Agent)"); server.log("*** body: " + request.body +" (in Agent)"); if (request.body == "0"){ //clear server.log("*** clears tickers (in Agent)"); CanUpdate = false quotes <- {}; } else if (request.body == "1"){ // refresh now CanUpdate = true server.log("*** enable Quotes (in Agent)"); getQuotes(); } else { local symbols = split(request.body, ","); server.log("*** add Quotes "+ request.body +" ( in Agent)"); foreach (s in symbols){ tickers.push(s); } getQuotes(); } //res.send(200, "received"); res.send(200, request.body); }); function getQuotes(){ server.log("function getQuotes called (in Agent)"); local url = "http://download.finance.yahoo.com/d/quotes.csv?s="; foreach (symbol in tickers){ url += symbol+"+"; } url += "&f=sl1"; server.log("****** String passed to download.finance.yahoo.com ******"); server.log(url); local response = http.get(url,{}).sendsync(); // response comes back like this: "Apple Inc.",439.41,439.39 "Google Inc.",753.88,753.68 "Microsoft Corpora",27.89,27.88 server.log("****** response fom download.finance.yahoo.com (in Agent) ******"); server.log(response.body ); server.log(""); local parse = split(response.body, "\ "); foreach (element in parse){ local stock = split(element,","); //server.log(stock[0]); //server.log(stock[1]); local ticker = stock[0]; local price = stock[1]; //if (stock[0]=="GOOG"){ //output.set(stock[1]); //} ticker = ticker.slice(1,ticker.len()-1); // get rid of " price = split(price, ".")[0]; // get rid of . quotes[ticker] <- price; } } function update(){ server.log("*** Function update called (in Agent)"); server.log("*** CanUpdate = " + CanUpdate ); if (CanUpdate){ getQuotes(); imp.wakeup(60.0,update); } } function send(){ server.log("Function send called (in Agent)"); // iterate through each current symbol and display for 5 seconds foreach (key, value in quotes){ device.send("display", key+" "+value); server.log("** Sending: " + key + " " + value +" (in Agent)"); //imp.sleep(5.0); } // repeat the cycle every 5 seconds imp.wakeup(10.0,send); } server.log("*************************************************************"); server.log("access me via: "+ http.agenturl()); server.log("*************************************************************"); update(); send();

Device:

`// Simple Stock Ticker

server.log(“Simple Stock Ticker Started (in Device)”);

local currentString = “”;
output <- OutputPort(“Google”, “number”);
function set(inputString) {
server.log(“function set called (in Device)”);
inputString = inputString.tostring();
inputString = inputString.toupper();
server.log(format("** Received %s (in Device)",inputString));
currentString = inputString;
}
imp.configure(“Simple Stock Ticker”, [], [output]);
set(“set example”);

agent.on(“display”,function(msg){
server.log(“agent.on called (in Device)”);
set(msg);
server.log("* msg = " + msg);
});`

Where do I put those two code parts? On the IDE there is a single code file editor?

I just saw the two dropdowns, but how do I create a new project?

Wow just found that, interesting interface setup…

@uncleunvoid - have you got it sorted out?

If you haven’t already, I suggest you take a look at the IDE guide (linked to in your beta invitation email) - it goes through where to put agent vs device code, how to create new projects, etc :slight_smile:

I will start testing the stock ticker code tonight.

Copied code in editor, now my IMP doesnt connect anymore and the logger window shows zero activity even when I run the code (run)

You may need to push-push (take your imp out of the socket, leave wait a couple seconds, then re-insert it) - alternatively, power off the imp for a few seconds and then power it back on.

I went back cleared my IMP and all nodes off the old planner, so I am starting afresh.
Work still might not have opened port 31314 for me, so I am playing the waiting game. Lets see tonight if it re-connects,. I really want to start playing with this thing.

OK home network that worked before does not work anymore.

reset my whole network, got green light occasionally, but no imp showing up in planner

@dolftraanberg : Tried your code on a sparkfun impee and got this :

Tue May 21 2013 08:18:32 GMT+0100 (BST): the index ‘quotes’ does not exist
Tue May 21 2013 08:18:32 GMT+0100 (BST): at getQuotes:54
Tue May 21 2013 08:18:32 GMT+0100 (BST): from update:62
Tue May 21 2013 08:18:32 GMT+0100 (BST): from main:79

any idea whats going on?

sorry, I made a copy/paste error.
before:
tickers <- [“AAPL”, “YHOO”, “GOOG”, “CHRM”];

put this lines:
// sending a 0 will clear all tickers
// sending a 1 will get quotes
// sending anything else will be interpreted as a stock ticker symbol to add
// ie curl -d “FB” “External URL”

quotes <- {};

Cool that got it working, so now its up to me to get the thing working with a bespoke JSON

Took me some time to figure out how this works, but got it working now!

cheers,
Guus