Showing posts with label MQTT. Show all posts
Showing posts with label MQTT. Show all posts

2020/04/15

MagicHome RGB light strip with ESPHome

I’ve been play with ESPHome for a bit and it is turning out to be really great. I love that I can just write declarative YAML to define what my device should do and it just works. I had been learning about the esp-01 devices using the Arduino IDE and setting up my own OTA system, coding my own MQTT functions, and my own temperature sensor and alerting logic so I already understood what I was doing at that level. That was all very fun too but ESPHome made it so much easier that I’ll probably be hooked for a while.
ESPHome works on all kinds of devices based on the ESP8266 or ESP32 chips. I picked up a MagicHome RGB light strip controller device because it has that chip and I wanted the option to customize it. Mine actually has a ESP8285 chip, which is just a smaller/cheaper version of the ESP8266.
I have this old big metal “R” on the wall that I wanted to backlight and this turned out to be a perfect match for this project. The R was a birthday gift from my wife. She found it in an antique store and learned that it was originally part of a SEARS store sign.

Attaching the light strip

I constructed a spacer with foam core and duct tape to hold the R about 1 inch off the wall and give a place to stick the adhesive RGBW light strip. It was quick, easy to work, with and turned out pretty well being all hidden behind the R.

Soldering required to access the serial port

Soldering is not really that hard but it’s a skill that can take a little practice. Removing components from an old circuit board is a good way to learn the basics. I’ve been using this cordless soldering iron and it’s super convenient.
To flash the ESP chip you need access to the serial connectors. Once ESPHome is flashed to the chip you’ll never need it again so just go ahead and desolder the wires. These sites have good instructions on where the wires need to be soldered:
Be sure to notice the part about connecting GPIO-0 to Ground to put the chip into programming mode. I use this little USB ESP-01 programmer and it has a convenient switch to do that.
Now you are ready to flash new firmware.

Stock firmware backup/restore

This is optional but sometimes it’s nice to have the option to restore the device if needed.
cd ~/esphome/ && python3 -m venv venv
source venv/bin/activate
pip install tornado esptool esphome

# backup
venv/bin/esptool.py --port /dev/cu.usbserial-1420 read_flash 0x00000 0x100000 original_firmware.bin

# erase
venv/bin/esptool.py --port /dev/cu.usbserial-1420 erase_flash

# restore
venv/bin/esptool.py --port /dev/cu.usbserial-1420 write_flash -fs 1MB -fm dout 0x0 original_firmware.bin

First time flashing ESPHome

Install ESPHome (see the getting started guides)
cd ~/esphome/ && python3 -m venv venv
source venv/bin/activate
pip install tornado esptool esphome
Flash from the command line
esphome config/magichome_led_strip.yaml wizard
esphome config/magichome_led_strip.yaml run
Flash from the web interface at http://localhost:6052/:
esphome config/ dashboard
After ESPHome is flashed everything can now be re-flashed OTA or over-the-air. You can desolder the extra wires and put away your USB programmer.

The YAML file

To reconfigure the device with ESPHome you just change the YAML file and upload it.
When you want to make changes start up the web interface at http://localhost:6052/
esphome config/ dashboard
Edit your YAML file and upload it. This is what my YAML file looks like:
esphome:
  name: magichome_led_strip
  platform: ESP8266
  board: esp01_1m

wifi:
  ssid: "your-wifi-network-name"
  password: "your-wifi-network-password"

api:

logger:

ota:

# https://esphome.io/components/mqtt.html
mqtt:
  broker: "your-mqtt-server-ip-address"
  username: "your-mqtt-username"
  password: "your-mqtt-password"

light:
  - platform: rgb
    id: light_1
    name: "LED Strip"
    red: red_channel
    green: green_channel
    blue: blue_channel
    effects:
      - automation:
          name: Fade
          sequence:
            - light.control:
                id: light_1
                brightness: 100%
            - delay: 1s
            - light.control:
                id: light_1
                brightness: 50%
            - delay: 1s

output:
  - platform: esp8266_pwm
    id: red_channel
    pin: GPIO5
  - platform: esp8266_pwm
    id: green_channel
    pin: GPIO12
  - platform: esp8266_pwm
    id: blue_channel
    pin: GPIO13

# https://esphome.io/components/remote_receiver.html
remote_receiver:
  dump: all
  pin:
    number: GPIO4
    inverted: True

