[Home IoT] ESP8266 MQTT Client Device

by RegisHsu in Circuits > Arduino

2427 Views, 5 Favorites, 0 Comments

[Home IoT] ESP8266 MQTT Client Device

IMG_20190702_230310.jpg
IMG_20190702_230453.jpg
IMG_20190630_115605.jpg

It is interesting that using a cheap WiFi-enabled MCU with the MQTT protocol to control the devices such as the automatic water feeder for my cat. There is my blog for more detail information( https://regishsu.blogspot.com/2019/07/home-iot-esp...

Specification:

  1. connect to a predefined Access-Point SSID and MQTT broker
  2. control the relay turn-on/off 3mins periodically, the esp8266 will into deep sleep mode while relay turn-off.
  3. Remote control by MQTT protocol from mobile phone

Description

mqtt_esp8266_key.png

The ECO system would be

Raspberry Pi 3B+

  1. MQTT broker
  2. Python: paho-mqtt, will apply more feature in the future, for example push notification to mobile phone with data analysis.

ESP8266

  1. control the relay
  2. as the MQTT Client

Schematic & Components

esp8266_relay_fzz_-_Fritzing_-__概要圖檢視_-2.png
IMG_20190619_195638.jpg

Parts list:

  • 1 x ESP6266 12E
  • 1 x 2P relay module
  • 2 x S8050 transistor
  • 2 x 100 ohm resistor
  • 1 x 10uF capacitor
  • 1 x 0.1uF capacitor
  • 1 x LM1117 3.3v module
  • 1 x HLK-PM01 230V AC to 5V/3W DC power module
  • 1 x 5x7cm perfboard 1 x AC Electrical socket

Tools:

  • 1 x 3D printer with PLA filament
  • 1 x Soldering iron

Equipment

  • 1 x Raspberry pi 3B+
  • 1 x Water feeder for example

Placement and Soldering

IMG_20190620_234959.jpg
IMG_20190620_235107.jpg
IMG_20190623_223941.jpg
IMG_20190623_224018.jpg

I put these 2 transistors and resistors under the ESP8266 module to save the space.

Be care that the wire arrangement and placement should not cross interference with others wire.

Notice:

One more thing is do the "open/short" inspection by multimeter to ensure all wire are the right connection.

Testing

2019-07-12_142855.png
IMG_20190626_005245.jpg

There 3 portions need to prepare well for testing. Modify the sketch of the SSID/Password , build the sketch and upload to ESP8266, setup the MQTT broker on RPI 3B+.

Setup the MQTT broker (It is option if you have MQTT broker already)

install the related package on RPI 3B+, and will start the MQTT broker service automatically.

  • sudo apt update
  • sudo apt upgrade sudo apt autoremove sudo apt autoclean sudo apt-get install mosquitto mosquitto-clients

check the MQTT service

  • service mosquitto status


Upload the sketch code

Download the sketch [basic version] and modify the SSID / Password and MQTT broker IP address.

  • #define AP_SSID "your-ssid"
  • #define AP_PASSWD "password"
  • #define MQTT_BROKER "xxx.xxx.xxx.xxx"

and then upload the sketch to ESP8266 module.

Open the terminal window of Arduino IDE on the PC to trace the log from ESP8266, turn on the power source, the esp8266 will start connect to your Wifi AP and then connect to MQTT broker.

Testing - MQTT Apps on Mobile Phone

Screenshot_20190712_100310_snr.lab.iotmqttpanel.prod.jpg
Screenshot_20190712_104509_snr.lab.iotmqttpanel.prod.jpg
2019年7月12日 105504 [GMT+0800].png
2019-07-12_134552.png

To verify this ESP8266 module can be control by others MQTT devices, there are several way to do.

Method 1: Send command from RPI by Python. (how to install Mqtt tools)

  • Turn on relay 1-
  • mosquitto_pub -h xx.xx.xx.xx -t Home/esp32_sub -m "11"
  • Turn off relay 1-
  • mosquitto_pub -h xx.xx.xx.xx -t Home/esp32_sub -m "10"

Method 2: Use Mobile Phone App.

  • I have tested several Apps, but why I suggest this one? Because of it seems easy for my stupid head, It is sure that you can use another one by your personal preference.
  • Follow the pictures to set the MQTT broker server and switch button as well as the log.

Making a Case (For Reference)

IMG_20190629_174829.jpg
IMG_20190629_184506.jpg
IMG_20190630_182119_BURST002.jpg
IMG_20190630_212932.jpg
IMG_20190702_230310.jpg

I am using Sketchup to made this case.

Another Useful Upgrade(For Reference)

MQTT_Config.png
esp8266_local_ota_和_MQTT_Config.png

I have done some useful feature that can remote config the SSID/Password and Borker IP address. And also can be OTA to upload the sketch, detail information is here (https://regishsu.blogspot.com/2019/07/home-iot-esp8266-mqtt-client-device-iot.html)