Thursday, January 7, 2016

ENC28J60 Ethernet Shield V1.0 for Arduino Nano 3.0 RJ45

Wouldn't it be great? You put a shield onto the Arduino Nano which then fits perfectly into the breadboard and then you can put together anything you'd like using just jumper wires.

p>Well, it is perfectly feasible with the ENC28J60 Ethernet Shield V1.0 for Arduino Nano 3.0 RJ45 (the so called Web Server module).

The only problem is that when you do stress it out (for example using ab or siege) then just after a few seconds and sometimes after few minutes or even after a few hours the module will just stop responding and die. Initially when I bought it I was pretty damn sure it must have some sort of manufacturing problem and so I have ordered another one. Oh boy was I crushed when I learned the new one after it was being shipped for over a month came to me with the same kind of problem... Damn!

Internet to the rescue

I started (yet again) digging into the matter using the most sophisticated tool mankind has ever created (that'd be google.com). Lo and behold I found this post where a gentleman who goes by the name of mmorcos found out that a similar issue occurred when connecting a similar module to a Duemilanove. The solution he found was to put a more powerful power source than the USB cable. I tried it using an old breadboard module and now the module is being bombarded (coming up on 11 hours) at a steady 150 requests a second without a single packet being lost! Yeah!

Alternatively you could try powering the hardware from a USB 3.0 which gives 900mAh over 500mAh from USB 2.0

Happy hacking!

Monday, January 4, 2016

Calculating pressure reduced to sea level (JavaScript)

Problem

The pressure as it is read from the BMP180 sensor is an absolute pressure. To be able to compare it with other sensors it is necessary to have a common level above the sea. For example the value of pressure as it is given in your weather forecast is reduced to the sea level. How do we do that in JavaScript?

The solution

The following calculateSeaLevelPressure() function is based on this blog post (in polish!) and validated using a PHP implementation

Happy coding!

Saturday, January 2, 2016

webled - a web-enabled light emitting diode

There are different styles of hello-world applications. Some just spit out to whatever output is provided the Hello, world! message, some read a digital input and react to it and some just toggle an LED. This one is the of the latter kind, using Raspberry Pi and a green LED.

Disclaimer

I am not responsible for any damage you might cause to your hardware. Use at your own risk.

The setup

It's really straight forward to wire everything up. You start by connecting PIN01 through a resistor (let's say 100Ohm) to anode (the longer lead) and then the cathode (the shorter lead) to PIN40. That's it.

Now we need a web application to work with that new high-end device of ours. We start by installing Flask - a Sinatra-inspired framework for Python.

pi@raspberrypi:~ $ sudo pip install flask

Code

That will take a while (~ a minute) and after it is done we finally get to do some coding.

The code is pretty straightforward. First we import flask and jsonify for later use, then we create a Flask application instance, map the root to hello function which when executed toggles the state and stores the new value to PIN40. In the initialization block first we setup the GPIO.BOARD mode to be able to refer to the pins as they are on the board (as opposed to the GPIO.BCM mode which refers to the CPU pins), set the PIN40 mode to GPIO.OUTPUT to be able to switch our device on and off, and initialize the initial state of that pin. When the application finishes we clean things up so that others can use the library.

The host = '0.0.0.0' makes it possible to access the application from other hosts as opposed to host = '127.0.0.1' (which is the default) which would only allow access from the board.

Test drive

To test drive it open up a web browser and navigate to the http://localhost:5000 URL and observe the LED to light up. When you refresh the page it will go dark again. And so on and so forth. Such is the purpose of a web-enabled LED (a webled)

Happy hacking!

Friday, January 1, 2016

Happy new year!

I m going to wish you a new year in which all your dreams come true! Let all your embedded systems work with 100% uptime never failing and always behaving! Let all your web applications work 100% of the time and all your web services respond under a tenth of a second!

May the force be with you! Always...

Thursday, December 31, 2015

Setting clock on DS3231 using RPi

The problem

When using the DS3231 module with for example Arduino-based project there is no easy way to properly set the actual time on that device. There is a way using Linux. Since we'll be using the I2C to hook up the clock module you are going to need either the i2c-tiny-usb interface or for example Raspberry Pi which has the bus with gold pins ready to use.

