Solved: Starting programs at boot didn’t work on raspberry

Today I made a new fresh install of Raspbian-Stretch on my old RaspberryPi 1B. That board mainly has some python programs on it that MQTT info onto my network to OpenHab. The previous install had become a bit broken I think, it would crash every two days and rather than sorting that out, I decided a fresh install would be the quickest.

After installing a fresh Stretch image, I put all the programs and required libraries back on, a quick test and that all worked.
OK, now it is time to have the programs start automatically at boot.
That is also simple, I usually add the programs I want to start at boot in my /etc/rc.local file.

Always worked for me.
However, this time it didn’t and for the life of me I couldn’t figure it out. Why is a setup that used to work suddenly not working anymore, starting the programs at boot, while the programs itself work when started manually.
Trying trying trying. Writing the output from each command to a log file didn’t help me, logfiles stayed empty.
Then I decided to write the output of the entire rc.local file to a logfile with:

exec > /tmp/rc-local.out 2>&1;set -x

put that in the rc.local file right before the line that says “# Print the IP address”:

I rebooted and checked the file: /tmp/rc-local.out.

Now it became clear…. the paho (mqtt) module that these python programs were using was not found.
Odd.. I am sure I installed it……and when I manually start the programs they work 100%

I was thinking….. in my previous setup, I had installed the paho.mqtt library manually. It resided in a folder in my /home/pi directory. This time I had installed it with pip. Now it wouldn’t be that…… no, it couldn’t, could it?
The commands in the rc.local file, as far as I know are given by root and my /home/pi directory was owned by ‘pi’…….but that hadn’t changed between two installs and root has privilege anywhere right?

After some more searching I learned that the library “paho” had been installed by pip in the folder “/home/pi/.local/lib/python2.7/site-packages” but “sudo python” searches this library in the folder “/usr/local/lib/python2.7/dist-packages”.

I was not using sudo… but that is in fact what the rc.local does, so I was guessing that that was where my problem lied.

So, I needed to link the one directory to the other somehow.

This is what did that:

cd /usr/lib/python2.7/dist-packages
sudo ln -s /home/pi/.local/lib/python2.7/site-packages/paho

Then it all worked again.

Interesting info on how to start your programs at boot can be found here.

Advertisements

Using limitswitches with diodes

DC motors are often used in electronic projects and a special case is when a DC motor needs to be brought to a certain begin or end position without going further. Typically limit switches are then used. Such a switch is pressed by the motor when that motor reaches its end position. Similarly such a switch can be used when a motor reaches its begin position again.

Obviously, when a limit switch is pressed an action needs to be taken to stop the motor. The solution for that is in fact simple and old news, but surprisingly many hobbyists don’t know about that solution or struggle with it, and that’s why I will explain it here (with apologies to all people who do know it)

Motor in middle position

In the picture above there is a typical circuit for a motor with limit switches. Let’s presume the left switch is opened when the motor comes to its desired left position and the right one opens when the motor comes to its desired right position. Let’s also presume that the motor in that picture is in between left and right position.

It is easy to see that the electrical circuit is closed and that it doesn matter what polarity is used.

We then apply a positive voltage to the top wire and a negative voltage to the bottom wire. The motor starts turning left, until it reaches, presses and opens SW1.


Motor in leftmost position

At that moment, the electric circuit is open: Switch S1 is open and the left diode blocks a positive voltage. The motor stops.

To make the motor go back to its original position, we need to reverse polarity, so now the left most diode sees a negative voltage on its cathode and wil conduct curent: the motor turns reverse. After it travelled a fraction, SW1 will close again (as in the firts picture)…but that only becomes relevant later. We let the motor travel reverse till it comes to its desired right position, where it opens SW2.



Motor in desired right position

As soon as SW2 is open, the circuit gets broken again, no current will flow and the motor stops. As SW1 has closed again after the motor started travelling, the circuit will now conduct for a positive voltage on the top connector. Though SW2 is open, the right-side diode will conduct.

Arduino Atmega 2560 with ethernet W5500 module

If you want to connect the Arduino Atmega2560 with ethernet, there are various options. The easiest might be to get a shield. The regular Arduino shield should fit on the Arduino Mega as well. there are 3 versions of the shield: with an ENC28J60, with a W5100 and with a W5500.
The shields are fairly expensive, it is cheaper to get an ethernet module. These are also available as ENC28J60, W5100 and W5500.

