Raspberry Pi, Arduino, Sensors and the data they produce

Category Archives: Uncategorized

8266 – We Need Control!

So far in this series,  I have show the build of sensors, and installed MQTT, configured Node-RED, and looked at a dashboard;  cool, but now I want to control things external to my system.

I have read and viewed several blog pages and YouTubes of people using home-built and commercial products to accomplish this goal.

Today, using a couple new libraries, I am exploring the Sonoff 8266 based relay units.  These are quite reasonably priced at $5-10 depending on where you procure them.


Disclaimer: I do not see an FCC marking nor a UL marking, so use at your own RISK. 

Disclaimer 2: I am programming this test via the FTDI Friend by AdaFruit, and I have modified this unit to deliver 3.3 volts versus the default 5 volts it comes from the factory with. Make sure you cut the line on the 5 v pad on the back of the board, and solder the 3.3 v pads together or you risk damaging the sonoff unit.  IF you are using a different unit, refer to the docvumentaiton that came with your unit to ensure you are only delivering VCC of 3.3 volts.

WARNING: Ensure you do not have any connectivity to the mains or AC voltage when you are doing this programming or wiring work!  Only do the programming when you have no AC connections! You can damage yourself and your equipment if you do not take this precaution.

Okay, that out of the way, let me tell you the setup of this unit for programming.

  1.  Open the Sonoff unit, remove the outer plastic shell to gain access to the board. You will be putting some type of connector onto the main board, which will allow your ftdi board to be connected.  I used a breakaway type header I had on hand. pin-header
  2. Find the five pin holes in the middle of the board, and using putty or foam, hold the pin header in place and solder it up. located-for-soldering_ink_liheader-installed
  3. Connect up the 3.3 v to pin 1 which is closest to the black switch.  Connect up ground to pin 4.
  4.  Connect up the TX from your ftdi to the TX on the Sonoff board, pin 3
  5. Connect up the RX from your ftdi to the RX on the Sonoff board, pin 2.
  6. Obtain  the code and libraries from this site.  I am using his connectivity and code for this example, and RUi’s site is a wealth of info that has helped my greatly along this journey!step-1-sonoff-reprogramming
  7. If you have not done so, go and setup the Arduino IDE for the 8266!
  8. Install Rui’s code, and make sure you modify it for you SSID and Password!
  9. Before connecting the ftdi board to your computer, make sure you setup the Sonoff in programming mode my holding down the black button, then plug the usb cable into the ftdi board.
  10. make sure you configure your IDE to use the generic 8266 board, and the right port for your ftdi cable.
  11. Upload the code to your Sonoff, and wait for the programming to complete, you’ll see the IDE show 100% when it is done. it-is-done-programming
  12.  Before disconnecting from the ftdi board, open the serial monitor and make sure the code is working, and that the Sonoff has connected to your wifi network.step-2-find-web-server
  13. Now, disconnect the ftdi board and cabling, and re-assemble the unit in the protective case, and make sure your AC wiring is correct, or get a person who is familiar with AC wiring to get you hooked up to mains power.  I used a three prong extension cord and split it in half, attaching the plug in to the input side of the Sonoff, and the socket to the Output side of the Sonoff.
  14. Now you can plug it up!  I used a desk lamp that was handy, and once plugged in, open a web page to the IP address obtained in step 12 above.
  15. Play with the web page, if you have wired it correctly, your lamp should power off and on with the buttons on this page.
  16. sonoff-web-control

Well, we are done with this step, we have control of the real world from the Sonoff.

The next installment will be to reprogram the Sonoff to use MQTT subscribe to accept commands, and to use MQTT publish commands to communicate the status of the unit!



Programming the 8266 as MQTT Publisher (with DHT-11)

You may recall my earlier post about the weather monitor from the Squix blog that I purchased. I built this unit, and then built the NodeMCU based local weather sensor, but wanted to get away from the cloud based service introduced by that author.  There are many good articles on how to use MQTT and the publish / subscription capabilities that I had to stop looking and just work with one.  I did end up with a blend of code, but a good start was found here: ESP8266 Send DHT  .

We will start with setting up the Arduino IDE, which is crucial to getting the ability to program the 8266.  As you recall there are two ways, one with Arduino IDE and the native one with NodeMCU firmware and Lua programming.  I chose Arduino. I will repeat the disclaimer: I do not know how to get back to the original code if you follow this path!