The solution

Connect the module to the I2C bus, load the appropriate kernel module, initialize the device letting the system know there is a hardware clock attached, synchronize the system clock and finally store that system clock value

pi@raspberrypi:~ $ sudo modprobe rtc_ds1307
pi@raspberrypi:~ $ echo ds1307 0x68 | sudo tee /sys/class/i2c-adapter/i2c-1/new_device
pi@raspberrypi:~ $ sudo ntpd -gq
pi@raspberrypi:~ $ sudo hwclock -w --local

That's it! Happy hacking!

InfluxDB, Telegraf and Grafana on Raspberry Pi 2

Grafana is a great data visualization application. I use it all the time for all kinds of purposes, most notably to chart information from my weather station. Recently there have been some serious changes in Telegraf that will make it play nicely with InfluxDB 0.9+

Since we're at the awesome I have recently bought a Raspberry Pi 2 Model B. It's a great little machine with 4 cores and 1GB of RAM. That got me thinking: how about installing influxDB, Telegraf and Grafana on it?

Unfortunately it is not so simple. There are no official packages of the latest version for Raspbian, not to mention the 0.3.0-beta2 version of Telegraf. So to use it I have had to compile it myself. Compiling Go programs is actually quite easy with GVM and RVM but one needs to learn how to use it first. There are some resources on the Internet to help out with the process but the biggest problem of all is high CPU usage over long period of time during compilation which leads to high CPU temperatures (up to 70°C). There are 3 methods to overcome that problem

  • Buy radiators (already on the way)
  • Cool it down with a fan
  • Find pre-compiled packages

I went with the second option which kept the CPU at a steady 34°C but let me tell you with the Go compiler compilation it took forever (5h+) on the little machine. To not have to do it again I have prepared all 3 packages so that I, and everyone else willing to try it out can grab them and skip the boring part.

influxdb_0.9.6.1_armhf.deb
telegraf_0.3.0-beta2_armhf.deb
grafana_2.6.0_armhf.deb

Now all that is left is to download those packages and to install them using dpkg -i <package-name>.

There are 2 things to note: one, there will be no telegraf database by default (need to create one yourself using the WUI - web user interface - CREATE DATABASE "telegraf") and there is no defaults file for telegraf which will make it impossible for it to start right out of the box. To fix it create one like so:

sudo touch /etc/default/telegraf

Also grafana-server service isn't enabled by default. This is mentioned at the end of the installation but for convenience here are the enchantments to make it work

sudo /bin/systemctl daemon-reload
sudo /bin/systemctl enable grafana-server
sudo /bin/systemctl start grafana-server

Edit on January 11th 2016
As oriste pointed out the grafana package is a lot smaller than the one for Linux x64 found on grafana site. I have looked at it and the reason for it is that my package does not include the phantomjs binary in /usr/share/grafana/vendor/phantomjs/ folder that is used to prepare snapshots for sharing. If you'd like to use it you can always get the binary from https://github.com/piksel/phantomjs-raspberrypi/raw/master/bin/phantomjs

Edit on January 11th 2016
I have verified the packages and added some installation procedures that will help out put a monitoring solution using the three tools.

Edit on October 6th, 2016
There are ready-made packages for the armhf for Telegraf and InfluxDB now. Use those instead!
. You might be puzzled as to how to download the deb packages for armhf. What you do is you take the URL of the amd64 package, change the architecture in the URL to armhf and presto - you get a link to the RPi package, for example:

https://dl.influxdata.com/telegraf/releases/telegraf_1.1.1_amd64.deb
change amd64 to armhf and you get
https://dl.influxdata.com/telegraf/releases/telegraf_1.1.1_armhf.deb

Similar with InfluxDB:

https://dl.influxdata.com/influxdb/releases/influxdb_1.1.0_amd64.deb
change amd64 to armhf and you get
https://dl.influxdata.com/influxdb/releases/influxdb_1.1.0_armhf.deb

It seems those packages are available for every build but not directly listed on the page - hence the URL magic you need to do to get them

Have fun!

