This example could be considered as Hello World of the CPS&IoT. The example demonstrates the basic functionality of arduino/node.js combination where by the request/response mechanism the LED is switched ON/OFF. The main idea is, that the hardware part is exposed to the internet, which provides enhanced functionality. The developed code should be saved to disc as an example01.js
First, let us prepare the hardware setup of example according to the electrical schematics diagram which is shown on the next figure. We will use Green Led Emitting Diode (LED) with 220 Ohm resistor R1 to limit the current. One pole of Green LED is connected to the ground pin (GND) and the other pole of the Green LED is connected to the 220 Ohm resistor which is then connected to Pin13 on Arduino. (If you want to zoom in/out the figure right click the figure and "Open in New Tab", than use Ctr+ or Ctrl- accordingly.)
On the top of the next figure is the Breadboard view which shows the realisation of the above schematics. We should use wires, black for GND and red for connection to Pin13. The 220Ω resistor is connected to digital Pin13. The negative leg of the LED is connected to the ground of Arduino (GND).
On the bottom part of the figure the connection to PC is shown together with connection to cloud/ the internet and interaction with users.
Minimal code that is needed for the example to work is printed in the next part. We apply http node.js module and Firmata. Firmata is a generic protocol for communicating with micro-controllers, in our case Arduino, from software on a host computer.
Integrated Development Environment will be used to write the code. In order to start cloud9 we should go to terminal on linux by pressing Ctrl+Alt+t :
First, we should create a new directory in terminal i.e. bash (Unix shell), here we write the command without $ sign:
$ sudo mkdir cps-iot-2019
We should then change to the cloud9 sdk directory by writting the command cd c9sdk (without $ sign):
$ cd c9sdk
Then we start cloud9 as the node.js application (setting the working directory to cps-iot-2019):
$ sudo node server.js -p 8181 -w /home/mpiuser/cps-iot-2019/
And then we open the IDE in Chrome browser by writing the address http://127.0.0.1:8181 (see next figure):
After writing the code, we run it in the cloud9 IDE. There are several ways to do this. One variant is to start it in the terminal (bash). We can first list the files with the command ls:
ls
The system lists the files, in our case example01.js Next, we can write the following command to start the application:
node example01.js
The following figure shows start from the terminal:
To turn the led on we enter the IP addres with port and add the parameter /1. This is in our case request which is sent to the server. Example of the address entered in the address input field (1 at the end turns the LED on and 0 turns the LED off): 192.168.254.128:8080/1
If everything is all right, the led on the breadboard should be lit.
In order to stop the execution of the code we should press Ctrl+c in terminal window (bash):
There is also alternative way to start the code by pressing the run button or start the code from the menu; there are several options. Shortcut key is Alt-F5:
We should press the Stop button to stop the execution:
require("http"); ~ Loading libraries with require(), we apply HTTP server to process requests and send appropriate responses
http.createServer ~ Creating web server object:
function(req, res){} ~ Function that handles request and responses
res.writeHead(200, {"Content-Type": "text/plain"}); ~ sends a response header in the form of a status code with header message
res.end("The value of operator: " + operator); ~ tells the server that all response headers have been sent + adding custom message
.listen(8080, "192.168.254.128"); ~ server listens and accepts the connections on port 8080 on URL http://192.168.254.128
nodejs https://nodejs.org
Nodejs Documentation https://nodejs.org/api/http.html
HTTP Transactions | https://nodejs.org/en/docs/guides/anatomy-of-an-http-transaction/
Firmata npm https://www.npmjs.com/package/firmata
Firmata na github https://github.com/firmata/firmata.js
Firmata.org http://firmata.org/
Firmata / Arduino: https://www.arduino.cc/en/Reference/Firmata
node-serialport: https://github.com/EmergingTechnologyAdvisors/node-serialport
serial-port npm package: https://www.npmjs.com/package/serialport