Go Down

Topic: Controlling Arduino with JavaScript in web browser (Read 2647 times) previous topic - next topic

stanleyhuang

May 21, 2012, 11:29 am Last Edit: May 21, 2012, 07:38 pm by stanleyhuang Reason: 1
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: [Select]
<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: [Select]
/* 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: [Select]
/* 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);


My Arduino blog: http://arduinodev.com/blog

mnelson07860

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?

Go Up