Seeed
Published

Smart Home IoT System Based on Raspberry Pi 4

A smart home system helps users control household devices with web UI or Tmall Elf. (Author: Huang Hao, Wu Shu)

IntermediateFull instructions provided2 days9,886
Smart Home IoT System Based on Raspberry Pi 4

Things used in this project

Hardware components

Raspberry Pi 4B
×1
NodeMCU v2 - Lua based ESP8266 development kit
×1
Grove Beginner Kit for Arduino
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Code

ESP8266 control air conditioner switch machine program

C/C++
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <Arduino.h>
#include <IRremoteESP8266.h>
#include <IRsend.h>
 
//ir
const uint16_t kIrLed = 4;  // ESP8266 GPIO pin to use. Recommended: 4 (D2).
 
IRsend irsend(kIrLed);  // Set the GPIO to be used to sending the message.
uint16_t OFF[101] = {5832, 7344,  514, 3426,  516, 3426,  516, 3428,  514, 3428,  518, 1374,  514, 3430,  512, 3428,  514, 3428,  518, 1374,  518, 1374,  516, 1376,  516, 1376,  518, 3424,  514, 1378,  518, 1374,  518, 1374,  520, 1372,  514, 3428,  516, 3426,  518, 3424,  518, 1374,  518, 1376,  514, 1378,  516, 3426,  518, 3424,  516, 1376,  514, 1376,  514, 1378,  516, 3426,  516, 3426,  518, 3426,  516, 1374,  514, 3428,  518, 1374,  516, 3426,  516, 1376,  516, 3426,  518, 1374,  516, 1376,  518, 1376,  518, 1374,  516, 3426,  516, 1376,  516, 3426,  518, 1374,  518, 3422,  518, 3424,  514, 3426,  516, 7196,  518};  // UNKNOWN FB60AF8D
uint16_t ON[101]= {5832, 7322,  540, 3426,  516, 3426,  510, 3430,  518, 3426,  516, 1376,  516, 3426,  516, 1376,  514, 1380,  516, 1374,  516, 1376,  516, 1376,  514, 1376,  514, 3428,  516, 1378,  512, 3430,  516, 3424,  518, 1376,  514, 3428,  518, 3424,  516, 1378,  516, 1374,  512, 3428,  518, 3424,  516, 1378,  514, 3426,  514, 1378,  518, 1374,  514, 3426,  518, 3424,  514, 1376,  516, 1378,  514, 3426,  518, 3424,  518, 1374,  516, 3428,  514, 1378,  516, 3424,  516, 1378,  516, 1376,  516, 1376,  518, 1374,  516, 3426,  516, 1376,  516, 3426,  516, 1376,  516, 3424,  516, 3426,  518, 3426,  516, 7194,  518};  // UNKNOWN 4C3D7CCD
 
 
 
// Update these with values suitable for your network.
const char* ssid = "xxx";
const char* password = "xxx";
const char* mqtt_server = "xxx";//youe raspberry pi ip address
 
WiFiClient espClient;
PubSubClient client(espClient);
int SwitchedPin = 0;
String switch1;
String strTopic;
String strPayload;
 
void setup_wifi() {
 Serial.begin(115200);
 irsend.begin();
  delay(100);
 
  // We start by connecting to a WiFi network
 
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  
  WiFi.begin(ssid, password);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
 
  Serial.println("");
  Serial.println("WiFi connected");  
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}
 
void callback(char* topic, byte* payload, unsigned int length) {
  payload[length] = '\0';
  strTopic = String((char*)topic);
  if(strTopic == "ha/switch1")
    {
    switch1 = String((char*)payload);
    if(switch1 == "ON")
      {
        Serial.println("ON");
        digitalWrite(SwitchedPin, HIGH);
        irsend.sendRaw(ON, 101, 38);  // Send a raw data capture at 38kHz.
        Serial.println("ON");
 
      }
    else
      {
        Serial.println("OFF");
        digitalWrite(SwitchedPin, LOW);
        irsend.sendRaw(OFF, 101, 38);  // Send a raw data capture at 38kHz.
        Serial.println("OFF");
 
      }
    }
}
 
 
void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    if (client.connect("arduinoClient")) {
      Serial.println("connected");
      // Once connected, publish an announcement...
      client.subscribe("ha/#");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}
 
void setup()
{
  setup_wifi(); 
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);
 
  pinMode(SwitchedPin, OUTPUT);
  digitalWrite(SwitchedPin, LOW);
}
 
void loop()
{
  if (!client.connected()) {
    reconnect();
  }
  client.loop();
}

Home Assistant configuration.yaml

YAML
mqtt:
  broker: 127.0.0.1
 
switch:
  - platform: mqtt
    state_topic: "ha/switch1"
    command_topic: "ha/switch1"
    payload_on: "ON"
    payload_off: "OFF"
 
tts:
  - platform: google_translate
    language: 'zh-cn'
 
media_player:
  - platform: vlc
 
yeelight:
  devices:
    192.168.123.182:
      name: MI Light
 
 
tunnel2local:
 # frpc命令位置
  frpc_bin: "/home/pi/bin/frpc"
 
 
group: !include groups.yaml
automation: !include automations.yaml
script: !include scripts.yaml

Credits

Seeed

Seeed

102 projects • 168 followers
Seeed R&D Team
Thanks to Hardi Huang.

Comments