In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces how to build a smart home gateway on the raspberry pie, which has a certain reference value. Interested friends can refer to it. I hope you will gain a lot after reading this article. Let the editor take you to know it.
Required components raspberry pie 3b + and later
Raspberry pie 3rd generation B+ is an ARM-based microcomputer motherboard with SD/ MicroSD card for storage. The motherboard provides USB interface and Ethernet interface to connect keyboard, mouse and network cable. The motherboard has the basic functions of PC, while raspberry pie integrates Wi-Fi, Bluetooth and a large number of GPIO, making it an ideal choice for smart home gateways.
EMQ X Edge
The communication protocols between smart home devices include MQTT,Wi-Fi, Bluetooth and so on. Among them, MQTT is an Internet of things communication protocol based on publish / subscribe mode, which is easy to implement, supports QoS and small messages. In this paper, we will use MQTT protocol as the communication protocol between smart home devices.
Due to the limited memory and processing capacity of Raspberry Pi, we choose EMQ X Edge, which is open source by EMQ, as MQTT broker,EMQ X Edge is a lightweight Internet of things edge computing message middleware that supports deployment on the edge hardware of the Internet of things with limited resources.
EMQ X Kuiper
The data transmission formats between smart home devices are different, and the data is volatile, so we need to deal with the data reported by the devices. In this article, we will use EMQ X Kuiper, which is open source by EMQ, to marginalize the data of smart home devices. EMQ X Kuiper is a lightweight edge streaming messaging engine based on SQL, which can run on edge devices with limited resources.
Through the real-time analysis of all kinds of data of smart home devices, the real-time state management and control of smart home devices can be realized.
Other components
BH1750FVI light intensity sensor
LED
330 Ω resistance
Bread board, several jumpers
Project schematic diagram
Build circuit connections in the environment
Raspberry pie configuration
We chose raspbian 8 as the raspberry pie operating system and python 3 as the project programming language
# create a project directory named smart-home-hubs mkdir ~ / smart-home-hubsEMQ X Edge install and run $cd ~ / smart-home-hubs# download software package $wget https://www.emqx.io/downloads/edge/v4.1.0/emqx-edge-raspbian8-v4.1.0.zip$ unzip emqx-edge-raspbian8-v4.1.0.zip$ cd. / emqx# run EMQ X Edge$. / bin/emqx startEMQ X Kuiper install and run $cd ~ / smart-home-hubs# download the package $wget https://github.com/emqx/kuiper/releases/download/0.4.2/kuiper-0.4.2-linux-armv7l.zip$ unzip kuiper-0.4.2-linux-armv7l.zip$ mv kuiper-0.4.2-linux-armv7l. / kuiper$ cd. / kuiper# to create the rules directory Used to store the rule file $mkdir. / rules# run EMQ X Kuiper$. / bin/server code to write BH1750FVI light sensor data upload
Write code to read and calculate the light intensity data of the BH1750FVI sensor, and publish the light intensity data to the smartHomeHubs/light topic through the MQTT protocol at a frequency of one time per second.
# gy30.pyimport jsonimport timeimport smbusfrom paho.mqtt import client as mqtt# BH1750FVI configDEVICE = 0x23 # Default device I2C addressPOWER_DOWN = 0x00POWER_ON = 0x01RESET = 0x07CONTINUOUS_LOW_RES_MODE = 0x13CONTINUOUS_HIGH_RES_MODE_1 = 0x10CONTINUOUS_HIGH_RES_MODE_2 = 0x11ONE_TIME_HIGH_RES_MODE_1 = 0x20ONE_TIME_HIGH_RES_MODE_2 = 0x21ONE_TIME_LOW_RES_MODE = 0x23bus = smbus.SMBus (1) # MQTT broker configbroker = '127.0.0.1'port = 1883topic = 'smartHomeHubs/light'def read_light (): data = bus.read_i2c_block_data (DEVICE ONE_TIME_HIGH_RES_MODE_1) light_level = round ((data [1] + (256x data [0])) / 1.2,2) return light_leveldef connect_mqtt (): client = mqtt.Client (client_id='light_01') client.connect (host=broker Port=port) return clientdef run (): mqtt_client = connect_mqtt () while True: light_level = read_light () publish_msg = {'lightLevel': light_level} mqtt_client.publish (topic) Payload=json.dumps (publish_msg) print (publish_msg) time.sleep (1) if _ _ name__ = "_ _ main__": run () configure EMQ X Kuiper stream processing rules
We will create a stream called smartHomeHubs on EMQ X Kuiper and configure rules to analyze the light intensity data in real time to control the LED lamp.
In this article, we will calculate the average light intensity, turning on LED when the average light intensity is less than 55 for 5 seconds (and turning off LED when it is greater than 55).
Create flow
$cd ~ / smart-home-hubs/kuiper$. / bin/cli create stream smartHomeHubs'(lightLevel float) WITH (FORMAT= "JSON", DATASOURCE= "smartHomeHubs/light")'
Write the open LED rule (. / rules/onLed.rule)
When the average light intensity for 5 seconds is less than 55, send a "{\" status\ ":\" on\ "}" message to the smartHomeHubs/led topic to open the LED.
{"sql": "SELECT avg (lightLevel) as avg_light from smartHomeHubs group by TUMBLINGWINDOW (ss, 5) having avg_light
< 55;", "actions":[ { "mqtt":{ "server":"tcp://127.0.0.1:1883", "topic":"smartHomeHubs/led", "sendSingle":true, "dataTemplate": "{\"status\": \"on\"}" } } ]} 编写关闭 LED 规则(./rules/offLed.rule) 当持续 5 秒钟平均光照强度大于 55 时,向 smartHomeHubs/led 主题发送 "{\"status\": \"off\"}" 消息关闭 LED。 { "sql":"SELECT avg(lightLevel) as avg_light from smartHomeHubs group by TUMBLINGWINDOW(ss, 5) having avg_light >55; "," actions ": [{" mqtt ": {" server ":" tcp://127.0.0.1:1883 "," topic ":" smartHomeHubs/led "," sendSingle ": true," dataTemplate ":" {\ "status\":\ "off\"}}]}
Add Rul
$. / bin/cli create rule onLed-f. / rules/onLed.rule $. / bin/cli create rule onLed-f. / rules/offLed.rule
View Rul
$. / bin/cli show rules
LED lamp control
Write code to connect to EMQ X Edge and subscribe to smartHomeHubs/led topics. Listen for subscribed MQTT message content, turn on LED when status is on, and close LED when status is off.
# led.pyimport paho.mqtt.client as mqttimport RPi.GPIO as GPIOimport json# MQTT broker configbroker = '127.0.0.1'port = 1883topic =' smartHomeHubs/led'def on_connect (client, userdata, flags, rc): print ("Connecting to the MQTT broker...") If rc = 0: print ("Connection success") else: print ("Connected with result code" + str (rc)) client.subscribe (topic) def on_message (client, userdata, msg): payload = json.loads (msg.payload) led_status = payload.get ('status') gpio_status = GPIO.input (4) if led_status =' on' and gpio_status = = 0: GPIO.output (4) True) print ('LED on') elif led_status =' off' and gpio_status = = 1: GPIO.output (4, False) print ('LED off') else: passdef run (): # connect MQTT broker client = mqtt.Client () client.on_connect = on_connect client.on_message = on_message client.connect (broker, 1883 60) # set Raspberry Pi GPIO pin GPIO.setmode (GPIO.BCM) GPIO.setwarnings (False) GPIO.setup (4, GPIO.OUT) try: client.loop_forever () except KeyboardInterrupt: GPIO.cleanup () if _ _ name__ = = "_ _ main__": run () run the test
Python gy30.py obtains the lighting data and reports the data to the * * smartHomeHubs/light * * topic.
Python led.py subscribes to smartHomeHubs/led topics and listens to LED control information.
When we manually lower or increase the light, we can see that the LED light is also turned on and off.
Thank you for reading this article carefully. I hope the article "how to build a smart home gateway on raspberry pie" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.