Introduction: [Home IoT] ESP8266 MQTT Client Device

About: My Chinese name is 許英豪. My background is Electronic and software engineering. I have over 30 years design experience on the SmartPhone, Tablet and Digital camera. I love DIY!

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

Step 1: Description

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

Step 2: Schematic & Components

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

Step 3: Placement and Soldering

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.

Step 4: Testing

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.

Step 5: Testing - MQTT Apps on Mobile Phone

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.

Step 6: Making a Case (For Reference)

I am using Sketchup to made this case.

Step 7: Another Useful Upgrade(For Reference)

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)