Exemple #1
0
def main():
    IoTHubConnectionString = "-- pull from configuration --"

    conn = network.Cellular()
    while not conn.isconnected():
        print("Waiting for network connection...")
        sleep(4)
    print("Network connected")

    device = AzureMQTT(IoTHubConnectionString)
    print("Connecting to Azure...")
    device.setup()
    print("Azure connected")
    device.print()

    for i in range(0, 10):
        device.send('send', str(i))
        sleep(1)

    # Loop forever checking for messages from Azure
    print("Checking for messages for Azure...")
    while True:
        message = device.check_msg()
        if message:
            print(message)
        sleep(1)
Exemple #2
0
def main():
    # IoT Hub Connection String
    # HostName=<Host Name>;DeviceId=<Device Name>;SharedAccessKey=<Device Key>
    IoTHubConnectionString = "FILL_ME_IN"
    if IoTHubConnectionString == "FILL_ME_IN":
        raise ValueError(
            "You need to update IoTHubConnectionString using information in the Azure Device Explorer Tool"
        )

    conn = network.Cellular()
    while not conn.isconnected():
        print("Waiting for network connection...")
        sleep(4)
    print("Network connected")

    device = AzureMQTT(IoTHubConnectionString)
    print("Connecting to Azure...")
    device.setup()
    print("Azure connected")
    device.print()

    # Send back a number of messages up to Azure
    print("Sending 10 messages to Azure...")
    for i in range(0, 10):
        device.send('send', str(i))
        sleep(1)

    # Loop forever checking for messages from Azure
    print("Checking for messages for Azure...")
    while True:
        message = device.check_msg()
        if message:
            print(message)
        sleep(1)
    def create_cellular_connection():
        print("Creating network connection")
        network_connection = network.Cellular()
        print("Connecting")
        while not network_connection.isconnected():
            print("waiting for network connection...")
            time.sleep(4)

        return network_connection
Exemple #4
0
def demo_sms_message():
    '''
    Demonstrates sending an SMS message to your phone.
    Also demonstrates how to receive an SMS message.
    '''
    import network
    c = network.Cellular()
    c.sms_send("3332221111", "Hello from your XBee")
    # Now send a message back from your phone. Then call the following function to receive it:
    msg = c.sms_receive()
    print("Received: %s" % msg)
def connect(client_id):
    conn = network.Cellular()

    aws_endpoint = 'a24e0i570zhegg.iot.us-east-2.amazonaws.com'

    ssl_params = {
        'keyfile': "/flash/cert/aws.key",
        'certfile': "/flash/cert/aws.crt",
        'ca_certs': "/flash/cert/aws.ca"
    }

    while not conn.isconnected():
        print("waiting for network connection...")
        time.sleep(4)

    c = MQTTClient(client_id, aws_endpoint, ssl=True, ssl_params=ssl_params)
    c.connect()
    return c
Exemple #6
0
Sample code released under MIT License.

Demonstration code for using network.Cellular() class's sms_receive() method.

Instructions:
  - Upload/run this code on a module connected to the Internet using
    "paste mode".
  - You can use c.isconnected() to see if the Cellular Modem has established
    a connection.
  - Type wait_for_sms() in the REPL to wait for an SMS to arrive and then
    print it out.
"""

import network, time, xbee

c = network.Cellular()

def timestamp(t = None):
    return "%04u-%02u-%02uT%02u:%02u:%02u" % time.localtime(t)[0:6]

def check_sms():
    msg = c.sms_receive()
    if msg:
        print('SMS at %s from %s:\n%s' % (timestamp(msg['timestamp']),
              msg['sender'], msg['message']))
    return msg

def wait_for_sms():
    print('Waiting for SMS to', xbee.atcmd('PH'))
    while not check_sms():
        time.sleep_ms(100)
from umqtt.simple import MQTTClient
from sensor.hdc1080 import HDC1080
from machine import I2C
import time, ujson, network

# AWS endpoint parameters
host = b'FILL_ME_IN'  # ex: b'abcdefg1234567'
region = b'FILL_ME_IN'  # ex: b'us-east-1'

aws_endpoint = b'%s.iot.%s.amazonaws.com' % (host, region)
ssl_params = {'keyfile': "/flash/cert/aws.key",
              'certfile': "/flash/cert/aws.crt",
              'ca_certs': "/flash/cert/aws.ca"}  # ssl certs

conn = network.Cellular()
while not conn.isconnected():
    print("waiting for network connection...")
    time.sleep(4)
print("network connected")

threshold_temp = 80.0
wait_timer = 10
def cb_func(topic, msg):
    global threshold_temp
    global wait_timer
    d = ujson.loads(msg.decode("utf-8"))
    if "threshold_temp" in d.keys():
        try:
            threshold_temp = float(d['threshold_temp'])
            print("updated threshold to %s" % (threshold_temp))
Exemple #8
0
def power_test():
    '''
    Puts the XBee3 into sleep mode for a short amount of time, sends an HTTP request, then puts the device back to sleep.
    Then we can hook up some hardware instrumentation in order to measure both the quiescent power consumption (sleeping)
    as well as maximum power consumption (cellular transmitting).
    '''

    print("Launching power test.")

    from machine import Pin
    import network
    import time
    import urequests
    import xbee

    cell = network.Cellular()
    led = Pin("D5", Pin.OUT, value=0)
    xb = xbee.XBee()

    while True:

        # Put the XBee to sleep. Turn off the LED first to indicate that we're going to sleep.
        led.value(0)
        SLEEPTIME_ms = 30000
        print("Going to sleep for %s seconds." % str(SLEEPTIME_ms / 1000.0))
        xb.sleep_now(
            SLEEPTIME_ms, pin_wake=False
        )  # Execution blocks here for the specified time. Device goes into low power mode (NOT the same as time.sleep() which sleeps the thread!)

        # Now the device is awake.
        with xb.wake_lock:

            # Blink the LEDs for 2 seconds to indicate that we've woken up.
            print("Woke up!")
            for _ in range(20):
                led.toggle()
                time.sleep_ms(100)

            # Hold on the LED to indicate we're getting ready to transmit.
            led.value(1)
            time.sleep_ms(1000)

            # Attempt to establish cell connection.
            print("Checking for cell connection.")
            print("Is active (not airplane mode)? %s" % cell.active())
            for _ in range(600):
                print(".", end="")
                if cell.isconnected():
                    break
                time.sleep_ms(100)

            if not cell.isconnected():
                print("Unable to get cell connection.")
                for _ in range(8):
                    led.toggle()
                    time.sleep_ms(500)
                continue

            # Cell connection was successful!
            print("Got cell connection!")
            print(cell.ifconfig())
            print("SIM:", cell.config('iccid'))
            print("IMEI:", cell.config('imei'))
            print("Provider:", cell.config('operator'))
            print("Phone #:", cell.config('phone'))
            print("Cell signal strength: %s" % xbee.atcmd('DB'))

            # Transmit an HTTP request.
            try:
                r = urequests.get("http://api.ipify.org/")
                print("HTTP response: %s, %s" % (r.status_code, r.text))
            except Exception as ex:
                print("HTTP request failed. Exception: %s" % ex)