Close
0%
0%

Home automation: thermostat with ESP8266 & OpenHab

A project about Home Automation. I want to replace my old thermostat with an IoT ESP8266 + OLED 128x64 Screen + OpenHab + MQTT + Micropython

Public Chat
Similar projects worth following
I show the data received from a DHT22 (Temperature-Humidity Sensor) connected to a ESP12 Nodemcu in a OLED 128x64 Screen via I2C (SSD1306) and also send the data to a Mosquitto (MQTT) Server in a Linux machine that is running OpenHab. I can send ON/OFF commands via MQTT from the OpenHab to start/stop the Boiler (in the prototype is just a led that I turn on/off).

I wanted to do this project because is a good starting point for home automation.
My idea is to use the nice ESP8266 and do everything in python (micropython).
I bought a Nodemcu ESP8266 a while ago and I wasn't using it, so when I heard about the micropython project, I got interested. I flashed the ESP12 with the latest micropython for ESP8266 and starting to play.

I find MQTT as the perfect solution for a project involving IoT and Home Automation, so that's what I was planning to use. As far as I know, nobody has done exactly this thing using micropython (you can find similar things using LUA or C) and adding a OLED 128x64 screen seemed like the right thing to do in a thermostat.

I think a thermostat is one of the most complicated things in home automation out there, so once ready it can be easily scaled to other parts of the home like appliances, lights, shaders, etc. that will be far more easy to automate.

main.py

The Main program. All the functionality here. https://github.com/Naish21/themostat

x-python - 3.77 kB - 08/03/2016 at 22:37

Download

ssd1306.py

Library for the oled 128x64 screen. It's been trimmed to include only I2C functions. Original from: https://github.com/micropython/micropython/blob/master/drivers/display/ssd1306.py

x-python - 3.68 kB - 08/02/2016 at 23:43

Download

umqtt.py

MQTT library for ESP8266. Original from: https://github.com/micropython/micropython-lib/tree/master/umqtt.simple

x-python - 4.52 kB - 08/02/2016 at 23:43

