Digi XBee Cellular 3G embedded modems provide a simple path to 3G (HSPA/GSM) with 2G fallback connectivity for OEMs with worldwide deployments. This modem is FCC/IC, PTCRB and AT&T certified which completely eliminates the cost, complexity, and risk involved in the certification process.

The modem is programmable, with support for custom MicroPython applications running directly onboard, allowing users to more efficiently manage their devices and eliminating the need for an external microcontroller in certain use-cases. It includes the full suite of standard Digi XBee API frames and AT commands, so existing customers can simply drop this modem into their existing designs to instantly achieve 3G cellular integration, without the pain and hassle of doing a complete re-design.

Table of content

  • Requirements
  • Setting up Digi XBee.
  • Installing XCTU Software.
  • Sending data to Ubidots.
    a) Setting the radio parameters
    b) Coding with MycroPython

Requirements

  • DIGI XBEE® CELLULAR 3G DEVELOPMENT KIT
  • Nano SIM Card (With data plan).
  • XCTU Software.
  • Ubidots license.

Setting up Digi XBee

1. First, plug-in the nano SIM inside the Digi XBee 3G module SIM slot.

2. Then install the Digi XBee 3G module on the development board.

3. Connect the antenna to the XBee Cellular Modem by aligning the U.FL connector carefully, then firmly press straight down to seat the connector.

4. Plug the 12V power supply to the power jack on the development board.

5. Now, connect the USB cable from a PC to the USB port on the development board. The computer searches for a driver, which can take a few minutes to install.

Installing XCTU Software.

  1. Download XCTU software.

2. Launch the program and click on Discover Radio Modules to search and install the Digi XBee development board.

3. Choose the port where the DIGI XBee development board is connected and click “Next”. Leave the default parameters and Finish the installation.

4. Select XBee module, then add the device by clicking on “Add selected devices”.

5. Click on the module to open the radio configuration box.

Sending data to Ubidots

a) Setting the radio parameters.

In order to send data to Ubidots you have to setup several parameters related to Cellular, Network, Addressing, Serial interfacing, I/O Settings, Sleep Commands, AT Command Options, MicroPython options and firmware version/Information.

1. Firstly, in the DL row, you’ll need to set the destination address, type “Industrial.api.ubidots.com” and then click on the write button.

2. Then, the 80 port is the default one to make HTTP requests. The DE text field receives hexadecimal format, so type 50 and click on the write button. Hexadecimal 50 is 80 in decimal.

3. Next, to access the MicroPython terminal switch the BD field to 115200 baud rate. Click on the write button.

4. Finally, change the AP field to MicroPython REPL [4] mode. Click on the write button.

NOTE: It is important to wait until the AI parameter shows 0 (Connected to the internet)

b) Coding with MicroPython.

5. Once you have the above parameters, is time to coding with MIcroPython. I|n the upper toolbar under the “Tools” drop-down, select MicroPython Terminal.

6. A new MicroPython terminal window will appear, click on the Open Button, select the  COM that the device uses, verify the Baud rate and other settings are correct, then, click OK.

The Open icon will change to Close, indicating the device is properly connected.

Once the MicroPython terminal is available, you can interact with it using the below REPL commands (Commands using your keyboard).

  • Ctrl-E: Enters the paste mode. Useful for testing purposes.
  • Ctrl-F: Enters the flash compile mode. Useful to upload and save the code to the device's flash memory.
  • Ctrl-D: Saves and exits the current mode. It could be either the paste or flash compile mode.
  • Ctrl-C: Cancels and exits the current mode. It could be either the paste or flash compile mode.
  • Ctrl-R: Executes the code currently saved in the device's flash memory.

TIP: If you want to paste the code in either the paste or flash compile mode, then you should use the right-click and select paste from the options list.

It’s time to type MicroPython code in the terminal. First, we need to import the libraries that are going to be used.

import socket
import time
import xbee

Next, we’re going to set a routine to, firstly, set the cellular network operator’s APN, and secondly, avoid a code error if the module disconnects. This configuration will force the device to reconnect.

IMEI=x.atcmd('IM')

TOKEN="YOUR-UBIDOTS-TOKEN-HERE"

x=xbee.XBee()

def Validation():

    while(x.atcmd('DI') != 0):

        x.atcmd('AN', "YOUR-APN-CELLULAR-NETWORK-OPERATOR")

        print("Awaiting connection...")

        time.sleep(5)

Time to type the function that sends the data to Ubidots.

def send_data(deviceLabel, token, body):

    s = socket.socket()

    s.connect(("industrial.api.ubidots.com", 80))

    request=bytes('POST /api/v1.6/devices/%s HTTP/1.1\r\nHost: industrial.api.ubidots.com\r\nX-Auth-Token: %s\r\nContent-Type: application/json\r\nContent-Length: %s\r\n\r\n%s\r\n' % (deviceLabel, token, len(body), body), 'utf8')

    print("Sending data to Ubidots")

    s.send(request)

    dump_socket(s)

def dump_socket(s):

    try:

        while True:

            data = s.recv(100)

            if data:

                print(str(data, 'utf8'), end='')

            else:

                print('')  # end with newline

                s.close()

                break

    except:

        s.close()

        raise


Now, a digital port will be initialized to obtain its state by a function that reads the D4 pin and calls the function we previously set.

from machine import Pin

sensor = Pin("D4", Pin.IN)

def Update():

    switch = sensor.value()

    body='{"var2": ' + repr(switch)+'}'

    send_data(IMEI, TOKEN, body)

Last but not least, the following is the loop that executes all the code every 10 seconds.

while(True):

    if (x.atcmd('DI') != 0):

        Validation()

        print("Connected")

    Update()

    time.sleep(10)

When the code has been compiled in “flash compile mode”, you'll be able to run the code using Ctrl-R. If the device is not connected to the internet, you'll get an “Awaiting connection” message. Once connected, the device will begin to send data to Ubidots.


The “status_code”: 201 response means that the data was successfully saved in the Ubidots' database. To learn more about Ubidots’ status codes, see our documentation.

Visualizing data on Ubidots.

Go to devices -> device section on your Ubidots account, you will see a new device named as the device's IMEI and a variable inside it.

Results.

We built this integration with starter equipment and simple know-how, you just need a few knowledge of python. This is a simple tutorial with 1 variable, but you can use more sensors and more variables to launch a complete application and create Ubidots Dashboards to visualize and interpret your data.

Now it's time to build your own data visualization. To do so, please explore the following articles:

Did this answer your question?