With prices all in the same range (currently around 2.60 Euro), it is best to get a W5500, the most modern chip of the range, that definitely needs less memory than the ENC28J60.

To connect it to an Arduino Mega2560, one only needs 7 connections as shown in the image below.

The SPI connections can be best taken from the ICSP connector  and the chipselect from Pin 10.
Connections on the W5500 module are equally simple as they are usually printed on the module:
SCS is the chipselect that goes to pin 10
SCLK is the clock signal that goes to SCK on the 6 pin header.

When using the W5500, the ‘old’ Ethernet library is not usable anymore. You need to use the Adafruit Ethernet2 library or the WIZ_Ethernet_Library that supports the W5100, W5200 and W5500

Is the Si1145 reliable or is it a piece of crap

Currently working on a project that includes the Si1145 lightsensor.
The Si1145 measures visible light and infrared light and then calculates the UVI from that, without measuring actual UVA or UVB levels.
According to Adafruit it is very reliable.
When i used it however, it seemed the visible and infrared readings always hung around 625 or were 0, while the UVI was always 0.
Now ofcourse I could have a faulty sensor, but some browsing on the internet showed that this was a common occurrence.
When I put the (adafruit) library next to the datasheet, at first glance I couldn’t help but get the impression the library had some shortcomingins that might well explain the erratic behavior.

After some searching I found a latched library that worked better……but even then, in total darkness the ambient light and IR light hung around 600, but at least in daylight I would get different values and even a UVI reading.

The latter though proved totally inaccurate, easily going up to 7 in overcast weather.

I will do some further digging in the code but for now I would not recommend this sensor

Adding ADC channels to an ESP8266

One of the drawbacks of the ESP8266 is that it only has one ADC port.
In case you need more ADC ports, you could add an I2C ADC converter like the PCF8591 or the ADS1115, but there is a cheaper way that is easier to program as well.
You could use an analog multiplexer like the HCT4051, that can switch 8 ADC channels individually to the ADC input of your ESP8266.
Does this method have drawbacks? yes, you need 3 I/O pins to control the HCT4051 and the ESP8266 does not have an abundance of pins to begin with.
The circuit looks like this:

I have chosen GPIO12-14 to control the multiplex chip. If all control lines are LOW, the A0 line is patched through to the ADC of the ESP8266. If all control lines are HIGH, then the A7 line is patched through to the ADC channel of the ESP8266, the table below, shows the state of the I/O pins need to be in to read every individual analog channel

      S0      S1      S2
      GPIO14  GPIO13  GPIO12
A0     0       0       0
A1 0 0 1
A2 0 1 0
A3 0 1 1
A4 1 0 0
A5 1 0 1
A6 1 1 0
A7 1 1 1

ESP8266-01 Neopixel module


In Chinese webstores a “ESP-01S RGB LED Controller Module for Arduino IDE WS2812 Light Ring Smart Electronic DIY” module is sold for less than a dollar  (if you buy it without the ESP8266-01). The module contains everything that is needed to have an ESP8266-01S control a Neopixel strip

The module uses GPIO2 as output for the Neopixel(s). Suitable Software can be found here. Make sure you use the new ESP8266-01S with this module. The ESP8266-01S has the necessary pull up resistors already available. If you use the standard ESP8266-01 you need to attach the required pullups yourself.

Mini WiFi RGB/RGBW/RGBWW ledstrip controller (MagicHome/Arilux AL LC0x)


Chinese webstores offer the I presume by now well known Mini Ledstrip controllers that depending on the model work with WiFi, WifFi/RF, or Bluetooth.

Model Color Voltages Control Price Link
LC01 RGB 5-28V WiFi ~€7 Banggood
LC02 RGBW 9-12V WiFi ~€14 Banggood
LC03 RGB 5-28V WiFi/IR ~€9.50 Banggood
LC04 RGBW 9-12V WiFi/IR ~€11 Banggood
LC08 RGBWW 5-28V WiFi ~€10 Banggood
LC09 RGB 5-28V WiFi/RF ~€11 Banggood
LC10 RGBW 9-28V WiFi/RF ~€12 Banggood
LC11 RGBWW 9-28V WiFi/RF ~€10 Banggood
LC12 WS2812 12-24V WiFi ~€10 Aliexpress
LC13 RGBW 12-24V Bluetooth ~€6 Aliexpress

