IOT RECIPE: 04 Headless Raspberry Pi programming

How many monitor you need to have to program a Raspberry Pi. 
No one, as you now: with ssh is possible to interact with Raspberry Pi from a remote machine. The majority of distributions enable SSH by default, so you can access it once connected to your local network with command ssh username@rpiname.local and replace rpiname with raspberry host name.
With text-only Nano editor is also always possible to develop directly on ssh interface. 

Is possible otherwise to develop a complex project like IOT Energy Monitor Webserver using a combination of SFTP, your favourite Text Editor and of course Terminal to run WebServer and debug.



SFTP Mac OSx Drive Mount



Cyberduck.io 

"Cyberduck is a libre FTP, SFTP, WebDAV, S3, Backblaze B2, Azure & OpenStack Swift browser for Mac and Windows."






ExpanDrive

"Map or Mount Amazon Cloud Drive, Google Drive, Dropbox, Box, OneDrive, SFTP, WebDAV, S3 and more as a Network Drive. Seamless access to files without sync."
ExpanDrive is not free.




Extreme text Editor

Sublime Text

"Sublime Text is a sophisticated text editor for code, markup and prose. You'll love the slick user interface, extraordinary features and amazing performance."




Atom Text Editor

"Atom is a text editor that's modern, approachable, yet hackable to the core—a tool you can customize to do anything but also use productively without ever touching a config file."





Terminal


Bash (Bourne Again Shell)

Terminal is the OSx shell. From here you can connect via ssh to the Raspberry Pi shell.
OSx terminal can be customized in colors and transparency.
Copy & Paste work as expected in terminal, and more than one session may be opened, if you need to run a server and rename/move/copy/list/... at the same time.


Ssh first connection

Once connected raspi to the local network, you need to find IP to access and configure the network name (and other configurations). Personally i use router DHCP list to find the assigned IP, but more options are available, like ip scanner.
The first connection to ssh host, terminal ask you if you want to add ssh key to known hosts, tap "yes" to gain access to the machine.

Ssh offending message (RSA key changed)

Sometime, when IP changes on Raspi, you are not more allowed to connect to remote raspi: an update is needed.
Use the command "ssh-keygen -R rpiname.local" to clean RSA key, then reconnect via ssh and generate another key, as a first connection.

Lost connection while running flask server

What happens if you lose connection while a flask server is running?
The session is closed and the server is terminated. To change interval here there is a guide to change timeout interval on Raspi.  
If you want to run the flask server on startup, follow the next step.

Running python script on startup via systemd

The simplest way to run a python script on startup on Debian distro (like Raspbian) is via UNIT files.
There are a lot of guides on the web, but the majority is related to init.d script. This approach is more simpler and effective (related also to running order). I had a lot of headache running python script via init.d cause "exited" python script status.

To run a script via systemctl follow these steps:
1) Be sure your python script works as expected
2) Paste the python executable version on top of the pythons script and save: #!/usr/bin/python
3) Make the script executable with chmod +x myscript.py
4) Create a configuration file with sudo nano /lib/systemd/system/flask.service
5) Paste the following content to the nano editor session and save

[Unit]
Description=Flask server

[Service]
Type=simple
ExecStart=/absolute_path_to_script.py
User=root
Group=root
Restart=on-failure
RestartSec=5
StartLimitInterval=60s
StartLimitBurst=3

[Install]
WantedBy=multi-user.target

6) Set unit file permission with: sudo chmod 644 /lib/systemd/system/flask.service
7) Load unit file with: sudo systemctl daemon-reload
8) Enable unit file with: sudo systemctl enable flask.service
9) Reboot

After reboot you can control status of the service, stop and restart using these commands:
  • sudo systemctl status flask.service
  • sudo systemctl stop flask.service
  • sudo systemctl start flask.service
  • sudo systemctl restart flask.service

OUTSIDE: Dead bug prototyping and Freeform circuitry

Dead bug prototyping and freeform circuitry is the way to create circuits without a pcb. Requiring a magnifiyng glass, lot of patience, enables 3d circuits making and custom electronic devices packed in acrylic (maybe little dangerous with caps!).

Check out this collection of incredible examples from the web.


TallmanLabs headphone amplifier casting in resin
O'Baka Arduino skeleton
Peter Vogel Klang Objekte art installation
Little wire dead bug art
Electro-Music forum is a great source of freeform circuitry

Dead bug led cube, via instructables

