• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

ESP32 SIM800 Cloud

Q&A Forum › Category: ESP32 › ESP32 SIM800 Cloud
0 Vote Up Vote Down
Gavin O’Connor asked 4 years ago

Hi All,
I have tried the Code below, but can’t seem to connect the server, I am using a Blue Host Domain but somehow it keeps “failing” to connect, any ideas where I am going wrong?, it works 100% with Wi-Fi
#include <Arduino.h>
// Your GPRS credentials (leave empty, if not needed)
const char apn[] = “iphone.vodacom.za”; // 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[] = “”;
// Server details
// The server variable can be just a domain name or it can have a subdomain. It depends on the service you are using
const char server[] = “https://xxxxxxxxxx/post-esp-data.php&#8221;; // domain name: example.com, maker.ifttt.com, etc
const char resource[] = “/post-esp-data.php”; // resource path, for example: /post-data.php
const int port = 443; // server port number
#define MODEM_PWKEY 33
#define MODEM_TX 16
#define MODEM_RX 17
#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>
#include <HTTPClient.h>
#ifdef DUMP_AT_COMMANDS
#include <StreamDebugger.h>
StreamDebugger debugger(SerialAT, SerialMon);
TinyGsm modem(debugger);
#else
TinyGsm modem(SerialAT);
#endif
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
Adafruit_BME280 bme;
// TinyGSM Client for Internet connection
TinyGsmClient client(modem);
String apiKeyValue = “tPmAT5Ab3j7F9”;
String sensorName = “BME280”;
String sensorLocation = “Repro Office”;
#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 */
void setup() {
// Set serial monitor debugging window baud rate to 115200
SerialMon.begin(115200);
// Set modem reset, enable, power pins
pinMode(MODEM_PWKEY, OUTPUT);
digitalWrite(MODEM_PWKEY, LOW);
delay (1000);
digitalWrite(MODEM_PWKEY, HIGH);
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);
}

// You might need to change the BME280 I2C address, in our case it’s 0x76
if (!bme.begin(0x77)) {
Serial.println(“Could not find a valid BME280 sensor, check wiring!”);
while (1);
}
// Configure the wake up source as timer wake up
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
}
void loop() {
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…”);
// Prepare your HTTP POST request data (Temperature in Celsius degrees)
// Prepare your HTTP POST request data
String httpRequestData = “api_key=” + apiKeyValue + “&sensor=” + sensorName
+ “&location=” + sensorLocation + “&value1=” + String(bme.readTemperature())
+ “&value2=” + String(bme.readHumidity()) + “&value3=” + String(bme.readPressure()/100.0F) + “”;
Serial.print(“httpRequestData: “);
Serial.println(httpRequestData);

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”));
}
}
// Put ESP32 into deep sleep mode (with timer wake up)
esp_deep_sleep_start();
}
 
 
 

Question Tags: SIM800
11 Answers
0 Vote Up Vote Down
Rui Santos Staff answered 4 years ago

Hello! I hope you are doing well and thanks for reaching out. As I’ve replied via email:

Unfortunately I don’t know what’s missing: it might be the antenna, wrong credentials, server blocking the connection, your mobile network not supporting 2G…
Did you check if it supports 2G?

Regards,
Rui

0 Vote Up Vote Down
Gavin O’Connor answered 4 years ago

Hi Rui,
Many thanks for the reply,
Yes the networks still support 2G
I will re-try again.
Regards,
Gavin

0 Vote Up Vote Down
Rui Santos Staff answered 4 years ago

Let me know your results. It can be the credentials, antenna, bad signal or something else missing… I’m not sure to be honest.

0 Vote Up Vote Down
Gavin O’Connor answered 4 years ago

Hi Rui,
I have tried multiple things, please see below on both port 80 and port 443
I will try a Thingspeak example to see if I can transmit any data
Initializing modem…
Connecting to APN: iphone.vodacom.za OK
Connecting to https://fvh.bwm.mybluehost.me fail
Connecting to APN: iphone.vodacom.za OK
Connecting to https://fvh.bwm.mybluehost.me/post-esp-data.php fail
 
 
 

0 Vote Up Vote Down
Rui Santos Staff answered 4 years ago

Instead of the HTTPS URL, can you use the HTTP URL?

http://fvh.bwm.mybluehost.me/post-esp-data.php
0 Vote Up Vote Down
Gavin O’Connor answered 4 years ago

Hi Rui,
I tried it and still nothing, I have it working on Thingspeak however, with a small error but I will open up a new question on that…;-)
Regards,
Gavin

-1 Vote Up Vote Down
Steve Mercer answered 4 years ago

Remember that when you change the url to http you also need to change the port from 443 to 80. For secure connections you need to use “TinyGsmClientSecure” instead of “TinyGsmClient”. I’m also fairly sure “server” should just be the hostname you are connecting to. The specific client that you use tells the library to use either http or https. In your case, I believe, “server” should be set to “feh.bwm.bluehost.me”. The “resource” variable holds the path to the actual file “post-esp-data.php”. Finally, have you tried the Diagnostics sketch to test out the board/library?

0 Vote Up Vote Down
Gavin O’Connor answered 4 years ago

Thanks Steve, I will give that a go and advise

0 Vote Up Vote Down
NUR AZLIN AIRUL AZLIN MOHAMED ZOHDI answered 4 years ago

My experience here in Malaysia, the LilyGo T-Call SIM800L only support Maxis & Celcom APN.  I tried using Digi, nothing happens. When I changed the network operator, i got immediate connection.  You might want to try different operator or simcard.
cheers!

0 Vote Up Vote Down
NUR AZLIN AIRUL AZLIN MOHAMED ZOHDI answered 4 years ago

On top of that – I also managed to use Telegram with the ESP32 LilyGo T-Call SIM800 module, over TinyGSM with SSLClient wrapper.  Goodluck.

0 Vote Up Vote Down
Sara Santos Staff answered 4 years ago

Hi.
Thanks for sharing your experience.
I didn’t know about that.
Regards,
Sara

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [eBook Updated] Learn Raspberry Pi Pico/Pico W with MicroPython eBook – Version 1.2 May 26, 2025
  • [New Edition] Build ESP32-CAM Projects eBook – 2nd Edition April 16, 2025

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2025 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.