Download

  • 1 × DHT22 Temperature/Humidity Sensor
  • 1 × Monochrome 0.96" 128x64 OLED graphic display I2C interface
  • 1 × Nodemcu ESP12 Prototyping board for ESP8266 (ESP12)
  • 1 × Raspberry Pi As MQTT Broker and OpenHab Server
  • 1 × WiFi Router

  • Late update

    Jorge12/06/2018 at 11:34 0 comments

    It's been a while since my last update, probably becauseI haven't doing much about this project.

    The thing is that things evolve and now where I said I was using openhub, I'd say I would be using HomeAssistant, that seems to be much easier to configure, and it looks like it's becoming something like an standard for Home Automation in the DIY world.

    So, apart from that change, things should be more or less the same, and the problems that were unsolved are in the same status right now. Those problems are mainly those that may ocur in an unexpected event like:

    - what happens when there's an unexpected reboot?

    - what if the raspberry pi stops working?

    - what if the ESP8266 stops working?

    and so on...

    Hope this helps.

  • Video of the prototype

    Jorge08/04/2016 at 21:25 0 comments

    Keep the anotations on:

  • What will be the future of this project?

    Jorge08/04/2016 at 21:02 0 comments

    My initial idea was to keep things as simple as possible. I think micropython is perfect for that.

    I started with a thermostat because it has inputs (temperature & humidity), and different outputs (oled 128x64 screen and a relay to turn on/off), and I think it's just one of the most complicated things you will need at home. The rest, starting from this point, would be just easier.

    So this is only the beggining. My idea is to make some "typical configurations" of modules that can be used as light switches, light shaders, blinds controllers, appliance controllers, presence control, valve control...

    The second phase would be to find a way to integrate all this in a conventional home, without much disturbance. The old switches should work, but you should be able to control the lights from OpenHab. I have some ideas about this that will discuss in the future.

    The most important part of the project would be OpenHab. There's where all the "intelligence" should be.

    So... why is my project different to others out there doing the same thing?
    Because I keep things simple and tidy. The things I'm using are cheap and the less things I can use, the better. (For example, Instead of using RF, I use WiFi).

  • OpenHab Configuration

    Jorge08/03/2016 at 22:44 0 comments

    Once you have your openhab installed, you will need to use the MQTT Binding addon in order to use MQTT with openhab.

    In your openhab configuration file (probably "openhab_default.cfg") you have to include the configuration of the MQTT browser. I've included:

    mqtt:mosquitto.url=tcp://localhost:1883
    mqtt:mosquitto.clientId=openhab
    mqtt:mosquitto.qos=1
    mqtt:mosquitto.retain=true
    mqtt:mosquitto.async=false

    I'm using message retain and qos=1 so everytime I read the MQTT from the ESP8266 there's always an ON or OFF message (that the esp8266 obeys).

    In the items folder, in the config file, you can add something like this:

    /* MQTT Sensors */
    Number Temperature_GF_Living "Temperature [%.1f °C]" <temperature> (Temperature, GF_Living) {mqtt="<[mosquitto:/sensor1/tem:state:default]";}
    Number Humidity_GF_Living "Humidity [%.1f %%]" <water> (Humidity, GF_Living) {mqtt="<[mosquitto:/sensor1/hum:state:default]";}
    Switch Light_GF_Living "Led" (GF_Living, Lights) {mqtt=">[mosquitto:/sensor1/led:command:on:on],>[mosquitto:/sensor1/led:command:off:off]";}

    Restart openhab and everything should work.

  • How did I start all this?

    Jorge08/03/2016 at 11:16 0 comments

    Mi first step was to make things work, one thing at a time.

    I started with the DHT22. Very easy in micropython as the library is already included.

    Then I stepped to the OLED 0,96" screen. Try to show some simple messages using the library from https://github.com/micropython/micropython/tree/master/drivers/display

    Then I stepped to MQTT. Once you understand the way it works, it won't be problematic but when I put everything together had some memory issues. I had to keep the OLED out for a while until I figured out how to maintain my memory usage low.

    The best tutorial I've found to understand MQTT is this one: https://www.linux.com/news/mqtt-building-open-internet-things

    The last thing is OpenHab. I'm into this at the moment...

  • Automation and data storage

    Jorge08/03/2016 at 11:07 0 comments

    That is why I'm using OpenHab: automation (boiler on/off) and data storage (sql database) will be done in this side, as it is an open solution for home automation.

  • Interruptions

    Jorge08/03/2016 at 11:05 0 comments

    I'm not using interruptions at themoment, so I receive messages in the ESP8266 every loop of the main program (every 10s)

    I've resolved this using persistence in the MQTT messages sent to the ESP8266 (messages that at the moment just turn on or off the led on the ESP). So... it works. It isn't the best way, but it just does.

    I'm sure this could make use of interruptions, but I may need some help with that.

  • Problem when MQTT Broker connection lost

    Jorge08/03/2016 at 11:01 0 comments

    As for the MQTT broker connection, it seems like the functions I'm using havesome problems if the connection to the server is lost.

    I'll have to take a look and try these:
    https://github.com/micropython/micropython-lib/tree/master/umqtt.robust

View all 8 project logs

  • 1
    Step 1

    Flash your ESP8266 with Micropython. Follow instructions from here:

    http://docs.micropython.org/en/latest/esp8266/esp8266/tutorial/intro.html
  • 2
    Step 2

    Edit the main.py file to connect to your own Wifi


  • 3
    Step 3

    In the main.py define the GPIO pins you connect the devices to.
    As it is when you download it:
    - DHT22 connected to GPIO16
    - SSD1306 connected using I2C: SDA to GPIO4, SCL to GPIO5
    - Output to the led of the ESP8266 (GPIO2)

View all 8 instructions

Enjoy this project?

Share

Discussions

rafununu wrote 01/14/2024 at 12:55 point

I replaced the defective thermostat of my freezer with a Nano, a relay, an SSD1306 display and a one wire probe. I chosed to lock the temp at -18°C and to display current, lowest and highest temps. Fast and easy.

  Are you sure? yes | no

