{"id":38,"date":"2016-02-19T23:08:24","date_gmt":"2016-02-19T23:08:24","guid":{"rendered":"http:\/\/nerostark.com\/b\/?page_id=38"},"modified":"2020-03-11T17:24:42","modified_gmt":"2020-03-11T17:24:42","slug":"sensors-and-raspberry-pi-to-control-a-mi-light-bulb","status":"publish","type":"page","link":"https:\/\/cantile.xyz\/b\/mi-light\/sensors-and-raspberry-pi-to-control-a-mi-light-bulb\/","title":{"rendered":"Sensors and Raspberry Pi to control a Mi Light bulb"},"content":{"rendered":"<p>Easily control the Mi Light using a Raspberry Pi and some sensors to send the commands found previously <a href=\"http:\/\/nerostark.com\/b\/mi-light-commands-and-tricks\/\">(link)<\/a><\/p>\n<h3>Using a PIR<\/h3>\n<p>Before going on, a quick check to be sure the PIR is working: just hook it up to a breadboard with a LED and power it up using the Raspberry GPIO pins.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-39 size-full\" src=\"https:\/\/cantile.xyz\/b\/wp-content\/uploads\/2016\/02\/pi_light_01.png\" alt=\"PIR check\" width=\"550\" height=\"453\" srcset=\"https:\/\/cantile.xyz\/b\/wp-content\/uploads\/2016\/02\/pi_light_01.png 550w, https:\/\/cantile.xyz\/b\/wp-content\/uploads\/2016\/02\/pi_light_01-300x247.png 300w\" sizes=\"(max-width: 550px) 100vw, 550px\" \/><\/p>\n<p>Raspberry to PIR<br \/>\nPin 2 (5V) connected to Vcc (on the + column of the breadboard)<br \/>\nPin 6 (Ground) connected to GND (on the &#8211; column)<\/p>\n<p>Then, Pin 1 (3.3V) connected to the LED anode (on row 14, the anode is the longer leg of the LED)<br \/>\nPIR signal (the middle pin) connected to the LED cathode (on row 18, the cathode is the shorter leg of the LED)<\/p>\n<p>The led will stay ON when the PIR signal is low (no motion detected), and will turn OFF when motion is detected. The duration of the OFF state in this case can be modified using the notch on the right (in the picture is set to the minimum, which is around 5 seconds). The PIR can be calibrated to our needs it by using the two notches on the board to set delay and sensitivity.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-41 size-full\" src=\"https:\/\/cantile.xyz\/b\/wp-content\/uploads\/2016\/02\/pi_light_03.png\" alt=\"PIR Notches\" width=\"550\" height=\"574\" srcset=\"https:\/\/cantile.xyz\/b\/wp-content\/uploads\/2016\/02\/pi_light_03.png 550w, https:\/\/cantile.xyz\/b\/wp-content\/uploads\/2016\/02\/pi_light_03-287x300.png 287w\" sizes=\"(max-width: 550px) 100vw, 550px\" \/><br \/>\nOnce this step is complete, it is time to send the PIR output signal to the Raspberry. To do this, remove the LED and connect the PIR output to the GPIO pin 18 (BCM 24). In the picture, the pins are connected on row 18 of the breadboard.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-40 size-full\" src=\"https:\/\/cantile.xyz\/b\/wp-content\/uploads\/2016\/02\/pi_light_02.png\" alt=\"PIR Wiring\" width=\"550\" height=\"450\" srcset=\"https:\/\/cantile.xyz\/b\/wp-content\/uploads\/2016\/02\/pi_light_02.png 550w, https:\/\/cantile.xyz\/b\/wp-content\/uploads\/2016\/02\/pi_light_02-300x245.png 300w\" sizes=\"(max-width: 550px) 100vw, 550px\" \/><\/p>\n<p>Now, using the following code I can send an UDP message that will turn on the Mi Light whenever the PIR will be triggered.<\/p>\n<pre><code>import RPi.GPIO as io\nimport socket\nimport time\n\nUDP_IP = \"192.168.1.72\"\nUDP_PORT = 8899\nMESSAGE_ON = \"4200\".decode('hex')\nMESSAGE_OFF = \"4100\".decode('hex')\n\nio.setmode(io.BCM)\npin_pir = 24\nio.setup(pin_pir, io.IN)\n\nwhile True:\n      if io.input(pin_pir):\n           sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, 0)\n           sock.sendto(MESSAGE_ON, (UDP_IP, UDP_PORT))\n           sock.close()\n      else: \n           sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, 0)\n           sock.sendto(MESSAGE_OFF, (UDP_IP, UDP_PORT))\n           sock.close()\n     time.sleep(0.5)\n<\/code><\/pre>\n<p>(check that UDP_IP and UDP_PORT correspond to the address and port of the Mi Light controller.)<\/p>\n<p>Then, once this is saved to pir.py, run<br \/>\n<code>sudo python pi_light.py &amp;<\/code><\/p>\n<p>This code will constantly send UDP packets to the Mi Light controller. Since the controller doesn&#8217;t send information on the state (ON\/OFF) of the lightbulb, and I&#8217;d prefer not to spam the controller with packets, it is possible to define a variable (light_state) and check against it in the <i>while<\/i> loop before sending a packet<\/p>\n<pre><code>while True:\n       if io.input(pin_pir):\n            if light_state == 0 : \n                sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, 0)\n                sock.sendto(MESSAGE_ON, (UDP_IP, UDP_PORT))\n                sock.close()\n                light_state = 1\n        else:\n            if light_state == 1 :\n                sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, 0)\n                sock.sendto(MESSAGE_OFF, (UDP_IP, UDP_PORT))\n                sock.close()\n                light_state = 0\n        time.sleep(0.5)<\/code><\/pre>\n<p>The resulting script doesn&#8217;t weigh too much on the Raspberry<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-44 size-full\" src=\"https:\/\/cantile.xyz\/b\/wp-content\/uploads\/2016\/02\/pi_light_06.png\" alt=\"ps aux | grep python\" width=\"875\" height=\"180\" srcset=\"https:\/\/cantile.xyz\/b\/wp-content\/uploads\/2016\/02\/pi_light_06.png 875w, https:\/\/cantile.xyz\/b\/wp-content\/uploads\/2016\/02\/pi_light_06-300x62.png 300w, https:\/\/cantile.xyz\/b\/wp-content\/uploads\/2016\/02\/pi_light_06-768x158.png 768w\" sizes=\"(max-width: 875px) 100vw, 875px\" \/><\/p>\n<h3>Using a Button to turn OFF the light<\/h3>\n<p>A different setup that uses a button to trigger an interrupt that turns off the light.<br \/>\n(a prerequisite for this script to work properly is that the PIR timer must be the lowest possible, usually it is 5 seconds, further improvements may be added easily to avoid this limitation)<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-42 size-full\" src=\"https:\/\/cantile.xyz\/b\/wp-content\/uploads\/2016\/02\/pi_light_04.png\" alt=\"Pi Light Button\" width=\"550\" height=\"398\" srcset=\"https:\/\/cantile.xyz\/b\/wp-content\/uploads\/2016\/02\/pi_light_04.png 550w, https:\/\/cantile.xyz\/b\/wp-content\/uploads\/2016\/02\/pi_light_04-300x217.png 300w\" sizes=\"(max-width: 550px) 100vw, 550px\" \/><\/p>\n<p>And here&#8217;s the code<\/p>\n<pre><code>import RPi.GPIO as io\nimport socket\nimport time\n\nUDP_IP = \"192.168.1.72\"\nUDP_PORT = 8899\nMESSAGE_ON = \"4200\".decode('hex')\nMESSAGE_OFF = \"4100\".decode('hex')\n\nio.setmode(io.BCM)\npin_pir = 24\nbutton_pin = 22\nlight_state = 0\n\nio.setup(pin_pir, io.IN)\nio.setup(button_pin, io.IN, pull_up_down=io.PUD_UP)\n\ndef BUTTON_Handler (pin):\n       global light_state\n       if io.input(pin) == False :\n          if light_state == 1:\n              sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, 0)\n              sock.sendto(MESSAGE_OFF, (UDP_IP, UDP_PORT))\n              sock.close()\n              time_sleep(1)\n              light_state = 0\n\nio.add_event_detect(button_pin, io.BOTH, callback = BUTTON_Handler, bouncetime=100)\n\ntry:\n       while True:\n          if io.input(pin_pir):\n             if light_state == 0 : \n                sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, 0)\n                sock.sendto(MESSAGE_ON, (UDP_IP, UDP_PORT))\n                sock.close()\n                light_state = 1\n                time.sleep(0.5)\n\nexcept KeyboardInterrupt:\n       io.cleanup()\n\nio.cleanup()<\/code><\/pre>\n<h3>Using a light sensor to control the light<\/h3>\n<p>By slightly modifying the previous script, it is possible to add a check to keep the Mi light OFF when there is already enough light (or daylight), or make it so that it turns ON at dusk.<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-43 size-full\" src=\"https:\/\/cantile.xyz\/b\/wp-content\/uploads\/2016\/02\/pi_light_05.png\" alt=\"Pi Light Sensor\" width=\"550\" height=\"358\" srcset=\"https:\/\/cantile.xyz\/b\/wp-content\/uploads\/2016\/02\/pi_light_05.png 550w, https:\/\/cantile.xyz\/b\/wp-content\/uploads\/2016\/02\/pi_light_05-300x195.png 300w\" sizes=\"(max-width: 550px) 100vw, 550px\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Easily control the Mi Light using a Raspberry Pi and some sensors to send the commands found previously (link) Using a PIR Before going on, a quick check to be sure the PIR is working: just hook it up to a breadboard with a LED and power it up using the Raspberry GPIO pins. Raspberry &hellip; <a href=\"https:\/\/cantile.xyz\/b\/mi-light\/sensors-and-raspberry-pi-to-control-a-mi-light-bulb\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Sensors and Raspberry Pi to control a Mi Light bulb<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":46,"menu_order":1,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"_links":{"self":[{"href":"https:\/\/cantile.xyz\/b\/wp-json\/wp\/v2\/pages\/38"}],"collection":[{"href":"https:\/\/cantile.xyz\/b\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/cantile.xyz\/b\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/cantile.xyz\/b\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cantile.xyz\/b\/wp-json\/wp\/v2\/comments?post=38"}],"version-history":[{"count":4,"href":"https:\/\/cantile.xyz\/b\/wp-json\/wp\/v2\/pages\/38\/revisions"}],"predecessor-version":[{"id":88,"href":"https:\/\/cantile.xyz\/b\/wp-json\/wp\/v2\/pages\/38\/revisions\/88"}],"up":[{"embeddable":true,"href":"https:\/\/cantile.xyz\/b\/wp-json\/wp\/v2\/pages\/46"}],"wp:attachment":[{"href":"https:\/\/cantile.xyz\/b\/wp-json\/wp\/v2\/media?parent=38"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}