Our first step is to get the board manager installed:

Start the Adrduino IDE, then open the preferences window, and find the” Additional Boards Manager URLs” location, and enter the following:


Now we will go to add  the actual files:  Tools -> Board -> Boards Manager -> search the ESP8266 and then click Install. (choose the newest version).

Now you wait a bit.


You will need to carefully select your board, exactly as you did when choosing the Arduino Uno versus an Arduino Mini, in this case pick the proper 8266 chipset.  In my case I am using the NodeMCU 1.0 (ESP-12E). You will also find the correct port, mine is “com3:”.

Now we need to ensure the libraries are available. The way you accomplish this is: Open Arduino IDE,  Sketch->Include Library -> manage libraries, search for and find this group of libraries:



The next step is the coding. Since I started with the  Thingspeak cloud based device, I started with this code, which I then modified to update for my wfi ssid and password, and updated to segregate the temperature from the humidity, and then updated to have my  MQTT server IP address.

You can play with the code from OSOYOO above and modify, or you can clone my code here: Github Link.

So to recap, the key changes to this code are as follows.

Put your Wifi information  (SSID and Password) in here:

const char* ssid = “your_SSID”;

const char* password = “Your_PASSWORD”;

Define the Pin you have attached your DHT-11 to here:

// Define NodeMCU D6 pin to as temperature data pin of  DHT11

#define DHT11_PIN D6

And finally, put in the IP of you MQTT Broker/Server:

const char* mqtt_server = “your MQTT server IP”;

You should be ready to send your program to the 8266 with the Arduino IDE at this point.

In the next post, we’ll talk about MQTT clients, testing your environment, and then introduce Node-RED environment.









On the 8266 Road, More Tools to Learn

Moving forward into the 8266 world, I found a variety of tools that people proposed for use, but the ones that came up most were the following: MQTT, Node-RED, and the small-DB of your choice if you want to save any of the data you are sending through from your sensors (maybe to data mine, and track).

MQTT  is a light-weight message transport system that works as a client server publish / subscribe environment.  It is easy to implement, and offers a lot of capabilities.

Node-RED is  a software tool that is used to “wire” up hardware devices, and is a good tool for working on an IoT project. It is described as a browser-based flow editor, (going to have to learn what that means) and if you have a Raspberry Pi with a recent version of the OS, it is already installed.

I know that I will want to data-mine  several of the sensors I have envisioned, so I will add a mysql database to the mix, so let’s get started installing and setting these up.

Since  the Raspberry Pi’s are one of my favorites, I am using the RPi 3, and the latest version of Jessie.  Node-RED is installed, so we will install MQTT first.

I referenced this link to install and test the MQTT environment: Random Nerd Tutorials

It literally took minutes, and I will recap here.

This reference uses Mosquitto, which is an Open Source MQTT Broker. The function of the broker  in this case is to take  the message packet from the sensors and get them to a dashboard, and to record them in mysql for later data mining.

First step is to update the repository, and we need a key to do so:

wget http//:repo.mosquitto.org/debian/mosquitto-repo.gpg.key

Now we will add the key to allow us to authenticate the package:

sudo apt-key add mosquitto-repo.gpg.key

Next we need to get to the directory to update the sources list:

cd /etc/apt/sources.list.d/

Run this command: 
/etc/apt/sources.list.d $ sudo wget http://repo.mosquitto.org/debian/mosquitto-jessie.list

Return to the root directory:


Now you can run the update:

sudo apt-get update

Finally, install the Mosquitto Broker:

sudo apt-get install mosquitto

You can test to make sure the broker is running:

mosquitto -v

You will get a feedback showing the version, and validation of the port the broker is listening on (1800 or 1883) which should be the default, if you are not already using that port.

The next post will build the sensor, and get the 8266 code updated to be able to publish to the broker with temperature and humidity data packets.







OpenHAB Configuration Scenario

I have talked to the sensors and openHAB configuration builds through several blog entries, so today I will summarize my current “as built” situation, for anyone wanting to follow my path.  I will not go into extreme depth, as I detailed each build previously, but I will link to the source pages I used in this build.