nodemcu12ecanada wrote 08/20/2020 at 19:14 point

You may be interested in these two home automation projects. One is replacing a waterbed thermostat that failed. The other modifies the thermostat for a gas furnace and also only turns on the hot water tank at low cost time of use in order to save $500+/year.  Two of many NodeMCU projects. All are documented here:https://sites.google.com/site/nodemcu12e/

https://hackaday.io/project/171474-wifi-waterbed-temperature-control

https://hackaday.io/project/172151-home-automation-with-nodemcu

  Are you sure? yes | no

andrew rose wrote 02/12/2018 at 00:59 point

Hi, I'm working on getting your project running but I'm running into some problems, any help would be appreciated

I'm using micro python 1.9.3 witch has umqtt already included though i have loaded your umqtt code and called upon that not the stock driver though i get a similar error with the included module

REPL throws this error

Traceback (most recent call last):
  File "main.py", line 146, in <module>
  File "main.py", line 93, in recepcionMQTT
  File "umqttt.py", line 101, in subscribe
TypeError: function missing 1 required positional arguments

  Are you sure? yes | no

Hack4Life wrote 01/20/2018 at 23:51 point

I put the umqtt.py file where the boot.py and the main.py is located and I get this error when starting the esp8266 board: 

Traceback (most recent call last):
  File "main.py", line 56, in <module>
ImportError: no module named 'umqtt.MQTTClient'

Any idea what I did wrong? 

  Are you sure? yes | no

Jorge wrote 01/21/2018 at 13:36 point

It looks like it can't see the module in the file umqtt.py

Try this: https://stackoverflow.com/questions/24380160/how-to-import-external-library-in-python

  Are you sure? yes | no

Hack4Life wrote 01/23/2018 at 09:48 point

I'm not sure how this link should help me. Connected via repl on my Mac in the console I can run import umqtt without problems. But from umqtt import MQTTClient throws the error.. 

Not quite sure if this helps but with help(umqtt) I get this output: 

object <module 'umqtt'> is of type module

  __name__ -- umqtt

  __path__ -- umqtt

  Are you sure? yes | no

Hack4Life wrote 06/20/2017 at 21:04 point

Step 3 on the Instruction: "DHT22 connected to GPIO16" - How exactly do I connect the DHT22 to the GPIO16 pin? I tried to look in the video for it but I wasn't able to get it..  
Thanks for your work!

  Are you sure? yes | no

gtcfink wrote 01/16/2017 at 04:18 point

Hey Jorge, Nice project.  I've got the ESP8266 loaded with the boot.py & main.py, and Pi loaded with Mosquitto, and can see that piece works.  But I'm not clear as to how to test/verify that my esp8266 is sending the temp/hum reading. 

  Are you sure? yes | no

Jorge wrote 05/03/2017 at 08:06 point

You have to subscribe to the topics of temp/hum. Check the video of the project.

  Are you sure? yes | no

photojpl360 wrote 12/13/2016 at 20:44 point

Very nice projects.  I got it to work with MQTT.  But there seems to be a big problem or there is something I don't understand right.  In your main.py loop seems to be stuck at c.wait().  You can send a topic to change the LED state this works.  But the program seems to hang at c.wait().  You could not do anything else since the loop is stopped.  How can you do something else like polling buttons on the ESP to send a topic while still being able to receive the Led topics?

  Are you sure? yes | no

Jorge wrote 12/14/2016 at 19:53 point

You have to use persitence of the messages in the MQTT server. If you do that, every time the program goes to read something from the MQTT topic, there's always something to read (and thus, it doesn't get stuck in c.wait() ).

  Are you sure? yes | no

Randy Lust wrote 11/20/2016 at 17:29 point

Great job here, trying to get running on 8266. Error I get when I boot is in main.py line 56 Import error no module names "mqtt.MQTTClient". I have the mqtt.py file loaded on ESP8266. Using Micropython 1.8.6-7

  Are you sure? yes | no

Jorge wrote 11/20/2016 at 20:19 point

have you tried using the one I have uploaded here?

  Are you sure? yes | no

Randy Lust wrote 11/20/2016 at 23:27 point

