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 ![]()
#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);}
}