I have a PLC connected to my raspberry pi and i can successfully send values to the PLC Datablocks with Python scripting (Snap7).
My final goal is to use the PLC to control the heat in the house with several floorheaters and those are controlled by several valves to control each room.
Now i bought 6 temparature sensors (WH-5) to send from each room trough RFXCOMe the data to domoticz on a RPI.
My problem is that I am not a Python scripting hero, so i hope some can help me with the scripting.
Code: Select all
import sys
import datetime
import time
import os
import subprocess
import urllib2
import json
import base64
#-----------------------------------
domoticzserver="192.168.1.xxx:8080"
domoticzusername = "xxxx"
domoticzpassword = "xxxxx"
device = "45"
log_to_file = True
#-----------------------------------
base64string = base64.encodestring('%s:%s' % (domoticzusername, domoticzpassword)).replace('\n', '')
domoticzurl = 'http://'+ domoticzserver +'/json.htm?type=command¶m=udevice&idx=' + device +'&nvalue=0&svalue=TEMP;HUM;HUM_STAT'
def log(message):
print message
if log_to_file == True:
logfile = open(sys.argv[0] + '.log', "a")
logfile.write(message + "\n")
logfile.close()
def domoticzrequest (url):
request = urllib2.Request(url)
request.add_header("Authorization", "Basic %s" % base64string)
print(request)
response = urllib2.urlopen(request)
return response.read()
def domoticzstatus():
json_object = json.loads(domoticzrequest(domoticzurl))
getvalue = 0
# Search the device in JSON list
if json_object["status"] == "OK":
getvalue = json_object["result"][0]["Data"]
else:
print (datetime.datetime.now().strftime("%H:%M:%S") + "- Error. Could not find device idx in Domoticz response.")
status = format(float(getvalue[:-1]), '.2f')
return status
log (datetime.datetime.now().strftime("%H:%M:%S") + "- script started.")
domoticzrequest("http://" + domoticzserver + "/json.htm?type=command¶m=udevice&idx=" + device + '&nvalue=0&svalue=TEMP;HUM;HUM_STAT')
http://xxxx:[email protected]:8080/js ... M;HUM_STAT
Code: Select all
{
status: "OK",
title: "Update Device"
}
If i run my python scripts i get as output:
Code: Select all
16:03:29- script started.
<urllib2.Request instance at 0x767f1c10>
So Temp =
and Hum =
So i can use those two variables to send to my PLC.