binary_sensor:
  - platform: remote_receiver
    name: "on"
    nec:
      address: 0x00FF
      command: 0xB04F
    on_press:
      then:
        - light.control:
            id: light_1
            state: on
        - light.turn_on: light_1

  - platform: remote_receiver
    name: "off"
    nec:
      address: 0x00FF
      command: 0xF807
    on_press:
      then:
        - light.control:
            id: light_1
            state: off
        - light.turn_off: light_1

  - platform: remote_receiver
    name: "red"
    nec:
      address: 0x00FF
      command: 0x9867
    on_press:
      then:
        - light.control:
            id: light_1
            red: 100%
            green: 0%
            blue: 0%

  - platform: remote_receiver
    name: "green"
    nec:
      address: 0x00FF
      command: 0xD827
    on_press:
      then:
        - light.control:
            id: light_1
            red: 0%
            green: 100%
            blue: 0%

  - platform: remote_receiver
    name: "blue"
    nec:
      address: 0x00FF
      command: 0x8877
    on_press:
      then:
        - light.control:
            id: light_1
            red: 0%
            green: 0%
            blue: 100%

  - platform: remote_receiver
    name: "white"
    nec:
      address: 0x00FF
      command: 0xA857
    on_press:
      then:
        - light.control:
            id: light_1
            red: 100%
            green: 100%
            blue: 100%

  - platform: remote_receiver
    name: "brighter"
    nec:
      address: 0x00FF
      command: 0x906F
    on_press:
      then:
        - light.control:
            id: light_1
            brightness: 100%

  - platform: remote_receiver
    name: "dimmer"
    nec:
      address: 0x00FF
      command: 0xB847
    on_press:
      then:
        - light.control:
            id: light_1
            brightness: 50%

  - platform: remote_receiver
    name: "fade"
    nec:
      address: 0x00FF
      command: 0x58A7
    on_press:
      then:
        - light.control:
            id: light_1
            effect: Fade

  - platform: remote_receiver
    name: "r2"
    nec:
      address: 0x00FF
      command: 0xE817
    on_press:
      then:
        - light.control:
            id: light_1
            red: 100%
            green: 30%
            blue: 0%
  - platform: remote_receiver
    name: "r3"
    nec:
      address: 0x00FF
      command: 0x02FD
    on_press:
      then:
        - light.control:
            id: light_1
            red: 100%
            green: 50%
            blue: 0%
  - platform: remote_receiver
    name: "r4"
    nec:
      address: 0x00FF
      command: 0x50AF
    on_press:
      then:
        - light.control:
            id: light_1
            red: 60%
            green: 40%
            blue: 0%
  - platform: remote_receiver
    name: "r5"
    nec:
      address: 0x00FF
      command: 0x38C7
    on_press:
      then:
        - light.control:
            id: light_1
            red: 100%
            green: 100%
            blue: 20%

  - platform: remote_receiver
    name: "g2"
    nec:
      address: 0x00FF
      command: 0x48B7
    on_press:
      then:
        - light.control:
            id: light_1
            red: 0%
            green: 100%
            blue: 50%
  - platform: remote_receiver
    name: "g3"
    nec:
      address: 0x00FF
      command: 0x32CD
    on_press:
      then:
        - light.control:
            id: light_1
            red: 30%
            green: 100%
            blue: 100%
  - platform: remote_receiver
    name: "g4"
    nec:
      address: 0x00FF
      command: 0x7887
    on_press:
      then:
        - light.control:
            id: light_1
            red: 0%
            green: 100%
            blue: 100%
  - platform: remote_receiver
    name: "g5"
    nec:
      address: 0x00FF
      command: 0x28D7
    on_press:
      then:
        - light.control:
            id: light_1
            red: 0%
            green: 50%
            blue: 50%

  - platform: remote_receiver
    name: "b2"
    nec:
      address: 0x00FF
      command: 0x6897
    on_press:
      then:
        - light.control:
            id: light_1
            red: 30%
            green: 0%
            blue: 100%
  - platform: remote_receiver
    name: "b3"
    nec:
      address: 0x00FF
      command: 0x20DF
    on_press:
      then:
        - light.control:
            id: light_1
            red: 70%
            green: 0%
            blue: 100%
  - platform: remote_receiver
    name: "b4"
    nec:
      address: 0x00FF
      command: 0x708F
    on_press:
      then:
        - light.control:
            id: light_1
            red: 100%
            green: 0%
            blue: 100%
  - platform: remote_receiver
    name: "b5"
    nec:
      address: 0x00FF
      command: 0xF00F
    on_press:
      then:
        - light.control:
            id: light_1
            red: 100%
            green: 0%
            blue: 50%

That’s all I’ve got! Have fun!

