Search code examples
arduinoiotesp8266

Thinger.IO client setup for GPRS enabled ESP32 project


I've been using the Thinger.io platform for some of my IoT projects (mostly ESP8266 modules) for quite a long time now. The way I implemented it is something similar to that:

#include <ThingerESP8266.h>
#include <ESP8266WIFI.h>

#define USERNAME "username"
#define DEVICE_ID "deviceid"
#define DEVICE_CREDENTIAL "devicecredential"

ThingerESP8266 thing(USERNAME, DEVICE_ID, DEVICE_CREDENTIAL);

void connectToWifi() {
  ...
}

void setup() {
  connectToWifi();
}

void loop() {
  thing.handle();
}

and it just works. It is good to also mention that I've been using WiFi all the way.

Now I am trying to achieve the same by taking advantage of a controller called TTGO T-Call ESP32. It is GPRS enabled (using the TinyGsmClient.h) and I have inserted a SIM card inside of it which successfully connects to the internet. The issue is that I can not really establish a connection to the Thinger.io platform where my devices are hosted. This is what my code looks like (making a reference to this library example)

    // Your GPRS credentials (leave empty, if not needed)
const char apn[]      = ""; // APN (example: internet.vodafone.pt) use https://wiki.apnchanger.org
const char gprsUser[] = ""; // GPRS User
const char gprsPass[] = ""; // GPRS Password

// SIM card PIN (leave empty, if not defined)
const char simPIN[]   = ""; 

// TTGO T-Call pins
#define MODEM_RST            5
#define MODEM_PWKEY          4
#define MODEM_POWER_ON       23
#define MODEM_TX             27
#define MODEM_RX             26
#define I2C_SDA              21
#define I2C_SCL              22

// Set serial for debug console (to Serial Monitor, default speed 115200)
#define SerialMon Serial
// Set serial for AT commands (to SIM800 module)
#define SerialAT Serial1

// Configure TinyGSM library
#define TINY_GSM_MODEM_SIM800      // Modem is SIM800
#define TINY_GSM_RX_BUFFER   1024  // Set RX buffer to 1Kb

// Define the serial console for debug prints, if needed
//#define DUMP_AT_COMMANDS

#include <Wire.h>
#include <TinyGsmClient.h>

#ifdef DUMP_AT_COMMANDS
  #include <StreamDebugger.h>
  StreamDebugger debugger(SerialAT, SerialMon);
  TinyGsm modem(debugger);
#else
  TinyGsm modem(SerialAT);
#endif

// I2C for SIM800 (to keep it running when powered from battery)
TwoWire I2CPower = TwoWire(0);

// TinyGSM Client for Internet connection
TinyGsmClient client(modem);

#define uS_TO_S_FACTOR 1000000UL   /* Conversion factor for micro seconds to seconds */
#define TIME_TO_SLEEP  3600        /* Time ESP32 will go to sleep (in seconds) 3600 seconds = 1 hour */

#define IP5306_ADDR          0x75
#define IP5306_REG_SYS_CTL0  0x00

bool setPowerBoostKeepOn(int en){
  I2CPower.beginTransmission(IP5306_ADDR);
  I2CPower.write(IP5306_REG_SYS_CTL0);
  if (en) {
    I2CPower.write(0x37); // Set bit1: 1 enable 0 disable boost keep on
  } else {
    I2CPower.write(0x35); // 0x37 is default reg value
  }
  return I2CPower.endTransmission() == 0;
}

void connectToApn(){
  SerialMon.println("Connecting to: internet.vivacom.bg ... ");

  while(!modem.gprsConnect(apn, gprsUser, gprsPass))
    delay(500);
  
  SerialMon.println("Successfully connected to: internet.vivacom.bg");
}

// #include <ThingerCore32.h> => ArduinoJson.h: No such file or directory
// #include <ThingerESP8266.h> => ESP8266WiFi.h : No such file or directory
#define USERNAME ""
#define DEVICE_ID ""
#define DEVICE_CREDENTIAL ""

#include <ThingerESP32.h>
ThingerESP32 thing(USERNAME, DEVICE_ID, DEVICE_CREDENTIAL);
//#include "arduino_secrets.h"



// Server details
const char server[]   = "vsh.pp.ua";
const char resource[] = "/TinyGSM/logo.txt";
const int  port       = 80;

#include <ArduinoHttpClient.h>
HttpClient http(client, server, port);



