Valores de lectura de la fórmula y envío de un correo electrónico
El script abre una aplicación en EcoStruxure Machine Expert e inicia sesión en el dispositivo. Si el controlador no está en modalidad RUNNING, se establecerá en dicha modalidad. A continuación, la variable iVar1 se lee y se muestra en la vista Mensajes o en la línea de comandos. Al final, la aplicación se cierra.
# Ejemplo de script ScriptEmail.py
# Get the primary project reference
project = projects.primary
# Retrieve active application
active_application = project.active_application
# Create online application
online_application = online.create_online_application(active_application)
# Login to application
online_application.login(OnlineChangeOption.Try, True)
# Start PLC if necessary
if not online_application.application_state == ApplicationState.run:
online_application.start()
# Wait 2 seconds
system.delay(2000)
# Define read values
watch_expressions = ["POU.value_1", "POU.value_2", "POU.value_3"]
# Read values from the controller
watch_values = online_application.read_values(watch_expressions)
# Open output file to write values
recipe_output_file = open(r"C:\Python\Objects\Output.txt", "w")
for i in range(len(watch_expressions)):
recipe_output_file.write(watch_expressions[i])
recipe_output_file.write(" = ")
recipe_output_file.write(watch_values[i])
recipe_output_file.write("\n")
# Close files
recipe_output_file.close()
# Send Email
# Import respective libraries
import smtplib
from email.mime.text import MIMEText
#open output file
recipe_output_file = open("C:\Python\Objects\Output.txt", "r")
mail = MIMEText(recipe_output_file.read())
recipe_output_file.close()
# Email address sender and recipient
sender = "sender@sender.com"
to = "to@to.com"
# Set sender and recipient
mail["Subject"] = "Attention value has changed"
mail["From"] = sender
mail["To"] = to
# Send email
smtp = smtplib.SMTP("Name of the SMTP Server")
smtp.sendmail(sender, [to], mail.as_string())
smtp.quit()
# Logout and close application
online_application.logout()
project.close()