SparkFun + imp - How to push data to my localhost


#1

I have phant installed in my local machine for testing at http://127.0.0.1:8080/

And I have tried to send my data from imp ide to http://127.0.0.1:8080/ but it does not seem to work at all.

I can see that imp is sending data in the log:

2016-06-19 19:52:49 UTC+8 [Agent] Sending particles=0 2016-06-19 19:52:50 UTC+8 [Agent] Phant response: 2016-06-19 19:53:17 UTC+8 [Agent] Sending particles=0 2016-06-19 19:53:17 UTC+8 [Device] Sending Data! 2016-06-19 19:53:17 UTC+8 [Device] particles: 0 2016-06-19 19:53:18 UTC+8 [Agent] Phant response: 2016-06-19 19:53:46 UTC+8 [Device] Sending Data! 2016-06-19 19:53:46 UTC+8 [Device] particles: 0 2016-06-19 19:53:46 UTC+8 [Agent] Sending particles=0 2016-06-19 19:53:47 UTC+8 [Agent] Phant response:

But there is no data receiving on http://127.0.0.1:8080/

Below is my code in imp ide:

///////////////// // Phant Stuff // ///////////////// local publicKey = "xxx"; // Your Phant public key local privateKey = "xxx"; // The Phant private key local phantServer = "127.0.0.1:8080"; // Your Phant server, base URL, no HTTP

/////////////////////
// postData Action //
/////////////////////
// When the agent receives a “postData” string from the device, use the
// dataString string to construct a HTTP POST, and send it to the server.
device.on(“postData”, function(dataString) {

server.log("Sending " + dataString); // Print a debug message

// Construct the base URL: https://data.sparkfun.com/input/PUBLIC_KEY:
local phantURL = "http://" +  phantServer + "/input/" + publicKey;
// Construct the headers: e.g. "Phant-Priave-Key: PRIVATE_KEY"
local phantHeaders = {"Phant-Private-Key": privateKey, "connection": "close"};
// Send the POST to phantURL, with phantHeaders, and dataString data.
local request = http.post(phantURL, phantHeaders, dataString);

// Get the response from the server, and send it out the debug window:
local response = request.sendsync();
server.log("Phant response: " + response.body);

});

Any ideas why and what I have missed?


#2

Yes, the imp does not talk locally, it’s all via the cloud (you’ll note that all the http functions are only available in the “agent” - the VM running in the cloud - and not on the device).

Your server is not visible to the imp cloud hence you can’t send data directly to it (unless you expose it on your externally visible IP, in which case it’ll work).

Local networking will be coming along later this year, though not in the same form as you see it in the agent.


#3

Don’t forget that your agent code runs on our servers. When you try to connect to “the local machine” at 127.0.0.1 from agent code, that refers to “the local machine” that your agent code is running on – namely, one of our cloud servers. So that won’t achieve anything useful.

Instead, you should make sure that your Phant server is accessible from the rest of the Internet (perhaps by setting up port forwarding, if it’s behind a router), and then provide your agent code with the externally-visible IP address.

Peter