-1

Good day all I'm working on a project that takes sensor data and sends it to an online database using HTTP requests.Im ussing the TTGO esp32 sim 800 board, Everything works fine and all the data is stored in my online database the problem is that I cant get the AT commands to work, I need the Signal strength, battery voltage, and amount of Data available on the sim card.

Can someone please assist with this matter?

kind regards Hansie

Code

#include <HCSR04.h>
#define SOUND_SPEED 0.034
#define CM_TO_INCH 0.393701

// 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>


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

#define IP5306_ADDR          0x75
#define IP5306_REG_SYS_CTL0  0x00


// 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


int deviceID=101;


const int trigPin = 5;
const int echoPin = 18;

//Calculation variable for depth
//define sound speed in cm/uS
long duration;
float distanceCm;
double damDepth =200;

// Your GPRS credentials 
const char apn[]      = "Vodacom APN"; // APN 
const char gprsUser[] = ""; // GPRS User
const char gprsPass[] = ""; // GPRS Password

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

// Server details
const char server[] = "grow-with-pierre.com"; // domain name:
const char resource[] = "/post-data.php";         // resource path, for example: /post-data.php
const int  port = 80;                             // server port number

// Keep this API Key value to be compatible with the PHP code
String apiKeyValue = "tPmAT5Ab3j7F9";

#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);

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 setup() 
{
  Serial.begin(115200);     // Starts the serial communication
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  pinMode(echoPin, INPUT);  // Sets the echoPin as an Input
  // 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();
  // use modem.init() if you don't need the complete 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);
}

void loop() 
{
        postData();
   
        Serial.print(String("  Percentage: ") + depth());
  
       

          // Put ESP32 into deep sleep mode (with timer wake up)
        esp_deep_sleep_start();
  


}

String postData()
{
   SerialMon.print("Connecting to APN: ");
  SerialMon.print(apn);
  if (!modem.gprsConnect(apn, gprsUser, gprsPass)) 
  {
    SerialMon.println(" fail");
  }
  else
  {
    SerialMon.println(" OK");
    
    SerialMon.print("Connecting to ");
    SerialMon.print(server);
    if (!client.connect(server, port)) 
    {
      SerialMon.println(" fail");
    }
    else 
    {
      SerialMon.println(" OK");
    
      // Making an HTTP POST request
      SerialMon.println("Performing HTTP POST request...");
      String httpRequestData = "api_key=" + apiKeyValue + "&value1=" + String(deviceID)
                             + "&value2=" + String(distanceCm) + "&value3=" + String(80) + "&value4=" + String(40) + ""+ "&value5=" + String(50) + "";
 
      // then, use the httpRequestData variable below (for testing purposes without the BME280 sensor)
     // String httpRequestData = "api_key=tPmAT5Ab3j7F9&value1=24.75&value2=49.54&value3=1005.14";
    
      client.print(String("POST ") + resource + " HTTP/1.1\r\n");
      client.print(String("Host: ") + server + "\r\n");
      client.println("Connection: close");
      client.println("Content-Type: application/x-www-form-urlencoded");
      client.print("Content-Length: ");
      client.println(httpRequestData.length());
      client.println();
      client.println(httpRequestData);

      unsigned long timeout = millis();
      while (client.connected() && millis() - timeout < 10000L)
      {
        // Print available data (HTTP response from server)
        while (client.available())
        {
          char c = client.read();
          SerialMon.print(c);
          timeout = millis();
        }
      }
      SerialMon.println();
    
      // Close client and disconnect
      client.stop();
      SerialMon.println(F("Server disconnected"));
      modem.gprsDisconnect();
      SerialMon.println(F("GPRS disconnected"));
       
    }
  }
}

float depth()  //Find Depth in cm
{
  double persentage;
   // Clears the trigPin
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  // Sets the trigPin on HIGH state for 10 micro seconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  
  // Reads the echoPin, returns the sound wave travel time in microseconds
  duration = pulseIn(echoPin, HIGH);

  // Calculate the distance
  distanceCm = duration * SOUND_SPEED/2;
  
  // Prints the distance in the Serial Monitor
  Serial.print("Distance (cm): ");
  Serial.println(distanceCm);


  persentage = (distanceCm/damDepth)*100;

  if(persentage>=100)
  {
     persentage=100;
  }
  else
  {
     persentage=100- persentage;
  }

  return persentage;
}
Hansie
  • 7
  • 6
  • 1
    What do you mean *"I cant get the AT commands to work"*? I don't see any attempt at that. I don't think you should be sending your own AT commands, if you are using a library for communication. – gre_gor Jan 31 '22 at 18:19
  • Thank you for the response. Sorry, I see I removed the attempts with the AT commands as I am too unsure if they are even close to correct. What would you suggest I try to get the Signal Quality, Battery Voltage, and Data amount? I am unable to find anything showing how its done – Hansie Jan 31 '22 at 19:11
  • [There's signal an battery.](https://github.com/vshymanskyy/TinyGSM/blob/master/examples/AllFunctions/AllFunctions.ino) I am not sure if data is possible. – gre_gor Jan 31 '22 at 19:14
  • Thank you I see what you say. I am not too sure how to use them when I run this part in my main code I don't get any feedback . Sorry for my stupidity its the first time working with the Sim800. int signal = modem.getSignalQuality(); Serial.print(signal); – Hansie Jan 31 '22 at 19:35
  • I can probably use the USSD code to get the amount of data available? – Hansie Jan 31 '22 at 19:57
  • If your problem is that you can't get the AT commands to work then I'd suggest that you put this program aside and write a very small program which does nothing but try to do some basic operations with the AT commands. If you can't get that to work, share the code that doesn't work and ask for help. If you can get it to work then you've solved that part of your problems. – romkey Feb 01 '22 at 02:45
  • Thank you for the advice will try that. The thing is with the help from gre_gor it seems that I am on the wrong track trying to use AT commands to accomplish these things. Been trying to use the library but that seems to be a problem as well – Hansie Feb 01 '22 at 04:39
  • Please include the code where you attend to obtain signal strength and battery reading. The library supports reading [signal strength](https://github.com/vshymanskyy/TinyGSM/blob/master/examples/AllFunctions/AllFunctions.ino#L216-L217) and [getBattStats](https://github.com/vshymanskyy/TinyGSM/blob/master/examples/AllFunctions/AllFunctions.ino#L446-L454). USSD is carrier-specific, you need to check with your carrier for the command code. Alternatively, if you want to use AT commands instead of a library, the commands for getting signal strength is `AT+CSQ` and for reading battery is `AT+CBC". – hcheung Feb 02 '22 at 01:45

1 Answers1

0

For at least some of the desired functions, it isn't necessary to push AT-commands.

Try signalStrength = modem.getSignalQuality(); instead. It works for me on the Lilygo T-Call SIM800L. You can check out TinyGSM's Github-page for additional commands or this handy class reference

Anders
  • 251
  • 3
  • 13
  • Thank you for the help this solved my problem with the signal strength for the amount of data on the sim card I am still working on finding a solution for that. – Hansie Feb 23 '22 at 06:13