Example #1
0
def features(on=None):
    if on is None:
        print("heartbeat   ", pycom.heartbeat_on_boot())
        print("lte         ", pycom.lte_modem_en_on_boot())
        try:
            print("pybytes     ", pycom.pybytes_on_boot())
        except:
            pass
        try:
            print("smart_config", pycom.smart_config_on_boot())
        except:
            pass
        print("wdt         ", pycom.wdt_on_boot())
        print("wifi        ", pycom.wifi_on_boot())
    else:
        print("configure all features as", on)
        pycom.heartbeat_on_boot(on)
        pycom.lte_modem_en_on_boot(on)
        try:
            pycom.pybytes_on_boot(on)
        except:
            pass
        try:
            pycom.smart_config_on_boot(on)
        except:
            pass
        pycom.wdt_on_boot(on)
        pycom.wifi_on_boot(on)
        features(None)
Example #2
0
def set_persistent_settings():
    _logger.info("Setting persistent settings...")
    pycom.wifi_on_boot(False)
    pycom.lte_modem_en_on_boot(False)
    pycom.heartbeat_on_boot(False)
    pycom.wdt_on_boot(True)
    pycom.wdt_on_boot_timeout(1000 * 30)
def show_boot_flags():
    _logger.info("pycom.wifi_on_boot():         %s", pycom.wifi_on_boot())
    with CheckStep(FLAG_LTE_FW_API, suppress_exception=True):
        # These methods are not available on old firmware versions
        # If they are missing, we need to upgrade the Pycom firmware
        _logger.info("pycom.lte_modem_en_on_boot(): %s",
                     pycom.lte_modem_en_on_boot())
        _logger.info("pycom.heartbeat_on_boot():    %s",
                     pycom.heartbeat_on_boot())
        _logger.info("pycom.wdt_on_boot():          %s", pycom.wdt_on_boot())
        _logger.info("pycom.wdt_on_boot_timeout():  %s",
                     pycom.wdt_on_boot_timeout())
Example #4
0
# boot.py -- run on boot-up
# 2019-0915 disabe wifi and wdt
# bron: https://docs.pycom.io/firmwareapi/pycom/pycom/
import micropython
import pycom
from machine import I2C

# allocate memory for exceptions...
# Pycom: https://docs.pycom.io/chapter/firmwareapi/micropython/micropython.html
micropython.alloc_emergency_exception_buf(100)

# disable WiFi on boot, i.e. no AP-netwerk on Wifi
pycom.wifi_on_boot(False)
print('Wifi AP-mode enabled: ', pycom.wifi_on_boot())

# If this flag is set, the application needs to reconfigure the WDT with a
# new timeout and feed it regularly to avoid a reset.
pycom.wdt_on_boot(False)  # no Watch Dog
print('WDT enabled: ', pycom.wdt_on_boot())

# setup i2c, use default PIN assignments (P10=SDA, P11=SCL)
# Expansion board 3.0: SCL=GPO17 ('P10'), SDA=GPO16 ('P9')
i2c = I2C(0, I2C.MASTER)
print('i2c scan:', i2c.scan())
Example #5
0
# WLAN().deinit()
# Server().deinit()
#### changing the following needs a reset of the device to take effect!
# pybytes.smart_config(False)
# pybytes.set_config("pybytes_autostart","true")
# pycom.wifi_on_boot(False)
# pycom.wdt_on_boot(False)

print("sys.platform:", sys.platform)
print("machine.unique_id:", ubinascii.hexlify(machine.unique_id()))
print("machine.freq:", machine.freq())
print("machine.info:")
machine.info()

print("pycom.wifi_on_boot:", pycom.wifi_on_boot())
print("pycom.wdt_on_boot:", pycom.wdt_on_boot())
# print("pycom.smart_config_on_boot:", pycom.smart_config_on_boot())

