0

EDIT: it seems that it starts randomly I have python script and I've created in lxsession/LXDE-pi/autostart file following command: sudo /usr/bin/python3.7 /home/pi/script.py

If I type this command in terminal the script is executed. After that, for some short period of time, the script will execute if rpi is rebooted. However, the problem is that if I shut down and boot rpi after cca 30 mins the script fails to execute. After I start it in terminal it will again execute if boot is made within this short period. Any ideas where might be the problem?

script code:

import bme280
import time
import os
from twilio.rest import Client
import gspread
from oauth2client.service_account import ServiceAccountCredentials
import requests
import datetime
from bs4 import BeautifulSoup

starttime=time.time()
cd_time=0
def read_settings():
    scope = ['https://spreadsheets.google.com/feeds','https://www.googleapis.com/auth/drive']
    creds = ServiceAccountCredentials.from_json_keyfile_name('/home/pi/Downloads/SpanskoMeteoStation.json', scope)
    client = gspread.authorize(creds)
    sheet = client.open('Postavke').worksheet("Settings")
    send_regular_sms=str(sheet.cell(4,5).value)
    send_pressure_sms=str(sheet.cell(6,5).value)
    measuring_diff=float(sheet.cell(8,5).value)
    pressure_diff=float(sheet.cell(10,5).value)
    sms_delay=float(sheet.cell(12,5).value)
    sms_pressure_stop=float(sheet.cell(14,5).value)
    sms_date_start=str(sheet.cell(16,5).value)
    sms_date_stop=str(sheet.cell(16,6).value)
    sms_time_start=str(sheet.cell(18,5).value)
    sms_time_stop=str(sheet.cell(18,6).value)
    sms_ddt=str(sheet.cell(20,5).value)
    sms_numbers=str(sheet.cell(22,5).value)
    twilio_sid=str(sheet.cell(4,10).value)
    twilio_auth=str(sheet.cell(6,10).value)
    twilio_from=str(sheet.cell(8,10).value)
    twilio_body=str(sheet.cell(10,10).value)
    meteo_link=str(sheet.cell(12,10).value)
    account_sid = twilio_sid
    auth_token = twilio_auth
    client = Client(account_sid, auth_token)
    return [client,send_regular_sms,send_pressure_sms,measuring_diff,pressure_diff,sms_delay,sms_pressure_stop,sms_date_start,sms_date_stop,sms_time_start,sms_time_stop,sms_ddt,sms_numbers,twilio_sid,twilio_auth,twilio_from,twilio_body,meteo_link]

def regular_sms_content(temperature, pressure, humidity, ex_pres, tel_from, tel_to, client):
    message = client.messages \
                .create(
                     body="Trenutacno stanje u balonu:\nTemperatura: "+str(round(temperature,1))+" C\nTlak: "+str(round(pressure,2))+" hPa\nVlaznost: "+str(round(humidity,0))+" %\nTlak izvan balona iznosi: "+str(round(ex_pres,2))+" hPa",
                     from_="+"+tel_from,
                     to="+"+tel_to
                 )
def difference_sms_content(pressure, ex_pres, tel_from, tel_to, client):
    message = client.messages \
                .create(
                     body="Razlika tlakova u i izvan balona iznosi " + str(round(pressure-ex_pres,2)) +" hPa, sto je izvan definirane granice!\nTlak u balonu iznosi: "+str(round(pressure,2))+" hPa\nTlak izvan balona iznosi: "+str(round(ex_pres,2))+ " hPa",
                     from_="+"+tel_from,
                     to="+"+tel_to
                 )
def recalculate_pressure(pressure, elevation, temperature):
    return pressure*(1-0.0065*elevation/(temperature + 0.0065*elevation +273.15))**(-5.257)
def update_sheets(temp, press, hum, ex):
    scope = ['https://spreadsheets.google.com/feeds','https://www.googleapis.com/auth/drive']
    creds = ServiceAccountCredentials.from_json_keyfile_name('/home/pi/Downloads/SpanskoMeteoStation.json', scope)
    client = gspread.authorize(creds)
    sheet = client.open('Aktualni podaci').worksheet("Dashboard")
    sheet.update_cell(12,6, round(temp,1))
    sheet.update_cell(11,6, round(press,2))
    sheet.update_cell(13,6, round(hum,0))
    sheet.update_cell(12,12, round(ex[0],2))
    sheet.update_cell(14,12, ex[1])
