2

I want to use Arduino Yun as a client, that connects to my nodejs server, receives a message from nodejs and then change led pin 13 on/off according to the message send.

I have this example of a nodejs server:

  var express = require('express');
var http = require('http');
var url = require('url');                       

var app = express();

//app.set('port', 8080);

var server = http.createServer(function(request, response){             
    console.log('Connection');                                                          var path = url.parse(request.url).pathname;                                 

    switch(path){                                               
        case'/':
            response.writeHead(200, {'Content-Type': 'text/html'});     
            response.write('Hello World');                          
            response.end();
            break;
        case '/arduino/digital/13/0':
            response.writeHead(200, {'Content-Type': 'text/html'});     
            response.write('Led 13 is off');                        
            console.log('Led 13 is off');
            response.end();
            break;
        case '/arduino/digital/13/1':
            response.writeHead(200, {'Content-Type': 'text/html'});
            response.write('Led 13 is on');
            console.log('Led 13 is on');
            response.end();
            break;
        default:
            response.writeHead(404);
            response.write("Oops, this doesn't exist - 404");   
            response.end();                                                     
            break;
    }

});

//server.listen(8080);

var server_port = process.env.OPENSHIFT_NODEJS_PORT || 8080
var server_ip_address = process.env.OPENSHIFT_NODEJS_IP || '127.0.0.1'

server.listen(server_port, server_ip_address, function () {
  console.log( "Listening on " + server_ip_address + ", server_port " + server_port )
});

The server is running on an openshift nodejs server - It's not Localhost!

Here is the Arduino sketch I use:

/*
 Possible commands created in this shetch:
 * "http://yunobomber-cuebo.rhcloud.com"                         -> Serial Monitor "Hello World"
 * "http://yunobomber-cuebo.rhcloud.com/arduino/digital/13/1"    -> Serial Monitor "Led 13 is on"  &  digitalWrite(13, HIGH)
 * "http://yunobomber-cuebo.rhcloud.com//arduino/digital/13/0"   -> Serial Monitor "Led 13 is off" &  digitalWrite(13, LOW)
 */

#include <Bridge.h>   
#include <HttpClient.h>

HttpClient client;

void setup() {
  Bridge.begin();                                                          // Bridge startup

  Serial.begin(9600);
  while (!Serial);                                                         // wait for a serial connection
}

void loop() {
  // Initialize the client library
  HttpClient client;

  // Make a HTTP request:
  client.get("http://yunobomber-cuebo.rhcloud.com");       // Insert alternative url for other commands - See explanation at top

  while (client.available()) {                             // Read message from server
    char c = client.read();
    Serial.print(c);
  }
  Serial.flush();
  delay(5000);
}

void process(HttpClient client) {
  String command = client.readStringUntil('/');

  if (command == "digital") {
    digitalCommand(client);
  }
}

void digitalCommand(HttpClient client) {
  int pin, value;

  // Read pin number
  pin = client.parseInt();

  // If the next character is a '/' it means we have an URL
  // with a value like: "/digital/13/1"
  if (client.read() == '/') {
    value = client.parseInt();
    digitalWrite(pin, value);
  }
  else {
    Serial.println('Error: Could not read url!');
  }
}

The serve works as it should, which can be seen here: http://yunobomber-cuebo.rhcloud.com

I'm also get the message send from the server, when I read serial monitor, so the connection to the server from the Yun is also working. I can also console.log on the server side and see that a connection is established from the Yun.

The problem seems to be with the readStringUntil(); method, what I want to do is actually readStringAfter(arduino/), but such a method doesn't exist or at least I'm not sure how to make one?

Also I wonder if my approach is wrong, since I use REST calls to control the Yun, maybe I should solve it diffenrently, since I've already established a connection to and receiving input from the nodejs server?

Hope that someone can help my understand and solve this issue?

Tommy Otzen
  • 123
  • 1
  • 8

0 Answers0