<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Python code to power on&#x2F;off your MM-lcd and to mqtt motion]]></title><description><![CDATA[<p dir="auto">Hello to everyone, this is my first post in the forum, please take that into consideration :D …<br />
So I have MM working on a pi zero w, I’ve modded the lcd to be able to power on and off the backlight using one of the Gpio using a python code on startup.<br />
The problem is that I’m having some problems, recently I’ve implemented in the same python code the sending of an mqtt message status every time the PIR is trigged ON or goes OFF, the fact is that time by time, let’s say every 24h the pi get stuck…<br />
This is the code:</p>
<pre><code>#!/usr/bin/env python

from time import time, sleep
import datetime
import os                 
import RPi.GPIO as GPIO
import urlparse
import paho.mqtt.client as paho

# Time to wait (in seconds)
ttw=59
cttw=ttw

# Setup Gpio
# GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIO.IN)         #Output PIR
GPIO.setup(25, GPIO.OUT)

# Mqtt
#mqttc = mosquitto.Mosquitto()
mqttc = paho.Client()

url_str = 'mqtt://192.xxx.yyy.zzz:1883'
url = urlparse.urlparse(url_str)
mqttc.username_pw_set("xxxx", "yyyyyy")

def sendmqtt(mess):
    print "Send mqtt"
    now = datetime.datetime.now()
    mqttc.connect(url.hostname, url.port)
#   mqttc.publish("pir/kitchen", str(now) + mess)
    mqttc.publish("pir/kitchen", mess)

while True:
    i=GPIO.input(23)
    if i==0:                 #When output from motion sensor is LOW, no movement
        cttw-=1
        print cttw
        if cttw==0: # Counter has reached zero
            print "Counter is 0"
            sendmqtt("OFF")
            GPIO.output(25, False)
            cttw=ttw    # Reset counter

    elif i==1:               #When output from motion sensor is HIGH, movement detected
        print "Motion detected"
        cttw=ttw   
        sendmqtt("ON")
        GPIO.output(25, True)
        sleep(1)    
    sleep(1)
</code></pre>
]]></description><link>https://forum.magicmirror.builders/topic/9623/python-code-to-power-on-off-your-mm-lcd-and-to-mqtt-motion</link><generator>RSS for Node</generator><lastBuildDate>Wed, 15 Apr 2026 16:41:55 GMT</lastBuildDate><atom:link href="https://forum.magicmirror.builders/topic/9623.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 27 Jan 2019 11:36:31 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Python code to power on&#x2F;off your MM-lcd and to mqtt motion on Sun, 27 Jan 2019 11:40:44 GMT]]></title><description><![CDATA[<p dir="auto">I’m a total noob at coding so really every kind of suggestion is appreciated</p>
]]></description><link>https://forum.magicmirror.builders/post/50899</link><guid isPermaLink="true">https://forum.magicmirror.builders/post/50899</guid><dc:creator><![CDATA[Cupola]]></dc:creator><pubDate>Sun, 27 Jan 2019 11:40:44 GMT</pubDate></item></channel></rss>