http post request trouble

Hello guys , im using esp32s and im trying to post http request to a server but im facing a trouble with send RSSI to the host server, if you please could check my code and tell me what is wrong with it
sorry for the mess inside it :smiley:

#include <WiFi.h>

#define buttonPin 0
#define LEDPin 5

// Network information
char* ssid = “ssid”;
const char* password = “***”;

// Host Settings
char server = “firefighters.000webhostapp.com”;
//String writeAPIKey = “ZRBW9QSHRASQ3JRC”;

// Constants
const unsigned long postingInterval = 15L * 1000L;

// Global Variables
unsigned long lastConnectionTime = 0;
int measurementNumber = 0;

void setup(){

Serial.begin(115200);
delay(5000);
Serial.print(“Begin Monitoring \n”);
delay(1000);
//pinMode(buttonPin,INPUT);
//pinMode(LEDPin, OUTPUT);
connectWiFi();

}

void loop(){
const int numberPoints = 7;
float wifiStrength;

// In each loop, make sure there is always an internet connection.
if (WiFi.status() != WL_CONNECTED) {
connectWiFi();
}

if (millis() - lastConnectionTime > postingInterval) {
//blinkX(2,250); // Verify the button press.
wifiStrength = getStrength(numberPoints);
httpRequest(wifiStrength);
//blinkX(measurementNumber,200); // Verify that the httpRequest is complete.
measurementNumber++;
}

// If a button press is detected, write the data to Host.
// if (digitalRead(buttonPin) == LOW){
// if (millis() - lastConnectionTime > postingInterval) {
// blinkX(2,250); // Verify the button press.
// wifiStrength = getStrength(numberPoints);
// httpRequest(wifiStrength);
// blinkX(measurementNumber,200); // Verify that the httpRequest is complete.
// measurementNumber++;
// }
//
// }
}

void connectWiFi(){
Serial.print(“Connecting to wifi \n”);
while (WiFi.status() != WL_CONNECTED){
Serial.print(“Searching \n”);
WiFi.begin(ssid, password);
delay(500);
}

// Show the user a connection is successful.
Serial.println(“Connected”);
blinkX(5,50);
}

void httpRequest(float rssi) {

WiFiClient client;

if (!client.connect(server, 443)){

Serial.println(“connection failed”);
lastConnectionTime = millis();
client.stop();
return;
}

else{

// create data string to send to host
//String data = “field1=” + String(field1Data) + “&field2=” + String(field2Data); //shows how to include additional field data in http post
String data = “rssi=” + String(rssi);
// POST data to ThingSpeak
if (client.connect(server, 443)) {

// Make a HTTP request

client.println(“POST www.firefighters.000webhostapp.com HTTPS/1.1”);
client.println(“Host: https://firefighters.000webhostapp.com:443/postRssiRobot.php”);
client.println(“User-Agent: ESP32WiFi/1.1”);
client.println(“Connection: close”);
client.println(“Content-Type: application/x-www-form-urlencoded”);
//client.println(“Accept: /”);
client.println("Content-Length: " + data.length());
client.println();
client.println(data);

Serial.println("RSSI = " + String(rssi));
lastConnectionTime = millis();
}
}
client.stop();
}

// Take a number of measurements of the WiFi strength and return the average result.
int getStrength(int points){
long rssi = 0;
//long averageRSSI=0;

for (int i=0;i < points;i++){
rssi += WiFi.RSSI();
delay(20);
}

return WiFi.RSSI();

}

// Flash the LED a variable number of times with a variable delay.
void blinkX(int numTimes, int delayTime){
for (int g=0;g < numTimes;g++){

// Turn the LED on and wait.
digitalWrite(LEDPin, HIGH);
delay(delayTime);

// Turn the LED off and wait.
digitalWrite(LEDPin, LOW);
delay(delayTime);

}
}

im facing a trouble with send RSSI to the host server

And what might that trouble be?

If you succeed in connecting to the server, why do you connect again?

The Host field contains the name of a server, NOT a protocol and not a script to be executed. The script to be executed is supposed to be part of the POST request. It currently isn’t.

You are using the WiFi library, which is ONLY for the Arduino WiFi shield, but apparently are using a ESP device.

int getStrength(int points){
    long rssi = 0;
    //long averageRSSI=0;
   
    for (int i=0;i < points;i++){
        rssi += WiFi.RSSI();
        delay(20);
    }

    return WiFi.RSSI();

   
   
}

Get the RSSI value 7 times. Then ignore those 7 values, and get the value again and return it. What is magic about the 8th value?

What is so secret about this value that you need the obscurity, and complexity, of a POST request? A GET request is far simpler, and doesn’t require pissing away resources on the String class.

PaulS: What is so secret about this value that you need the obscurity, and complexity, of a POST request? A GET request is far simpler, and doesn't require pissing away resources on the String class.

A GET request is supposed to be idempotent, meaning you can "get" the resource from the server multiple times without affecting the state of the server. Since the OP appears to be logging data to the server, this is not an idempotent request, so a GET request is not appropriate here, but a POST request is.

One problem I see is that you're connecting to port 443 and trying to use HTTPS, but you're not actually encrypting the request with TLS. If the server supports HTTP requests (probably on port 80), use that instead. I highly doubt you can even do TLS on the Arduino.