Stuck at Local Communication example

I have successfully implemented the first three Annotated Examples but have become stuck at the Local Communication example. I have no idea how to set up the simple_server.js. Would you walk me through how to do this on a Mac running the Chrome browser?

1 Like

@Boulderwalker,

There are many ways to have a Local server so for this example, a node.js or ruby server is used.

You can do the following:

  1. Install Node.js on Mac http://nodejs.org/download/

  2. Once you are done, download the simple_server.js example
    https://github.com/spark/local-communication-example

  3. On your command line node /path/to/simple_server.js

  4. Using Spark Cloud IDE, flash the following firmware:

    https://github.com/spark/local-communication-example/blob/master/firmware.cpp

  5. Run the following command:

    curl https://api.spark.io/v1/devices/0123456789abcdef01234567/connect
    -d access_token=1234123412341234123412341234123412341234
    -d “args=IPADDRESS”

I must say this example is rather outdated and getting it doing that a little practice.

There’s a simpler way to test out local example…

2 Likes

Or you can try this example code:

https://gist.github.com/kennethlimcp/6add4d73594c3645edaa

If you just run a simple web server locally, try a html file. Replace the IP address in the code above and call ‘yourhtmlname.html’

<HTML>
   <HEAD>
      <TITLE>
         Local Server Example
      </TITLE>
   </HEAD>
<BODY>
   <P>Congrats for connecting successfully!</P> 
</BODY>
</HTML>

I opted for the HTML route. I made local.html, put your above 10 lines of of html code in it and modified the client example to point to local.html. I flashed the modified example to my core but nothing happened. When I opened local.html in my browser the page only says “Congrats for connecting successfully!” What did I do wrong?

Can I see what you changed in the code?

Also, You can see the output on serial for more info of what’s happening

This is what I flashed to the Core.

TCPClient client;
 
char server[] = "File://Mini09_HD/Users/boulderwalker/Local_Server/local.html";   //change to local path or ip address
 
void setup() {
  Serial.begin(9600);
  delay(3000);
  Serial.println("connecting...");
 
  // if you get a connection, report back via serial:
  if (client.connect(server, 80)) {
    Serial.println("connected");
    // Make a HTTP request:
    client.println("GET /search?q=sparkcore HTTP/1.1"); //Add in your own file path
    client.println("Host: www.google.com");
    client.println("Connection: close");
    client.println();
  } 
  else 
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
}
 
 
void loop() {
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
    }
  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop(); 
    } 
}

I’ve edited your post to properly format the code. Please check out this post, so you know how to do this yourself in the future. Thanks in advance! ~Jordy

Change this to your server/computer IP address

char server[] = “192.168.1.100”;

Change this to

It depends like what is the path to your local.html file. If you go to your server eg. 192.168.100 and to access the file it’s 192.168.100/xxx/yyyy/local.html. Then below you have to change to /xxx/yyy/local.html

client.println(“GET /local.html HTTP/1.1”); //Add in your own file path
client.println(“Host:YOUR IP ADDRESS”);

1 Like

Thanks, I got it to work.

1 Like

I see from this thread that I am not the only one who remains mystified by the “Local Server” example?

What I really want from sparkcore is a low cost WAN-connected micro-controller board - so understanding this example - and answering the question how do I find out what IP address my core has been allocated - is kinda key to that!

But I am really struggling. I think the cause of my confusion is that I don’t really understand how the eco-system it is using is laid out. It’s connecting to the core, but its connecting to a server - what server? there is no slot for me to add a server name or IP?- am I supposed to have a server set-up listening on port 9000 already? What’s a quick and dirty way to do that? and the “Connect” event/function - how do I initiate that - and to what?

Could be there is a wonderfully lucid and illustrated article to go with this example, but I haven’t found it. Can anyone lighten my darkness for me…?

In any case, IMO, that example on the intro page could do with a lot more comments in the code and an accompanying diagram illustrating the overall scheme of things AND some example commands (as in the other code examples) showing how to use curl or other easily available utilities to make it the entry-level learning tool that it aims to be.

Alan T

Hi @kennethlimcp

Good day to you :smiley:
I kinda of stuck at the local communication too. I have manage to get the nodejs server running and I have three cores connected to it. Therefore I have 3 local ip address for each of the core. I like to control the outpin pin independent of each core.

The ip of each core is
190.178.0.104
190.178.0.111
190.178.0.105

For example, I like to have

D0 of 190.178.0.104 (core 1) to be HIGH and D1 of 190.178.0.111(core 2) to be LOW. Is it possible to achieve that?

Looking forward to hear from you and thank you so much for your time

Bryan

@bosslee,

1.) Do you mean you have the Local :cloud: running?

2.) Do you have spark-cli installed? With that, you can easily control each core via the command line tool. For what you are doing, i presume you are using the default tinker app or wrote some Spark.function() to do that?

Yeah. I have the local cloud running.

Recently got a few spark cores, wanted to make a small project for home automation at home.
Plan is to use a pi as a hub (running nodejs server) talking to 3 other spark cores locally.

Yeah. I do have the spark-cli installed, but using spark-cli it will be using the wifi to control the spark core?
Would it be possible to control it locally?
I’m using the sample from here right now.

I would recommend using the tinker firmware instead since that example is more of TCP…

Since you have a local :cloud: running, you can use a user account and interact with your core via your local network.

Not sure if you have setup a local :cloud: using this: https://community.spark.io/t/tutorial-local-cloud-1st-time-instructions-30-nov-14/5589

but there’s instructions in there on how to create a “profile” for you to point to the local :cloud:.

Once that is done, you can use Spark-cli to flash the tinker app to the cores using spark flash core_name tinker.

With that, controlling them via Spark-cli is simply spark call digitalwrite "D0,HIGH" for example

Just to be more complete, Spark-cli can be used to interact with any spark :cloud: in general. Simply a switch of profile will do. :wink: