Pages: [1]   Go Down
Author Topic: Controlling Arduino with JavaScript in web browser  (Read 2331 times)
0 Members and 1 Guest are viewing this topic.
Sydney
Offline Offline
Full Member
***
Karma: 1
Posts: 108
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

ArduGate is an open-source web gateway software I am developing, for interfacing Arduino with JavaScript. It is actually made up of a serial-to-HTTP gateway (based on my miniweb project), some JavaScript routines performing communication with AJAX and an agent sketch running on Arduino.
It makes interaction between in-browser JavaScript and Arduino possible, and thus makes developing a up-stream application for Arduino that runs on any platform with a browser a lot easier.



You can find more information and download it on the project homepage.
Right now access to following Arduino APIs/objects are implemented:
  • pinMode
  • digitalRead/digitalWrite
  • analogRead/analogWrite
  • EEPROM (read/write)
  • Servo (attach/detach/read/write)
  • Wire

The key concept is using JavaScript inside the web browser to interact with Arduino. Here are several demostrating JavaScript code clips. You will find the code similar to that in an ordinary Arduino sketch.

Blinking Example
Code:
<html>
<body>
<script language="javascript" src="serial.js"></script>
<script language="javascript" src="arduino.js"></script>
<script language="javascript">
var port = "COM18";
var on = false;

self.setInterval(blink, 1000);

function Start()
{
    connected = SerialOpen(port, 57600);
    if (!connected) {
        alert("Error connecting with Arduino on " + port);
        return;
    }
    pinMode(13, OUTPUT);
    alert("Connected!");
}

function Stop()
{
    SerialClose();
    connected = false;
    alert("Disconnected");
}

function blink()
{
    if (connected) {
        digitalWrite(13, on ? HIGH : LOW);
    }
    on = !on;
}
</script>

<input type="button" value="Start" onClick="Start()" />
<input type="button" value="Stop" onClick="Stop()"/>
</body>
</html>

Accessing EEPROM
Code:
/* write a byte of 0x80 to EEPROM at address 0xA0*/
var address = 0xA0;
var data = 0x80;
if (EEPROM.write(addr, data))
{
/* success, now read back from EEPROM */
data = EEPROM.read(addr);
alert(data);
}
Controlling a servo
Code:
/* create servo object */
var myServo = new Servo();
/* attach servo to pin 3 */
myServo.attach(3);
/* set servo angle */
myServo.write(90);
/* get servo angle */
var angle = myServo.read();
alert("Current angle is " + angle);

« Last Edit: May 21, 2012, 12:38:47 pm by stanleyhuang » Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am unable to get the "ArduGate Demo" http://localhost:8880/ to communicate with my Arduino. The port box is empty. there are no com port choices to select from. When I try to send a command, I get a message from the webpage: Error sending command .  command: 0x10  code:0 Can anyone help me with this problem?
Logged

Pages: [1]   Go Up
Jump to: