Frank replied to the topic 'Help script Python3 from Stellarmate' in the forum. 3 years ago

Hello Dave,
At first, it's important to know that it's my firt steps with the code...
My first script to manage the GPIO. This script is ok until the last line :


#!/usr/bin/python3
# Import des bibliotheques et numerotation des GPIO
from tkinter import *
import subprocess
import RPi.GPIO as GPIO
import socket
import os
import shutil
import time
GPIO.setmode(GPIO.BCM)

Prise01 = 5
Prise02 = 6
Monture = 17
Alimentation = 26
Camera = 27

#!GPIO.setup(Prise01, GPIO.OUT)
#!GPIO.output(Prise01, not GPIO.input(Prise01))

GPIO.setup(Prise02, GPIO.OUT)
GPIO.output(Prise02, not GPIO.input(Prise02))

GPIO.setup(Monture, GPIO.OUT)
GPIO.output(Monture, not GPIO.input(Monture))

GPIO.setup(Alimentation, GPIO.OUT)
GPIO.output(Alimentation, not GPIO.input(Alimentation))

GPIO.setup(Camera, GPIO.OUT)
GPIO.output(Camera, not GPIO.input(Camera))

print ("Turning off observatory equipment... Wait 5s.")
time.sleep(5)

#Execute l'envoi d'email
exec(open("Email.PY", encoding="utf-8").read(),globals())

And the script 'Email.PY" to email me the weather. :

#!/usr/bin/python3
print ("Envoi message... ")

import requests
import json
import datetime
import time

#Meteo
#Consultation API météo avec openweathermap

#récupération de la ville choisie par l'utilisateur
ville = "Pouzauges"

#récupère le temps actuel
url_weather = "api.openweathermap.org/data/2.5/weather?...xxxxxx&units={metric}"
r_weather = requests.get(url_weather)
data = r_weather.json()
#print (data)

now = datetime.datetime.now()

#Fonction de conversion temps
def time_converter(time):
converted_time = datetime.datetime.fromtimestamp(
int(time)
).strftime('%I:%M %p')
return converted_time

#Soleil
Lever_sun = data
Coucher_sun = data

#état du ciel
temps = data[0]

#Nuage
Nuage = data

#temperature actuelle
t = round(data,1)

#écart de température
t_min = round(data,1)
t_max = round(data,1)

#temperature ressentie
tr = round(data,1)

#taux d'humidité
humidite = data

#Pression
Pression = data

#Vent
Vent_speed = data
Vent_deg = data

#Mise à jour serveur meteo
majs = data

#Création du message à envoyer
message_meteo = ("Données Météo à Home Observatory") \
+ '\n' + "*******************************************************" \
+ '\n' + "Fin de session StellarMatte : " + (now.strftime("%Y-%m-%d %H:%M:%S")) \
+ '\n' + ("Lever du soleil : {}".format(time_converter(Lever_sun))) \
+ '\n' + ("Coucher du soleil : {}".format(time_converter(Coucher_sun))) \
+ '\n' + ("Conditions climatiques : {}".format(temps)) \
+ '\n' + ("Couverture nuageuse : {} %".format(Nuage)) \
+ '\n' + ("Taux d'humidite de {} %".format(humidite)) \
+ '\n' + ("Pression : {} hpa".format(Pression)) \
+ '\n' + ("Vitesse Vent : {} km/h".format(round(Vent_speed*60*60/1000),1)) \
+ '\n' + ("Orientation : {} ° (N=0° / Est = 90° / Sud = 180° / Ouest = 270°)".format(Vent_deg)) \
+ '\n' + ("La temperature en fin de session est de {} °c".format(round(t-273.15),1)) \
+ '\n' + ("La temperature ressentie en fin de session est de {} °c".format(round(tr-273.15),1)) \
+ '\n' + ("Les temperatures varient entre {}".format(round(t_min-273.15),1) + " a {} °c ce jour".format(round(t_max-273.15),1)) \
+ '\n' + "*******************************************************" \
+ '\n' + ("Mise à jour serveur météo : {} ".format(time_converter(majs))) \
#Fin Meteo

#!Envoie email de fin de session


import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

msg = MIMEMultipart()
msg = 'This email address is being protected from spambots. You need JavaScript enabled to view it.'
msg = This email address is being protected from spambots. You need JavaScript enabled to view it.'
msg = 'STELLARMATE. End = ' + now.strftime("%Y-%m-%d %H:%M:%S")
message = 'La session de Stellarmate est finie. ' +'\n' + '\n' + message_meteo
msg.attach(MIMEText(message))
mailserver = smtplib.SMTP('smtp.gmail.com', 587)
mailserver.ehlo()
mailserver.starttls()
mailserver.ehlo()
mailserver.login(This email address is being protected from spambots. You need JavaScript enabled to view it.', xxxxxxx')
mailserver.sendmail('This email address is being protected from spambots. You need JavaScript enabled to view it.', 'This email address is being protected from spambots. You need JavaScript enabled to view it.', msg.as_string())
mailserver.quit()
print ("Message envoyé... ")
exit(0)


In fact, at the end of the first script, I would like to the Email.PY. As I said, the scripts run in the console with the command "python3 Email.PY".

Read More...