예제 #1
0
import config
import time
from network import WLAN
from network import Server

# Conexion a la WiFi
wlan = WLAN(mode=WLAN.STA)  # Modo adaptador wifi
wlan.connect(
    config.wifi_ssid,
    auth=(None,
          config.wifi_pass))  # Orden y parámetros de conexión a la red wifi
if config.REPL:
    while not wlan.isconnected():
        print('No conectado a WiFi')
        time.sleep(5)
    if wlan.isconnected():
        print('Conectado a WiFi: ' + config.wifi_ssid)

# Servicio telnet
server = Server(login=(config.user, config.password), timeout=60)
server.timeout(300)  # change the timeout
예제 #2
0
def setup():
    global wdt, sd, logger, AP, pin_relay_AP, pin_relay_LED, pin_switch_setupMode, pin_switch_RPi, wdt
    global server, server_timer, adc_c, taskTime_daily, taskTime_hourly

    # Logger
    logging.basicConfig(level=logging.DEBUG)
    logger = logging.getLogger(__name__)

    logger.info('===========================')
    logger.info(' Starting CTLR ')
    logger.info('===========================')

    # HW Setup
    wdt = WDT(timeout=20 * 60 * 1000)
    wdt.feed()
    sd = SD()
    os.mount(sd, '/sd')
    adc = machine.ADC()
    # adc_c = adc.channel(pin='P19', attn=ADC.ATTN_11DB)
    adc_c = adc.channel(pin='P18', attn=ADC.ATTN_11DB)
    # Output Vref of P22
    # adc.vref_to_pin('P22')
    # while True:
    #     time.sleep(1)
    # Set calibration - see note above
    adc.vref(1100)

    # # TEMPERATURE/HUMIDITY SENSOR
    # py = Pysense()
    # si7006a20  = SI7006A20(py)

    pin_relay_AP = Pin('P20', mode=Pin.OUT, pull=Pin.PULL_DOWN)
    pin_relay_LED = Pin('P19', mode=Pin.OUT, pull=Pin.PULL_DOWN)
    pin_switch_setupMode = Pin('P9', mode=Pin.IN, pull=Pin.PULL_DOWN)
    pin_switch_RPi = Pin('P7', mode=Pin.OUT, pull=Pin.PULL_DOWN)

    # Network Setup
    # CHANGE AP IN pybytes_config.json!
    pbconfig = pybytes.get_config()
    AP = pbconfig['wifi']['ssid']

    # WIFI Connection
    if AP in ['GCAM1_AP', 'GCAM2_AP', 'GCAM2_AP']:
        pin_relay_AP.value(1)
        wlan = WLAN(mode=WLAN.STA)
        while not wlan.isconnected():
            nets = wlan.scan()
            logger.info(nets)
            if AP in [net.ssid for net in nets]:
                pybytes.connect_wifi()
            time.sleep(5)
            # for net in nets:
            #     if net.ssid == 'RUT230_7714':
            #         pybytes.connect_wifi()
        # # No Internet Case
        # wlan.ifconfig(config=('192.168.1.100', '255.255.255.0', '192.168.1.1', '8.8.8.8'))
        # wlan.connect('RUT230_7714', auth=(WLAN.WPA2, 'Ei09UrDg'), timeout=5000)
        # while not wlan.isconnected():
        #     machine.idle() # save power while waiting

    socket_svr.switch_setupMode = pin_switch_setupMode.value()
    socket_svr.setup()
    # _thread.stack_size(16384)

    # UPDATE TIME BY GPS
    logger.info('GPS time fixing start...')
    shmGPSclock.update_RTC_from_GPS()
    socket_svr.state = 1
    logger.info('GPS time fixing done...')

    server = Server()
    server.timeout(5)

    # Creating data/date folder of today
    dirToday = '/sd/data/{}'.format(date_string(time.time()))
    if mkdir(dirToday):
        logger.info('{} created'.format(dirToday))
    gc.enable()

    # CREATING FOLDER FOR FTP
    mkdir('/sd/data/{}'.format(date_string(time.time())))
    # PERIODIC TASKS
    taskTime_daily = getNextGridTime(time.time(), 3600 * 24)
    taskTime_hourly = getNextGridTime(time.time(), 3600)