ECE497 Project: Node.js Weather Station

From eLinux.org
Revision as of 19:42, 18 February 2012 by Caogecym (talk | contribs) (Executive Summary)
Jump to: navigation, search


Team members: Yuming Cao, Ziyi Zhang

Executive Summary

Our project aims to implement the emerging nodejs technique (the server side javascript) as a lightweight web server on the beagleboard and accomplish a series of remote monitoring and control of hardware pins like gpio/led/i2c on the beagleboard. We also tried to make our web server send automatic email when temperature goes too high. There are a lot of things we can do on the nodejs web server, but we try to make it related to our beagleboard hardware interface.

The automatic email sender is not working correctly. More work needs to be done to set a proper configuration to the SMTP server 'postfix'.

End with a two sentence conclusion.

The sentence count is approximate and only to give an idea of the expected length.

Installation Instructions

1. Here's our github path that includes everything useful to our project: https://github.com/caoy1/Project.

2. Additional packages installed via opkg:

  • install nodejs
opkg update
opkg install nodejs
opkg install nodejs-dev
  • install node package manager
curl http://npmjs.org/install.sh | sh
  • install socket.io module
npm install socket.io
  • install binary module
npm install binary
  • install mailer module
npm install mailer
  • install SMTP server postfix

Go to [1] download the source of postfix, then unzip the .gz file, then run 'make', then run

sh postfix-install
  • Here's the guideline to install Cloud9 easily: [2]

User Instructions

Once everything is installed, how do you use the program? Give details here, so if you have a long user manual, link to it here.

Highlights

Here is where you brag about what your project can do.

Consider including a YouTube demo.

Theory of Operation

Give a high level overview of the structure of your software. Are you using GStreamer? Show a diagram of the pipeline. Are you running multiple tasks? Show what they do and how they interact.

To build a web server, we need, in essence, both client side and server side scripts. Client side scripts mainly just involve ordinary javascripts that can be embedded in HTML file; while at the server-side, we adopt the emerging node.js, which is a really simple server side script that accomplishes equivalent tasks like ASP, PHP will do, but with even simpler implementation details.

Work Breakdown

1. We've already successfully using JavaScript read and write data to the linux gpio file.

fs.writeFileSync("/sys/class/leds/beagleboard::usr0/trigger", "heartbeat");
fs.writeFileSync("/sys/class/gpio/export", ""+5);

2. Even though we can access to the gpio in the above way, we still need to find someway to access the I2C information. In the I2C exercises, from the C code provided in exercise 5, we can think of the following two options:

a) To translate tons of the C codes into JavaScript...including rewriting the union structures like i2c_smbus_write_byte() and i2c_smbus_read_byte(), which requires far more knowledge in understanding how to translate the underlying hardware detail into upper-level script languages...

b) Try to run the ./myi2c excutable file directly inside nodjs script...

  • It seems that the second way is easier... We've already found a ActiveXObject.run method... We're working on this... turns out this does not work for some reason.
  • At last we successfully excute the ./myi2c file inside our javascript using the following code, the big idea is create another child process to handle it:
var exec  = require('child_process').exec,
    child;
child = exec('./myi2c',
  function (error, stdout, stderr) {
    console.log('stdout:', stdout);
    console.log('stderr:', stderr);
    if (error !== null) {
      console.log('exec error:', error);
    }
});

And the terminal will show the temperature.

3. Now we are going to working on how to show this temperature information on the website so that we can visit it from any place!!!

  • I've successfully done it!!! The pics are already uploaded to the github. Check the following code:
var fs = require("fs");
var http = require("http");

try {
	http.createServer(function(request, response) {

		response.writeHead(200, {"Content-Type": "text/plain"});
		var exec = require('child_process').exec, child;
		child = exec('./myi2c',
	    		function (error, stdout, stderr) {
				response.write(stdout);
				console.log('stdout:', stdout);
				console.log('stderr:', stderr);
				response.end();
				if(error != null) {
					console.log('exec error:', error);
				}
			}
		);
	}).listen(8888);
} catch(ex3) {
	console.log("sb");
}

4. The next thing we will do is to refresh the website every other time...

  • We are now able to load html file inside nodejs scripts so that we can put normal HTML stuff inside the web page and load it from server scripts. The following code snippet can accomplish this (borrowed and modified based on Jadon's code):
function loadHTMLFile(uri, res, temp) {
 var filename = path.join(process.cwd(), uri);
 path.exists(
  filename,
  function(exists) {
   if(!exists) {
    res.writeHead(404, {"Content-Type": "text/plain"});
    res.write("404 Not Found\n");
    res.end();
    return;
   }

   fs.readFile(
    filename,
    encoding='utf8',
    function(err, file) {
     if(err) {
      res.writeHead(500, {"Content-Type": "text/plain"});
      res.write(err + "\n");
      res.end();
      return;
     }
     res.writeHead(200, {"Content-Type": "text/html"});
     var str = ("" + file).replace("<!--%OUTPUT%-->", temp);
     res.write(str);
     res.end();
    }
   );
  }
 );
}

And outside the above function, the main routine, we have similar code as listed in 3, but insert the function call:

child = exec('./myi2c',
	    		function (error, stdout, stderr) {
				console.log('stdout:', stdout);
				console.log('stderr:', stderr);
				loadHTMLFile('/index.html', response, stdout);
				if(error != null) {
					console.log('exec error:', error);
				}
			}
		);
  • Now we can update the temperature information on the website, both triggered by a button on the page, and by just automatically refreshing the temperature value whenever the temperature changes. Here is the simple client script for accomplishing this:
<html>
<head>
<script language="javascript">
setInterval("document.forms[0].submit()",5000);
</script>
</head>
<body>
<center>
<h1>Welcome to the world of Node.js</h1>

<p>Here is the demo of the weather station.</p>
<h2>Current temperature:</h2>
<pre>
<span id="temperature output"><!--%OUTPUT%--></span>
<form action = "/test.js" method="post">
<input type="submit" value="get temperature">
</form>
</center>
</body>
</html>

5. Now we are trying to do something fancy! We want to send the user an automatic warning email when the temperature is too high. Here's the Javascript code using node.js mailer module. This code use SMTP server to send email to the destination.

var email = require('mailer');

email.SMTP = {
    host: 'smtp.gmail.com',
    port: 587,
    ssl: false,
    use_authentication: false,
}

email.send({
    to : "caoy1@rose-hulman.edu",
    from : "obama@whitehouse.gov",
    subject : "I love beagleboard!",
    body: "Hello beagle world.",
}, 

function(err, result) {
    if(err) {
	console.log(err);
    }
});
  • However, the configuration on our beagle has some problem. The server has no response to mailer's request. We've tried telnet localhost 587, and connection can be connected. This means the 587 port is already opened. Then I tried the same code under my ubuntu system, it turns out everything works fine, the caoy1@rose-hulman.edu can receive the e-mail, except some client-side warning error when running the mail.js file. I think the reason it works on ubuntu is because when installing postfix, there's an automatic configuration step, which doesn't exist on our beagle installation. So there must be some problem with the postfix configuration of beagleboard. I've tried to copy all the configuration files under /etc/postfix to our beagleboard, but there's just the same problem. If I have more time, or someone else who's interested in continuing this project, I suggest you have some research on the configuration thing. Everytime you make some config changes, just run
postfix reload

to apply the change.

Conclusions

Give some concluding thoughts about the project. Suggest some future additions that could make it even more interesting.