示例#1
0
def mainBeta(everySeconds=10):
    global client
    client = connect_and_subscribe()  # connect and get a client reference
    last_Temp = 0
    publicaMQTT(topic_subCalInit, b'On')
    showSetCalderaStatus()
    adc = machine.ADC(0)
    while True:
        try:
            client.check_msg(
            )  # Check por new messages and call the callBack function
        except KeyboardInterrupt:
            pass
        except Exception as e:
            myLog('Error check_msg: ' + str(e))
            restart_and_reconnect()
        now = utime.ticks_ms()
        if utime.ticks_diff(now, last_Temp) > (everySeconds * 1000):
            last_Temp = now
            showSetCalderaStatus()
            batRaw = adc.read()
            if batRaw > 5:
                publicaMQTT(topic_subBatRaw, str(batRaw).encode('utf-8'))
                batVolt = batRaw * 4.36 / 1023
                publicaMQTT(topic_subBatVolt,
                            ('%.2f' % batVolt).encode('utf-8'))
        time.sleep_ms(100)
示例#2
0
def run():
    test()

    myLog('Waiting 5 seconds for wifi setup...')
    wait4Wifi(5)

    MQTT_base.connect_and_subscribe(
        (config.topic_subLedRGB, config.topic_subCalderaStatus),
        MySubCheckTopics)  # connect and get a client reference

    last_Beat = 0  # int(round(time.time() * 1000))
    MQTT_base.client.publish(config.topic_subInitConsola, 'On')
    while True:
        try:
            MQTT_base.client.check_msg(
            )  # Check por new messages and call the callBack function
            now = int(round(time.time() * 1000))
            if (now - last_Beat) > config.tRefresco:  # 60 segundos
                getSensorData()
                publishMQTTData()
                showUIData()
                last_Beat = now
            if btnA.wasPressed():
                last_Beat = 0  # fuerza un refresco
            if btnC.wasPressed():
                sendCalderaOn()
            if btnB.wasPressed():
                sendCalderaOff()
        except Exception as e:
            myLog('Main exception: ' + str(e), saveToFile=True)
示例#3
0
def mainBeta(everySeconds=60):
    connect_and_subscribe()  # connect and get a client reference
    last_Temp = 0
    #publicaMQTT(topic_subMSInit,b'On')
    #adc = machine.ADC(0)
    while True:
        try:
            client.check_msg(
            )  # Check por new messages and call the callBack function
        except KeyboardInterrupt:
            pass
        except Exception as e:
            myLog('Error check_msg: ' + str(e))
            restart_and_reconnect()
        now = utime.ticks_ms()
        if utime.ticks_diff(now, last_Temp) > (everySeconds * 1000):
            last_Temp = now
            try:
                #batRaw = adc.read()
                # publicaMQTT(topic_subBatRaw,str(batRaw).encode('utf-8'))
                #batVolt = batRaw*4.367/1023
                #publicaMQTT(topic_subBatVolt,('%.2f' % batVolt).encode('utf-8'))
                publicaMQTT(topic_subTemp,
                            MeteoSalon.bme.temperature.encode('utf-8'))
                publicaMQTT(topic_subPress,
                            MeteoSalon.bme.pressure.encode('utf-8'))
                publicaMQTT(topic_subHum,
                            MeteoSalon.bme.humidity.encode('utf-8'))
            except KeyboardInterrupt:
                pass
            except Exception as e:
                myLog('Error check_msg: ' + str(e))
                restart_and_reconnect()
        time.sleep_ms(200)
示例#4
0
def testBME280():
    global bme

    if bme == None:
        initBME280()
    if bme != None:
        getSensorData()
        myLog(strData)
示例#5
0
def publicaMQTT(topic, msg):
    global client
    try:
        myLog('MQTT > ' + str(topic.decode("utf-8")) + ':' +
              str(msg.decode("utf-8")))
        client.publish(topic, msg)  # qos = 1
    except KeyboardInterrupt:
        pass
    except Exception as e:
        myLog('Publish>' + str(e))