sigfox = Sigfox(mode=Sigfox.SIGFOX, rcz=RCZ)
print("sigfox version:", sigfox.version())
print("sigfox ID:", ubinascii.hexlify(sigfox.id()))
print("sigfox PAC:", ubinascii.hexlify(sigfox.pac()))
print("sigfox region:", RCZ)
print("sigfox frequencies:", sigfox.frequencies())
print("sigfox rssi offset:", sigfox.rssi_offset())
# sigfox config
rcz3_config_test_mode = (
    1, 0x2ee0, 0x100
)  # ie, 1 retry, 12sec timeout, 3rd config is always ignored in R3
# rcz3_config_default = (3,   5000, 0x100) # ie, 3 retries, 5sec timeout
print("sigfox config:", sigfox.config())
Example #6
0
    WAIT_FOR_LORA_S = const(30)
    SLEEP_TIMEOUT_S = const(600)

    # Colors
    OFF = const(0x000000)
    VIOLET = const(0x9400D3)
    INDIGO = const(0x4B0082)
    BLUE = const(0x0000FF)
    GREEN = const(0x00FF00)
    YELLOW = const(0xFFFF00)
    ORANGE = const(0xFF7F00)
    RED = const(0xFF0000)

    pycom.wifi_on_boot(False)
    pycom.wdt_on_boot(False)
    pycom.heartbeat(False)
    if DEBUG:
        pycom.rgbled(VIOLET)
        print("Collecting environment data")
    py = Pysense()
    mp = MPL3115A2(py, mode=PRESSURE)
    si = SI7006A20(py)
    battery = py.read_battery_voltage()
    temperature = si.temperature()
    humidity = si.humidity()
    pressure = mp.pressure()
    if DEBUG:
        print("Battery voltage =", battery, "V")
        print("Temperature =", temperature, "°C")
        print("Humidity =", humidity, "%")
Example #7
0
    import machine
    uid = binascii.hexlify(machine.unique_id()).decode("utf-8")
    name = os.uname().sysname.lower() + '-' + uid[-4:]
    print(name, uid)

    features()
    if False:
        features(True)  # turn everything on
        features(False)  # turn everything off
        # print
        import pycom
        print('heartbeat', pycom.heartbeat_on_boot())
        print('lte', pycom.lte_modem_en_on_boot())
        print('pybytes', pycom.pybytes_on_boot())
        print('smart_config', pycom.smart_config_on_boot())
        print('wdt', pycom.wdt_on_boot())
        print('wifi', pycom.wifi_on_boot())
        # off
        pycom.heartbeat_on_boot(False)
        import pycom
        pycom.lte_modem_en_on_boot(False)
        pycom.wdt_on_boot(False)
        pycom.wifi_on_boot(False)
        pycom.smart_config_on_boot(False)
        pycom.pybytes_on_boot(False)
        # on
        pycom.heartbeat_on_boot(True)
        pycom.lte_modem_en_on_boot(True)
        pycom.pybytes_on_boot(True)
        pycom.smart_config_on_boot(True)
        pycom.wdt_on_boot(True)
Example #8
0
# boot.py -- run on boot-up
import pycom
from lib.indicators import Indicators
import _thread
from machine import SD
import os

pycom.wifi_on_boot(False)

ind = Indicators()
_thread.start_new_thread(ind.start, ())

sd = SD()
os.mount(sd, '/sd')
# check the content
os.listdir('/sd')

# try some standard file operations
f = open('/sd/test.txt', 'w')
f.write('Testing SD card write operations')
f.close()
f = open('/sd/test.txt', 'r')
aaa = f.read()
print("Test SD: " + str(aaa))
f.close()

