示例#1
0
    def _make_button(self, key, value):

        ctrl_name = "{}".format(key)
        ctrl_pin = value["pin"]
        ctrl_pin = Pin(ctrl_pin, Pin.IN, Pin.PULL_UP)

        btn_ctrl = ButtonControl(name=ctrl_name, _pin=ctrl_pin, on_value=1)
        btn_ctrl.set_callback(self.ctl_cb)
示例#2
0
 def b4_setup(self):
     pin = 4
     ctrl_pin = Pin(pin, Pin.IN, Pin.PULL_UP)
     self.btn_ctrl = ButtonControl(name="B4",
                                   _pin=ctrl_pin,
                                   debug=True,
                                   on_value=0,
                                   off_value=1)
     self.btn_ctrl.set_callback(self.b4_cb)
示例#3
0
    def button_control(self, pin=4):

        ctrl_pin = Pin(pin, Pin.IN, Pin.PULL_UP)
        self.btn_ctrl = ButtonControl(name="B4",
                                      _pin=ctrl_pin,
                                      debug=self.debug,
                                      on_value=0,
                                      off_value=1)
        self.btn_ctrl.start()

        _ = _thread.stack_size(5 * 1024)
        th1 = _thread.start_new_thread("THRD_B_C", self.button_push_check, ())