2019/08/18

RaspberryPi rtl_433 to MQTT gateway

Parts list:

  1. RasberryPi Zero W (and optional case)
  2. Micro SD card
  3. any USB power supply and micro USB cable
  4. RTL2838U DVB USB tuner with an external antenna

First get the RaspberryPi running:

  1. Download the latest Raspbian Lite OS image
  2. Flash the Raspbian image to your SD card with Etcher
  3. Enable WiFi and SSH on your SD card before putting it in the Pi

    mount the SD card on your computer

    Enable SSH:

    cd /Volumes/boot/ && touch ssh

    Setup WiFi:

    echo "ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
    update_config=1
    country=us
    network={
        ssid=\"YOUR_SSID\"
        psk=\"YOUR_WIFI_PASSWORD\"
        key_mgmt=WPA-PSK
    }
    " >> wpa_supplicant.conf
  4. Eject the SD card, put it in the RaspberryPi, and boot the Pi.

Find the Pi's IP address and ssh to it.
arp -a
nmap -p 22 --open -sV 192.168.1.0/24

ssh pi@IPADDRESS
default password is "raspberry"

sudo raspi-config
expand the file system to use the whole SD card space - should happen automatically on first boot

change the password or use ssh-copy-id setup access

upgrade the software:
sudo apt-get update -y && sudo apt-get dist-upgrade -y && sudo apt-get autoremove -y

Now your RaspberryPi has a base OS that is ready to setup for whatever work you want it to do. In this case I want it to run the rtl_433 software to capture wireless temerature sensor data and send it to my MQTT server.

Setup the rtl_433 software

Here is a great guide to setting up and running the rtl_433 software: http://www.sensorsiot.org/install-rtl_433-for-a-sdr-rtl-dongle-on-a-raspberry-pi/

I have Acurite sensors and want to see the temperature in Fahrenheit so I used " -R 40 -R 41 -C customary" in my config file.

This shows how I'm running the software:

pi@raspberrypi:~ $ cat /etc/supervisor/conf.d/rtl_433.conf

[program:rtl_433]
command=/home/pi/rtl_433/build/src/rtl_433 -R 40 -R 41 -C customary -F "mqtt://YOUR_MQTT_SERVER:1883,,user=YOUR_MQTT_USERNAME,pass=YOUR_MQTT_PASSWORD,events=YOUR_MQTT_TOPIC"
user=pi
autostart=yes
autorestart=yes
startretries=100
stderr_logfile=/var/log/rtl_433/rtl_433.err.log
stdout_logfile=/var/log/rtl_433/rtl_433.log



That's it! Now just plug it in somewhere that it can recieve the 433Mhz signals and connect to the Wifi. You should start getting the temperature data on your MQTT topic.

2019/07/27

ESP8266 fridge/freezer monitor

I haven't posted anything for a while but I've continued to explore new technology and some of my recent interests have involved the Arduino ecosystem and the ESP8266 chip. My intro to Arduino was a few years back when I built a "Koala ambulance" for my preschooler's car show day. It was pretty basic and just flashed the LED lights but it was a big hit with that age group.

Later on I heard about the low cost ESP8266 boards with WiFi that are compatible with the Arduino platform. As I learned more about these boards I found them in all sorts of cheap IoT devices like light bulbs and IR blasters. There are better boards available now using the ESP32 chip and others that support encryption and can be used securely with cloud IoT services.

I decided that the older refrigerator in my garage needed some temperature monitoring and this was the perfect job for something like the tiny ESP-01 board. This project used the ESP-01 board, the onewire DS18B20 temp sensor, an I2C 1620 LCD display, a MQTT message bus, Node-RED, Node-RED dashboard, and IFTTT.

At a high level the system operates like this. When the ESP-01 is turned on it joins the WiFi network and connects to the MQTT message broker. Then it starts sending temperature sensor readings to the MQTT channel and updating the LCD display. If the temperature is outside the desired range the piezo buzzer beeps a warning tone. Elsewhere on the network a Raspberry Pi is running Node-RED and is subscribed to the MQTT channel to receive the temperature sensor messages. Node-RED records and graphs the temperatures and is also connected to IFTTT for sending alerts to a smartphone. You can see the Arduino source code for my temperature sensor on GitHub.




Some future improvements will be 3D printing a nice case so it doesn't look like the cardboard and duct tape mess it is now. Also adding a backup battery power supply would keep it showing and reporting the temperature when the power is out. Showing the max/min temps over the last few weeks would be useful too.

Some of the excellent resources I found along the way were: