[Home IoT] ESP8266 MQTT Client Device

185

2

2

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)

Share

    Recommendations

    • Backyard Contest

      Backyard Contest
    • Sew Tough Challenge

      Sew Tough Challenge
    • Barbeque Challenge

      Barbeque Challenge

    2 Discussions

    0
    None
    memjr73

    1 day ago

    I don't know what kind of relay module you are using, but given I have never seen one with a built in diode in it, you might want to add a freewheeling diode across each if your relays input pins to avoid damage to the two driving transistors.

    1 reply
    0
    None
    RegisHsumemjr73

    Reply 1 day ago

    Yes, I understand what your point. Here is the schematic for the relay module for you reference.

    2019-07-15_120336.png