Hello everyone! My (old) company is based on software development but also our other team is education based. We are giving robotic coding education to elementary/high schools. We have a website (https://mucitiz.biz) that has a lot of lessons about robotics and 3D printers. We are redesigning this site and I am part of this. When I looked at the site I wondered how these boards work, and what kind of things can make with them. I searched on Google, I was a little bit confused so finally, I got all of the ingredients from our office and start this post. Letâs make âa homemade light switchâ together and see how it works. I will use Fedora but you can use any Linux distros, Windows or MacOs.
Ingredients For Your Homemade Light Switch
- Arduino UNO with USB port (You can buy it on this site: Direnc.net)
- Arduino IDE
- OpenJDK
- Git (Itâs not necessary)
- An Internet connection
- âRootâ access to the development machine
- Node.js
- Johnny-Five and narf
Letâs Start
- Install Arduino IDE from https://www.arduino.cc/en/main/software, select your OS, download the file, and setup or install from the command lineÂ
sudo dnf install arduino
- Install openJDK from https://jdk.java.net/13/ or from the command line:Â
sudo dnf install java-11-openjdk.x86_64
 (for detecting your Arduino Card). - Plug the board to your computer with USB TTL serial cable (USB-A to USB-B).
- Letâs check our device that connects to our PC correctly:
Our command is:Â dmesg | tail
 It will list our all of connections. If you see like below output:
[fedora@localhost ~]$ dmesg | tail
[ 144.148444] atkbd serio0: Use 'setkeycodes e02b <keycode>' to make it known.
[ 1079.091148] usb 1-1: USB disconnect, device number 5
[ 1079.092304] ch341-uart ttyUSB0: ch341-uart converter now disconnected from ttyUSB0
[ 1079.092381] ch341 1-1:1.0: device disconnected
[ 1081.563604] usb 1-1: new full-speed USB device number 7 using xhci_hcd
[ 1081.691846] usb 1-1: New USB device found, idVendor=1a86, idProduct=7523, bcdDevice= 2.54
[ 1081.691856] usb 1-1: New USB device strings: Mfr=0, Product=2, SerialNumber=0
[ 1081.691862] usb 1-1: Product: USB2.0-Serial
[ 1081.694861] ch341 1-1:1.0: ch341-uart converter detected
[ 1081.695788] usb 1-1: ch341-uart converter now attached to ttyUSB0
If you see something like this, congratulations! Your device connected successfully.
- Set up project workspace:
You will need to create and set up a project workspace for creating the Arduino application. For our project, we will be using Node.js as the language for creating the switch. There are several ways to create this kind of application, It doesnât matter, you can use any language for this, but to help get you started, I created an HTML page and a JavaScript file you can use for your own setup. You can find my demo code available on GitLab. For this project, you will want a copy of the index.html and connectArduino.js files. You can copy and paste the two files into the project workspace you created earlier. GitLab Project:Â https://gitlab.com/burakibis/hello-little-arduino
- Setting up Node.js
Now that we have our workspace and files needed for running the project, we will need to set up a Node.js server to run the application. To begin with, running the âlight switch serverâ, you will need to install Node.js and NPM, the package manager for Node.js applications. Enter the following commands to install the necessary dependencies with the Node package manager: For installing Node.js:
sudo dnf install npm nodejs
For building JSON API with Javascript:
npm install narf
For connection to our Arduino board:
npm install serialport
We need a robotics framework for coding our board. We will use Johnny Five. Johnny-Five is an Open Source, Firmata Protocol-based, IoT and Robotics programming framework, developed at Bocoup. Johnny-Five programs can be written for Arduino (all models), Electric Imp, Beagle Bone, Intel Galileo & Edison, Linino One, Pinocchio, pcDuino3, Raspberry Pi, Particle/Spark Core & Photon, Tessel 2, TI Launchpad, and more!
npm install johnny-five
For install all of dependencies:
npm install
- Some of dependencies were changed and wonât work correctly, so you can get an error like these:
home/burak/Arduino/projects/hello-little-arduino/node_modules/pageserver/lib/pageserver.js:97
var type = mime.lookup(filepath);
^
TypeError: mime.lookup is not a function
at /home/burak/Arduino/projects/hello-little-arduino/node_modules/pageserver/lib/pageserver.js:97:37
at suppressedCallback (fs.js:199:5)
at FSReqWrap.args [as oncomplete] (fs.js:140:20)
Donât worry!. We just fix a file on YOUR_PATH/hello-little-arduino/node_modules/pageserver/lib/pageserver.js:97
In the new version of mime
renamed some methods so we should go to file and change this line var type = mime.lookup(filepath);
to var type = mime.getType(filepath);
Save and close this file.
FINAL
So we are ready for run our project! Letâs do it: run node connectArduino.js
on console and write http://127.0.0.1:8079/index.html
to your browser. TADA! You will see two button (On-Off). Click someone and look at your boardâs lights. Ehm.. You are awesome robotic programmer bro. Do more!
Photo by Rockân Roll Monkey on Unsplash