I am setting up an ESP8266 based device to measure temperature from a DS18B20 temp sensor and read data from a HAN enabled electricity meter. These data are sent out using MQTT to a local broker.
Both of these functions work well.
Now I also want to send a start report to my website database where I can see the WiFi connection IP address and so I have copied the function from another IoT project I completed in 2017 and which has been running ever since in my attic with temp data sent to the website database. It also has this start report.
So I figured I could reuse the sending function from the old project, but now the build generates errors due to deprecated functions, so I googled it and included what I thought would fix the problem but it does not even though the warnings have disappeared from the build....
So now I ask for help in pinpointing the reason for the failure.
Here is the old SendStartReport code from 2017, which works fine on the device in my attic (I have removed some debug serial prints and obfuscated the call URL):
void SendStartReport()
{
HTTPClient http;
//Build call string:
String dataline = "http://www.*******.com/php/espreport.php";
dataline += "?hostname=" + String(WiFi.hostname());
dataline += "&ipaddr=" + WiFi.localIP().toString();
dataline += "&macaddr=" + String(WiFi.macAddress());
//Call espreport.php web script
bool httpResult = http.begin(dataline); // <== THIS IS DEPRECATED
if(!httpResult)
{
Serial.println("Invalid HTTP request:");
Serial.println(dataline);
}
else
{
int httpCode = http.GET();
if (httpCode > 0)
{ // Request has been made
Serial.printf("HTTP status: %d Message: ", httpCode);
String payload = http.getString();
Serial.println(payload);
StartMessageSent = true;
}
else
{ // Request could not be made
Serial.printf("HTTP request failed. Error: %s\r\n", http.errorToString(httpCode).c_str());
}
}
http.end();
}
So after googling around I wound up with this in my new project where I added the WiFiClientSecure item:
void SendStartReport()
{
static bool res = false; //Static variable used to only allow one execution
if (res == true) return;
Serial.println("========= SendStartReport called ===========");
WiFiClientSecure client; // <== New object, don't know what it does...
client.setInsecure();
HTTPClient http;
//Build call string:
String dataline = "http://www.*********.com/php/espreport.php";
dataline += "?hostname=" + String(WiFi.hostname());
dataline += "&ipaddr=" + WiFi.localIP().toString();
dataline += "&macaddr=" + String(WiFi.macAddress());
//Call espreport.php web script
bool httpResult = http.begin(client, dataline); // <== New call format
if(!httpResult)
{
Serial.println("Invalid HTTP request:");
Serial.println(dataline);
}
else
{
int httpCode = http.GET();
if (httpCode > 0)
{ // Request has been made
Serial.printf("HTTP status: %d Message: ", httpCode);
String payload = http.getString();
Serial.println(payload);
res = true; //Stop repeated transmissions
}
else
{ // Request could not be made
Serial.printf("HTTP request failed. Error: %s\r\n", http.errorToString(httpCode).c_str());
Serial.println(dataline); }
}
Serial.println("========= SendStartReport exit ===========");
http.end();
}
When I use this changed code I get the following debug message printed:
========= SendStartReport called ===========
HTTP request failed. Error: connection refused
http://www.***********.com/php/espreport.php?hostname=esp-hanreader&ipaddr=192.168.119.191&macaddr=5C:CF:7F:1A:03:F2
========= SendStartReport exit ===========
So it seems most of it is working, but not the final part...
Now I am at wits end because I do not really understand what is happening behind the scenes...
Any suggestion on how I should modify my SendStartReport function so it actually works?
EDIT:
The call built when I run the function is:
http://www.********.com/php/espreport.php?hostname=esp-hanreader&ipaddr=192.168.119.191&macaddr=5C:CF:7F:1A:03:F2)
And when I run this from FireFox it executes correctly and sends the expected email.