I added the links (and prices) only as an example, they may or may not be cheaper at Aliexpress (check here for other brand names as well) or at another supplier. One example of unexpected price difference for instance is the LC10/LC11 difference, where the controller with more functionality is in fact 2 euro cheaper.

Here, I will focus on the WFi controller.
There are basically 3 models: one with 4 pins, one with 5 pins and one with 6pins. The 4 pin model is for an RGB strip and the one with 5 pins is for an RGBW strip, the one with 6 pins, so you can control RGB, warmwhite and coolwhite. The remaining pin is usually the common anode. And then there is a model to be used with WS2812 programmable LED’s.

The models can take various voltages (see table above) and usually work with a 12 Volt LEDstrip. At this moment it isnt quite clear to me whether attaching 28 Volt will still make it useable for a 12 Volt strip (as I have no 28 Volt PSU), but for now I am not risking anything trying that.

The WiFi model is usually controlled by the MagicHome App or MagicHomePro App.

The setup for it to recognize your device is not that hard, but not always intuitive. You should be able to do it within a matter of minutes though. Roughly the process consists of the app recognizing the controller, then you switch to its WiFi accesspoint (it is the one with the weird name in your SSID list) and fill out your WiFi credentials.
With your device a little card should come that contains the link to a full instruction PDF

Once you have done that, the app allows you to switch to direct contact, so your commands will be sent to the device within your network, so you are not depending on a server somewhere in China. Upto 8 phones will be able to control the device.

Inspite of its less than perfect rating on the play store, it has some cool features. It can synchronize your lights with music or a microphone….if you are in that sort of thing

Many people though will be interested to control their devices from within their own home automation system.
there are in fact various possibilities to do that.

1-Open a MagicHome account, if you havent already done so.
You can do that via your App, as described in the instruction PDF
Then use either GoogleHome or Alexa to add the MagicHome service and you should be ready to go. As per now, I understand the GoogleHome App offers more functionality for this device than Alexa.

2-You don’t have/don’t want GoogleHome or Alexa (no worry, google knows you don’t want it and has your name on a list that will resurface when they rebrand themselves to SkyNet)
Well, if you happen to have OpenHAB, there is a binding that will let you control this device directly. The process is described thoroughly in the given link, but it comes down to the following steps:
Power up the LED Strip
open PAPER UI and install the WIFI LED Binding
in PaperUI inside THINGS should be something like “AK001-ZJ100” (WiFi LED Device) with corresponding MAC-Adress
click on it and u see something like: “wifiled:wifiled:DC30B89D35CE:power”
add this to your xxx.items File, using the following channels:
{channel=”wifiled:wifiled:DC30B89D35CE:power”}
{channel=”wifiled:wifiled:DC30B89D35CE:white”}
{channel=”wifiled:wifiled:DC30B89D35CE:color”}
{channel=”wifiled:wifiled:DC30B89D35CE:programSpeed”}

3-You just want to use python
Then this is for you: After installing the flux_led software, you will be able to use command line commands to control your WiFi LED

4-You do not have OpenHAB, or Alexa, or GoogleHome (but still want your own access)
Though I am sure that if the controller can be controlled from OpenHAB, it can be controlled from other Domotix systems such as HAS, but I am not sure if anyone already developed that. In that case, you have the option of flashing your controller, as it has an ESP8266 in it. It is not as easy as reflashing a sonoff, as you will need to do some soldering to tiny pads, but it is doable. Xose ‘tinkerman’ Perez describes how. Have a look here as well. Beware though that older replacement software, may not always be compatible as somewhere along the development apparently pin assignments were changed.
Also beware that not all replacement software out there  will implement e.g. your IR or RF functions if you happen to have a combined model.
The IR programming follows the NEC protocol with  the following codes:

UP 0xFF906F
DOWN 0xFFB847
OFF 0xFFF807
ON 0xFFB04F
R 0xFF9867
G 0xFFD827
B 0xFF8877
W 0xFFA857
1 0xFFE817
2 0xFF48B7
3 0xFF6897
FLASH 0xFFB24D
4 0xFF02FD
5 0xFF32CD
6 0xFF20DF
STROBE 0xFF00FF
7 0xFF50AF
8 0xFF7887
9 0xFF708F
FADE 0xFF58A7
10 0xFF38C7
11 0xFF28D7
12 0xFFF00F
SMOOTH 0xFF30CF

If your flashing goes wrong, supposedly the original firmware can be found here, but given the fact that there are various models, I presume the bin file somehow is configurable.