相关文章推荐
谦逊的板栗  ·  c++ - In CMake, how ...·  7 月前    · 
谦逊的板栗  ·  jpa repository ...·  11 月前    · 
谦逊的板栗  ·  rowid and rownum in ...·  11 月前    · 
谦逊的板栗  ·  ORA-01654 unable to ...·  11 月前    · 
谦逊的板栗  ·  文件系统 | 微信开放文档·  11 月前    · 
谦逊的板栗  ·  Keeping Web Shells ...·  1 年前    · 
坏坏的麦片  ·  SqlParameter.Value ...·  1小时前    · 
爱旅游的铁链  ·  Uses of Class ...·  1小时前    · 
独立的红豆  ·  SqlParameter ...·  1小时前    · 
聪明伶俐的课本  ·  SpringBoot ...·  3 小时前    · 

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

Jorge Jorge
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.

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

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

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.

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

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.

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...

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.

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

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

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?

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

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?

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.

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.

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....

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...

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

 
推荐文章