示例#4
0
class Runner:
    '''

    '''

    # init
    def __init__(self, debug=False):

        self.debug = debug
        self.wlan = False
        self.mqtt_bus = {}
        self.btn_ctrl = False
        self.button_control()

    #BUTTON CHECK
    def button_push_check(self):

        while True:
            self.btn_ctrl.push_check
            _thread.wait(200)

    def button_control(self, pin=4):

        ctrl_pin = Pin(pin, Pin.IN, Pin.PULL_UP)
        self.btn_ctrl = ButtonControl(name="B4",
                                      _pin=ctrl_pin,
                                      debug=self.debug,
                                      on_value=0,
                                      off_value=1)
        self.btn_ctrl.start()

        _ = _thread.stack_size(5 * 1024)
        th1 = _thread.start_new_thread("THRD_B_C", self.button_push_check, ())

    def config(self):

        print("Start Config")

        self.wifi()

        self.led2_setup()
        self.btn_ctrl.set_callback(self.b4_cb)

        self.client_id = b"esp32_" + ubinascii.hexlify(machine.unique_id())
        #iot.eclipse.org
        #192.168.100.240

        self.config = {
            "broker": 'iot.eclipse.org',
            "port": 1883,
            "delay_between_message": -500,
            "client_id": self.client_id,
            "topic": b"devices/" + self.client_id + "/#",
            "ping": b"devices/" + self.client_id + "/ping",
        }

        if self.wlan_check():
            self.http()
            self.start_mqtt()

    def wlan_check(self):

        if self.wlan:
            return self.wlan.isconnected()
        else:
            return False

    #mqtt
    def mqtt_sub_cb(self, topic, msg):

        self.c_mqtt.status == 0

        if topic == self.config['ping']:

            if msg.decode() == "1":
                self.c_mqtt.status == 1

        if self.debug:
            print("Sub Message: Pub: %s, %s" % (topic.decode(), msg.decode()))

    def mqtt(self):
        self.c_mqtt = False
        try:
            self.c_mqtt = MQTTClient(self.config['client_id'],
                                     self.config['broker'],
                                     self.config['port'],
                                     timeout=1,
                                     sbt=self.config['topic'],
                                     debug=True)

            self.c_mqtt.set_callback(self.mqtt_sub_cb)

        except (OSError, ValueError):
            print("Couldn't connect to MQTT")

    def mqtt_check(self):

        delay_send_message = -300
        delay_wait_message = -1000

        while True:
            if self.wlan_check():

                if self.c_mqtt and self.c_mqtt.status == 0:
                    self.c_mqtt.communicate()

                if self.c_mqtt and self.c_mqtt.status == 1:

                    retain = False

                    if self.mqtt_bus:

                        for key, value in self.mqtt_bus.items():
                            t_start = utime.ticks_ms()

                            if value:
                                # DEBUG
                                if self.debug:
                                    print("Message Ready: Pub: %s, %s" %
                                          (key, value))

                                result = self.c_mqtt.publish(
                                    self.config[key], bytes(value, 'utf-8'),
                                    retain)

                                while utime.ticks_diff(t_start, utime.ticks_ms(
                                )) >= delay_send_message:
                                    yield None

                                if result == 1:
                                    self.mqtt_bus[key] = None

                                # DEBUG
                                if self.debug:
                                    print("Result pub to MQTT", result)

                    #wait_message
                    w_t_start = utime.ticks_ms()
                    while utime.ticks_diff(
                            w_t_start, utime.ticks_ms()) >= delay_wait_message:
                        yield None

                    print("Before wait_msg")
                    self.c_mqtt.wait_msg()
                    self.mqtt_bus['ping'] = '1'
            # else:
            #     if self.debug:
            #         print("Error: No Wifi connect")

            yield True

    def _run_mqtt_process(self):

        pubwait = self.mqtt_check()

        while True:
            try:
                next(pubwait)
            except StopIteration:
                if self.debug:
                    print("StopIteration")

            _thread.wait(1000)

    def start_mqtt(self):

        self.mqtt()

        _thread.stack_size(4 * 1024)
        self._http_thread = _thread.start_new_thread("MqttSe",
                                                     self._run_mqtt_process,
                                                     ())

    #http
    def http(self):

        _routeHandlers = []

        # System
        _routeHandlers.append(SystemHandler(debug=self.debug).route_handler)
        _routeHandlers.append(
            LedHandler(debug=self.debug, relay=self.led_2).route_handler)

        if self.debug:
            print("Routes = %s" % _routeHandlers)

        server_sehttp = SimpleHttp(port=80,
                                   web_path="/flash/www",
                                   debug=self.debug,
                                   route_handlers=_routeHandlers)
        server_sehttp.start()

        if server_sehttp.started:
            server_sehttp_runner = ProcessRuner(http_server=server_sehttp,
                                                debug=self.debug)
            server_sehttp_runner.start_http()

    #wifi
    def wifi(self):

        print("")
        print("Starting WiFi ...")
        sta_if = network.WLAN(network.STA_IF)
        _ = sta_if.active(True)
        sta_if.connect("WIFI", "PASSWORD")
        utime.sleep_ms(10000)

        if sta_if.isconnected():
            ifcfg = sta_if.ifconfig()
            print("WiFi started, IP:", ifcfg[0])
            self.wlan = sta_if
            network.ftp.start(user="******",
                              password="******",
                              buffsize=1024,
                              timeout=300)

        else:
            print("No connect to WiFi ...")

    #BUTTON CALLBACK

    def b4_cb(self):

        if self.btn_ctrl.state == "ON":
            self.led_2.change_state()

        if self.debug:
            print("Button is = %s" % self.btn_ctrl.state)

    def led2_setup(self):

        LED_2_pin = Pin(2, Pin.INOUT)
        LED_2_on = 1

        self.led_2 = RelayControl(name="Led2",
                                  _pin=LED_2_pin,
                                  on_value=LED_2_on,
                                  default=LED_2_on)
        self.led_2.set_callback(self.relay_cb)

    #RELAY CALLBACK
    def relay_cb(self, relay):

        if self.debug:
            print("Led: %s = %s" % (relay.name, relay.state))
示例#5
0
from machine import Pin
from relay_control import RelayControl
from button_control import ButtonControl

_debug = True
_message = "run in ... ."

#Led
LED_2_pin = Pin(2, Pin.INOUT)
LED_2_on = 1
LED_2 = RelayControl(name="Led2", _pin=LED_2_pin, on_value=LED_2_on, default=LED_2_on)

#BUTTON

b4_pin = Pin(4, Pin.IN, Pin.PULL_UP)
b4 = ButtonControl(name="B4", _pin=b4_pin, debug=True, on_value=0, off_value=1) #TTP223 Active Gnd, Defaul VCC