def read_external_station(meteo_link):
    url = meteo_link
    response = requests.get(url)
    soup = BeautifulSoup(response.text, "html.parser")
    for head in soup.find_all('div', attrs={'class': 'module__container'}):
        for head_child in head.find_all('div', attrs={'class': 'module__header'}):
            if head_child.text==" Current Conditions ":
                for div in head.find_all('span', attrs={'class': 'test-false wu-unit wu-unit-pressure ng-star-inserted'}):
                    for div_child in div.find_all('span', attrs={'class': 'wu-value wu-value-to'}):
                        ex_pressure = float(div_child.text)*33.86389
                for div in head.find_all('span', attrs={'class': 'test-true wu-unit wu-unit-temperature is-degree-visible ng-star-inserted'}):
                    for div_child in div.find_all('span', attrs={'class': 'wu-value wu-value-to'}):
                        ex_temp = (float(div_child.text)-32)*5/9
    for head in soup.find_all('div', attrs={'class': "dashboard__title ng-star-inserted"}):
        for head_child in head.find_all('span', attrs={'class': 'ng-star-inserted'}):
            ex_refresh = str(head_child.text.split("updated ",1)[1].split(" ago",1)[0])    
    return [recalculate_pressure(ex_pressure, 121, ex_temp),ex_refresh]

def read_sensor_data(meteo_link,pressure_diff, tel_from, tel_to, client,send_pressure_sms,send_regular_sms,sms_date_start,sms_date_stop,sms_time_start,sms_time_stop,sms_delay, base_clock):
    global cd_time
    temperature, pressure, humidity = bme280.readBME280All()
    pressure=recalculate_pressure(pressure, 117,temperature)
    ex_data=read_external_station(meteo_link)
    update_sheets(temperature, pressure, humidity, ex_data)
    if send_pressure_sms=='TRUE':
        if (round(pressure,2)-round(ex_data[0],2)) < pressure_diff:
            if cd_time<=0:
                cd_time=sms_delay-base_clock
                difference_sms_content(pressure, ex_data[0], tel_from, tel_to, client)
                print("Pressure difference SMS sent at "+str(datetime.datetime.now()))
                return cd_time
            if cd_time>0:
                cd_time-=base_clock
                return cd_time
    if send_regular_sms=='TRUE':
        start_date=datetime.datetime.strptime(sms_date_start,'%d.%m.%Y.').date()
        stop_date=datetime.datetime.strptime(sms_date_stop,'%d.%m.%Y.').date()
        start_time=datetime.datetime.strptime(sms_time_start,'%H:%M').time()
        stop_time=datetime.datetime.strptime(sms_time_stop,'%H:%M').time()
        date = datetime.datetime.now().date()
        time = datetime.datetime.now().time()
        minute = str(datetime.datetime.now())[14:16]
        if date>=start_date and date<=stop_date and time>=start_time and time<=stop_time and minute==str(start_time)[3:5]:
              regular_sms_content(temperature, pressure, humidity, ex_data[0], tel_from, tel_to, client)
              print("Regular SMS sent at "+str(datetime.datetime.now()))
while True:  
    settings=read_settings()
    read_sensor_data(settings[17],settings[4],settings[15],settings[12],settings[0],settings[2],settings[1],settings[7],settings[8],settings[9],settings[10],settings[5],settings[3])
    time.sleep(settings[3] - ((time.time() - starttime) % settings[3]))
  • What does your script do? – CoderMike Oct 13 at 15:04
  • it has bme280 sensor connected so it measures in minute interval the conditions – PostarLakogSna Oct 13 at 15:10
  • Edit your post and add your script in a code block. – CoderMike Oct 13 at 15:13
  • It's added..... – PostarLakogSna Oct 13 at 16:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.