Open Source Hardware and The Solar Poem Machine: Programming Task 2 (Fork on Github Entry “Control a BeagleBone Black using Socket.IO”)

‪#‎solarpoemmachine‬ is a way to take anger at #‎makeamericagreatagain‬, and channel it into modern art for #‎OpKKK‬ and #OpPLATO.

Template (and here and on Github here) for how to make a Node.js webpage that interacts with Arabic smartphone users and American Beaglebone Black powered #‎solarpoemmachines‬.  The Github entry is something that I can fork to learn about how to work with Bonescript for the Beaglebone and connect it to other languages.  I like the idea of making the Beaglebone the brain of the #solarpoemmachine because it is cheap and open source.  It is very interesting the developments of open source hardware that are occurring in the computing world.  One interesting thing that was covered in this tutorial is the use of the Cloud9 IDE environment to run code in the cloud.  I learned about this at the Hack.Summit() with this talk on Heroku.

Heroku a cloud platform that runs finished code is:

Heroku is a cloud platform that lets companies build, deliver, monitor and scale apps — we’re the fastest way to go from idea to URL, bypassing all those infrastructure headaches.

Cloud9 an IDE for development is:

A cloud environment with an Ubuntu environment with 40 languages.

PRO
Provides with a simple way to deploy apps

Cloud9 allows quickly deploying apps via CLI. There are instructions on how to deploy to Azure, CloudFoundry, OpenShift, NodeJitsu, Modulus and Heroku. For example, all hosted environments have Heroku’s toolbelt installed by default so all heroku commands are available from the get-go.

 

PLEASE NOTE: This site is no longer being actively maintained. For frequently updated BeagleBone resources please visit Beaglebone.org.

Smart PhoneBeagleBone Black with expandable case (orange)

Overview

Bonescript is an easy way to get started with programming on the BeagleBone Black. This tutorial explains how to create a mobile web interface for your BeagleBone Black, using Javascript and Bonescript. You’ll be able to control the brightness of LEDs from a mobile device (cell phone, tablet) and, on supported phones, you can use motion to control the LEDs. This example uses node.js as the web server, socket.io for communication between the web page and the BeagleBone Black and Mobile JQuery for the web page lay-out.

What You Will Need

Steps To Follow

STEP 1

Install the WiFi adapter (tutorial)

STEP 2

Connect the 3 LEDs and 3 resistor to the BeagleBone (wiring diagram)

With a Network cable plugged-in we will install the socket.io library

STEP 3

First set the time (just to be sure)

/usr/bin/ntpdate -b -s -u pool.ntp.org

STEP 4

Install socket.io

cd /var/lib/cloud9
npm install socket.io

This takes a few minutes, wait until installation is complete.

STEP 5

Installing the Javascript and HTML code Find the tutorial and code on:https://github.com/lgxlogic/BoneScript-SocketIO

Bonescript Program Listing

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Creates a websocket with socket.io
// Make sure to install socket.io: terminal, goto /var/lib/cloud9 and enter: npm install socket.io
// Installing this takes a few minutes; make sure to wait until the installation is compelete
var app = require('http').createServer(handler);
var io = require('socket.io').listen(app);
var fs = require('fs');
var b = require('bonescript');
app.listen(8080);
// socket.io options go here
io.set('log level', 2);   // reduce logging - set 1 for warn, 2 for info, 3 for debug
io.set('browser client minification', true);  // send minified client
io.set('browser client etag', true);  // apply etag caching logic based on version number
console.log('Server running on: http://' + getIPAddress() + ':8080');
var ledRed = "P9_14";
var ledGreen = "P8_19";
var ledYellow = "P9_16";
// configure pins and set all low
b.pinMode(ledRed, b.OUTPUT);
b.pinMode(ledGreen, b.OUTPUT);
b.pinMode(ledYellow, b.OUTPUT);
b.analogWrite(ledRed,1);
b.analogWrite(ledYellow,1);
b.analogWrite(ledGreen,1);
function handler (req, res) {
  if (req.url == "/favicon.ico"){   // handle requests for favico.ico
  res.writeHead(200, {'Content-Type': 'image/x-icon'} );
  res.end();
  console.log('favicon requested');
  return;
  }
  fs.readFile('HtmlLedDemo.html',    // load html file
  function (err, data) {
    if (err) {
      res.writeHead(500);
      return res.end('Error loading index.html');
    }
    res.writeHead(200);
    res.end(data);
  });
}
io.sockets.on('connection', function (socket) {
  // listen to sockets and write analog values to LED's
  socket.on('ledRed', function (data) {
    b.analogWrite(ledRed, 1-(data/100));
//    console.log('Red: ' + data);
  });
  socket.on('ledGreen', function (data) {
    b.analogWrite(ledGreen, 1-(data/100));
//    console.log('Green: ' + data);
  });
  socket.on('ledYellow', function (data) {
    b.analogWrite(ledYellow, 1-(data/100));
//    console.log('Yellow: ' + data);
  });
  socket.on('mode', function (data) {
    console.log("Mode: " + data);
    // switch mode
  });
});
// Get server IP address on LAN
function getIPAddress() {
  var interfaces = require('os').networkInterfaces();
  for (var devName in interfaces) {
    var iface = interfaces[devName];
    for (var i = 0; i < iface.length; i++) {
      var alias = iface[i];
      if (alias.family === 'IPv4' && alias.address !== '127.0.0.1' && !alias.internal)
        return alias.address;
    }
  }
  return '0.0.0.0';
}
1

Download HtmlLedDemo .js File
Save this file as “HtmlLedDemo.js”

STEP 6

WebPage HTML listing

Start another new file (click on + tab) and copy and paste the code, which is available in this download here:
Download HtmlLedDemo .html File
Save this file as “HtmlLedDemo.html”

STEP 7

Now go to the .js file we created earlier and click the RUN button in the Cloud9 IDE In the console you’ll see the IP address of the BeagleBone Black on your network. Enter this IP address in a web browser on your cell phone or other device. Motion control only works on cell phones and tablets with modern browsers such as Google Chrome on Android.

STEP 8

Using the Smart Phone interface we can control the LEDs

Beaglebone Black Tutorial - Screen shot

This tutorial is also posted on the Logic Supply Blog

Advertisements

About kayaerbil

I am a Berkeley educated chemistry Ph.D. who is moving into the area of working on developing appropriate technology for communities that are subjected to socio-economic oppression. The goal is to use simple and effective designs to empower people to live better lives. Currently, I am working with Native Americans on Pine Ridge, the Lakota reservation in South Dakota. I am working with a Native owned and run solar energy company. We are currently working on building a compressed earth block (CEB) house that showcases many of the technologies that the company has developed. The CEB house is made of locally derived resources, earth from the reservation. The blocks are naturally thermally insulating, keeping the house cool in the summer and warm in the winter. Eventually, a solar air heater and photovoltaic panels will be installed into the house to power the home and keep it warm, while preserving the house off the grid. A side project while in Pine Ridge is a solar computer. I hope to learn about blockchain encryption software for building microgrids. In addition, it is an immediate interest of mine to involve local youth in technology education.
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s