Example #1
0
def ntp():
    ntptime.host = key_store.get('ntp_host')
    print("NTP Server:", ntptime.host)
    while utime.time() < 10000:  # Retry until clock is set
        ntptime.settime()
        utime.sleep(1)
    print('UTC Time:   {}-{:02d}-{:02d} {:02d}:{:02d}:{:02d}'.format(*utime.localtime()))
def ntp():
    import ntptime
    ntptime.host = key_store.get('ntp_host')
    print("NTP Server: ", ntptime.host)
    start_ntp = utime.ticks_ms()
    while utime.time() < 10000:  # Clock is not set with NTP if unixtime is less than 10000
        ntptime.settime()
        if utime.ticks_diff(utime.ticks_ms(), start_ntp) > 10000:  # 10 second timeout
                print('NTP Timeout... Resetting Device')
                reset()
    print('  UTC Time:  {}-{:02d}-{:02d} {:02d}:{:02d}:{:02d}'.format(*utime.localtime()))
    print()
import key_store
from sys import exit

import AnalogDevices_TMP36

if 'TinyPICO' in uname().machine:
    import TinyPICO_RGB as led
    led.off()

# Get Unique Machine ID
from client_id import client_id

# Get ADC Pin from key_store.db
if 'esp32' in uname().sysname:
    if key_store.get('ADC_PIN') is None:
        key_store.set('ADC_PIN', input('Enter ADC Pin Number - '))
    ADC_PIN = key_store.get('ADC_PIN')
elif 'esp8266' in uname().sysname:
    ADC_PIN = 0  # gpio_pin_number is ignored since there is only one ADC on ESP8266
else:
    print('unknown hardware')
    exit(1)

# Get InfluxDB server:port:database:measurement from key_store.db
# i.e. influxdb.localdomain:8086:test:garage
if key_store.get('influxdb') is None:
    print('Need to add settings to key_store.db...')
    key_store.set('influxdb',
                  input('Enter InfluxDB server:port:database:measurement - '))
server, port, database, measurement = key_store.get('influxdb').split(':')
# Utilize LED if available
#-------------------------
if 'TinyPICO' in uname().machine:
    import TinyPICO_RGB as led
    led.off()

#----------------------
# Get Unique Machine ID
#----------------------
from client_id import client_id

#----------------------------------------------------------------
# Get InfluxDB server:port:database:measurement from key_store.db
#   i.e. influxdb.localdomain:8086:Garage:DHT22
#----------------------------------------------------------------
if key_store.get('influxdb') is None:
    print('Need to add settings to key_store.db...')
    key_store.set('influxdb',
                  input('Enter InfluxDB server:port:database:measurement - '))
server, port, database, measurement = key_store.get('influxdb').split(':')

#-------------------------------------
# Get Sleep Interval from key_store.db
#-------------------------------------
if key_store.get('sleep_interval') is None:
    key_store.set('sleep_interval',
                  input('How many seconds between sensor reads - '))
sleep_interval = int(key_store.get('sleep_interval'))

#----------------------------
# Set URL for Database Writes
import key_store
from client_id import client_id
from sys import exit

import Milone_eTape

if 'TinyPICO' in uname().machine:
    import TinyPICO_RGB as led
    led.off()

# Get Unique Machine ID
from client_id import client_id

# Get ADC Pin from key_store.db
if 'esp32' in uname().sysname:
    if key_store.get('ADC_PIN') is None:
        key_store.set('ADC_PIN', input('Enter ADC Pin Number - '))
    ADC_PIN = key_store.get('ADC_PIN')
elif 'esp8266' in uname().sysname:
    ADC_PIN = 0  # gpio_pin_number is ignored since there is only one ADC on ESP8266
else:
    print('unknown hardware')
    exit(1)

# Get InfluxDB server:port:database:measurement from key_store.db
# i.e. influxdb.localdomain:8086:Garage:DHT22
if key_store.get('influxdb') is None:
    print('Need to add settings to key_store.db...')
    key_store.set('influxdb', input('Enter InfluxDB server:port:database:measurement - '))
server,port,database,measurement = key_store.get('influxdb').split(':')
import dht
from machine import reset, Pin
from time import sleep
import urequests

import key_store
from client_id import client_id
from sys import exit

