Target
Press the button in the mobile app to open the bedroom door.
Basic principle
Raspberry Pi as a control device: It runs a Linux system, so it is powerful and provides a GPIO interface, so it is easy to control the physical door mechanism.
Yeelink Platform as a cloud provider: it is very difficult to build your own Web server, Yeelink provides free internet of things cloud services, and open APIs that can be used to write mobile apps.
Register a switch-type sensor on the Yeelink platform, usually with value of 0.
Raspi every 5s through the Yeelink HTTP API to check the value values, when detected 1 o'clock open.
Develop an Android app that can manipulate value values via the Yeelink HTTP API to enable remote door opening.
"Initial Configuration Raspi"
Assemble the RASPI housing. Heat sinks should be installed. Consider adding a cooling fan during the summer.
(View CPU Temperature command: CAT/SYS/CLASS/THERMAL/THERMAL_ZONE0/TEMP)
Connect the TF card to the Windows computer via the card reader and burn the RASPI system to the TF card with Win32diskimager.
RASPI system image can be downloaded from official website.
If you need to restore the TF card, you can burn bootsector image.
To configure video output:
Open the TF card on Windows, find Config.txt, modify the content
Hdmi_force_hotplug=1
hdmi_group=2
Hdmi_mode=16
hdmi_drive=2
Config_hdmi_boost=4
sdtv_mode=2
arm_freq=800
Plug the tf card, keyboard, HDMI cable, and other required peripherals into the RASPI and turn the power on.
The first boot into the semi-graphical setup interface, select extended memory card space, modify the PI user password, select the boot default command line, TimeZone set to Asia, the keyboard is set to 104-us, confirm and automatically restart.
Configure Auto Connect WiFi:
Make sure the wireless card is working properly.
sudo nano/etc/wpa_supplicant/wpa_supplicant.conf adds a shape at the end of the file as follows network={ssid= "name" psk= "password" to save the exit.
sudo reboot
Test to automatically connect to the specified WiFi when powered on.
View the IP address of the RASPI from the boot output or from the router.
Using Putty on Windows, use Connectbot on your Android phone to connect to SSH.
Use FileZilla on Windows to make an SFTP connection with the connection address starting with sftp://. You can no longer use stand-alone displays and keyboards.
"Timed Shutdown"
sudo crontab-u root-e
Add the following content
* * * */sbin/init 0
"Start-up Run"
sudo nano/etc/rc.local
Add the following content
sudo nohup python-u/home/pi/unlockhandler.py >/home/pi/unlockhandler.log 2>&1 &
"Download Installation Dependencies"
sudo apt-get install Python-dev
Download rpi.gpio-0.5.11.tar.gz to/home/pi/
Tar xvzf rpi.gpio-0.5.11.tar.gzcd rpi.gpio-0.5.11sudo python setup.py install
Download requests-2.7.0.tar.gz to/home/pi/
Tar xvzf requests-2.7.0.tar.gz
CD requests-2.7.0
sudo python setup.py install
RM requests-2.7.0.tar.gz
RM rpi.gpio-0.5.11.tar.gz
sudo rm-rf requests-2.7.0
sudo rm-rf rpi.gpio-0.5.11
"Python-based Gpio"
Four Gpio pins control the stepper motor, three Gpio pins control RGB tri-color LED lights.
Writing unlockhandler.py
Source:
Import Jsonimport requests import Rpi.gpio as Gpio import Time gpio.setmode (Gpio. BOARD) led_r = 40led_g = 38led_b = 36stepper_a = 37stepper_b = 35stepper_c = 33stepper_d = 31 gpio.setwarnings (F Alse) Gpio.setup (Led_r, GPIO. Out) Gpio.setup (Led_g, GPIO. Out) Gpio.setup (Led_b, GPIO. Out) Gpio.setup (Stepper_a, GPIO. Out) Gpio.setup (Stepper_b, GPIO. Out) Gpio.setup (Stepper_c, GPIO. Out) Gpio.setup (Stepper_d, GPIO. Out) gpio.setwarnings (True) gpio.output (led_r, 0) gpio.output (led_g, 0) gpio.output (led_b, 0) Gpio.output ( Stepper_a, 0) gpio.output (stepper_b, 0) gpio.output (stepper_c, 0) gpio.output (stepper_d, 0) def SetStep (W1, W2, W3, W4): Gpio.output (stepper_a, W1) Gpio.output (Stepper_b, W2) GPI O.output (Stepper_c, W3) Gpio.output (stepper_d, W4) def forward (delay, steps): for I in Ran GE (0, steps): setstep (1, 0, 1, 0) time.sleep (delay) SetStep (0,1, 1, 0) time.sleep (delay) SetStep (0, 1, 0, 1) Time.sleep (delay) SETST EP (1, 0, 0, 1) Time.sleep (delay) def backwards (delay, steps): for I in range (0, steps): &nbs P SetStep (1, 0, 0, 1) Time.sleep (delay) SetStep (0, 1, 0, 1) Time.sleep (delay) & nbsp SetStep (0, 1, 1, 0) time.sleep (delay) SetStep (1, 0, 1, 0) time.sleep (delay) & Nbsp; apiurl = ' http://api.yeelink.net/v1.0/device/*****/sensor/*****/datapoints ' apiheaders = {' U-apikey ': ' ******************************** '} payload={' value ': 0}rpost = Requests.post (Apiurl, headers =apiheaders, Data=json.dumps (payload)) Print time.strftime ('%h:%m:%s '), print ("Ready.") gpio.output (Led_r, 1) time.sleep (0.5) gpio.output (led_r, 0) gpio.output (led_g, 1) time.sleep (0.5) Gpio.output (led_g, 0) gpio.output (led_b, 1) time.sleep (0.5) gpio.outPut (led_b, 0) while True: rget = Requests.get (apiurl,headers=apiheaders) DIC = Json.loads (rget.text) #print time.strftime ('%h:%m:%s '), if dic[' value '] = = 1: Print Time.strftime ('%h:%m:%s '), print ("unlocking!") Gpio.output (Led_r, 1) # unlock work flow forward (0.01, 128)
Time.sleep (2) backwards (0.01) setstep (0, 0, 0, 0) payload={' value ': 0} rpost = Requests.post (Apiurl, Hea Ders=apiheaders, Data=json.dumps (payload)) print ("Done.") Gpio.output (led_r, 0) time.sleep (1) Else: #print ("Stand by.") Gpio.output (Led_g, 1) time.sleep (0.2) gpio.output (led_g, 0) time.sleep (4.8) Brief description:
Use the Requests library to manipulate HTTP requests and use the JSON library to process the returned JSON data.
At the beginning of the run, the pins are configured, the value is initialized to 0, and the three lights blink.
Read value value once per 5s: if it is 1, the stepper motor is opened, then the value is set back to 0, the process is red, and if it is a 0 flash green light.
"Android App"
Main Source code:
public class Mainactivity extends actionbaractivity { Posthandler handler = null; Te Xtview TextView = null; Button Startbutton = null; @Override protected void O Ncreate (Bundle savedinstancestate) { super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); handler = new Posthandler (); TextView = (TextView) Findviewbyid (R.id.textview); Startbutton = (B Utton) Findviewbyid (R.id.startbutton); Startbutton.setonclicklistener (new OCL ()); } Private class OCL implements View.onclicklistener { Public V OID OnClick (View v) { Postthread pt = new Postthread (); &NBSP ; New Thread (PT). Start (); &NBsp } } class Posthandler extends Handler { public Po Sthandler () { } public Posthandler (Looper L) { & nbsp Super (L); } @Override Publi c void Handlemessage (Message msg) { super.handlemessage (msg); //Receive Message update ui Integer StatusCode = msg.what; &NB Sp String status = null; switch (statusCode) {   ; Case 0:status = "failed to send instructions, please check the network!" ";break; Case 200:status =" Command sent successfully! "; startbutton.setenabled (false);break; Default:status = "Error return value:" + StatusCode + "! ";break; } textview.append (" \ n "+ stat US); } } class Postthread implements Runnable { &N Bsp public void Run () { HttpClient HttpClient = null; &NBSP ; HttpGet httpget = null; HttpPost httppost = null; &nbs P HttpResponse response = null; the Integer statusCode = 0; httpClient = new Defaulthttpclient (); &N Bsp HttpGet = new HttpGet ("Http://api.yeelink.net/v1.0/device/*****/sensor/*****/datapoints"); HttpPost = new HttpPost ("Http://api.yeelink.net/v1.0/device/*****/seNsor/*****/datapoints "); Httpget.addheader (" U-apikey "," ******************** "); Httppost.addheader (" U-apikey "," ********************* "); Jsonobject obj = new Jsonobject (); & nbsp Try { Obj.put ("value", 1); httppost.setentity (New Stringentity (Obj.tostring ())); &NBS P response = Httpclient.execute (httppost); StatusCode = Response.getstatusline (). Getstatuscode (); } catch (Exception e) { & nbsp e.printstacktrace (); } &NBS P &NBSp Network thread Send message Message msg = new Message (); Msg.what = statuscode; handler.sendmessage (msg); }&NB Sp }
Setting up a remote door system for Raspberry Pi mobile phone