def b4_cb():

    if b4.state == "ON":
        LED_2.change_state()

    if _debug:
        print("Button is = %s" % b4.state)
        print("Led is  = %s" % LED_2.state)



# #BUTTON CHECK
def button_push_check():
示例#6
0
_debug = True
_message = "run in ... ."

#Led
LED_2_pin = Pin(2, Pin.INOUT)
LED_2_on = 1
LED_2 = RelayControl(name="Led2",
                     _pin=LED_2_pin,
                     on_value=LED_2_on,
                     default=1 - LED_2_on)

#BUTTON

b4_pin = Pin(4, Pin.IN, Pin.PULL_UP)

b4 = ButtonControl(name="B4", _pin=b4_pin, debug=True, on_value=1,
                   off_value=0)  #Holtek - BS83A02A-4 , Active VCC, default Gnd


def b4_cb():

    if b4.state == "ON":
        LED_2.change_state(LED_2_on)
    else:
        LED_2.change_state(1 - LED_2_on)

    if _debug:
        print("Button is = %s" % b4.state)
        print("Led is  = %s" % LED_2.state)


# #BUTTON CHECK
示例#7
0
def wait_msg():

    if c_mqtt and c_mqtt.status == 1:
        c_mqtt.wait_msg()


def pubm():

    next(publish)


ctrl_pin = Pin(button_pin_1, Pin.IN, Pin.PULL_UP)
b2 = ButtonControl(name="B4",
                   _pin=ctrl_pin,
                   debug=debug,
                   on_value=0,
                   off_value=1)
b2.start()

#BUTTON CALLBACK


def btn_ctrl_cb():

    if b2.state == "ON":
        relay_1.set_state(relay_on)
        pulse_relay()


