I have installed Paho-mqtt python libray by "pip3 install paho-mqtt" on my RPi which I use for home automation purposes.
I made a python script to regulate my heatpump.
My script:
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Programme de régulation de température
"""
import time
import json
import requests
from requests.auth import HTTPBasicAuth
import paho.mqtt.subscribe as subscribe
import paho.mqtt.publish as publish
#***********Récupération de l'état d'un élément de Domoticz***********
def dom_elmt(idx, valeur):
url= 'http://127.0.0.1/json.htm?type=devices&rid='
adresse= url+idx
r=requests.get(adresse)
status=r.status_code
if status == 200:
# l'API renvoie 200 si tout est OK
getinfos=r.json()
for i, v in enumerate(getinfos["result"]):
#Récupération de la donnée "Status" du switch
result= getinfos["result"][i][valeur]
return result
#***********FIN Récupération de l'état du clavier************
#***********
Prev_Setpoint = 0
while True:
msg = subscribe.simple("zigbee2mqtt/Thermo-Hygro Salle", hostname="127.0.0.1")
y = json.loads(msg.payload)
Temp = y["temperature"]
print(time.strftime('%H:%M:%S', time.localtime()) ,"Température salle : ",Temp)
#******** lecture du point de consigne idx: 230 ************
Consigne=dom_elmt('230', "SetPoint")
Consigne=float(Consigne)
if Temp > Consigne + 0.2:
Setpoint = Consigne - 0.5
if Temp >= Consigne - 0.2 and Temp <= Consigne + 0.2:
Setpoint = Consigne
if Temp < Consigne - 0.2:
Setpoint = Consigne + 0.5
Setpoint = str(Setpoint) # Transformation d'un float en str
if Setpoint != Prev_Setpoint: # Le point de consigne calculé est différent du précédent
a = "{"
b = '"temperature"'
c = ":"
d = "}"
sendtemp = a+b+c+Setpoint+d
publish.single("heatpump/set", sendtemp, hostname="127.0.0.1")
Prev_Setpoint = Setpoint # Mise à jour de Prev_Setpoint
If I launch this script on a console it works perfectly. I need but I can't launch it at boot in the rc.local.
....
if [ "$_IP" ]; then
printf "My IP address is %s\n" "$_IP"
fi
python /home/pi/thermostat.py &
exit 0
This don't work and I don't understand what happen ?! (I launched other scripts in rc.local whithout any problem)
Does someone has an idea to help me ???
Thanks in advancecd
This is because the python script will get killed when the script that started it exits even if it is pushed to the background with &.
The hacky solution is to prefix the command with nohup (which means the process will not get sent the Hang Up signal when the parent script exits).
The better option would be to write a proper systemd service definition for your script.
Related
So, my problem is :
I have a file (data_test.json) on the web. I wrote and uploaded it myself.
{
"a" : 1,
"b" : 42,
"c" : 3.14
}
My local python code in Renpy read it rightly.
init python:
def get_stats():
# Déclaration des variables
global test_a
global test_b
global test_c
# Importation des modules
import requests
import json
# Atteindre le fichier
r = requests.get("https://lecot-christophe.com/rpu_json/data_test.json")
# Aquérir les données sous forme de texte
r.text
# Convertir en dictionnaire Python
var = json.loads(r.text)
# Boucler pour extraire et distribuer les résultats à travers les variables
for i in var:
test_a = var['a']
test_b = var['b']
test_c = var['c']
Then the python code makes an update. I think python send rightly the data cause of the response of the requests. post is positive.
def post_stats(file):
global test_a
global test_b
global test_c
global send_test
import requests
import json
url = "https://*monsite*/rpu_json/renpy_json_manager.php"
# Composition des données
file_name = file+".json"
aDict = {
"a":test_a,
"b":test_b,
"c":test_c
}
json_object = json.dumps(aDict)
# Adresser le fichier
data_test = requests.post(url, json=json_object)
send_test = data_test.status_code
PHP receive the posted data or not, and update the json file.
<?php
$json = $_POST['data'];
$info = json_encode($json);
$file = fopen('data_test.json','w+') or die("File not found");
fwrite($file, $info);
fclose($file);
?>
But the content of data-test.json is now just "null".
null
So... Where did I make a big mistake ?
I use the library TextRazor to analyze a text and save it in a text file but when I open the file it's empty
import os
textrazor.api_key = ""
client = textrazor.TextRazor(extractors=["word","entities", "topics","sentence","words"])
response = client.analyze("Twenty-four isolates (60%) were associated with pneumonia, 14 (35%) with upper respiratory tract infections, and 2 (5%) with bronchiolitis. Cough (82.5%), fever (75%), and malaise (58.8%) ")
for entity in response.entities():
print(entity.id, entity.relevance_score, entity.confidence_score, entity.freebase_types)
cmd = os.popen('ls -w').read() # Récupération de la sortie de ls -a dans la variable cmd
print(cmd) # Affichage de la sortie
with open('monfichier.txt', 'w') as file:
s=str(cmd)
file.write(s)
ls -w is not a valid command as an integer is needed after the -w. Furthermore, do not use os.popen: it is not portable. Use the Python API instead (see os.walk).
so I have a simple code that create a graphical interface with Tkinter on python, but when I convert it to exe using PyInstaller I get this issue , I don't understand what's happening
Error loading Python lib '/Users/salwafakhir/dist/app/Python': dlopen: dlopen(/Users/salwafakhir/dist/app/Python, 10): no suitable image found. Did find:
/Users/salwafakhir/dist/app/Python: code signature invalid for '/Users/salwafakhir/dist/app/Python'
here is a part of the code
creer une premiere fenetre
masterWindow= Tk()
#personnaliser ma fenetre
masterWindow.title("logistics interface")
masterWindow.geometry("1280x1024")
masterWindow.minsize(480 , 360)
masterWindow.config(background='white')
#creer frame
frameProduct_1W=Frame(masterWindow,bg='white')
image = PhotoImage(file="/Users/salwafakhir/Desktop/seaver-sangle-connecte-equitation-technologie.png")
label = Label(image=image)
label.pack(expand=YES)
#ajouter un premier texte
Label_title =Label(frameProduct_1W,text="Welcome to SeaverLogictec",font=("Time New Roman",40),bg='white', fg="#3A67C1").pack(expand=YES)
ajouter un second texte
Label_subtitle =Label(frameProduct_1W,text="We are here to help you with Stock management and order placing ",font=("Time New Roman",15),bg='white', fg="#3A67C1").pack(expand=YES)
#connection a la base de donne logistic
conn = MongoClient()
db = conn.admin
#ajouter un premier bouton
startButton= Button(frameProduct_1W,text="Let's get started",font=("Time New Roman",40),bg='white', fg='#3A67C1', command=open).pack(expand=YES)
#ajouter frameProduct_1W
frameProduct_1W.pack(expand=YES)
#afficher
masterWindow.mainloop()
I'm trying to build a client and a server using xmlrpc in python, I HAVE to use a class named FunctionWrapper which has a method and the client use it, the method's name is sendMessage_wrapper(self, message), and the server is declared in another class, I'm trying to register the method in the server but when i call the method from de client I raise and error, can you help me, please?
Cliente:
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import xmlrpclib
from SimpleXMLRPCServer import SimpleXMLRPCServer
from os import path
sys.path.append(path.dirname(path.dirname(path.abspath(__file__))))
from Constants.Constants import *
class MyApiClient:
def __init__(self, contact_port = DEFAULT_PORT,contact_ip=LOCALHOST_CLIENT):
self.contact_port = contact_port
self.contact_ip = contact_ip
self.proxy = xmlrpclib.ServerProxy(contact_ip+str(self.contact_port)+"/")
def sendMessage(self,message):
self.proxy.sendMessage_wrapper(message)
a = MyApiClient()
a.sendMessage("Hi")
a.sendMessage("WORKING")
Server:
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import xmlrpclib
from SimpleXMLRPCServer import SimpleXMLRPCServer
from os import path
sys.path.append(path.dirname(path.dirname(path.abspath(__file__))))
from Constants.Constants import *
class MyApiServer:
def __init__(self,wrapper, my_port = DEFAULT_PORT):
self.port = my_port
self.server = SimpleXMLRPCServer((LOCALHOST,self.port))
self.wrapper = wrapper
self.server.register_instance(self.wrapper)
print("Running")
self.server.serve_forever()
class FunctionWrapper:
def __init__(self):
self.message = None
"""
Procedimiento que ofrece nuestro servidor, este metodo sera llamado
por el cliente con el que estamos hablando, debe de
hacer lo necesario para mostrar el texto en nuestra pantalla.
"""
def sendMessage_wrapper(self, message):
self.message = message
self.showMessage()
def showMessage(self):
print ("Mensaje "+self.message)
#raise NotImplementedError( "Should have implemented this" )
a = FunctionWrapper()
b = MyApiServer(a)
Here are the constants in case you need it
#! /usr/bin/env python
# -*- coding: utf-8 -*-
#Nombres para etiquetas login local y remoto
MY_PORT_NUMBER_TITLE = "Cual es mi puerto?"
OTHER_PORT_NUMBER_TITLE = "Cual es el puerto de contacto?"
OTHER_IP_NUMBER_TITLE = "Cual es la direccion ip de contacto?"
LOGIN_TITLE = "Acceder"
#Nombres para las etiquetas del chat
CONVERSATION_TITLE = "Conversacion"
SEND_TITLE = "Responder"
#Titulo de las ventans GUI
LOGIN_WINDOW = "Login"
CHAT_WINDOW = "Chat"
#Modos de acceso al chat, local o remoto
LOCAL = "Local"
REMOTE = "Remote"
#Mensajes de error
WARNING = "¡Alerta!"
MISSING_MESSAGE = "No hay ningun mensaje para enviar"
#Localhost
LOCALHOST = "localhost"
DEFAULT_PORT = 5000
LOCALHOST_CLIENT = "http://localhost:"
I'm newest with arduino i want to read informations from arduino and write it to dream Box DM800 enigma 2.
i tried this code and it didn't work
# Module de lecture/ecriture du port série
from serial import *
# Port série ttyACM0
# Vitesse de baud : 9600
# Timeout en lecture : 1 sec
# Timeout en écriture : 1 sec
with Serial(port="/dev/ttyACM0", baudrate=9600, timeout=1, writeTimeout=1) as port_serie:
if port_serie.isOpen():
while True:
ligne = port_serie.read_line()
print ligne
Can someone help me please