# Part 3: Introduction to Narrow Band LTE ###### tags: `TA Stuff ESP32` `PyCom` `ESP32` This tutorial intends to demonstrate the basic usage of Narrow-Band LTE. We need either a [**FiPy**](https://pycom.io/product/fipy/) or [**GPy**](https://pycom.io/product/gpy/) from [**PyCom**](https://pycom.io/) and a narrow band sim card from a local provider like; [**1nce**](https://1nce.com/en/) that works worldwide. The content of this document was extracted from [PyCom documentation](https://docs.pycom.io/) and you always can check for the latest changes. ## What is Narrow Band LTE? NB LTE is a Low-Power Wide-Area Network (LPWAN) developed primarily for the Internet of Things (IoT) application area. It operates on the cellular network and communicates with low bandwidth and power consideration over long distances. Sensor devices that need to send data infrequently can use NB LTE's **Sleep Mode** capability to reduce power consumption. This will let the sensors operate for weeks and months without recharging the batteries. There exist two standards for NB LTE; **Cat-M1** and **NB-IoT**. According to [Ericsson](https://www.ericsson.com/en/blog/2019/2/difference-between-nb-iot-cat-m1), Cat-M1 operates at 1.4 MHz bandwidth with lower latency and more accurate device positioning which makes it a better choice for voice connectivity and movable object (M stands for "mobility"). On the other hand, NB-IoT operates at 200KHz with higher latency and lower data rate compared to Cat-M1 but it operates more efficiently under challenging radio conditions. NB-IoT could be a better choice for utility meters and sensors while Cat-M1 is suitable for connected vehicles, wearable devices, trackers, and alarm panels. At the time of writing this tutorial, NB LTE is not operational in all countries, and some are still under development. Most countries support both Cat-M1 and NB-IoT while few only support one of them. You can find the network coverage in your neighborhood from [**GSMA**](https://www.gsma.com/iot/deployment-map/) homepage. ## Narrow Band Simcard Providers There are different narrow-band providers today and more will be joining soon. [**1nce**](https://1nce.com/en/) provides low-cost sim cards worldwide and its network coverage could be checked [here](https://1nce.com/en/coverage/). [**Hologram**](https://www.hologram.io/) is another international player and its network coverage can be checked [here](https://www.hologram.io/pricing/coverage). [**IoT Creators**](https://iotcreators.com/) is a Europe focused provider that has coverage on [these](https://iotcreators.com/coverage-eu/) countries. ## Firmware Preparation on FiPy and GPy :::info There are two different firmware on FiPy and GPy one is the board firmware which contains MicroPython libraries, OS, WiFi, BLE, LoRa, and Sigfox implementation requirements. The second firmware belongs to the LTE modem which needs to be updated before we can use them. ::: :::danger **You should always connect the LTE antenna before accessing and using the modem, the absence of it could damage your LTE modem.** ::: :::warning We follow PyCom documentation to update the firmware and you always need to check the links for the latest updates and instructions. ::: Before we proceed with the LTE modem firmware update we need to update the FiPy/GPy board firmware to the latest version and instructions can be found [**HERE**](https://docs.pycom.io/updatefirmware/device/). Then, we follow instructions in [**This Link**](https://docs.pycom.io/updatefirmware/ltemodem/) to update LTE modem firmware successfully. There are several ways suggested by PyCom and we follow firmware transfer using **SD Card** from the documentation. ### Preparing SD Card with Firmware Files Before updating the modem firmware, check the current modem firmware version using: ```python= import sqnsupgrade sqnsupgrade.info() ``` The bottom two lines explain the LTE firmware edition: ``` LR5.xx is for CAT-M1 LR6.xx is for NB-IoT ``` Then we follow these instructions to copy desired firmware files to our SD Card: - **Step 1**: Register in the PyCom forum [**here**](https://forum.pycom.io/). - **Step 2**: Go to [**this post**](https://forum.pycom.io/topic/4020/firmware-files-for-sequans-lte-modem-now-are-secured) to get credentials for downloading firmware files. - **Step 3**: Go to [**this link**](https://software.pycom.io/downloads/sequans2.html) and with the credentials you got from the previous steps you can download **Cat-M1** or **NB-IoT** firmware files. - **Step 4**: Insert SD Card into your PyCom expansion board and run the following commands to format the card. ```python= from machine import SD import os sd = SD() os.mount(sd, '/sd') # mount it os.fsformat('/sd') # format SD card fs = os.mkfat(sd) print(os.listdir('/sd')) # list its content which is empty in this stage ``` - **Step 5**: Disconnect the expansion board remove the SD card and connect it to your computer to copy downloaded firmware files to your SD card. :::info At the time of this tutorial, we can copy ```CATM1-5.2-48829-1.dup``` and ```CATM1-5.2-48829-2.dup``` for **Cat-M1 Firmware** OR ```NB1-41019.dup``` for **NB-IoT Firmware**. Be sure that the files are copied to the root folder of your SD card and do not make any folder (for ease of usage later). ::: - **Step 6**: Connect the SD card back to your PyCom expansion board and run the following command to mount the SD and prepare it for usage in the next section: ```python= from machine import SD import os sd = SD() os.mount(sd, '/sd') print(os.listdir('/sd')) # This will list all file available in the SD card ``` **Now** everything is ready to go to the next two sections to either update board with Cat-M1 or NB-IoT firmware. :::danger You always need to consider these notes because a mistake could damage your LTE modem firmware and make it inaccessible. - For inserting in and removing both SD card and Sim Card you always need to disconnect power and remove the USB connection to your computer. - It is suggested to put your board in a stable state (**Not Sleep mode or crashed in the middle of modem connection**) before changing or updating the LTE modem firmware. - For safety, it is better to put your board in Safe Boot Mode shown in [**Here**](https://docs.pycom.io/gettingstarted/programming/safeboot/) before the firmware update. - **Never** disconnect or move the board in the middle of the LTE firmware update, wait until it shows you the command REPL. - You can either use **Cat-M1** or **NB-IoT** at a time on your LTE modem so depending on your application and the network coverage you should choose which one to update your board with. ::: ### Cat-M1 Firmware on FiPy and GPy Follow these steps: **Step 1**: Run the following line: ```python=- import sqnsupgrade sqnsupgrade.run('/sd/CATM1-5.2-48829-1.dup', load_fff=False) ``` wait until the LTE modem resets and prints out something similar to this: ![](https://i.imgur.com/QpY0zHO.jpg) **Step 2**: Run this line: ```python=- sqnsupgrade.run('/sd/CATM1-5.2-48829-2.dup') ``` Wait until the LTE modem resets and shows command REPL. ![](https://i.imgur.com/N05IGZD.jpg) :::success Now your board is updated with the new Cat-M1 firmware and ready to use. You always can check the version with the following commands: ```python= import sqnsupgrade sqnsupgrade.info() ``` ::: ### NB-IoT Firmware on FiPy and GPy Run the following commands on REPL: ```python=- import sqnsupgrade sqnsupgrade.run('/sd/NB1-41019.dup') ``` Wait until the LTE modem resets and shows command REPL. ![](https://i.imgur.com/AQWb2Y0.jpg) :::success Now your board is updated with the new NB-IoT firmware and ready to use. You always can check the version with the following commands: ```python= import sqnsupgrade sqnsupgrade.info() ``` ::: ## Code Example of the NB LTE Operation The following MicroPython code is based on PyCom documentation and adapted from [**Here**](https://docs.pycom.io/tutorials/networks/lte/power/). You can copy it to `main.py` and it works on both FiPy and GPy, the code is compatible with both **Cat-M1** and **NB-IoT** firmware. A narrow band LTE sim card from [**1nce**](https://1nce.com/en/) was used for internet connection, therefore we used **1nce** APN and Europe frequency. The following code tries to connect the LTE modem to the internet and then sends an HTML Get request to [Firefox](http://detectportal.firefox.com/) and prints the response on the terminal: ```python= from network import LTE # LTE Library import time # Time Library import socket # Used by HTML get request import machine # Used for deepsleep # Attach to the 1nce network def attach(): start = time.time() if lte.isattached(): print("Already Attached!") else: print("Attaching...") try: lte.attach(band=20, apn="iot.1nce.net") except: print("Attaching Failed!") machine.deepsleep(60000) while not lte.isattached(): time.sleep(1) print("attached after", time.time() - start, "seconds") # Connecting to the 1nce network def connect(): print("Connecting...") start = time.time() try: lte.connect() except: print("Connection Failed!") machine.deepsleep(60000) while not lte.isconnected(): time.sleep(0.5) print("connected after", time.time() - start, "seconds") # Sending HTML GET request and print the response def http_get(url = 'http://detectportal.firefox.com/'): _, _, host, path = url.split('/', 3) addr = socket.getaddrinfo(host, 80)[0][-1] s = socket.socket() s.connect(addr) s.send(bytes('GET /%s HTTP/1.0\r\nHost: %s\r\n\r\n' % (path, host), 'utf8')) time.sleep(1) rec_bytes = s.recv(10000) print(rec_bytes) s.close() # main print("Modem Initializing...") try: lte = LTE() except: print("Modem Initialization Failed!") machine.deepsleep(60000) attach() connect() print("Getting URL...") try: http_get() except: print("Could Not Get URL! ( Failed! )") machine.deepsleep(60000) print("Modem Deinitializing...") try: lte.deinit() except: print("Modem Deinitialization Failed!") print("Going deepsleep...") # Board sleeps for 60 minutes machine.deepsleep(60 * 60 * 1000) ``` ## Conclusion There are several communication standards adopted for Low-Power Wide-Area Network (LPWAN) in the IoT world. **NB LTE** beside [**LoRa**](https://lora-alliance.org/) and [**Sigfox**](https://www.sigfox.com/en) make the sensor nodes connected and each has its strength and limits. **NB LTE** is useful in the areas where there are already cellular connections by adding narrow-band capabilities. It also seems interesting in smart connected vehicle worlds where the sensors need to take us home safely. *[APN]: Access Point Name *[HTML]: Hyper Text Markup Language *[REPL]: Read-Evaluate-Print Loop <style> .markdown-body code{ font-size: 1em !important; } .markdown-body .a{ font-size: 5em !important; } .markdown-body pre { background-color: #333; border: 1px solid #333 !important; color: #dfdfdf; font-weight: 600; } .token.operator, .token.entity, .token.url, .language-css .token.string, .style .token.string { background: #000; } .markdown-body table { display: table; padding: 1em; width: 100%; } .markdown-body table th, .markdown-body table td, .markdown-body table tr { border: none !important; } .markdown-body table tr { background-color: transparent !important; border-bottom: 1px solid rgba(0, 0, 0, 0.2) !important; } </style>