pycom.pybytes_on_boot(False)
pycom.wdt_on_boot(True)
pycom.wdt_on_boot_timeout(720000)
pycom.heartbeat(False)
def runMain():
    import pycom
    #first thing first
    pycom.wdt_on_boot(True)
    pycom.wdt_on_boot_timeout(60000 * 5)  # 5 min

    import _thread
    import uos

    from globs import *
    from helpers import hardwareInit, buildPFAlarmMessage, initConfigurationVariable, getConfigurationVariable, getRandom
    import pycom
    import time
    import machine
    from machine import Pin
    from loratask import loraTask
    from modbus import readRegisters, initModbus, deinitModbus, readPilot, getLatestInstMeasurments, MB_SUCCESS
    from network import WLAN
    from machine import WDT
    import binascii
    import network
    from machine import WDT
    import gc

    import _thread
    from queuewrapper import QueueWrapper

    print('** MAIN POC ' + __version__ + ' By ' + __author__ + ' Copyright ' +
          __copyright__ + ' **')
    print(str(uos.uname()))

    DEFAULT_STACK_SIZE = 5000

    _thread.stack_size(DEFAULT_STACK_SIZE)

    AS = 0
    try:
        from deviceid import antenna_setting
        AS = antenna_setting
    except:
        print('antenna_setting internal')
    else:
        print('antenna_setting is' + str(AS))

    initConfigurationVariable(NVS_ANTENNA_CONFIGURATION, AS)
    antenna = getConfigurationVariable(NVS_ANTENNA_CONFIGURATION)
    # init the Hardware ( mainly the LoRa switch) and get the running mode, PF or NORMAL
    mode = hardwareInit(antenna)
    pycom.nvs_set(POWER_FAIL_STATE, mode)
    if mode == MODE_POWER_FAIL:
        LED_OUT = Pin(LED_OUT_PIN)
        LED_OUT.value(1)
        timeOfFault = pycom.nvs_get(POWER_FAIL_TS)
        alarmLoraMessageToSend = buildPFAlarmMessage(timeOfFault)
        lora_queue_immediate_semaphore.acquire(0)
        LoraQueueImmediate.put(alarmLoraMessageToSend)
        _thread.start_new_thread(
            loraTask, ()
        )  # we just start the lora Task, send the PF alarm and go back to sleep
        if lora_stop_flag.locked():
            lora_stop_flag.release()

        time.sleep_ms(WAIT_TIME_BEFORE_SLEEP *
                      10)  # time for the LoRA MAC layer to send that message
        rand = int(getRandom() * 20)  # 0-20 value
        #machine.deepsleep((WAKEUP_DELAY+rand)*1000) # GO TO SLEEP NOW AND PRESERVE POWER

    from wdttask import wdtTask
    from wifitask import wifiTask
    from loopimpedancetask import loopImpedanceTask
    from modbustask import modbusTask
    from maintask import mainTask
    from eventtask import eventTask
    from computingtask import computingTask

    FCTMode = False  # if true this mode is used in the factory during FCT
    SERIAL_NUMBER_REGISTER = 48888  # only works for M11 and C11 with SPM93
    MIN_SERIAL_NUMBER_VAL = 10000000
    MAX_SERIAL_NUMBER_VAL = 99999999
    # Read config variables
    CT = 20  # default is 20 for M11 (only apply to M11)
    try:
        from deviceid import ctRatio
        CT = ctRatio
    except:
        pass

    print('M11 SW Ct Ratio is' + str(CT))

    RO = int(getRandom() * 300)  # 0-300 value
    try:
        from deviceid import radio_offset
        RO = radio_offset
    except:
        print('Radio Offset not defined, using random ' + str(RO))
    else:
        print('Radio Offset is' + str(RO))

    DT = 0  # assume M11
    try:
        from deviceid import deviceType
        DT = deviceType
    except:
        print('Device type is not defined, using M11')
        FCTMode = True  # no config means FCT phase
    else:
        print('Device type is' + str(DT))

    if FCTMode == True:
        print('FCT mode')
        # read the S/N from the meter
        initModbus()
        serial_number = 0
        dev_type = 0
        MB_ADD = 22
        MODEL_M11 = "00"  #M11
        MODEL_M11E_20 = "01"  #M11E-20
        Model = MODEL_M11
        # ONLY SUPPORT m11 AT PRESENT
        try:
            raw_serial_number = readRegisters(MB_ADD, dev_type,
                                              SERIAL_NUMBER_REGISTER, 2)
            # convert to Uint32
            serial_number = int(raw_serial_number[2] << 8
                                | raw_serial_number[3]
                                | raw_serial_number[0] << 24
                                | raw_serial_number[1] << 16)
        except:
            print("Calibration mode")
            serial_number = 0

        print('S/N:' + str(serial_number))
        uid = str(binascii.hexlify(machine.unique_id()))
        #validate
        Ssid = "TESTPP"
        if serial_number >= MIN_SERIAL_NUMBER_VAL and serial_number <= MAX_SERIAL_NUMBER_VAL:
            # calibration successfull use the SN as SSID
            #
            #  Read current and voltage M11 (5A - 230V) if successfull append S if not append F
            Result = "F"
            try:
                res = readPilot(MB_ADD, dev_type,
                                20)  # M11, address 22 with  ct is 20
                if res != MB_SUCCESS:
                    res = readPilot(MB_ADD, dev_type,
                                    20)  # M11, address 22 with  ct is 20
                    if res != MB_SUCCESS:
                        res = readPilot(MB_ADD, dev_type,
                                        20)  # M11, address 22 with  ct is 20

                data = getLatestInstMeasurments()
                V = float(data[INST_VOLTAGE])
                I = float(data[INST_CURRENT])
                print("Voltage:" + str(V) + "V - Current" + str(I) + "A")
                if (V >= 228) and (V <= 232) and (I >= 4.8) and (I <= 5.2):
                    Result = "S"
                    Model = MODEL_M11E_20  # if this is successful then it means we have CT ratio set up
            except:
                pass

            if dev_type == 0:
                DEV = "M"
            else:
                DEV = "C"
            Ssid = DEV + "{0:0=8d}".format(serial_number) + "-{0:0=3d}".format(
                VERSION_SOFTWARE) + uid[2:14].upper() + Result + Model
            # M19000001-022000000000000S03
            # TSSSSSSSSFFFMMMMMMMMMMMMRII

        else:
            #possible calibration in progress
            # display serial port must be high impedance:
            deinitModbus()
            p3 = Pin('P3', mode=Pin.IN, pull=None)
            p4 = Pin('P4', mode=Pin.IN, pull=None)
            wdt = WDT(timeout=60000 * 5)  # 7 min
            print("Released display lines and waiting")
            while True:  #forever loop there
                wdt.feed()
                time.sleep(10)

        print('SSID:' + Ssid)
        wlan = WLAN()
        wlan.init(mode=WLAN.AP,
                  ssid=Ssid,
                  auth=(WLAN.WPA2, 'powerpilot'),
                  channel=8,
                  antenna=WLAN.INT_ANT,
                  hidden=False)

        server = network.Server()
        server.deinit()  # disable the server
        # enable the server again with new settings
        server.init(login=('factory', 'pilot'), timeout=120)

    else:  # FCTMode == False

        # Init the configurable parameters in NVS if needed (first time)
        initConfigurationVariable(NVS_RADIO_DELAY, RO)
        initConfigurationVariable(NVS_HV_ALARM,
                                  VOLTAGE_EXCURSION_6_HIGH_THRESOLD)
        initConfigurationVariable(NVS_LV_ALARM,
                                  VOLTAGE_EXCURSION_6_LOW_THRESOLD)
        initConfigurationVariable(NVS_VHV_ALARM,
                                  VOLTAGE_EXCURSION_10_HIGH_THRESOLD)
        initConfigurationVariable(NVS_VLV_ALARM,
                                  VOLTAGE_EXCURSION_10_LOW_THRESOLD)
        initConfigurationVariable(NVS_CT_RATIO, CT)
        initConfigurationVariable(NVS_DEVICE_TYPE, DT)
        initConfigurationVariable(NVS_INST_DATA_FREQ, 10)

        time_set_flag.acquire(0)  #time need to be set (released = TIME is set)

        # NORMAL MODE, start all tasks
        _thread.start_new_thread(modbusTask, ())
        gc.mem_free()
        _thread.start_new_thread(loopImpedanceTask, ())
        _thread.start_new_thread(eventTask, ())
        _thread.start_new_thread(wdtTask, ())
        gc.mem_free()
        _thread.start_new_thread(computingTask, ())
        #_thread.stack_size(DEFAULT_STACK_SIZE )
        _thread.start_new_thread(loraTask, ())
        _thread.start_new_thread(wifiTask, ())
        gc.mem_free()
        _thread.start_new_thread(mainTask, ())

        time.sleep(1)

        if mb_stop_flag.locked():
            mb_stop_flag.release()

        if compute_stop_flag.locked():
            compute_stop_flag.release()

        if event_stop_flag.locked():
            event_stop_flag.release()

        if li_stop_flag.locked():
            li_stop_flag.release()

        if wifi_stop_flag.locked():
            wifi_stop_flag.release()

        if lora_stop_flag.locked():
            lora_stop_flag.release()

        if main_stop_flag.locked():
            main_stop_flag.release()