Monday, December 19, 2016

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

Sunday, December 18, 2016

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

Thursday, December 15, 2016

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


Monday, December 12, 2016

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.


Monday, December 5, 2016

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.

Friday, December 2, 2016

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):



Tuesday, November 29, 2016

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