Closed ilgarbenli closed 10 months ago
I realized problem is not about 2 client. Problem is about client. When I try to POST json request to my server it returns this 403 error, but not reqbin.com gives any error, even it works perfect. Do you have any idea @govorox @RobertByrnes
2 things I'd check. You are using the same gsm layer for both clients. This could make state tracking very tricky for the application, and also make for a broken state. First of all make the 2 stacks have a unique set of objects for simplicity, safety, and clarity. Also, 403 forbidden, yes is client error but also check the path you are trying to access is correct and is available to your client from the web servers side ....
Also given you are using 2 clients for the one modem you should be fine be ensure you are calling client.stop() between the request to clean up the state. You might also want to look into doing something similar with the gsm transport layer. Not sure exactly what TinyGsm offers for this vut probably something there...
@RobertByrnes first of all thank you for your respond. I made a test with this repo : https://github.com/Samorange1/Connect-ESP32-to-Firebase-via-GSM and it worked. I can send data to firebase with this method. But when I change code to sending json request to my server it gives 403 error, each time even if connects endpoint. I dont know how to make things works but I'm trying to send json data. Actually my last php code works well with reqbin.com but returns 403 with sim800l request.
Its like something refuses arduino request
Another thing I've done before is to build the request in Postman. Then you have visibility over exactly which headers you are sending and can view the full requests and responses in the console. Can help
Sent from Outlook for Androidhttps://aka.ms/AAb9ysg
From: Ilgar @.> Sent: Wednesday, December 13, 2023 11:12:06 PM To: govorox/SSLClient @.> Cc: Robert Byrnes @.>; Mention @.> Subject: Re: [govorox/SSLClient] Connecting 2 Client Conflict (Issue #66)
@RobertByrneshttps://github.com/RobertByrnes first of all thank you for your respond. I made a test with this repo : https://github.com/Samorange1/Connect-ESP32-to-Firebase-via-GSM and it worked. I can send data to firebase with this method. But when I change code to sending json request to my server it gives 403 error, each time even if connects endpoint. I dont know how to make things works but I'm trying to send json data. Actually my last php code works well with reqbin.comhttps://reqbin.com/ but returns 403 with sim800l request.
Its like something refuses arduino request
— Reply to this email directly, view it on GitHubhttps://github.com/govorox/SSLClient/issues/66#issuecomment-1854838003, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AOEK4OPI3GDJJPDIE6PXRDDYJIY4NAVCNFSM6AAAAABASKI65WVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQNJUHAZTQMBQGM. You are receiving this because you were mentioned.Message ID: @.***>
@RobertByrnes I'm just trying Postman, its also working with postman requests, and not suspicious headers there. I changed body types to x-www-form-urlencoded in reqbin, it returns same 403 forbidden, switching back application/json made things work again.
I confused about is my headers wrong or another permission problem exist. It's not make sense for me.
this is my last structure:
#include <Arduino.h>
#if defined ARDUINO_ARCH_ESP8266
#include <ESP8266WiFi.h>
#elif defined ARDUINO_ARCH_ESP32
#include <WiFi.h>
#else
#error Wrong platform
#endif
#include <Wire.h>
#include "SSLClient.h"
//To make http request esay: https://github.com/arduino-libraries/ArduinoHttpClient
#include <ArduinoHttpClient.h>
//Please enter your CA certificate in ca_cert.h
#include "ca_cert.h"
// ESP32 LilyGo-T-Call-SIM800 SIM800L_IP5306_VERSION_20190610 (v1.3) pins definition
#define MODEM_UART_BAUD 9600
#define MODEM_RST 5
#define MODEM_PWRKEY 4
#define MODEM_POWER_ON 23
#define MODEM_TX 27
#define MODEM_RX 26
#define I2C_SDA 21
#define I2C_SCL 22
#define LED_PIN 13
#define IP5306_ADDR 0x75
#define IP5306_REG_SYS_CTL0 0x00
// Set serial for debug console (to the Serial Monitor)
#define SerialMon Serial
// Set serial for AT commands (to the 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 MAX_CONNECTION_TIMEOUT 5000
#define MAX_WIFI_SCAN 127
// Include after TinyGSM definitions
#include <TinyGsmClient.h>
// Your GPRS credentials (leave empty, if missing)
const char apn[] = "internet"; // Your APN
const char gprs_user[] = ""; // User
const char gprs_pass[] = ""; // Password
const char simPIN[] = ""; // SIM card PIN code, if any
const char hostname[] = "******.com";
const String resource = "/gpt.php";
int port = 443;
// Layers stack
TinyGsm sim_modem(SerialAT);
TinyGsmClient gsm_transpor_layer(sim_modem);
SSLClient secure_presentation_layer(&gsm_transpor_layer);
HttpClient http_client = HttpClient(secure_presentation_layer, hostname, port);
// Power configuration for SIM800L_IP5306_VERSION_20190610 (v1.3) board
bool setupPMU()
{
bool en = true;
Wire.begin(I2C_SDA, I2C_SCL);
Wire.beginTransmission(IP5306_ADDR);
Wire.write(IP5306_REG_SYS_CTL0);
if (en)
{
Wire.write(0x37);
}
else
{
Wire.write(0x35);
}
return Wire.endTransmission() == 0;
}
// Modem initial setup (cold start)
void setupModem()
{
pinMode(MODEM_RST, OUTPUT);
pinMode(MODEM_PWRKEY, OUTPUT);
pinMode(MODEM_POWER_ON, OUTPUT);
pinMode(LED_PIN, OUTPUT);
// Reset pin high
digitalWrite(MODEM_RST, HIGH);
// Turn on the Modem power first
digitalWrite(MODEM_POWER_ON, HIGH);
// Pull down PWRKEY for more than 1 second according to manual requirements
digitalWrite(MODEM_PWRKEY, HIGH);
delay(200);
digitalWrite(MODEM_PWRKEY, LOW);
delay(1200);
digitalWrite(MODEM_PWRKEY, HIGH);
// Initialize the indicator as an output
digitalWrite(LED_PIN, LOW);
}
void setup()
{
SerialMon.begin(9600);
delay(100);
// Start board power management
if (!setupPMU())
{
Serial.println("Setting board power management error");
}
// Set SIM module baud rate and UART pins
SerialAT.begin(115200, SERIAL_8N1, MODEM_RX, MODEM_TX);
//Add CA Certificate
secure_presentation_layer.setCACert(root_ca);
// SIM modem initial setup
setupModem();
}
void loop()
{
SerialMon.print("Initializing modem...");
if (!sim_modem.init())
{
SerialMon.print(" fail... restarting modem...");
setupModem();
// Restart takes quite some time
// Use modem.init() if you don't need the complete restart
if (!sim_modem.restart())
{
SerialMon.println(" fail... even after restart");
return;
}
}
SerialMon.println(" OK");
// General information
String name = sim_modem.getModemName();
Serial.println("Modem Name: " + name);
String modem_info = sim_modem.getModemInfo();
Serial.println("Modem Info: " + modem_info);
// Unlock your SIM card with a PIN if needed
if (strlen(simPIN) && sim_modem.getSimStatus() != 3)
{
sim_modem.simUnlock(simPIN);
}
// Wait for network availability
SerialMon.print("Waiting for network...");
if (!sim_modem.waitForNetwork(240000L))
{
SerialMon.println(" fail");
delay(10000);
return;
}
SerialMon.println(" OK");
// Connect to the GPRS network
SerialMon.print("Connecting to network...");
if (!sim_modem.isNetworkConnected())
{
SerialMon.println(" fail");
delay(10000);
return;
}
SerialMon.println(" OK");
// Connect to APN
SerialMon.print(F("Connecting to APN: "));
SerialMon.print(apn);
if (!sim_modem.gprsConnect(apn, gprs_user, gprs_pass))
{
SerialMon.println(" fail");
delay(10000);
return;
}
digitalWrite(LED_PIN, HIGH);
SerialMon.println(" OK");
// More info..
Serial.println("");
String ccid = sim_modem.getSimCCID();
Serial.println("CCID: " + ccid);
String imei = sim_modem.getIMEI();
Serial.println("IMEI: " + imei);
String cop = sim_modem.getOperator();
Serial.println("Operator: " + cop);
IPAddress local = sim_modem.localIP();
Serial.println("Local IP: " + String(local));
int csq = sim_modem.getSignalQuality();
Serial.println("Signal quality: " + String(csq));
/// HTTP Test
if (sim_modem.isGprsConnected())
{
Serial.println("");
if (http_client.connect(hostname, port)) {
Serial.println("Connected to API endpoint\n");
http_client.beginRequest();
String body = "{\"considerIP\":false,\"wifiAccessPoints\":" + getSurroundingWiFiJson() + "}";
String request = "POST " + String(resource) +" HTTP/1.1\r\n";
request += "Host: " + String(hostname) + "\r\n";
request += "User-Agent: ESP-32\r\n";
request += "Content-Type:text/plain\r\n";
request += "Content-Length:" + String(body.length()) + "\r\n";
request += "Accept: */*\r\n";
request += "Accept-Encoding: gzip, deflate, br\r\n";
request += "Connection: keep-alive\r\n\r\n";
request += body;
http_client.println(request);
http_client.endRequest();
int status_code = http_client.responseStatusCode();
String response = http_client.responseBody();
Serial.print("Status code: ");
Serial.println(status_code);
Serial.print("Response: ");
Serial.println(response);
Serial.println("----------: ");
Serial.println(request);
http_client.stop();}
else{
Serial.println("https errpr");
}
}
else
{
Serial.println("...not connected");
}
// Disconnect GPRS
sim_modem.gprsDisconnect();
SerialMon.println("GPRS disconnected");
digitalWrite(LED_PIN, LOW);
//Turn off the moden (if use, you need run setupModem() again)
//sim_modem.poweroff();
//SerialMon.println("Modem poweroff");
//delay(1000);
//setupModem();
delay(15000);
}
String getSurroundingWiFiJson()
{
String wifiArray = "[";
int8_t numWifi = WiFi.scanNetworks();
if (numWifi > MAX_WIFI_SCAN)
{
numWifi = MAX_WIFI_SCAN;
}
for (uint8_t i = 0; i < numWifi; i++)
{
wifiArray += "{\"macAddress\":\"" + WiFi.BSSIDstr(i) + "\",";
wifiArray += "\"signalStrength\":" + String(WiFi.RSSI(i)) + ",";
wifiArray += "\"channel\":" + String(WiFi.channel(i)) + "}";
if (i < (numWifi - 1))
{
wifiArray += ",\n";
}
}
WiFi.scanDelete();
wifiArray += "]";
return wifiArray;
}
and this is php code from server
<?php
require 'firebaseLib.php';
const DEFAULT_URL = '*********.firebasedatabase.app/';
const DEFAULT_TOKEN = '**********************';
$DEFAULT_PATH = '/location'; // Firebase'deki ana yolunuzu düzenleyin
header("Access-Control-Allow-Origin: *"); // Tüm domainlere izin ver
header("Access-Control-Allow-Methods: *"); // Tüm HTTP metodlarına izin ver
header("Access-Control-Allow-Headers: *"); // Tüm başlık bilgilerine izin ver
header("Content-Type: application/json"); // Yanıtın JSON formatında olduğunu belirt
$geolocation_hostname = "www.googleapis.com";
$geolocation_resource = "/geolocation/v1/geolocate?key=********************";
$geolocation_port = 443;
// Gelen POST verilerini al
$post_data = file_get_contents("php://input");
// Google Geolocation API'ye POST request gönder
$context_options = array(
'http' => array(
'method' => 'POST',
'header' => "Content-type: application/json\r\n" .
"Content-Length: " . strlen($post_data) . "\r\n",
'content' => $post_data
)
);
$context = stream_context_create($context_options);
$response = file_get_contents("https://" . $geolocation_hostname . $geolocation_resource, false, $context);
// Google Geolocation API'den gelen cevabı decode et
$location_data = json_decode($response, true);
// Firebase'e gönderilecek verileri hazırla
$latitude = $location_data['location']['lat'];
$longitude = $location_data['location']['lng'];
$_devicestatus = array(
'latitude' => $latitude,
'longitude' => $longitude
);
// Firebase'e veriyi gönder
$firebase = new \Firebase\FirebaseLib(DEFAULT_URL, DEFAULT_TOKEN);
$firebase->update($DEFAULT_PATH, $_devicestatus);
// Başarı mesajını döndür
echo "POST_SUCCESSFUL";
?>
may it about file_get_contents() func?
and also in postman, changing json to xml mades return 403
its my arduino side serial monitor response
and this is reqbin request (its working)
do you see any mistake?
The only thing that jumps out at me is that you have no space after the : in your Content-Type and Content-Length headers...
Also double check you are happy sending from esp32 as text/plain to php and then from php as application/json. Should be fine...
@RobertByrnes Didn't worked unfortunatelly.
But its my own vds server, do you have any advice to change some settings about security or permission?
@RobertByrnes I found what causes 403 error, its line which includes file_get_contents() func in my php code. I changed this and it worked.
@ilgarbenli glad to hear you managed to resolve the issue
Hello everyone,
I'm trying to getting datas from a client and then sending data to another client. The probem is, one client needs ssl/tls connection (which is no problem I can connect and take datas) and other client is using http port:80 (probllem is here).
When I tried to connect and send data individually to this http server its okay everythings work. But when I try with other ssl/tls cleint I think there is some kind of conflict.
this is my structure of clients.
and the POST response from database_client:
I guess its no problem with server I can send data induvidually, I also tested with https://reqbin.com. there is no problem.
But when I use with other client, its not working.
I am open to any suggestion.