def initUI():
    global labelT, labelP, labelH
    myLog('Init UI')
    labelT = M5TextBox(0, 0, "T: --- ", lcd.FONT_DejaVu72, 0x88FFFF, rotate=0)
    labelP = M5TextBox(0, 85, "P: --- ", lcd.FONT_DejaVu40, 0x88FF88, rotate=0)
    labelH = M5TextBox(0,
                       130,
                       "H: --- ",
                       lcd.FONT_DejaVu40,
                       0xFF55FF,
                       rotate=0)
def testBME280():
    global bme
    global temp, press, hum, strData

    if bme == None:
        initBME280()
    if bme != None:
        temp, press, hum = getDataBME280()
        strData = 'Temp: ' + str(temp) + ' Pres: ' + str(
            press) + ' Hum: ' + str(hum)
        myLog(strData)
示例#8
0
def publishMQTTData():
    global temp, press, hum, strData

    msgTime = MyDateTime.getLocalTimeHumanFormat()
    MQTT_base.client.publish(config.topic_subTemp, temp)
    MQTT_base.client.publish(config.topic_subPress, press)
    MQTT_base.client.publish(config.topic_subHum, hum)
    msg = msgTime + ', ' + strData
    MQTT_base.client.publish(config.topic_subData, msg)
    MQTT_base.client.publish(config.topic_subConsolaStatus, 'On')
    myLog('Published: ' + msg)
示例#9
0
def initBME280():
    global bme
    try:
        #i2c = machine.I2C(sda = machine.Pin(21),scl = machine.Pin(22)) # configuramos el acceso al bus i2c
        i2c = machine.I2C(
            sda=machine.Pin(config.pin_SDA), scl=machine.Pin(
                config.pin_SDL))  # configuramos el acceso al bus i2c
        # myLog(i2c.scan()) # Comprobamos que se detecta el dispositivo en la direccion 0x76 (118)
        bme = BME280.BME280(i2c=i2c, address=config.BME280_ADDRESS)
        myLog('BME280 found @ ' + str(config.BME280_ADDRESS), saveToFile=True)
    except Exception as e:
        myLog('BME280 not found: ' + str(e), saveToFile=True)
        bme = None
示例#10
0
def showUIData():
    global bme
    global labelT, labelP, labelH
    global temp, press, hum, strData
    global bCalderaOn
    myLog('Show UI Data', level=1)
    if bme == None:
        initBME280()
    if labelT == None:
        initUI()
    if bme != None:
        lcd.clear()
        strTemp = str(temp)
        myPrint(0, 0, strTemp, lcd.FONT_DejaVu72, 0x88FFFF)
        myLog('T: ' + strTemp)
        myPrint(0, 75, "P: " + str(press), lcd.FONT_DejaVu40, 0x88FF88)
        myPrint(0, 115, "H: " + str(hum), lcd.FONT_DejaVu40, 0xFF55FF)
        text = ' - '
        color = 0xffffff
        if bCalderaOn == True:
            color = 0xf54242
            text = 'On'
        elif bCalderaOn == False:
            color = 0x20C8F5
            text = 'Off'
        myPrint(5, 168, text, lcd.FONT_DejaVu72, color)
        marco(0, 165, 120, 70, 5, color)

        # lcd.circle(48, 200, 30, 0xd7ceca,0xf54220)
        lcd.circle(163, 200, 30, 0xd7ceca, 0x20C8F5)
        lcd.circle(258, 200, 30, 0xd7ceca, 0xf54220)
        myLog('Showed UI Data', level=1)
    else:
        myLog('No sensor', saveToFile=True)
示例#11
0
def testUI():
    myLog('Black background')
    setScreenColor(0x000000)
    myLog('image', level=2)
    lcd.image(100, 100, '/flash/img/paper_128.jpg', type=lcd.JPG)
    myLog('Circle', level=2)
    lcd.circle(130, 201, 60, 0xd7ceca, 0xf5be2b)
    myLog('Label', level=2)
    label0 = M5TextBox(10,
                       10,
                       "Consola",
                       lcd.FONT_DejaVu56,
                       0xFFFFFF,
                       rotate=0)
    myLog('speaker', level=2)
    speaker.tone(880, 120, 1)
示例#12
0
def connect_and_subscribe():
    global client, client_id, topic_sub, topic_subLedRGB, topic_subLed
    try:
        client = MQTTClient(client_id, config.MQTT_SERVER)
        client.set_callback(sub_CheckTopics)
        client.connect()
        client.subscribe(topic_subFree)
        client.subscribe(topic_subCaldera)
        myLog('Connected to %s MQTT broker, subscribed to %s topic' %
              (config.MQTT_SERVER, topic_subCaldera))
        return client
    except KeyboardInterrupt:
        pass
    except Exception as e:
        myLog('Connect&Subscribe>' + str(e))
        restart_and_reconnect()
示例#13
0
def connect_and_subscribe():
    global client, client_id, mqtt_server, topic_sub, topic_subLedRGB, topic_subLed
    try:
        client = MQTTClient(client_id, mqtt_server)
        client.set_callback(sub_CheckTopics)
        client.connect()
        #        client.subscribe(topic_subFree)
        #        client.subscribe(topic_subLed)
        client.subscribe(topic_subLedRGB)
        #        print('Connected to %s MQTT broker, subscribed to %s topic' % (mqtt_server, topic_subLedRGB))
        return client
    except KeyboardInterrupt:
        pass
    except Exception as e:
        myLog('Connect&Subscribe>' + str(e))
        restart_and_reconnect()
示例#14
0
def sub_CheckTopics(topic, msg):
    global client
    try:
        myLog("MQTT < " + str(topic.decode("utf-8")) + ':' +
              str(msg.decode("utf-8")))
        if topic == topic_subCaldera:  # Check for Led Topic
            if msg == b'On':
                caldera_test.enciendeCaldera()
            else:
                caldera_test.apagaCaldera()
            showSetCalderaStatus()
        elif topic == topic_subFree:  ## Check for free memory
            freeMem = helpFiles.free()
            publicaMQTT(topic_subMem, str(freeMem))
    except KeyboardInterrupt:
        pass
    except Exception as e:
        myLog('Error checking topics>' + str(e))
def showUIData():
    global bme
    global labelT, labelP, labelH
    global temp, press, hum, strData

    if bme == None:
        initBME280()
    lcd.clear()
    if labelT == None:
        initUI()
    if bme != None:
        strTemp = str(temp)
        labelT.setText(strTemp)
        myLog('T: ' + strTemp)
        labelP.setText("P: " + str(press))
        labelH.setText("H: " + str(hum))
    else:
        myLog('No sensor')
def testWifi():
    wl = network.WLAN(network.STA_IF)
    wl.active(True)

    if wl.isconnected():
        print('Wifi - Connected to ' + wl.config('essid'))
        lcd.image(0, 200, '/flash/img/wifi-48.jpg', type=lcd.JPG)
    else:
        print('connecting')
        wifi_config = deviceCfg.get_wifi()
        if wifi_config == []:
            myLog('no wifi config')
        #    import wifiWebCfg
        #    wifiWebCfg.config_by_web()
        else:
            ssid = wifi_config[0]
            pwd = wifi_config[1]
            print('wifi: ' + ssid)
            wl.connect(ssid, pwd)
            lcd.image(0, 200, '/flash/img/wifi-48.jpg', type=lcd.JPG)
示例#17
0
def sub_CheckTopics(topic, msg):
    global client
    try:
        myLog("MQTT < " + str(topic.decode("utf-8")) + ':' +
              str(msg.decode("utf-8")))
        """
        if topic == topic_subLed:     # Check for Led Topic
            if msg == b'On':
                print('Led:On')
                MeteoSalon.led.off()
            else:
                print('Led:Off')
                MeteoSalon.led.on()
        el"""
        if topic == topic_subLedRGB:  ## Check for RGB Topic
            MeteoSalon.color(msg)
        """elif topic == topic_subFree:        ## Check for free memory
            freeMem = helpFiles.free()
            client.publish(topic_subMem, str(freeMem))"""
    except KeyboardInterrupt:
        pass
    except Exception as e:
        myLog('Error checking topics>' + str(e))
示例#18
0
def MySubCheckTopics(topic, msg):
    global bCalderaOn
    myLog(str(topic) + ' : ' + str(msg))

    if topic == config.topic_subLedRGB:  ## Check for RGB Topic
        showLedRGBColor(msg)
    elif topic == config.topic_subCalderaStatus:
        if msg == b'On':
            if bCalderaOn != True:
                showUIData()
                bCalderaOn = True
            myLog('StatusCaldera: On')

        else:
            if bCalderaOn != False:
                showUIData()
                bCalderaOn = False
            myLog('StatusCaldera: Off')
示例#19
0
def initUI():
    myLog('Init UI', level=1)
    myLog('Initted UI', level=1)
示例#20
0
def restart_and_reconnect():
    publicaMQTT(topic_subCalInit, b'Reset')
    myLog('Failed to connect to MQTT broker. Reconnecting...')
    time.sleep(5)
    myLog('Reset!!')
    machine.reset()
示例#21
0
def enciendeCaldera():
    rele.on()
    np[0] = red
    np.write()
    myLog('Caldera ON', saveToFile=True)
示例#22
0
def showLedRGBColor(c):
    neoP[0] = NeoPixelTHO.colorByName(c)
    myLog('Color:' + str(c))
    neoP.write()
示例#23
0
def sendCalderaOn():
    MQTT_base.client.publish(config.topic_subCalderaAction,
                             config.msg_calderaOn)
    myLog('Sent Caldera On', saveToFile=True)
    speaker.tone(440, 120, 1)
示例#24
0
def apagaCaldera():
    rele.off()
    np[0] = blue
    np.write()
    myLog('Caldera OFF', saveToFile=True)
示例#25
0
v = '1.5.6'

# import machine

moduleName = 'main-Caldera'
from Utils import identifyModule, myLog
identifyModule(moduleName, v)

try:
    import MyDateTime
    # import WebServerControlRele
    import MQTT_caldera
    myLog('start Time: ' + MyDateTime.getLocalTimeHumanFormat(),
          saveToFile=True)
    import network, time
    import config
    w = network.WLAN(network.STA_IF)
    w.active(True)
    print('Conectando a Wifi')
    w.connect(config.SSID, config.SSID_PASSWD)
    while not w.isconnected():
        print('.', end='')
        time.sleep(0.5)
    print('Conectado a wifi')

    MQTT_caldera.mainBeta()

except Exception as e:
    errorMsg = str(e)
    myLog(errorMsg, saveToFile=True)
    myLog('SelfReset', saveToFile=True)
示例#26
0
def restart_and_reconnect():
    myLog('Failed to connect to MQTT broker ' + config.mqtt_server + '. Reconnecting...')
    time.sleep(10)
    myLog('Reset...')
    machine.reset()
示例#27
0
def connect_and_subscribe(topics2Subscribe,callbackFunc):
    global client, client_id
    myLog('create client')
    client = MQTTClient(client_id, config.mqtt_server)
    myLog('set callback')
    client.set_callback(callbackFunc)
    try:
        myLog('Connecting to %s MQTT broker ' % (config.mqtt_server), saveToFile = True)
        client.connect()
        myLog('connected')
        for topic in topics2Subscribe:
            myLog('subscribe 2 ' + str(topic))
            client.subscribe(topic)
            myLog('Subscribed to ' + str(topic))
        return client
    except MQTTException as e:
        myLog('MQTT exception: ' + str(e), saveToFile = True)
        #restart_and_reconnect()
        exit()
示例#28
0
def sub_CheckTopics(topic, msg):
    myLog('Base:' + str((topic, msg)))
def sub_CheckTopics(topic, msg):
    myLog(topic + ' : ' + msg)

    if topic == topic_subLedRGB:  ## Check for RGB Topic
        showLedRGBColor(msg)