The GPRSBBee is an alternative for the Arduino GPRS Shield and has the bee form factor that can be used in any system that has a bee socket like the Seeeduino Stalker or the Arduino Fio. The GPRSbee uses SIM cards of the MicroSIM form factor to connect to and communicate with the internet. 

It has the XBee form factor, so any electronic project with an XBee socket should be compatible with the GPRSBee. 

In this tutorial we’ll explore and detail how to send a value to Ubidots with the GPRSBee device.

Following this guide you will be able to POST data to Ubidots using the GPRSBee device connected to an Seeeduino Stalker kit v2.3 in just a couple of minutes!

Requirements

  • GPRSBee
  • A micro-controller board to manage and monitoring the GPRSBee through an UART interface. We’ll use a Seeeduino Stalker kit v2.3 in this tutorial.
  • Ubidots Account

Step-by-Step

  1. Hardware Setup
  2. Sending (POST) Data to Ubidots
  3. Summary 

1. Hardware Setup 

1. To begin, place the GPRSBee atop the Seeeduino Stalker kit v2.3 and apply gentle pressure to connect the GPRSBee pins with the board's headers. Now with the GPRSBee + Seeeduino Stalker assembled you are able to connect to the internet via cellular. 

2. Now, to be able to program the Seeeduino Stalker kit v2.3 you will need an UART interface, such as an UartSBee module (image below):

 3. Follow the diagram and table below to establish the connection between the Seeeduino Stalker kit v2.3 and the UartSBee properly:

After establish the connection, you will be able to program the board through the UartSBee interface.

2. Sending (POST) data to Ubidots

By following the code below you will be able to POST values to Ubidots over Cellular. 

