How to send data to website php script?

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.

OK, so I apparently got the code from a bad google hit...
When I changed it like this:

	//WiFiClientSecure client;
	//client.setInsecure();
	WiFiClient client;
	HTTPClient http;

...
	//bool httpResult = http.begin(client, dataline);
	bool httpResult = http.begin(client, dataline.c_str());

With all else remaining it worked like it should!!!

I don't know now from where the WiFiClientSecure came but it is obviously not good! And maybe c_str() also helped?

Next question:
If I want to use https instead of plain http, how should I modify the code?

Go back to your original version but change the "http" to "https" in the URL.
Are you sure your server supports HTTPS?

Yes, testing the same call in Firefox while changing between http and https results in the same response.

Do you mean the use of WiFiClientSecure in place of WiFiClient?
And adding the line:

client.setInsecure();

With all else the same as now?
Or just changing the url prefix to https?

Yes. In addition to changing the URL.

Thank you for your comment!
I have now changed the code section to look like this:

	WiFiClientSecure client; //Testing use of https
	client.setInsecure();
	HTTPClient http;

	//Build call string:
	String dataline = "https://www.**********.com/php/espreport.php";
	dataline += "?hostname=" + String(WiFi.hostname());
	dataline += "&ipaddr=" + WiFi.localIP().toString();
	dataline += "&macaddr=" + String(WiFi.macAddress());

And it did work on first try!
So now I can use the https more secure protocol for these calls.
I have another task also related to this type of call and that is to report both temperature at the device as well as an electricity meter's current energy reading. So I will use https also for that.
:grin: :grin: :grin:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.