Salve a tutti,

Mi chiamo Andrea D'alessandro e sto cominciando a leggere e scrivere con
python.....
Leggendo vari esempi e vari libri stavo provando a fare un applicazione web
con flask che gira su raspberry pi3 alla quale sono collegati 4 RELE' ,un
orologio, un sensore di temperatura e umidita', ed una fotoresistenza.
Ho creato una pagina web con 4 tasti che accende e spegne i RELE' legge le
variabili ora temperatura ed umidita'. Funziona alla grande

Il problema e' la fotoresistenza, ho provato a far girare un altro
script ceck_current()     (threaded=True)
che rileva lo stato di una luce ed a seconda se spenta o accesa lo comunica
alla variabile status della pagina Web ma purtroppo  non va.

Se qualcuno mi potrebbe dare qualche consiglio gli sarei molto grato.

vi allego lo script python e la pagine web

Saluti
Andrea
Title: STATO CORRENTE

Lista Devices e Stato

La Data e l'ora sul server e': {{ time }} UTC

La Temperatura e': {{ temp }} C°

L' umidità e': {{ hum }} %

LINEA: {{ status }}

{% for pin in pins %}

Il RELE {{ pins[pin].name }} {% if pins[pin].state == true %} e' attualmente ON (SPEGNI) {% else %} e attualmente OFF (ACCENDI) {% endif %}

{% endfor %} {% if message %}

{{ message }}

{% endif %}
# -*- coding: utf-8 -*-
import threading
import time
import fotoresistenza
import dht11
from collections import OrderedDict #non necessaria serve a ordinare i pulsanti se sono nominati con RELE1 RELE2 etc
import RPi.GPIO as GPIO
from flask import Flask, render_template,request
import datetime

app = Flask(__name__)

def ceck_current():
    global status
    GPIO.setmode(GPIO.BCM)
    GPIO.setup(19,GPIO.IN)
    while True:
        value= GPIO.input(19)
        if value == 0:
            status = 'LINEA in CORRENTE'
            return(status)
            print(status)
        else:
            status = 'ASSENZA RETE ELETTRICA'
            return(status)
            print(status)
    time.sleep(1)
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)


pins = {
    4:  {'name':'MODEM','state': GPIO.LOW,'pos':1},
    17: {'name': 'SENSORE','state': GPIO.LOW,'pos':2},
    22: {'name':'GPS','state': GPIO.LOW,'pos':3},
    27: {'name':'SENSORE1','state': GPIO.LOW,'pos':4},
    }

pins1 = OrderedDict (sorted (pins.items(),key= lambda x: x[1]['pos'], reverse=False))#non necessaria serve a ordinare i pulsanti se sono nominati con RELE1 RELE2 etc

print (pins1) #non necessaria serve a ordinare i pulsanti se sono nominati con RELE1 RELE2 etc
for pin in pins1:

    GPIO.setup(pin,GPIO.OUT)
    GPIO.output(pin,GPIO.LOW)

@app.route("/")

def main(): 
    
    now= datetime.datetime.now()
    timestring = now.strftime("%Y-%m-%d %H:%M")
    instance = dht11.DHT11(pin=5)
    result = instance.read()
    temp= result.temperature
    hum = result.humidity
    
    #variabile = fotoresistenza.get_temp_hum()
    
    for pin in  pins1:
        pins[pin]['state'] = GPIO.input(pin)
    templateData =  {
        'time' : timestring,
        'pins' : pins1,
        'temp' : temp,
        'hum'  : hum,
        'status' : status
        }

    return   render_template('index.html', **templateData)


@app.route("/<changePin>/<action>")


def action(changePin,action):

    variabile = fotoresistenza.get_temp_hum()
    instance = dht11.DHT11(pin=5)
    result = instance.read()
    temp= result.temperature
    hum = result.humidity
    
    now= datetime.datetime.now()
    timestring = now.strftime("%Y-%m-%d %H:%M")

    changePin = int(changePin)
    deviceName = pins[changePin]['name']
    if action == "on":
        GPIO.output(changePin, GPIO.HIGH)
        message = deviceName,  " ACCESO."
    if action == "off":
        GPIO.output(changePin, GPIO.LOW)
        message =  deviceName,  " SPENTO."
    if action == "toggle":
        GPIO.output(changePin, not GPIO.input(changePin))
        message = "Toggled " + deviceName + "."
    for pin in sorted (pins):
        pins[pin]['state'] = GPIO.input(pin)
    templateData = {
        'message' : message,
        'pins' : pins1,
        'time' : timestring,
        'temp' : temp,
        'hum'  : hum,
        'status': status,
        }
    return render_template('index.html', **templateData)

if __name__ == "__main__":
    ceck_current()    #    se attivo questa funzione la pagina veb si blocca
                 #    se attivo questa funzione la pagina web si blocca
    app.run(host='0.0.0.0', port=80, debug=True, threaded=True)

_______________________________________________
Python mailing list
Python@lists.python.it
https://lists.python.it/mailman/listinfo/python

Rispondere a