Reputation: 31
I am trying to control and receive data from my arduino over the web by using the serial port (serial=USB. I don't want to use Ethernet shield as I'm connecting the USB to an XBEE device).
After researching, I seem to believe the best approach will be javascript (PHP seems to be problematic with reading data from the arduino over serial).
Currently I'm trying to interface with the arduino using Processing.
I began with a simple script to read sensor data off the arduino which works fine when ran from processing (Java). when I try to export to view through a web browser (by using javascript in processing) I see everything in the browser except for the sensor data itself.
My understanding (more like a guess) is that for some reason the COM port will not open when called from the web browser (maybe security issue?). The same phenomena happens on both PC (WIN7) and MAC (OSX).
Help will be much appreciated!
Here is the code: (comments removed)
import processing.serial.*;
Serial myPort;
int xPos = 1;
void setup () {
size(400, 300);
println(Serial.list());
myPort = new Serial(this, Serial.list()[0], 9600);
myPort.bufferUntil('\n');
background(0);
}
void draw () {
}
void serialEvent (Serial myPort) {
String inString = myPort.readStringUntil('\n');
if (inString != null) {
inString = trim(inString);
float inByte = float(inString);
inByte = map(inByte, 0, 1023, 0, height);
stroke(127,34,255);
line(xPos, height, xPos, height - inByte);
if (xPos >= width) {
xPos = 0;
background(0);
}
else {
xPos++;
}
}
}
Upvotes: 3
Views: 1227
Reputation: 1086
Have you looked at using something like ser2net or Windows Serial Proxy? Run that on the machine to proxy the serial port to a TCP port, then in Javascript, use WebSockets to talk to it.
Some sort of server layer might be the best solution, but if you're intent on doing it all in JS, I think this is the way to go.
Upvotes: 1
Reputation: 51837
As Drew R points out you could whip up a quick 'server' application that reads Arduino data via Serial but then broadcasts it via HTTP to what you need.
I recommend also having a look at cosm which might do a bit of that work for you and provides a nice js API for the Arduino devices you build feeds off. Also have a look at their examples.
Upvotes: 1
Reputation: 3088
This may be dodging the issue but it sounds like you need to implement a layer of separation between the device and the HTTP service.
You could write a basic HTTP service using node.js or something that sits on the PC and arbitrates requests for data so that the request is not being made directly to the device from the browser.
It makes sense that a browser would strictly control client side JS access to hardware interfaces, if that is indeed what you're attempting.
Upvotes: 1