OUTSIDE: Automata with Tom Haney

Before robot was automata. Ancient romans and before greeks, used the word Automata to describe machines, movements, stunts.
In our times Tom Haney from Atlanta, is an artist who express himself and his art with automata.

Contraption - 2009 - Tom Haney

He says about himself:

"Having never wanted to be pigeonholed as an artist, I’ve sought to do unique works that combined many varied methods. Initially, I started with woodcarving - practically a lost art in our world­ - a tradition people have been using for centuries to tell their stories. Since any kind of mechanical movement has always been a fascination of mine, to combine this with figurative carvings became irresistibly compelling. Eventually I learned about automata, “a moving mechanical device made in imitation of a human being”, an ancient pursuit more thoroughly developed in the 13th – 19th centuries mostly in Europe but also in Asia. The work I create today is a modern offshoot of the time-honored Old World tradition of automata."

Check out his work on his site or on youtube


IOT RECIPE 03: Pellet / Wood chips level sensors

In this post i'll cover actual sensor setup for pellet and wood chips bulk level monitoring.

Pellet bulk level sensors

How to monitor pellet bulk level if you can't access to warehouse?
My warehouse is a room covered by wood, with sloped floor and a moving reclaimer on the center. The total volume is approximately 12 cubic meters and once filled i've no access to the room.



Obviously i need to know residual pellet level, so a pellet bulk sensor is needed. While googling i've found few examples, all of them related to water level in tank and using ultrasonic distance sensor approach.

The room is more or less 2 by 3 meters: in this configuration, pellet or wood chips forms non uniform piles in the room, with lower point just above the reclaimer area, so is needed to use an array of sensors averaged to find the level: 9 ultrasonic sensors are arranged in a matrix configuration.

Ultrasonic sensors

For the project i've selected HC-SR04: the sensor is a low cost complete ultrasonic measurement device. The sensors are housed in waterproof electrical box.

Once activated a signal burst is emitted from the emitter, then the signal is captured from the receiver, mirrored by a surface. Using simple timing formula based on sound speed, the library output distance in cm. Simple. 3 cm to 2 meters is the real distance range, while effective usage suggests the sensor is not so accurate over time: in my experience temperature compensation is needed to give at least not wrong measurements (15% of fluctuation is possible).

Most libraries provides averaging for each reading, with 10 to 25 signal bursts before measurement average.

From distance to weight

Pellet weight in bulk storage is more or less 650 kg/mc.
To provide weight measurement the equation is simple:

Estimated weight = Average(empty distance - measured distance)*(area)*(bulk weight)


Data acquisition

To acquire 9 sensors outside warehouse a lot of cables are routed outside warehouse to an external waterproof electrical box.

The little circular window in the front of electrical box is PMMA window sealed with hot glue (my new electronic projects best friend). 

In the box i've installed an Arduino Mega with three additional sensors:
  • A remote DHT22 inside pellet warehouse to provide internal reading of temperature and humidity;
  • DHT22 to provide same readings of external conditions;
  • A three-in one sensor: light, pressure/altitude and temperature

Measurement routing

Distance between Arduino Mega and Raspberry PI is more or less 4 meters. After a bit of googling, i've found a 5 meters USB cable is reliable. I can confirm is true.
The USB cable connect Arduino to RPi inside the boiler room (gray cable coming from the top of the electrical box).


Inside the box a Raspberry PI with Node-Red with MQTT client (more details in the future).
Another Arduino is installed and connected to read out energy monitor.
RPi communicates with a central server (another RPi) using ethernet.


RC SERVO ROBOT ARM: Weekend project

This is our weekend project: a simple RC servo robot arm. 


Host controller is Mac using Python scripting, while servo controller is a spare Fubarino SD.




I tried with MSP430 Launchpad, but Serial + 3 servos was over the capability of this little Mac-hated board. Fubarino is a cherry plenty of power, and the ported SoftwareServo library from ChipKit is stable and effective.
Fubarino accepts directly joint angle in ms. Python script uses a simple Inverse Kinematics (Updated versione: math credits to COÅžKUN YETÄ°M):

def ik(self, position):
#ARM LENGHTS
l1 = self.arm1
l2 = self.arm2

#END EFFECTOR POSITION IN RESPECT OF ORIGIN NODE
x = position.x - self.home_position.x
y = position.y - self.home_position.y