void setup() {
  // Set serial monitor debugging window baud rate to 115200
  SerialMon.begin(115200);

  // Start I2C communication
  I2CPower.begin(I2C_SDA, I2C_SCL, 400000);

  // Keep power when running from battery
  bool isOk = setPowerBoostKeepOn(1);
  SerialMon.println(String("IP5306 KeepOn ") + (isOk ? "OK" : "FAIL"));

  // Set modem reset, enable, power pins
  pinMode(MODEM_PWKEY, OUTPUT);
  pinMode(MODEM_RST, OUTPUT);
  pinMode(MODEM_POWER_ON, OUTPUT);
  digitalWrite(MODEM_PWKEY, LOW);
  digitalWrite(MODEM_RST, HIGH);
  digitalWrite(MODEM_POWER_ON, HIGH);

  // Set GSM module baud rate and UART pins
  SerialAT.begin(115200, SERIAL_8N1, MODEM_RX, MODEM_TX);
  delay(3000);

  // Restart SIM800 module, it takes quite some time
  // To skip it, call init() instead of restart()
  SerialMon.println("Initializing modem...");
  modem.restart();

  // Unlock your SIM card with a PIN if needed
  if (strlen(simPIN) && modem.getSimStatus() != 3 ) {
    modem.simUnlock(simPIN);
  }

  // Configure the wake up source as timer wake up  
  esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);

  // Connect to APN
  connectToApn();
}

void loop() { 
  thing.handle();
  
  SerialMon.println("In the loop ...");
  delay(3000);

  SerialMon.print(F("Performing HTTP GET request... "));
  int err = http.get(resource);
  if (err != 0) {
    SerialMon.println(F("failed to connect"));
    delay(10000);
    return;
  }

  int status = http.responseStatusCode();
  SerialMon.print(F("Response status code: "));
  SerialMon.println(status);
  if (!status) {
    delay(10000);
    return;
  }

  SerialMon.println(F("Response Headers:"));
  while (http.headerAvailable()) {
    String headerName  = http.readHeaderName();
    String headerValue = http.readHeaderValue();
    SerialMon.println("    " + headerName + " : " + headerValue);
  }

  int length = http.contentLength();
  if (length >= 0) {
    SerialMon.print(F("Content length is: "));
    SerialMon.println(length);
  }
  if (http.isResponseChunked()) {
    SerialMon.println(F("The response is chunked"));
  }

  String body = http.responseBody();
  SerialMon.println(F("Response:"));
  SerialMon.println(body);

  SerialMon.print(F("Body length is: "));
  SerialMon.println(body.length());
  // Put ESP32 into deep sleep mode (with timer wake up)
  // esp_deep_sleep_start();
}

NOTE: The board I've picked from the Arduino IDE is called ESP32 Wrover Module


Solution

  • This is the code which worked for me:

    const char apn[]      = ""; // APN (example: internet.vodafone.pt) use https://wiki.apnchanger.org
    const char gprsUser[] = ""; // GPRS User
    const char gprsPass[] = ""; // GPRS Password
    
    // SIM card PIN (leave empty, if not defined)
    const char simPIN[]   = ""; 
    
    // TTGO T-Call pins
    #define MODEM_RST            5
    #define MODEM_PWKEY          4
    #define MODEM_POWER_ON       23
    #define MODEM_TX             27
    #define MODEM_RX             26
    #define I2C_SDA              21
    #define I2C_SCL              22
    
    // Set serial for debug console (to Serial Monitor, default speed 115200)
    #define SerialMon Serial
    // Set serial for AT commands (to SIM800 module)
    #define SerialAT Serial1
    
    // Configure TinyGSM library
    #define TINY_GSM_MODEM_SIM800      // Modem is SIM800
    #define TINY_GSM_RX_BUFFER   1024  // Set RX buffer to 1Kb
    
    #include <Wire.h>
    #include <TinyGsmClient.h>
    
    TinyGsm modem(SerialAT);
    
    // TinyGSM Client for Internet connection
    TinyGsmClient client(modem);
    
    void connectToApn(){
      SerialMon.println("Connecting to: internet.vivacom.bg ... ");
      while(!modem.gprsConnect(apn, gprsUser, gprsPass))
        delay(500);
      
      SerialMon.println("Successfully connected to: internet.vivacom.bg");
    }
    
    #define USERNAME ""
    #define DEVICE_ID ""
    #define DEVICE_CREDENTIAL ""
    
    #include <ThingerTinyGSM.h>
    
    ThingerTinyGSM thing(USERNAME, DEVICE_ID, DEVICE_CREDENTIAL, Serial1);
    
    void setup() {
      // Set serial monitor debugging window baud rate to 115200
      SerialMon.begin(115200);
    
      // Set modem reset, enable, power pins
      pinMode(MODEM_PWKEY, OUTPUT);
      pinMode(MODEM_RST, OUTPUT);
      pinMode(MODEM_POWER_ON, OUTPUT);
      digitalWrite(MODEM_PWKEY, LOW);
      digitalWrite(MODEM_RST, HIGH);
      digitalWrite(MODEM_POWER_ON, HIGH);
    
      // Set GSM module baud rate and UART pins
      SerialAT.begin(115200, SERIAL_8N1, MODEM_RX, MODEM_TX);
      delay(3000);
    
      // Restart SIM800 module, it takes quite some time
      // To skip it, call init() instead of restart()
      SerialMon.println("Initializing modem...");
      modem.restart();
    
      // Unlock your SIM card with a PIN if needed
      if (strlen(simPIN) && modem.getSimStatus() != 3 ) {
        modem.simUnlock(simPIN);
      }
    
      // Connect to APN
      connectToApn();
    }
    
    void loop() { 
      thing.handle();
    }