b2.set_callback(btn_ctrl_cb)
示例#8
0
class Runner():
    def __init__(self):

        loop = asyncio.get_event_loop()
        self.normal = "start"
        self.service = {}

        #FTP
        self.ftpd = FTPClient(port=25)

        #telnet
        self.telnet = TelnetServer()

        #button control
        self.b4_setup()

        #LED - Heartbeat
        self.led2_setup()
        loop.create_task(self._heartbeat())

        #WIFI
        self.wifi = WifiManager()
        loop.create_task(self.wifi.sta_start())

        #services
        self.service["wifi_STA"] = False
        self.service["wifi_AP"] = False

        self.service["ftp_STA"] = False
        self.service["ftp_AP"] = False

        self.service["telnet"] = False

        self.service["mqtt"] = False
        self.mqtt = False

        self.service["http"] = False

    def _config(self):
        #Relay

        self.relay_setup()

        #MQTT
        from mqttse import MQTTClient

        self.mqtt = MQTTClient(client_id)

        # "local1": "192.168.100.240",
        # "local2": "192.168.254.1",
        # "eclipse": "iot.eclipse.org"

        self.mqtt.server = "192.168.100.240"

        self.mqtt.set_callback(self.mqtt_sub_cb)

        self.mqtt.set_topic("status", "status")
        self.mqtt.set_topic("services", "services")

        self.mqtt.set_topic("sw1_set", "sw1/set")
        self.mqtt.set_topic("sw1_state", "sw1/state")

        self.mqtt.set_topic("sw2_set", "sw2/set")
        self.mqtt.set_topic("sw2_state", "sw2/state")

        #i2c
        # esp32i2cPins = {'sda': 23, 'scl': 22}
        # sclPin = esp32i2cPins['scl']
        # sdaPin = esp32i2cPins['sda']
        i2c = I2C(scl=Pin(22), sda=Pin(23))

        #si7021
        from si7021 import SI7021
        self.s_si = SI7021(i2c)
        self.s_si.read_delay = 30
        self.s_si.sensor_detect()
        self.s_si.start()

        self.mqtt.set_topic("si_t", "si/temperature")
        self.mqtt.set_topic("si_h", "si/humidity")

        self.s_si.set_callback(self.s_si_cb)

        #bmp180
        from bmp180 import BMP180
        self.s_bmp = BMP180(i2c)
        self.s_bmp.read_delay = 30
        self.s_bmp.sensor_detect()
        self.s_bmp.start()

        self.mqtt.set_topic("bmp_t", "bmp/temperature")
        self.mqtt.set_topic("bmp_h", "bmp/pressure")
        self.mqtt.set_topic("bmp_a", "bmp/altitude")

        self.s_bmp.set_callback(self.s_bmp_cb)

        #http

        from httpse import HTTPSE
        from httpse.route_system import SystemHandler
        from httpse.route_led import LedHandler

        _routeHandlers = []
        _routeHandlers.append(SystemHandler().route_handler)
        _routeHandlers.append(LedHandler(relay=self.sw_1).route_handler)

        self.http = HTTPSE(route_handlers=_routeHandlers)

    def s_bmp_cb(self):

        if self.mqtt:
            self.mqtt.mqtt_bus["bmp_t"] = self.s_bmp.temperature
            self.mqtt.mqtt_bus["bmp_h"] = self.s_bmp.pressure
            self.mqtt.mqtt_bus["bmp_a"] = self.s_bmp.altitude

    def s_si_cb(self):

        if self.mqtt:
            self.mqtt.mqtt_bus["si_t"] = self.s_si.temperature
            self.mqtt.mqtt_bus["si_h"] = self.s_si.humidity

    #MQTT
    def mqtt_sub_cb(self, topic, msg):

        # print("Sub Message: Pub: %s, %s" % (topic.decode(), msg.decode()))
        # print("TOPIC sw1_set , %s" % (self.mqtt.topics['sw1_set']))

        if topic.decode() == self.mqtt.topics['sw1_set']:
            # print("1")

            if msg.decode() == "ON":
                # print("on")
                self.sw_1.on()

            if msg.decode() == "OFF":
                # print("off")
                self.sw_1.off()

        if topic.decode() == self.mqtt.topics['sw2_set']:

            if msg.decode() == "ON":
                self.sw_2.on()
            if msg.decode() == "OFF":
                self.sw_2.off()

    # BUTTON b4
    def b4_setup(self):
        pin = 4
        ctrl_pin = Pin(pin, Pin.IN, Pin.PULL_UP)
        self.btn_ctrl = ButtonControl(name="B4",
                                      _pin=ctrl_pin,
                                      debug=True,
                                      on_value=0,
                                      off_value=1)
        self.btn_ctrl.set_callback(self.b4_cb)

    def b4_cb(self):

        if self.btn_ctrl.state == "ON":
            if self.normal == "start":
                self.normal = "fail"
                print("Start Fail Safe Mode")

            print("Button is = %s" % self.btn_ctrl.state)

        if self.btn_ctrl.state == "OFF":
            print("Button is = %s" % self.btn_ctrl.state)

    # Blink flash LED
    def led2_setup(self):
        LED_2_pin = Pin(2, Pin.OUT)
        LED_2_on = 1
        self.led_2 = RelayControl(name="Led2",
                                  _pin=LED_2_pin,
                                  on_value=LED_2_on,
                                  default=LED_2_on)

    async def _heartbeat(self):
        while True:

            if not self.wifi.status and not self.wifi.status_ap:
                self.led_2.on()
                await asyncio.sleep_ms(100)
                self.led_2.off()
                await asyncio.sleep_ms(200)

            if self.wifi.status:
                self.led_2.off()
                await asyncio.sleep_ms(500)
                self.led_2.on()
                await asyncio.sleep_ms(5000)

            if self.wifi.status_ap:
                self.led_2.off()
                await asyncio.sleep_ms(200)
                self.led_2.on()
                await asyncio.sleep_ms(1000)

    # RELAY
    def relay_setup(self):
        sw_1_pin = Pin(26, Pin.OUT)
        sw_1_on = 0
        self.sw_1 = RelayControl(name="sw1",
                                 _pin=sw_1_pin,
                                 on_value=sw_1_on,
                                 default=1 - sw_1_on)
        self.sw_1.set_callback(self.relay_cb)

        sw_2_pin = Pin(27, Pin.OUT)
        sw_2_on = 0
        self.sw_2 = RelayControl(name="sw2",
                                 _pin=sw_2_pin,
                                 on_value=sw_2_on,
                                 default=1 - sw_2_on)
        self.sw_2.set_callback(self.relay_cb)

    def relay_cb(self, relay):

        if self.mqtt:
            self.mqtt.mqtt_bus[relay.name + '_state'] = relay.get_state()

        print("SW: %s = %s" % (relay.name, relay.state))

    #Main Loop
    async def _run_main_loop(self):
        # Loop forever
        mins = 0
        while True:
            gc.collect()  # For RAM stats.
            mem_free = gc.mem_free()
            mem_alloc = gc.mem_alloc()

            print("STA status: {}".format(self.wifi.status))
            print("AP status: {}".format(self.wifi.status_ap))

            self.status = {
                "Uptime": "{}".format(mins),
                "MemFree": "{}".format(mem_free),
                "MemAlloc": "{}".format(mem_alloc)
            }

            print("Status: {}".format(self.status))
            print("Services: {}".format(self.service))
            # print("Uptime: {}".format(mins))
            # print("MemFree: {}".format(mem_free))
            # print("MemAlloc: {}".format(mem_alloc))
            # print("Services: {}".format(self.service))

            if self.mqtt:
                self.mqtt.mqtt_bus["status"] = ("{}".format(self.status))
                self.mqtt.mqtt_bus["services"] = ("{}".format(self.service))

            mins += 1

            await asyncio.sleep(60)

    #Service Loop
    async def _run_service_loop(self):

        while True:

            if self.wifi.status:
                self.service["wifi_STA"] = self.wifi.wlan().ifconfig()[0]

            else:
                self.service["wifi_STA"] = False

            if self.wifi.status_ap:
                self.service["wifi_AP"] = self.wifi.accesspoint().ifconfig()[0]
            else:
                self.service["wifi_AP"] = False

            if self.service["wifi_STA"]:

                if not self.service["ftp_STA"]:
                    self.service["ftp_STA"] = self.ftpd.run(
                        self.service["wifi_STA"])

                if not self.service["mqtt"] and self.mqtt:
                    self.service["mqtt"] = self.mqtt.run()

                if not self.service["telnet"]:
                    self.service["telnet"] = self.telnet.start()

                if not self.service["http"] and self.http:
                    self.service["http"] = self.http.start()

            if self.service["wifi_AP"]:

                if not self.service["ftp_AP"]:
                    self.service["ftp_AP"] = self.ftpd.run(
                        self.service["wifi_AP"])

                if not self.service["telnet"]:
                    self.service["telnet"] = self.telnet.start()

                if not self.service["http"] and self.http:
                    self.service["http"] = self.http.start()

            await asyncio.sleep(10)

    async def main(self):

        print("Wait: Press Control Button")
        await asyncio.sleep(10)

        if self.normal is not "fail":
            print("Start: Normal Mode")
            self._config()
            self.normal = "run"

        log.info("Start Service Loop")
        loop = asyncio.get_event_loop()
        loop.create_task(self._run_service_loop())

        log.info("Start main_loop")
        while True:
            try:
                await self._run_main_loop()
            except Exception as e:
                print("Global communication failure: %s " % e)
示例#9
0
from runner import Runner

_debug = True
_message = "Run in DEBUG . B4 Control Pin"


#BUTTON CHECK
def button_push_check():

    while True:
        b4.push_check
        _thread.wait(200)


b4_pin = Pin(4, Pin.IN, Pin.PULL_UP)
b4 = ButtonControl(name="B4", _pin=b4_pin, debug=True, on_value=0, off_value=1)
b4.start()
_ = _thread.stack_size(6 * 1024)
th1 = _thread.start_new_thread("THRD_B4", button_push_check, ())


def main():
    if _debug:
        print("Main = %s" % _message)

    if b4.state != "ON":
        print("START = %s" % "Runner")
        r1 = Runner(name="r1", debug=True, control_pin=b4, control_thread=th1)

    else:
        print("STOP = %s" % "Runner")