1. To post your first value in Ubidots, open the Arduino IDE and paste the sample code below. Once you have pasted the code, you will need to assign the following parameters:

  • Your unique Ubidots TOKEN 
  • The Variable ID of the variables you will be updating in Ubidots. If you do not have the variable IDs, simply create a shell variables and then update your firmware to contain the correct Variable IDs. To locate the variable IDs, simply click here. 
  • APN credentials of your cellular provider inside the method configGPRS() of the sample code provided
      /*

      GPRSBee Seeeduino

      Circuit:

      Pin GND:   GND of UARTSBee
      Pin 2:     RX of UARTSBee
      Pin 3:     TX of UARTSBee
      Pin USB5v: VCC of UARTSBee

      GPRSBee into the Bee Socket of the Seeeduino
      LiPo Battery connected to the GPRSBee

      NOTE: Make sure to turn on the GPRSBee using its power button

      Created 13 Feb 2015
      by Alejandro Gomez for Ubidots

      This example code is in the public domain.

    */  


    #include <SoftwareSerial.h>

    //SoftwareSerial for Serial Monitor output
    #define PIN_RX 2
    #define PIN_TX 3
    SoftwareSerial monitorSerial = SoftwareSerial(PIN_TX, PIN_RX);

    String inputString = "";
    char input[512];
    boolean stringComplete = false;

    //Ubidots Information
    char ubidotstoken[] = "xxxxxxxxxxxxxxxxxxxxxx";      //your token here
    char idvariable1[] = "xxxxxxxxxxxxxxxxxxxxxx";             //ID of your variable

    void setup(){
      //Configure the baud rate for each Serial port
      monitorSerial.begin(9600);
      Serial.begin(4800);
      delay(2000);
    }

    void loop(){
      configGPRS();
      send2Ubidots(888, idvariable1);
      delay(4000);
      checkResponse(10000);
      delay(60000);
    }

    void configGPRS(){
      //the AT Command needed to configure the GPRS to send data
      while(!SendATCommand("AT", "OK"));
      while(!SendATCommand("AT+CGATT=1", "OK"));
      SendATCommand("AT+CSTT=\"web.vmc.net.co\"", "OK");
      SendATCommand("AT+CIICR", "OK");
      SendATCommand("AT+CIFSR");
      while(!SendATCommand("AT+CIPSTART=\"tcp\",\"industrial.api.ubidots.com\",\"80\"", "CONNECT OK"));
    }

    void send2Ubidots(int value, char* id){
      //Send one value to ubidots
      SendATCommand("AT+CIPSEND");

      char url1[] = "POST /api/v1.6/variables/";
      char* url2 = id;
      char url3[] = "/values HTTP/1.1\n";
      int lurl = strlen(url1) + strlen(url2) + strlen(url3);
      char url[lurl];
      sprintf(url,"%s%s%s",url1,url2,url3);

      Serial.print(url);
      delay(100);
      checkResponse(100);

      Serial.print("X-Auth-Token: ");
      delay(100);
      checkResponse(100);

      Serial.print(ubidotstoken);
      delay(100);
      checkResponse(100);

      Serial.print("\n");
      delay(100);
      checkResponse(100);

      Serial.print("Content-Type: application/json\n");
      delay(100);
      checkResponse(100);

      Serial.print("Host: industrial.api.ubidots.com\n");
      delay(100);
      checkResponse(100);

      Serial.print("Content-Length: ");
      delay(100);
      checkResponse(100);

      char data1[] = "{\"value\":";
      char data2[get_int_len(value)+1];
      char data3[] = "}";
      itoa(value,data2,10);
      int ldata = strlen(data1) + strlen(data2) + strlen(data3);
      char data[ldata];
      sprintf(data,"%s%s%s",data1,data2,data3);

      Serial.print(ldata);
      delay(100);
      checkResponse(100);

      Serial.print("\n");
      delay(100);
      checkResponse(100);

      Serial.print("\n");
      delay(100);
      checkResponse(100);

      Serial.print(data);
      delay(100);
      checkResponse(100);

      Serial.print("\n");
      delay(100);
      checkResponse(100);

      Serial.print((char)26);
      delay(100);
      checkResponse(100);

      Serial.print("\n");
      delay(100);
      checkResponse(100);

      checkResponse(10000);
    }

    //This method sends an ATCommand to the GPRSbee
    void SendATCommand(char* data){
      flushSerial();
      monitorSerial.print("ATCommand: ");
      monitorSerial.println(data);
      Serial.println(data);
      delay(2000);
      checkResponse();
      delay(1000);
    }

    //This method sends an ATCommand and checks for a response
    boolean SendATCommand(char* data, char* response){
      flushSerial();
      monitorSerial.print("ATCommand: ");
      monitorSerial.println(data);
      Serial.println(data);
      delay(100);
      return checkResponse(response);
    }

    //Check if there's a response in the internal buffer
    void checkResponse(){
      long contador = 1;
      while(contador < 10000){
        while(Serial.available() > 0){
          char inChar = (char)Serial.read();
          inputString += inChar;
          if (inChar == '\n') {
            stringComplete = true;
          }
        }
        if(stringComplete) {
          monitorSerial.println(inputString);
          inputString = "";
          stringComplete = false;
        }
        contador++;
      }
    }

    //Check if there's a specific response in the internal buffer
    boolean checkResponse(char* data){
      long contador = 1;
      int cont = 0;
      while(contador < 100000){
        while(Serial.available() > 0){
          char inChar = (char)Serial.read();
          if ((inChar == '\n')) {
            stringComplete = true;
            break;
          }else{
          input[cont] = inChar;
          cont++;
          }
        }
        if(stringComplete) {
          //monitorSerial.print(input);
          //monitorSerial.print(" Compared with ");
          //monitorSerial.println(data);
          //int a = strcmp(input, data);
          if(strncmp(input, data, strlen(data)) == 0){
            resetInput();
            cont = 0;
            stringComplete = false;
            return true;
          }

          resetInput();
          cont = 0;
          stringComplete = false;
        }
        contador++;
      }
      return false;
    }

    //Check if there's a response in the internal buffer with a specific timeout
    void checkResponse(long timeout){
      long contador = 1;
      while(contador < timeout){
        while(Serial.available() > 0){
          char inChar = (char)Serial.read();
          inputString += inChar;
          if (inChar == '\n') {
            stringComplete = true;
          }
        }
        if(stringComplete) {
          monitorSerial.println(inputString);
          inputString = "";
          stringComplete = false;
        }
        contador++;
      }
    }

    //flush the serial input
    void flushSerial() {
        while (Serial.available())
        Serial.read();
    }

    //reset the temporal buffer to read data
    void resetInput(){
      for(int i=0; i<255; i++){
        input[i]=0;
      }
    }

    //used to count the number of chars in a value
    long get_int_len (long value){
      int l=1;
      while(value>9){ l++; value/=10; }
      return l;
    }

2. Next, Verify your code within the Arduino IDE. To do this, in the top left corner of our Arduino IDE you will see the "Check Mark" icon; press it to verify your code. 

3. Upload the code into your Seeeduino Stalker + GPRSBee. To do this, choose the "right-arrow" icon beside the "check mark" icon. 

4. To verify the connectivity of the device and the data sent, open the serial monitor by selecting the "magnifying glass" icon in the top right corner of the Arduino IDE to see the connectivity logs. 

NOTE: If no response is seen in the serial monitor, try unplugging the board and then plugging it again. Also, make sure the baud rate of the Serial monitor is set to the same one specified in your code 115200 

At this point, the variable assigned in the code will be updating with the readings taken from the board.

3 . Summary

With this simple tutorial you are able to POST data to Ubidots with the ease of the Arduino IDE and an Seeeduino Stalker + GPRSBee.

Now its time to create Ubidots Dashboards to visualize your data and deploy your IoT solution!  :) 

Other readers have also found useful...

Did this answer your question?