Site updated at 2016-08-31 05:05:07 UTC
This commit is contained in:
parent
e9165c0f8b
commit
1fb5b5b9ea
136 changed files with 2212 additions and 1236 deletions
|
@ -4,7 +4,7 @@
|
|||
<title><![CDATA[Category: Micropython | Home Assistant]]></title>
|
||||
<link href="https://home-assistant.io/blog/categories/micropython/atom.xml" rel="self"/>
|
||||
<link href="https://home-assistant.io/"/>
|
||||
<updated>2016-08-30T21:57:10+00:00</updated>
|
||||
<updated>2016-08-31T05:03:39+00:00</updated>
|
||||
<id>https://home-assistant.io/</id>
|
||||
<author>
|
||||
<name><![CDATA[Home Assistant]]></name>
|
||||
|
@ -13,6 +13,108 @@
|
|||
<generator uri="http://octopress.org/">Octopress</generator>
|
||||
|
||||
|
||||
<entry>
|
||||
<title type="html"><![CDATA[ESP8266 and MicroPython - Part 2]]></title>
|
||||
<link href="https://home-assistant.io/blog/2016/08/31/esp8266-and-micropython-part2/"/>
|
||||
<updated>2016-08-31T04:17:25+00:00</updated>
|
||||
<id>https://home-assistant.io/blog/2016/08/31/esp8266-and-micropython-part2</id>
|
||||
<content type="html">< was pretty lame, right? Instead of getting information out of Home Assistant we are going a step forward and create our own sensor which is sending details about its state to a Home Assistant instance.
|
||||
|
||||
<!--more-->
|
||||
|
||||
Beside [HTTP POST](https://en.wikipedia.org/wiki/POST_(HTTP)) requests, MQTT is the quickest way (from the author's point of view) to publish information with DIY devices.
|
||||
|
||||
You have to make a decision: Do you want to pull or to poll? For slowly changing values like temperature it's perfectly fine to wait a couple of seconds to retrieve the value. If it's a motion detector the state change should be available instantly. This means the sensor must take initiative.
|
||||
|
||||
An example for pulling is [aREST](/components/sensor.arest/). This is a great way to work with the ESP8266 based units and the Ardunio IDE.
|
||||
|
||||
### <a class='title-link' name='mqtt' href='#mqtt'></a> MQTT
|
||||
|
||||
You can find a simple examples for publishing and subscribing with MQTT in the [MicroPython](https://github.com/micropython/micropython-lib) library overview in the section for [umqtt](https://github.com/micropython/micropython-lib/tree/master/umqtt.simple).
|
||||
|
||||
The example below is adopted from the work of [@davea](https://github.com/davea) as we don't want to re-invent the wheel. The configuration feature is crafty and simplyfies the code with the usage of a file called `/config.json` which stores the configuration details. The ESP8266 device will send the value of a pin every 5 seconds.
|
||||
|
||||
|
||||
```python
|
||||
import machine
|
||||
import time
|
||||
import ubinascii
|
||||
import webrepl
|
||||
|
||||
from umqtt.simple import MQTTClient
|
||||
|
||||
# These defaults are overwritten with the contents of /config.json by load_config()
|
||||
CONFIG = {
|
||||
"broker": "192.168.1.19",
|
||||
"sensor_pin": 0,
|
||||
"client_id": b"esp8266_" + ubinascii.hexlify(machine.unique_id()),
|
||||
"topic": b"home",
|
||||
}
|
||||
|
||||
client = None
|
||||
sensor_pin = None
|
||||
|
||||
def setup_pins():
|
||||
global sensor_pin
|
||||
sensor_pin = machine.ADC(CONFIG['sensor_pin'])
|
||||
|
||||
def load_config():
|
||||
import ujson as json
|
||||
try:
|
||||
with open("/config.json") as f:
|
||||
config = json.loads(f.read())
|
||||
except (OSError, ValueError):
|
||||
print("Couldn't load /config.json")
|
||||
save_config()
|
||||
else:
|
||||
CONFIG.update(config)
|
||||
print("Loaded config from /config.json")
|
||||
|
||||
def save_config():
|
||||
import ujson as json
|
||||
try:
|
||||
with open("/config.json", "w") as f:
|
||||
f.write(json.dumps(CONFIG))
|
||||
except OSError:
|
||||
print("Couldn't save /config.json")
|
||||
|
||||
def main():
|
||||
client = MQTTClient(CONFIG['client_id'], CONFIG['broker'])
|
||||
client.connect()
|
||||
print("Connected to {}".format(CONFIG['broker']))
|
||||
while True:
|
||||
data = sensor_pin.read()
|
||||
client.publish('{}/{}'.format(CONFIG['topic'],
|
||||
CONFIG['client_id']),
|
||||
bytes(str(data), 'utf-8'))
|
||||
print('Sensor state: {}'.format(data))
|
||||
time.sleep(5)
|
||||
|
||||
if __name__ == '__main__':
|
||||
load_config()
|
||||
setup_pins()
|
||||
main()
|
||||
```
|
||||
|
||||
Subscribe to the topic `home/#` or create a [MQTT sensor](/components/sensor.mqtt/) to check if the sensor values are published.
|
||||
|
||||
```bash
|
||||
$ mosquitto_sub -h 192.168.1.19 -v -t "home/#"
|
||||
```
|
||||
|
||||
```yaml
|
||||
sensor:
|
||||
- platform: mqtt
|
||||
state_topic: "home/esp8266_[last part of the MAC address]"
|
||||
name: "MicroPython"
|
||||
```
|
||||
|
||||
[@davea](https://github.com/davea) created [sonoff-mqtt](https://github.com/davea/sonoff-mqtt). This code will work on ESP8622 based devices too and shows how to use a button to control a relay.
|
||||
|
||||
]]></content>
|
||||
</entry>
|
||||
|
||||
<entry>
|
||||
<title type="html"><![CDATA[ESP8266 and MicroPython - Part 1]]></title>
|
||||
<link href="https://home-assistant.io/blog/2016/07/28/esp8266-and-micropython-part1/"/>
|
||||
|
|
|
@ -98,6 +98,44 @@
|
|||
|
||||
<h2>2016</h2>
|
||||
|
||||
<article>
|
||||
<div class="grid">
|
||||
|
||||
<div class="grid__item one-fifth palm-one-whole">
|
||||
<time datetime="2016-08-31T04:17:25+00:00" pubdate>
|
||||
<span class='month'>Aug</span> <span class='day'>31</span>
|
||||
</time>
|
||||
</div>
|
||||
<div class="grid__item four-fifths palm-one-whole">
|
||||
<h1 class="gamma"><a href="/blog/2016/08/31/esp8266-and-micropython-part2/">ESP8266 and MicroPython - Part 2</a></h1>
|
||||
|
||||
<footer class="meta">
|
||||
<span>
|
||||
<i class="icon-tags"></i>
|
||||
<ul class="tags unstyled">
|
||||
|
||||
|
||||
<li><a class='category' href='/blog/categories/esp8266/'>ESP8266</a></li>
|
||||
|
||||
<li><a class='category' href='/blog/categories/how-to/'>How-To</a></li>
|
||||
|
||||
<li><a class='category' href='/blog/categories/mqtt/'>MQTT</a></li>
|
||||
|
||||
<li><a class='category' href='/blog/categories/micropython/'>Micropython</a></li>
|
||||
|
||||
|
||||
</ul>
|
||||
</span>
|
||||
</footer>
|
||||
|
||||
<hr class="divider">
|
||||
</div>
|
||||
|
||||
</div>
|
||||
</article>
|
||||
|
||||
|
||||
|
||||
<article>
|
||||
<div class="grid">
|
||||
|
||||
|
@ -189,6 +227,12 @@
|
|||
<ul class="divided">
|
||||
|
||||
|
||||
<li class="post">
|
||||
<a href="/blog/2016/08/31/esp8266-and-micropython-part2/">ESP8266 and MicroPython - Part 2</a>
|
||||
</li>
|
||||
|
||||
|
||||
|
||||
<li class="post">
|
||||
<a href="/blog/2016/08/28/notifications-hue-fake-unification/">0.27 is here to break eggs and take names: notifications, Hue fakery, safety and unification come to Home Assistant</a>
|
||||
</li>
|
||||
|
@ -212,12 +256,6 @@
|
|||
</li>
|
||||
|
||||
|
||||
|
||||
<li class="post">
|
||||
<a href="/blog/2016/08/07/optimizing-the-home-assistant-mobile-web-app/">Optimizing the Home Assistant mobile web app</a>
|
||||
</li>
|
||||
|
||||
|
||||
</ul>
|
||||
</section>
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue