Review images
The source code
Old habits, first throw the source code, the old driver can read the source code directly
motivation
- Recently, the company has an interesting project to manipulate hardware with lego-like visual programming tools (on the web).
- So you want to wrap the hardware’s response as a service
- Raspberry PI hardware requires root privileges, but as a service there are no restrictions
- The decoupling
idea
- Treat wechat as a control interface and acquire networking capabilities (remote operation)
- Think of raspberry PI as a medium that connects code to the physical world
Architecture design
- Flask is the initial utility as a Web framework
- Flask-sockets provides the websocket service :led_websocket.py
- Treat led_server as the lower machine and API as the instruction set
Raspberry PI with GPIO
How to use the pins of raspberry PI to operate external devices is recommended to read Mango’s Introduction to RASPberry PI GPIO 01- Using GPIO interface to control LED flickering, written very clearly
With the Rpi.gpio library, we can easily manipulate hardware with Python code with little knowledge of the hardware
The interface definition
Let’s take led as an example to demonstrate how to expose hardware functions as APIS, which can be manipulated via HTTP requests (regardless of operation permissions/security issues, you can simply define a secret key if you wish:? Key =xx), interested in the details of the emotional partners can follow up my source library, I have a plan to do it complete.
Currently we define three functions:
Then use Flask to expose it as a Web API
The code is easy to read:
Route ('/led_up') def led_up(): Rpi.gpio. output(14, True) return 'ok' @app.route('/led_down') def led_down(): Output (14, False) return 'ok' @app.route('/led_up_down') def led_up_down(): for i in range(0, 5): Rpi.gpio. output(14, True) # return 'ok' rpi.gpio. output(14, True) # return 'ok'Copy the code
See led_server.py for the complete source code
Now we can run the service: sudo python led_server.py
Ps: websocket version refer to led_websocket.py
The test interface
The current IP address of my raspberry PI is 192.168.0.106
We tested these apis:
- Curl 192.168.0.106/ led_UP
- Off: curl 192.168.0.106/ led_DOWN
- Blink blink: curl 192.168.0.106/led_up_down
You can also control these lights in your browser (using js) :
xmlhttp=new XMLHttpRequest(); XMLHTTP. Open (" GET ", "http://192.168.0.106/led_up_down", true); / / flashing XMLHTTP. The send ();Copy the code
So we can control the hardware from the browser
Docking WeChat
Docking wechat this step is very simple, if you have read my previous article: the chat bot connected to wechat, directly look at the source. It doesn’t matter if you haven’t read the previous section, the code is easy to understand, so let me list the core parts:
Wxbot import wxbot import requests led_server = 'http://127.0.0.1:5000/' class MyWXBot(wxbot): def _led(self,msg,user_input,action): Format (user_input) self. send_MSg_by_uid (response, MSG ['user']['id']) url = led_server+action requests. Get (url) Response = 'done {}'. Format (user_input) self.send_msg_by_uid(response, msg['user']['id']) def handle_msg_all(self, msg): if msg['msg_type_id'] == 4 and msg['content']['type'] == 0: User_input = MSG ["content"]["data"] #payload={"user_input":user_input} Self. _led(MSG,user_input,'led_down') if 'open' in user_input: self._led(MSG,user_input,'led_up') if 'flash' in user_input: self._led(msg,user_input,'led_up_down')Copy the code
The source code is here: wechat_pi.py
Run it: Python wechat_pi.py. Since you need to scan the code on wechat, connect your raspberry PI to the screen first (using the graphical interface). I’ll use VNC
imagination
Because wechat can be used in the wan, you can control raspberry PI from any place connected to the Internet, for remote control. One of my recent imaginations is to use wechat to send raspberries a message to turn on the air conditioner on the way home from work, so that the air conditioner can be turned on in advance
The principle is also very simple to use the infrared receiver to learn the command set of the remote control of air conditioning (as long as the command of the switch machine), and then use the infrared emitter to forge the remote control of air conditioning. Finally, you can receive wechat
At present, the infrared signal collection part has been completed