direct_modem fo HTTP
so that we can bypass Blynk and send HTTP-related commands directly, not via hungry BlynkProtocol.h because we don’t need Blynk for that HTTP purpose.
If not working, we might have to modify TinyGSM library a little bit to simultaneously support 2 instances.
The code is as follows
....
#ifdef DUMP_AT_COMMANDS
#include <StreamDebugger.h>
StreamDebugger debugger(SerialAT, SerialMon);
TinyGsm modem(debugger);
#else
TinyGsm modem(SerialAT);
// Creating Second instance direct_modem
TinyGsm direct_modem(SerialAT);
#endif
TinyGsmClient client(modem);
// Using Second instance direct_modem
TinyGsmClient direct_client(direct_modem);
HttpClient http(direct_client, server, port);
....
void HTTPClientHandle(void)
{
if (!connectionStatus)
{
SerialMon.print(F("One time connection. Waiting for network..."));
if (!modem.waitForNetwork())
{
SerialMon.println(F(" fail"));
connectionStatus = false;
//delay(3000);
return;
}
SerialMon.println(F(" OK"));
SerialMon.print(F("Connecting to "));
SerialMon.print(apn);
if (!modem.gprsConnect(apn, gprsUser, gprsPass))
{
SerialMon.println(F(" fail"));
connectionStatus = false;
//delay(3000);
return;
}
SerialMon.println(F(" OK"));
connectionStatus = true;
}
#if 1
// Test client directly, using direct_modem, bypassing Blynk, see Arduino_TinyGSM.ino
// Using Second instance direct_modem
SerialMon.print(F("Connecting to "));
SerialMon.print(server);
if (!direct_client.connect(server, port))
{
SerialMon.println(F(" fail"));
//delay(10000);
return;
}
SerialMon.println(" OK");
// Make a HTTP GET request:
SerialMon.println("Performing HTTP GET request...");
direct_client.print(String("GET ") + resource + " HTTP/1.1\r\n");
direct_client.print(String("Host: ") + server + "\r\n");
direct_client.print("Connection: close\r\n\r\n");
direct_client.println();
unsigned long timeout = millis();
while (direct_client.connected() && millis() - timeout < 10000L)
{
// Print available data
while (direct_client.available())
{
char c = direct_client.read();
SerialMon.print(c);
timeout = millis();
}
}
SerialMon.println();
// Shutdown
direct_client.stop();
#else
// Using Second instance direct_modem
SerialMon.print(F("Performing HTTP GET request... "));
int err = http.get(resource);
if (err != 0)
{
SerialMon.println(F("failed to connect"));
//delay(5000);
return;
}
int status = http.responseStatusCode();
SerialMon.println(status);
if (!status)
{
//delay(5000);
return;
}
/* /// this is for informational reasons only not needed ///
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());
// Shutdown
http.stop();
#endif
SerialMon.println(F("Server disconnected"));
///modem.gprsDisconnect(); /// leave GPRS connected ///
///SerialMon.println(F("GPRS disconnected"));
SerialMon.println(F("GPRS is NOT disconnected"));
}
void check_status()
{
///static unsigned long checkstatus_timeout = 0;
// Send status report every STATUS_REPORT_INTERVAL (60) seconds: we don't need to send updates frequently if there is no status change.
if ((millis() > checkstatus_timeout) || (checkstatus_timeout == 0))
{
HTTPClientHandle();
checkstatus_timeout = millis() + STATUS_CHECK_INTERVAL;
///SerialMon.printf("Now millis are:%d \nThe new checkstatus_timeout is: %d\n",millis(),checkstatus_timeout);
}
}
void loop() {
check_status();
Blynk.run();
timer.run();
}
Use Blynk Webhook for HTTP
Another way to try is to use Blynk WebHook (with just one instance of modem)
Dear @khoih,
you are a non stop fantastic supporter and I would like to thank you one more time!
When at home, I will test the first proposed solution and I will let you know.
The second one, the WebHook is a working way, I have test it but I prefer general solutions not tightly bounded to Blynk…
Dear @khoih
YES YOU DID IT !!! You are genius !
I followed your Nr 1 solution.
You need just at the end of your HTTPClientHandle function, you need to reinitialize Blynk.begin( … ) because when the function is started the Blynk is stop working… See:
///modem.gprsDisconnect(); /// leave GPRS connected ///
///SerialMon.println(F("GPRS disconnected"));
SerialMon.println(F("GPRS is NOT disconnected"));
SerialMon.println(F("Reconnecting Blynk..."));
Blynk.begin(auth, modem, apn, gprsUser, gprsPass);
}
That’s nice.
Could you post the serial log as I’m still curious to see why Blynk is stop working.
I don’t like that and later when I have the board, I will try to find out the root cause and fix if possible as it’s always better to have both working independently.
Dear @khoih
thanks a lot for your support in this. Did you receive your TTGO T-Call ?
I will test your new release when I have a little time, I am having hard time in my job because my company will be participated in a very big exchibition in the London / UK and I have lot of things to take care …
Dear @mikekgr
Just received the TTGO T-Call yesterday.
I updated the Blynk_GSM library to v1.0.2 to enable GSM/GPRS and WiFi to run simultaneously. Already tested OK with TTGO-TCall as well as normal ESP32/ESP8266.
Also update the link here
Hope you find it useful as well as use it somehow somewhere.
Regards,
KH
Dear @khoih,
thanks a lot for keeping your fine work and to support the community.
I will test is as soon as I can and I will report the result back to you.
Best Regards,
Mike Kranidis
It’s a standard ESP32 chip, so has 3 hardware UARTS.
The Serial port (UART0) is connected to the pins labelled Tx (GPIO1) and Rx (GPIO3). It’s also connected via an onboard TTL to USB chip to the USB C power/programming connector.
The Serial1 port (UART1) is connected to the SIM800L chip using pins 26 (Tx) and 27 (Rx).
The Serial2 port (UART2) can be accessed by almost any of the remaining available pins by specifying them in the Serial2.begin command…