Read more of this post

openHAB Success

Since the last blog entry, I have been consumed with building a working environment, and building additional sensors.  I am currently modifying the environment with those additional sensors.

One sensor was particularly difficult for me. It was not really the sensor, nor openHAB, but the way I went about studying the how-to-do-it pages. I’ll explain, just to show how hard you can make a task, even though it is relatively simple.

Read more of this post

openHAB Designer

openHAB Designer is a utility that I think is worthwhile to add to the environment. It allows the editing of the openHAB configuration files with much more ease that with “sudo nano x” on the RPi itself.  I completed my initial installation all from the command line, so I don’t want to hear any commentary on how I should stay true to the platform, I already can do it, now I want to do it easier!

Read more of this post

openHAB Alternate Installation

My last two posts were based on some pages found on the MySensors.org, written by a user who has had much success in getting started, and we good for me, as I really like to play from a script the first few times I mess in a new environment.

Not everyone thinks like I do, so here is an alternate way to start.


First, read through this openHAB getting started page. You can follow this exactly, and if you want to mirror my environment you can make these adjustments below to get rolling.
I used the distribution found here:


Read more of this post

OpenHAB Install and Configuration


The installation for openHAB is fairly simple.  You can use different methods, such as apt-get and download/extraction.  I chose the download/extraction method. I will use quotes to “bring” attention to specific entries, just know that they are not part of the text to be entered.

I started up my Raspberry Pi (RPi) and I established a session via putty from my PC.

Next I logged in with the default pi account.

I recommend using a version 1.7.1 (or higher if available).

Create a directory under /opt, label it “openhab”

sudo mkdir /opt/openhab