Sunday, December 27, 2015

Running LCDProc on Digispark

The project I was working on recently was to get the Digispark module to act as a Linux I2C-to-USB driver. It wasn't all so straight forward so I decided to put a little put a little tutorial on how to do it.

That's how the breadboard version of it looks like

Let's get to hacking!

Requirements

You will obviously need a Digispark or a clone of it. I used one I bought from aliexpress.com and it does the trick quite nicely

Let's assume you don't have the micronucleus bootloader installed and let's build one from scratch. You'll need a AVR programmer like USBasp (cheap and does the job very well)

Done. In case you're missing any libraries or tools that's the list of packages that is required

build-essential, avrdude, avr-libc, binutils-avr gcc-avr

You'll also need an LCD with PCF8547A or PCF8547T extender. I used one with T which required me to apply a patch to the LCDProc sources. More on that soon

The project

First we need the littlewire version of the firmware for our Digispark. You can get it form github

git clone https://github.com/nopdotcom/i2c_tiny_usb-on-Little-Wire

Buinding it and installing onto your Digispark is very straight-forward and described in details here.

Let's get to the hacky part of it!

Patching LittleWire firmware

As it turns out the original driver for i2c-tiny-usb was reluctant to recognize the hardware as proper I2C adapter. I needed to update the vendor and device IDs to make it work

After building it and installing again on the Digispark and loading the i2c-tiny-usb and i2c-dev modules using modprobe the command i2cdetect -l finally started spitting out some good news :)

padcom@aphrodite:~$ sudo i2cdetect -l
i2c-0 i2c        i915 gmbus ssc                   I2C adapter
i2c-1 i2c        i915 gmbus vga                   I2C adapter
i2c-2 i2c        i915 gmbus panel                 I2C adapter
i2c-3 i2c        i915 gmbus dpc                   I2C adapter
i2c-4 i2c        i915 gmbus dpb                   I2C adapter
i2c-5 i2c        i915 gmbus dpd                   I2C adapter
i2c-6 i2c        DPDDC-B                          I2C adapter
i2c-7 i2c        DPDDC-C                          I2C adapter
i2c-8 i2c        i2c-tiny-usb at bus 001 device 057 I2C adapter

After hooking up the LCD to pins 0 (SDA) and 2 (SCL) and adding 2 4.7k pull-up resistors the LCD has been properly recognized as well :)

padcom@aphrodite:~$ sudo i2cdetect 8
WARNING! This program can confuse your I2C bus, cause data loss and worse!
I will probe file /dev/i2c-8.
I will probe address range 0x03-0x77.
Continue? [Y/n] 
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- -- 
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
20: -- -- -- -- -- -- -- 27 -- -- -- -- -- -- -- -- 
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
70: -- -- -- -- -- -- -- --                         

Success! Well, actually to get to this point took me over 2 days to figure out all the moving parts but the joy of having it actually working was just great :)

Now it's time to do some serious work with that new piece of hardware we have. Let's use LCDProc to display some system statistics!. Download, extract... and if you're using the PCF8547T version of the extender apply the following patch before building:

All that is left is to configure and build and configure the LCDProc package. Tested also on Raspberry Pi 2 and works flawlessly.

./configure --enable-drivers=hd44780 && make && sudo make install

Now edit the /usr/local/etc/LCDd.conf file as follows

DriverPath=/usr/local/lib/lcdproc/
Driver=hd44780
ServerScreen=off

And under the section [hd44780] make sure you have the following values

ConnectionType=i2c
Device=/dev/i2c-8 # that is the I2C bus id you have from i2cdetect -l
Port=0x27         # that is the I2C device id you have from i2cdetect 8
Check out also other options - they are all properly commented so it should be easy to figure out what they mean

Starting it all up

First start the LCDd daemon. You might want to start it initially with the -f (foreground) parameter to check if it works ok.

LCDd -f

Next you need to run the client. Yes! It is a client-server architecture! Use lcdproc --help for the list of all available options. I use the SMP-CPU version the most

lcdproc -f P

That's it! I admit this is more hassle that I generally like but getting it working is worth every late night minute I spent on it :)

Happy hacking!