Go Down

Topic: ££'s for someone clever than me, openhab&arduino sensors integration (Read 773 times) previous topic - next topic

Squalls

hi all,

I have a raspberry pi running openhab, controlling various sonoffs, gpio relays, even the garage door.
I have recently got another rpi connected to an adafruit lux sensor, a couple of adafruit PIR's and a reed switch sensor on the garage door, and got that communicating via mqtt to openhab, so garage lights come on when I open the door, the bathroom and spare bedroom lights only come on if the light is fading and the room is occupied. The problem is, its not very reliable, and crashes more times than the ex driving the car, forcing me to pee in the dark until I go and restart the python code on the 'sensor' pi.

this is the python code ive used (read, cobbled together off the internet). I know its probably going to look a bit rough to you, but hey ho

#! /usr/bin/python3.5
import sys
import RPi.GPIO as GPIO
import paho.mqtt.client as mqtt
import time
import board
import busio
import adafruit_veml7700

i2c = busio.I2C(board.SCL, board.SDA)
veml7700 = adafruit_veml7700.VEML7700(i2c)

broker="test.mosquitto.org"
broker="192.168.1.42"
#define callback
def on_message(client, userdata, message):
   msg=str(message.payload.decode("utf-8"))
   #print("message =",msg)
   topic=message.topic
   messages.append([topic,msg])
def on_connect(client, userdata, flags, rc):

    if rc==0:
        client.connected_flag=True
        client.subscribe(sub_topic)
    else:
        client.bad_connection_flag=True
        client.connected_flag=False

cpio_channels=[4,5,6,12,13,16,17,18,19,20,21,22,23,24,25,26,27]# base channels
print("using ",sys.version) #what version of python
mode=GPIO.getmode()
print("mode is ",mode)
GPIO.setmode(GPIO.BCM)
mode=GPIO.getmode()
print("mode is ",mode)
GPIO.setup(4,GPIO.OUT)
GPIO.output(4,1)
print("value is ",GPIO.input(4))
GPIO.setup(5,GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
print("input value is ",GPIO.input(5))
GPIO.setup(6,GPIO.IN)
print("input value is ",GPIO.input(6))
GPIO.setup(13,GPIO.IN)
print("input value is ",GPIO.input(13))
#GPIO.cleanup()
print("board ",GPIO.BOARD)
print("bcm ",GPIO.BCM)

##MQTT
messages=[]
sub_topic="pi/GPIO/control/#"
client= mqtt.Client("GPIO-client-001")  #create client object client1.on_publish = on_publish                          #assign function to callback client1.connect(broker,port)                                 #establish connection client1.publish("house/bulb1","on") 
######
client.on_message=on_message
client.on_connect=on_connect
client.connected_flag=False
client.connect(broker)#connect
while True:
    client.loop(0.01)
    client.publish("skynet/garage/door",GPIO.input(5))
    client.publish("skynet/bathroom/PIR",GPIO.input(6))
    client.publish("skynet/bathroom/LUX",veml7700.light)
    client.publish("skynet/office/PIR",GPIO.input(13))
    time.sleep(2)
    if len(messages)>0:
        m=messages.pop(0)
        print("received ",m)
        GPIO.output(4,int(m[1])) #set
        print("value is ",GPIO.input(4))

its a bit like my Turkish language skills, I can maybe understand snippets of a conversation, but I can only order coffee fluently  :)

In order to try and make it a more robust system, I've opted to replace the pi with a shiny new Arduino mega, along with an ethernet shield 2, which arrived this morning. yeay.
After another 14 hour night shift, I took a look at what lay ahead of me, and decided to throw in the towel before I even start


As a Yorkshireman, I consider myself a Scotsman, but with all the generosity beaten out of me.
That being said, I'm willing to pay someone (or be forever in their debt) to write/convert a bit of code, because I don't have the time/inclination/brains to do it myself. Its taken me over 3 years and numerous disappointments/temper tantrums to get Openhab to the point its doing what I want(kind of).

if anyone is willing to help, please let me know

Cheers, Squalls

ps, there is no emoticon for grovelling  :smiley-grin:

Go Up