Now, how ever you want to do this step, you can.  I am lazy, so I started up a browser on the RPi, and went to the openhab web site [http://www.openhab.org/getting-started/downloads.html], and downloaded the following files:



Now you want to extract these. Change to our new directory.


sudo unzip /home/pi/downloads/distribution-1.7.1-runtime.zip -d /opt/openhab

Now make a directory for the addons:

mkdir addons

sudo unzip /home/pi/downloads/distribution-1.7.1-addons.zip -d /opt/openhab/addons

Okay, you now have an installation.

Let’s get the configuration started. change to the configurations directory.

cd /opt/openhab/configurations

Now we will edit the demo items to get our first sensor setup.

sudo nano items/demo.items

insert the information about your arduino gateway/ftdi interface, [Note this /dev device can be verified by examining dmesg];

String Arduino “Arduino” { serial=“/dev/ttyUSB0@115200” }

Next populate some sensor information:

Number      baseTemp01 “Temperature [%.1f °F]” <temperature>    Number      baseHum01 “Humidity [%.1f %%]” <temperature>

Now startup the environment, and watch for the INFO event to show up:

[INFO ] [runtime.busevents ] Arduino state updated to 0;0;3;0;14;Gateway startup complete.

Next post we will look into rules.

Most of this information came from TimO, within the mysensors.org forum:







A Fork in the Sensor Road

As you have seen in the prior posts, our group has been delving into sensors that we can use to secure our homes, as well as provide knowledge about what is happening in the home environment.

To recap the efforts to date:

  1. Created a group of sensors based on the MySensors.org information:
    1. Sound Sensor
    2. Temperature  Sensor
    3. Motion Sensor
  2. Created a Gateway device, to aggregate the sensor data and deliver it to the “controller” product (PiDome, openHAB)

Controller – the GUI-Dashboard  and Data Processing Engine

After some research, the prior post was about PiDome as the Dashboard for a group of sensors we have build based on the MySensors.org site.  One of the guys also had looked at openHAB project, and suggested we also evaluate that application and compare it to the PiDome.

At the end of the last effort, I was struggling to get enough info on the PiDome / MySensors gateway connection, which left me a bit frustrated.  Both parts seemed to work, but ran into trouble with the integration, so I decided to create a parallel environment with openHAB.

Now, I don’t want to disparage PiDome, in fact, I think it is a solid product, and intend to continue to learn about it and build on it, I just need to dig in and get better with it.

So the fork in the road led me to look for information on the openHAB installation on a Raspberry Pi, and I was rewarded with several different methods on installation, and multiple sets of documentation on how people did so (blogs, instructables, etc).

I started with this instructable as a reference:


This got me the demo system on Raspberry Pi, and I was able to do the demo setup and play around with it. I then went looking for details on how to use mysensors units and gateway with openHAB.  A “google” later, and I had a dozen good links to work with.

My next step was to delete the environment, and I  used the rmdir -Rf to wipe out the openHAB directory.

I then followed the installation steps (I will detail these in the next post) to get a much new release installed.  Basically requiring a directory creation, download of two zip files, and then hooking up the sensor “gateway” to the usb port on the Raspberry Pi.

Next, you need to setup the items & rules for the sensors you are monitoring.  A couple links here for reference materials:



All that being said, it is fairly easy, once you know the secrets!

Here are the basics to get you started (assumption you have loaded the demo configuration):

Check out the directory /opt/openhab/configurations

Note the sub-directories, /items & /rules, you will need to make sure they have what we need to get rolling.

TO “bind” the mysensor gateway, openHAB need to know about the device, and how to talk to it. TO do this you bring in the appropriate .jar file, in my case I brought in the entire group of these. I hope that later in the effort I can remove those I do not think are necessary.  so I have deployed the following:


These are all the components that are necessary to get the environment up and running.  Additional details to follow on the configuration needed to get this off the ground, but we can start by updating the demo.items to include a couple of sensors. we label them and give them an ID. Next we update the demo.rules to define the variable names for these items, as well as what to do when an item (sensor) delivers data.

Once that is completed, you start up the server, plug in the arduino gateway, and do some checks to make sure we are “seeing” the gateway, and then receiving the data:

Here is an example:

2015-04-02 20:19:47.057 [INFO ] [runtime.busevents ] Arduino state updated to 0;0;3;0;9;read:1011010s=0,c=1,t=1,pt=7,l=5:44.7 101;0;1;0;1;44.7 2015-04-02 20:19:47.114 [INFO ] [runtime.busevents ] livingHum01 state updated to 44.7 Hum item: livingHum01 hum: 44.7

Finally, you create the Dashboard page that relays the received sensor data to the user by configuring the “sitemap”.

Once this is completed, you can open a browser and see your data. It is running on Port 8080, which is a standard port.


SO that is in a nutshell what it takes to get openHAB up and running.  There is a bit of configuration to do to get a final setup completed, but most of those steps are just repeats of what was outlined above.  The next post will give the nitty-gritty detail of this installation.





Sensor Project – Moving Forward – Part 2

After the prior success of the PiDome installation (with a couple glitches) I was all ramped up to see the first sensor get installed, and was hopeful it would be simple.

I first went to the Raspberry Pi [RPi]  to hook in the Arduino Pro Mini to the USB port and boom,  The RPi immediately rebooted, taking down the PiDome with it. Bummer!

Apparently the voltage drop is such that the RPi sees it as a problem!  With research, it is indicated that it is best to use a powered USB hub if you are going to hotswap. So if you don’t want this to happen, plug and unplug the USB devices when powered down, or use a powered USB hub, or use the new B+ and Pi2 which manage this current fluctuation better. I also see where a larger amperage of power supply might also alleviate this, so that is another experiment to follow.

This does also point out that I need to investigate the way I start up the PiDome as well. When I found that i was starting before the eth0: has an IP, thus confusing the webserver component, I took it out of the init sequence, and now I think I need to resolve that such that it can be automated, if the RPi restarts and I am not there, everything will be offline. A quick search shows the newer option in the OS to allow the boot to “wait” for the network stratup to complete before finishing, so tnow I can go back and put the PiDome back into the init.d script!

Back to the task at hand, the gateway was attached, but not yet recognized, as seen by the command: lsusb, I see the ftdi friend, but not the Arduino Pro Mini and 12cdetect -y 1 shows no devices, so I have a new problem to research.

The overall approach I am taking is this:


PiDome toBe diagram

The lsusb check shows this:


Now the 1c2detect shows nothing:

i2cdetect result

I will have to dig deeper to figure out why this is not being recognized, before any additional progress can be made.

My next step is to check the logs, and I find that indeed the system is finding the ftdi chip attached to my Arduino Pro Mini:

FTDI Uart Recognized

Now I will investigate how this uart is “spoken” to by PiDome.