#TO LIMIT END POSITION WITHIN ARM REACHABLE CIRCLE
dist = min(l1+l2, sqrt(x*x + y*y))
dub = sqrt(x*x + y*y) / dist
x /= dub
y /= dub

#SIMPLE, NO?
thetar = acos(x/dist)
theta1 = acos((l1*l1 + x*x + y*y - l2*l2) / (2*l1*dist)) + thetar
theta2 = pi - acos((l1*l1 + l2*l2 - (x*x + y*y))/(2*l1*l2))

self.set(180-degrees(theta1), 180-degrees(theta2))

Graphic is provided by a simple Svg to path script. The library used (svg.path) can provide point coordinates for Line, Arc, CubicBezier and QuadraticBezier without pain. All graphic may be created within Inkscape.


Overall code may be downloaded at this link.

IOT RECIPE: 02 Energy Monitor

In this post i'll cover actual sensor setup for energy monitor.

Energy monitor

To evaluate power consumption i've routed main cable coming from utility energy monitor in a new electrical panel. In this panel i've installed:
  • 3 three-phase energy meter (16 kW of maximum power)
  • 1 single-phase energy meter
This is an internal view of my setup inside an ABB electrical panel (upper part, with two of these energy monitor) after thermal-magnetic circuit breaker and surge protector.

For this duty i preferred to go with an industrial solution, because i was not confident with DIY solutions on main power side.
My solution is a products from Italian manufacturer LOVATO:
LOVATO DMED300T2 is a compact (four modules) energy meter. The main advantage is the presence of two programmable pulse output (up to 1000 pulses/1kWh or programmable threshold).
The output is an open collector, in which each pulse has a duration of 60ms: an open collector is a transistor based output that can be read by an external PLC (like arduino).

The electrical connection requires only to route an input pin of the arduino to open collector and connect the two IC grounds to have the same reference level and let current from arduino to flow once open collector is open.

There are two methods to read these output, with interrupts or with pooling.
Because i had only one external interrupt, i had to go with pooling: loop forever and check if digital input is high or low. If is high, and previous one was low, than increment a counter and go on to next input. 

First of all, in my system maximum power is 16 kW: this means each hour i can have a maximum number of pulses of 16 * 1000 = 16.000 pulses/hour equal to 4.45 pulses/second, each one of 60 ms.
If entire loop timing is less than 60ms, no pulse is missed.

Because open collector is transistor based, i found no debounce routine is needed (situation like button press may need a stabilisation in input reading).

My piece of code for pooling is:

//INPUT SETUP
pinMode(LOVATO1, INPUT);           // set pin to input

digitalWrite(LOVATO1, HIGH);       // turn on pullup resistors

//INPUT LOOP
READ1 = digitalRead(LOVATO1);

if (READ1 != LASTSTATE1) {
    if (LASTSTATE1 == 1) {
      COUNTER1 ++;
    }
    LASTSTATE1 = READ1;
  }

Taking into account the time delta between reading A and reading B (60s in my case), is possible to average the energy consumption during this period, and to define average power consumption during the period.
Passing via serial milliseconds and pulse numbers between loops, i can reduce the time spent communicating with host, leaving calculation on other side.

Resulting chart of 1 day power consumption average (1 tick/30 minutes):



IOT RECIPE: 01 Introduction - Sensor web interface

I'll start a new series of posts on IOT matter from the end.
This is the web interface running on my local area network.



Running the site is a mini flask web server running on a Raspberry PI. In the screenshot, charts are provided by highcharts.com and Justgage javascript library.

In this series of posts i'll cover all the ingredients of my recipe:
- Sensors overview (DHT22, Energy, Light, Barometric pressure, Pellet height)
- Raspberry Pi data acquisition with Arduino
- Launchpad Tiva CC3200 with WiFi
- Node Red Engine
- MQTT, Mosquitto, Paho
- Rrdtool, circular timeseries database
- Flask server
- Charting with javascript
- Debian unit files
- Telegram Bot Api

OUTSIDE: Dan Gelbart, marvellous craftman

Check out a series of 18 videos on prototypes by Dan Gelbart.

With simple language and real examples Dan will capture you as nobody did before; his videos are not only about prototypes or machinery: each time i look at Dan's videos, he says to me that if you really believe in yourself, you can spend all your life doing what you like and be proud to share your achievement with others like you.

My favourite one is #5 on spot welding.




Check out Dan Gelbart Youtube channel for other videos

PEATOL/TAIG: Milling attachment 1220