I have, I do not see a boot.py.  I have loaded main.py and other 2, with the needed modifications

Do you use a boot.py?

  Are you sure? yes | no

Jorge wrote 11/27/2016 at 22:15 point

I do use a boot.py. The one that is loaded by default, if I remember it well...
Are you sure the main.py is being executed?

  Are you sure? yes | no

arnaud.goulut wrote 10/24/2016 at 10:09 point

Hello Jorge, I'm trying to make something similar, except I want to display traffic duration from Google API.

But I don't get why you call recepcionMQTT() every time in the main While loop. What if a message arrives when you are not listenning to the topic ?

is c.set_callback(sub_cb) not event driven ?

Thank you.

  Are you sure? yes | no

Jorge wrote 10/30/2016 at 21:10 point

You can use persistence of the messages in the server (and that's what I'm using) so there's always something to read from the server. I'm not sure if that answers your question...

  Are you sure? yes | no

archana wrote 09/22/2016 at 12:03 point

hey!!! i am doing your project but i am unable to connect to the broker ...i am putting local ip address of mosquitto broker but it failed to connect..will u plzz clear my doubts by giving simple steps to connect ...

  Are you sure? yes | no

Jorge wrote 09/22/2016 at 12:14 point

Some tests you can do:
1. Connect a client to the broker (both on the same machine)
2. Connect a client (in a different machine) to the broker
3. Try with the ESP8266

In the computer where you have the Mosquitto running, do the following (In separate terminal windows)
Start the broker:
mosquitto

Start the command line subscriber:
mosquitto_sub -v -t 'test/topic'

Publish test message with the command line publisher:
mosquitto_pub -t 'test/topic' -m 'helloWorld'

As well as seeing both the subscriber and publisher connection messages in the broker terminal the following should be printed in the subscriber terminal:

test/topic helloWorld

If that works, you have succeded in step 1.

  Are you sure? yes | no

damien wrote 09/16/2016 at 14:09 point

Hi Jorge, I am going to build a home and was thinking of automation. Being a newbie in programming I tried to find what would be the best way and came up with openhab,mqtt and micropython so when I saw your project I was delighted.... In your description you wrote about retrofitting light switches but making my wirering from ground up I thought I could save on time and money by having only wireless switches. I 'm going to replicate your project to get an idea and later code (try) an esp8266 to talk to an other one controlling a relay. If you have any comment.... 

  Are you sure? yes | no

Jorge wrote 09/28/2016 at 22:40 point

I missed this message! sorry!

What I would do is install ESP8266 in each light with a relay (or piezoelectric relay) to turn the light on/off (I'd try to maintain the light switches working so you'd have to think about that too). And then control everything with openhab (in a raspberry pi or something similar). You could connecto to your openhab server with your phone, tablet or pc and control everything from there...

  Are you sure? yes | no

s10jam wrote 08/29/2016 at 20:27 point

Do you plan to control a furnace with this project?

  Are you sure? yes | no

Jorge wrote 08/29/2016 at 21:42 point

Yes. Among other things (lights, appliances, blinds, alarm...)

  Are you sure? yes | no

s10jam wrote 08/29/2016 at 21:56 point

Excellent. This is what I'm looking for. I'm finding commercial solutions to be too expensive and lack many key features. 

  Are you sure? yes | no

Jorge wrote 08/30/2016 at 10:48 point

You could use some logic for turning on/off the furnace in the ESP8266 and/or use the OpenHab for this (that's my idea).
At the moment I have to solve a problem that happens when the connection with the mqtt server is lost. Apart from that, the *most* secure form to do all this would be:
- dedicated wifi for the esp8266/mqtt server (raspberry pi)
- SAI (some small batteries) both for the raspberry and the wifi router
- SAI (small battery) for the esp8266

  Are you sure? yes | no

Jorge wrote 08/20/2016 at 09:55 point

If anyone find problems when trying to replicate my project, please let me know in the comments. I'll try to help.

  Are you sure? yes | no

Similar Projects

Does this project spark your interest?

Become a member to follow this project and never miss any updates