# Get Unique Machine ID
from client_id import client_id
print('Client ID:', client_id)

# Get InfluxDB server:port:database:location from key_store.db
# i.e. influxdb.localdomain:8086:garage:DHT22
server, port, database, name = key_store.get('influxdb').split(':')
url = 'http://%s:%s/write?db=%s' % (server, port, database)
print(url)

sleep_interval = 30  # Seconds

# Any GPIO pin should work
sensor = dht.DHT22(Pin(18))


def main():
    print('=' * 45)
    print()

    # Read the Temperature and Humidity
    sensor.measure()
Example #7
0
from machine import reset, deepsleep
import mqtt
import SparkFun_TMP102 as tmp102

sleep_interval = 20  # Seconds
sleep_type = 'normal'  # normal or deep

# Get Unique Client ID
from ubinascii import hexlify
from machine import unique_id
client_id = hexlify(unique_id()).decode(
    'utf-8')  # String with Unique Client ID

# Get MQTT Broker IP from key_store.db
import key_store
broker = key_store.get('mqtt_broker')
topic = 'devices/' + client_id


def main():
    # Read Timestamp and Data
    timestamp = utime.time()  # Epoch UTC
    temperature = round(tmp102.read_temp('F'), 1)

    # Send Data to MQTT Broker
    mqtt.publish(broker, topic + '/temp/value', str(temperature))

    # Log Timestamp and Data locally?
    log_local(timestamp, temperature)

    t = utime.localtime(timestamp)
Example #8
0
            timestamp = str(utime.time())
            key_store.set(timestamp, str(current_power_status))
            last_power_status = current_power_status

        # MQTT pings every 2 seconds of current power status
        if counter >= 2000:  # elapsed milliseconds
            # If WiFi is down the following will pause for the duration.
            await client.publish('devices/' +
                                 config['client_id'].decode('utf-8') +
                                 '/power/value',
                                 str(current_power_status),
                                 qos=1)
            counter = 0
        else:
            counter += 100  # Since we are waiting 100ms in asyncio.sleep_ms(100)


# Override default mqtt_as.py config variable settings
config['wifi_coro'] = wifi_handler
config['ssid'] = key_store.get('ssid_name')
config['wifi_pw'] = key_store.get('ssid_pass')
config['server'] = key_store.get('mqtt_broker')

MQTTClient.DEBUG = False  # Optional: print diagnostic messages
client = MQTTClient(config)
loop = asyncio.get_event_loop()
try:
    loop.run_until_complete(main(client))
finally:
    client.close()  # Prevent LmacRxBlk:1 errors
    try:
        if color == 'red':
            TinyPICO_RGB.solid(255,0,0)
        elif color == 'blue':
            TinyPICO_RGB.solid(0,0,255)
        elif color == 'purple':
            TinyPICO_RGB.solid(255,0,255)
        else:
            TinyPICO_RGB.off()
    except:
        pass  # If TinyPICO_RGB.py is missing do nothing

# Load secrets from local key_store.db
try:
    import key_store
    ssid_name = key_store.get('ssid_name')
    ssid_pass = key_store.get('ssid_pass')
except:
    try:
        wdt_feed(WDT_CANCEL)
    except:
        pass
    key_store.init()
    reset()

# Connect to WiFI
def wlan_connect(ssid, password):
    import network
    from ubinascii import hexlify
    wlan = network.WLAN(network.STA_IF)
    if not wlan.active() or not wlan.isconnected():
Example #10
0
import key_store
from iot_api import iot_api
from client_id import client_id
import AnalogDevices_TMP36 as tmp36
from sys import exit
from uos import uname

# Get Unique Machine ID
from client_id import client_id

print('Client ID:', client_id)

# Get ADC Pin from key_store.db
if 'esp32' in uname().sysname:
    ADC_PIN = key_store.get('ADC_PIN')
    if ADC_PIN is None:
        key_store.set('ADC_PIN', input('Enter ADC Pin Number - '))
        reset()

# Get iot-api server:port from key_store.db
try:
    server, port = key_store.get('iot-api').split(':')
except:
    server = input('Enter iot-api server:port - ')
    key_store.set('iot-api', server)
    reset()

if server == 'api.thingspeak.com':
    client_id = key_store.get('thingspeak_api_key')
    if not client_id: