This entry will show you a step-by-step guide to follow in order to POST a message using REST API to Sofia2 Smart Platform, by using the versatile Xadow BLE+GSM module from Seeed. This module is the central unit of the Rephone Kit, a modular approach designed to build innovative IoT scenarios.
Xadow BLE+GSM is a tiny development board and a great fit for mobility applications. Based on one of the smallest chips in the market, Xadow BLE+GSM provides developers with an enriching blend of communication technologies, thanks to Bluetooth Smart (BLE) capabilities paired with a 2G GSM/GPRS modem (850/900/1800/1900MHz).
The board is based on the MT2502 SoC from Mediatek, featuring:
- Micro-controller: 32-bit ARM7EJ-STM RISC processor
- RAM Memory: 4 MB
- FLASH Memory: 16 MB
- Power supply: 3.3~4.2V (no SIM) / 3.5~4.2V (with SIM)
- Power consumption: 20mW (@standby, no radio), 30 mW (@standby GSM), 45 mW (@standby BLE)
- 4-band modem: (850/900/1800/1900 MHz)
- GPRS modem class 12
- Clock: 260 MHz
- Connector: 35-pin connector and PIN Connector to interconnect Rephone extra modules (GPS board, GPIO expansion board, LCD,…) JST1.0 Battery connector.
- Interfaces: LCD, Audio, I2C,SPI, UART ad GPIOs
Let’s start programming! First step is to set up the development environment. Seeed helps us with this purpose giving us some manuals dealing with proper drivers and software installation. Since we are using Eclipse, this is the link with the associated manual http://wiki.seeedstudio.com/wiki/Eclipse_IDE_for_RePhone_Kit .
Next step is to import one of the featured examples included in the LinkIt Assist 2502 SDK 2.0. You will find these examples under File->New->Project->LinkIt Assist 2502 SDK 2.0 Example-> Example Application.
On the next screen you will be prompted to give the Project a name and to select the development platform you are using (LinkIt Assist 2502). After filling these fields and accepting, then you may select the example named Network_HTTPS. Now you may see your Project available on the selected workspace.
Before writing any code it is worth noting how the system behaves. Our applications will run over an event-driven system based on lower-level API calls triggering callbacks to the user threads. The application entry point will always be vm_main method. For this example, this method is defined on HTTPS.c. As shown in the picture, the example application is registered to system event’s callbacks.
This example uses the VM_EVENT_CREATE system event to launch the main logic. This event is triggered when the system is up & running after a power-up sequence.
vm_timer_create_non_precise sets up a timer that will run the desired method upon expiration. The example sets a timer up to 1 minute to call http_send_request. This method pairs system callbacks with the custom method to be called depending on the actual callback triggered. In addition, set_custom_apn is called to configure the modem according to the SIM card data. These are the fields that require configuration:
The APN from your network provider should be stored in CUST_APN, and for the other parameters, they are only used in case the network provider uses proxies to establish connections. It is important to notice it is required to eliminate PIN-code verification to access the SIM card in order to make it work with Rephone. To achieve this you can use an Android phone.
Network connection begins with the call to vm_https_channel. If everything is alright, the system should call https_send_request_set_channel_rsp_cb as a callback. The latter is the method we are going to modify to be able to send a POST to Sofia2. Changes should be applied to the parameters used on the call to vm_https_send_request :
- VM_HTTPS_METHOD_POST instead of VM_HTTPS_METHOD_GET
- Configuration of VMHTTPS_TEST_URL and HTTP_HEADER with the proper values from Sofia2
- Insert HTTP_HEADER and insert_sofia as message payload, with their length.
After applying these changes, the code should look like this:
The first message must be a JOIN message. Here is an example of a canonical JOIN message:
After editing the JOIN message with the appropriate parameters, the join_sofia should be inserted as a parameter (in place of insert_sofia).
Once vm_https_send_request is called, the system will trigger a callback on https_send_read_request_rsp_cb method. If ‘result’ value is equal to zero, then the sending process has been successfully completed. In order to add some debugging, the following lines print server’s traces to the monitor.
At this point we are done with the code! Next step is to flash it to the device. To achieve this, just click on the Build Application button.
If the application is successfully loaded into the device, you may open the monitor view to actually check if our code is working as expected.
Inside the monitor view, you will need to select the debugging port, tagged as Debug, and then connect to it to fetch the log from Xadow BLE+GSM. After one minute (as we explained before, this was the expiration time set up at the beginning), you will be able to see https_send_request call and then you may see the debugging lines with successful content:
Keep us posted in case you make it through this tutorial! Specially if after the JOIN you managed to INSERT a frame into Sofia2.