Page 1 of 1

How to restart services with python ?

Posted: Saturday 18 March 2017 11:45
by tontze
Hi !

I have a conflict problem with my bt beacon and miflora sensor. Whenever i run miflora update, beacon stops responding, so i thought to restart
beacon service in the end of a miflora python script. How is it easiest to do ?

Re: How to restart services with python ?

Posted: Saturday 18 March 2017 12:44
by ubee
I would have started the beacon service as a systemd service. Then it will be automatically restarted by the Linux system if it terminates for some reason. If you do not know how to start a script as a systemd service, you can find an example on github, bengtner/GPIOinput. You write a small service definition file and put in /etc/systemd/system, then use the systemctl command to enable the service.

Re: How to restart services with python ?

Posted: Saturday 18 March 2017 16:38
by tontze
Beaconing is made by this wiki :

http://www.domoticz.com/wiki/Presence_d ... gy_Beacon)

And is it run from init.d

It runs hci0 port, but if anything else accesses it, it will stop responding, im not sure does it die, or just doesnt respond anymore ..

Im noob on this so .. :)

EDIT: by checking status of a service, i see it is running, but not responding

-T

Re: How to restart services with python ?

Posted: Sunday 19 March 2017 10:38
by ubee
I see, since status reports "running" no system surveillance will solve your problem.

I'm sorry, but I can't help you in this matter.

Re: How to restart services with python ?

Posted: Monday 20 March 2017 19:29
by tontze
ubee wrote:I see, since status reports "running" no system surveillance will solve your problem.

I'm sorry, but I can't help you in this matter.
Ok, thnx anyways, an attempt was made :D

Does anyone else have ideas ? I simply would like to "sudo /etc/init.d/beqacon restart" service at the end of miflora script. Mi flora is run every 6hours.

I just dont know how to do it with python :/

Re: How to restart services with python ?

Posted: Monday 20 March 2017 19:42
by gerardvs
Something like this?

Code: Select all

import os
os.system("sudo /etc/init.d/beqacon restart")
Make sure sudo is not asking for a password.

--Gerard

Re: How to restart services with python ?

Posted: Tuesday 21 March 2017 13:10
by tontze
gerardvs wrote:Something like this?

Code: Select all

import os
os.system("sudo /etc/init.d/beqacon restart")
Make sure sudo is not asking for a password.

--Gerard
Thanks, that did it :)

Re: How to restart services with python ?

Posted: Wednesday 21 June 2017 7:11
by Lora46
Ease from there you'll have the ability to download Fildo the within the Fildo App. The Fildo Apk could be downloaded.

Re: How to restart services with python ?

Posted: Wednesday 21 June 2017 8:35
by devros
hello, iv had same problem as you, so i changed script
with this command

Code: Select all

os.system("timeout 2 hcitool lescan 5")
miflora works OK in my tests and it works without sudo

Code: Select all

#!/usr/bin/python3
import urllib.request
import base64
import time
import os
from miflora.miflora_poller import MiFloraPoller, \
    MI_CONDUCTIVITY, MI_MOISTURE, MI_LIGHT, MI_TEMPERATURE, MI_BATTERY

# Settings for the domoticz server

# Forum see: http://domoticz.com/forum/viewtopic.php?f=56&t=13306&hilit=mi+flora&start=20#p105255

domoticzserver   = "XXXX"
domoticzusername = "XXXX"
domoticzpassword = "XXXX"

# So id devices use: sudo hcitool lescan
base64string = base64.encodestring(('%s:%s' % (domoticzusername, domoticzpassword)).encode()).decode().replace('\n', '')

def domoticzrequest (url):
  request = urllib.request.Request(url)
  request.add_header("Authorization", "Basic %s" % base64string)
  response = urllib.request.urlopen(request)
  return response.read()

def update(address,idx_moist,idx_temp,idx_lux,idx_cond):

    poller = MiFloraPoller(address)
    loop = 0
    try:
        temp = poller.parameter_value("temperature")
    except:
        temp = 201

    while loop < 2 and temp > 200:
        print("Patched: Error reading value retry after 5 seconds...\n")
        time.sleep(5)
        poller = MiFloraPoller(address)
        loop += 1
        try:
            temp = poller.parameter_value("temperature")
        except:
            temp = 201

    if temp > 200:
        print("Patched: Error reading value\n")
        return

    global domoticzserver

    print("Mi Flora: " + address)
    print("Firmware: {}".format(poller.firmware_version()))
    print("Name: {}".format(poller.name()))
    print("Temperature: {}°C".format(poller.parameter_value("temperature")))
    print("Moisture: {}%".format(poller.parameter_value(MI_MOISTURE)))
    print("Light: {} lux".format(poller.parameter_value(MI_LIGHT)))
    print("Fertility: {} uS/cm?".format(poller.parameter_value(MI_CONDUCTIVITY)))
    print("Battery: {}%".format(poller.parameter_value(MI_BATTERY)))

    val_bat  = "{}".format(poller.parameter_value(MI_BATTERY))

    # Update temp
    val_temp = "{}".format(poller.parameter_value("temperature"))
    domoticzrequest("http://" + domoticzserver + "/json.htm?type=command&param=udevice&idx=" + idx_temp + "&nvalue=0&svalue=" + val_temp + "&battery=" + val_bat)

    # Update lux
    val_lux = "{}".format(poller.parameter_value(MI_LIGHT))
    domoticzrequest("http://" + domoticzserver + "/json.htm?type=command&param=udevice&idx=" + idx_lux + "&svalue=" + val_lux + "&battery=" + val_bat)

    # Update moisture
    val_moist = "{}".format(poller.parameter_value(MI_MOISTURE))
    domoticzrequest("http://" + domoticzserver + "/json.htm?type=command&param=udevice&idx=" + idx_moist + "&svalue=" + val_moist + "&battery=" + val_bat)

    # Update fertility
    val_cond = "{}".format(poller.parameter_value(MI_CONDUCTIVITY))
    domoticzrequest("http://" + domoticzserver + "/json.htm?type=command&param=udevice&idx=" + idx_cond + "&svalue=" + val_cond + "&battery=" + val_bat)
    time.sleep(1)

# format address, moist (%), temp (°C), lux, fertility
os.system("timeout 2 hcitool lescan 5")
print ("predping")
update("C4:7C:8D:61:B4:DA","142","133","136","143")