Milling attachment
Part # 1220

From Taig: The milling attachment is used to hold the workpiece while the cutting tool is held in the headstock spindle. The attachment provides vertical travel of approximately 1 3/4 inches. The dial provides travel in .001 inch increments.

The cutters (end mills) are held in the spindle with collets to provide maximum rigidity. Miniature end mills come with 3/16 inch diameter shanks and various size cutting diameters. The end mills will cut all materials steel, aluminum, brass, plastic and wood.









Milling on a lathe? If you already have a mill, you will find are needed more passes and lighter cut with this attachment.
The attachment may also be used for gear cutting. Check out this video by xynudu on youtube:





OUTSIDE: Making gears at home

Making gears at home is quite difficult and a lot of tools and machinery are needed.

At least you need a rotary attachment for a mill or lathe with a specialized tool (involute gear cutter).
Another drawback is that to cut a specific gear you need a specific size of involute gear cutter, so a complete set (normally 8 pieces, around 100 bucks) is needed.

Chet out this great video by Tubalcain:

Another option is to build a small spur gear hobber machine (project already in the wishlist) like this beautiful one by Jack Hayes:

Also in this case, only the hobber, quite difficult to find, at least requires 80 bucks.

Solution? Not for helical or spur gears, but there is a particular set of gears, called worm gears you can machine at home. 

From wikipedia: "Worm-and-gear sets are a simple and compact way to achieve a high torque, low speed gear ratio. For example, helical gears are normally limited to gear ratios of less than 10:1 while worm-and-gear sets vary from 10:1 to 500:1"
Is possible to find this set of gears in telescope aligning mechanism or in a mill 4th rotary axis. In this set the two axis are perpendicular, and only one is driving.
The drived gear can be machined easily using only a lathe, and the other one may be simply a machine screw with imperial, metric, acme or custom thread. Is only needed to have the a screw tapper.

Check out this self-explanatory video by Dalibor Farny:

Finally a link of great kinematics models. From KMODDL site: "KMODDL is a collection of mechanical models and related resources for teaching the principles of kinematics--the geometry of pure motion. The core of KMODDL is the Reuleaux Collection of Mechanisms and Machines, an important collection of 19th-century machine elements held by Cornell's Sibley School of Mechanical and Aerospace Engineering."
http://kmoddl.library.cornell.edu/stillImages/Chicago/small/011.jpg
Digital file property of Cornell University Library. Made available under the same terms as a Creative Commons Attribution Non-commercial Share Alike License

ELECTROMAGNETIC CLOCK: Kundo (Germany) clock

A vintage Germany clock from manufacturer Kundo, around 1960's.




This particular and clever kind of clock keep time as mechanical watches do, with a swinging pendulum. But there is no mechanical winding: instead a magnetic force applied by the solenoid inside brass cylinder to the pendulum arm, gives to the pendulum a momentum maintaining pulses during time.







The solenoid is mechanically activated by a micro switch linked to the pendulum on the upper side.

Accuracy is provided by the brass bell, which is screwed on the pendulum. Each turn right raise the bell on the pendulum linkage, keeping the period shorter. On the other side, each turn left lower the bell, keeping the period longer.






I've replaced original battery (visible in the photo) with two AA size 1.5V batteries, and i've never replaced them in 4 years now.


PEATOL/TAIG: Radius turner 1210

Radius turner
Part # 1210

From Taig: The round nose tool bit can machine a radius at the end of the workpiece by swinging around the material. The bearing's inner race on the radius attachment bolts to the crosslide and then the entire base can rotate with the bearing. The rotation is done by hand.




If you want to see a radius turner in action, check out this video from ElationProduction on custom built ball turner for taig.



COIN-OP: mini arcade cabinet update

Just finished an update to the COIN-OP






I've replaced the switch plate covers with a metal one. The metal plate is a thick (20 gauge) stainless steel, cutted with Air sheet metal cutter and bended with Sheet metal bender.

On the bottom side i've completed the small window to recover coins.


Actually i've programmed the coin op with old 500 lire (out-of-course). I was surprised to realize the coin acceptor can recognize this kind of bimetal coin (bronzital - acmonital).

The girl on the coin obverse depict Republic: this kind of coin was common until Italy switched to euro on January 2001. In fact the coin acceptor is an educative trick to limit the children time spent into the game.

Next step: T-mold all over the laminated wood (not so easy to find here).

Popular posts