Home Assistant using MQTT with LoRa and Tuya integration

Student: Zsugya Norbert-Gabriel

Mater: ACES II

Video, Presentation and Source code: https://1drv.ms/f/s!AiFSSSAJY4tDjVj-gACJLlt7F4Vg?e=sLH02d

Objective

I will implement a control system for house lights. I will use 2 ESP8266 boards to communicate with each other by LoRa RFM95 modules. One ESP board will have a microphone sensor for clap detection and will transmit over radio this event. On the other side, the other ESP board will receive over radio the microphone status and will communicate over MQTT with Home Assistant server. A smart switch will be connected to Home Assistant using Tuya plugin, and will create a scenario, each time clap is detected, the switch will change its state.

Hardware description & implementation

Components:

The schematic diagram of the connections between ESP8266, Microphone and LoRa RFM95 is the following:

|System Schematic Diagram

Software description & implementation

The application is based on 3 different parts like in Figure 5:

|System architecture

Having a sensor and a bridge enables the fact that the sensor can be placed at a long distance from Wi-Fi router, feature enabled by LoRa protocol. This architecture is IoT Level 5, meaning that we have a cloud application, a coordinator node and many end nodes.

The dashboard for controlling the lights can be used from both web browser or mobile application and it is fully customizable.

home_assistant_phone.jpeg

For controlling the smart switch using Tuya plugin, I set an automation scene which sets the state of the switch when a new value on MQTT clap detection topic is reached.

There are necessary the following MQTT settings for Home Assistant server to be able to communicate with the subscriber.

#add mqtt devices
mqtt:
  sensor:
    - state_topic: "home/livingroom/temp"
  switch:
    - unique_id: livingroom_led_switch
      name: "Livingroom Led Switch"
      state_topic: "home/livingroom/led_switch"
      command_topic: "home/livingroom/led_switch/cmd"
      #availability:
      #  - topic: "home/bedroom/switch1/available"
      payload_on: "255"
      payload_off: "0"
      state_on: "255"
      state_off: "0"
      optimistic: false
      qos: 2
      retain: true
void callback_LoRa(int packetSize) {
  // received a packet
  TRACE_LOG("Received packet '");
  // read packet
  String message = LoRa.readString();
  TRACE_LOG(message);  
  // print RSSI of packet
  TRACE_LOG("' with RSSI ");
  TRACE_LOGln(LoRa.packetRssi());
  if(message == LIVING_LED_SWITCH_CMD.path){
    led_state = 255 - led_state;
    digitalWrite(LED_PIN, 255 - led_state);  
    client.publish(LIVING_LED_SWITCH_STATE.path.c_str(), String(led_state).c_str());  
  }  
}

Clap detection function features:

//reset clap_counts
if(clap_counts >= NOISE_CLAPS_NO){
  if ((millis() - last_clap_millis) <= NOISE_DETECTION_INTERVAL){
    TRACE_LOGln("Detected " + String(clap_counts) + " claps in " + String(NOISE_DETECTION_INTERVAL) + "s. Disabling Clap feature for " + String(CLAP_FEATURE_DISABLE_TIME) + "s");
    clap_enable = DISABLED;
  }
  clap_counts = 0;
  last_clap_millis = millis();
  TRACE_LOGln("Reset clap abundance phase");
}
// enable again clap feature
if ((millis() - last_clap_millis) >= CLAP_FEATURE_DISABLE_TIME && clap_enable == DISABLED){
  clap_enable = ENABLED;
  TRACE_LOGln("Clap feature enabled");
}

The dashboard for controlling the lights can be used from both web browser or mobile application and it is fully customizable.

Challenges

Conclusions

In conclusion, in this project I managed to create a full home smart light automation system with clap detection flavor. I learned how to work with MQTT and LoRa protocols and integrate all with Home Assistant. I also experimented with house electrical circuits.

Resources