Example: Simple wireless control of Arduino LED - nilakshdas/baudcast GitHub Wiki

In this example, an Arduino is connected to a system running the baudcast server which commands the Arduino via the serial port.

Arduino sketch

void setup() {
	Serial.begin(9600);

	pinMode(13, OUTPUT);
	digitalWrite(13, LOW);
}

void loop() {
	if (Serial.available()) {
		char val = Serial.read();

		if (val == '1') digitalWrite(13, HIGH);
		else if (val == '0') digitalWrite(13, LOW);
	}
}

Server code

This example uses voodootikigod / node-serialport to access the Arduino's serial port.

var baudcast = require('baudcast')();
var SerialPort = require('serialport').SerialPort;

// Enter the correct COM port here
var serialPort = new SerialPort('/dev/ttyACM0', {baudrate: 9600});

baudcast.from('webclient', function(data) {
	if (data.content.led !== undefined) {
		if (data.content.led) serialPort.write('1');
		else serialPort.write('0');
	}
});

Client script

The web client is also served by the server running baudcast.

<input type="checkbox" name="sw" id="led" onchange="switch(this.checked);">
ON/OFF

<script src="https://cdn.socket.io/socket.io-1.0.4.js"></script>
<script type="text/javascript">
	// Enter the correct local IP of the server here
	var socket = io('http://192.168.x.x:8888');

	function switch(value) {
		socket.emit('baudcast', 'webclient', {led: value});
	}
</script>

Watch it in action

Click to watch the video

⚠️ **GitHub.com Fallback** ⚠️