Hello there, I apologize if this is a duplicate thread, but after researching for a while, I've not been able to find what I seek in the forums, so I'm here to request any possible help.
I'm a complete noob when it comes to Arduino programming, I literally began a week ago to challenge myself with a small school project, although I do have C/C++ knowledge.
And I have no knowledge of PHP scripting whatsoever.
I'm trying to achieve a successful data transfer between my NodeMCU esp8266 to a PHP script running on my website http://www..com/myPHPscript.php/
I basically want to check if there's a connection between the NodeMCU esp8266 and the PHP script and print out that status to a text file on my webpage: http://www..com/myTextFile.txt/
I've so far achieved the following, although I have no idea how to communicate with the PHP script from the esp8266 and I'm not sure if I'm approaching this correctly in my PHP script itself.
PHP script:
<?php
$var1 = $_GET['NetworkStatus'];
$fileContent = "Network Status: ".$var1.;
$fileStatus = file_put_contents('myFile.txt',$fileContent,FILE_APPEND);
if($fileStatus != false){
echo "SUCCESS: Data written to file";
}else{
echo "FAIL: Could not write data to file";
}
?>
C++ code:
#include "SoftwareSerial.h"
#include <WiFiClientSecure.h>
#include <ESP8266WiFi.h>
#include <HttpClient.h>
const char* ssid = "ssid";
const char* ssidpw = "password";
SoftwareSerial esp(2, 3);// RX, TX
String server = "www.webpage.com";
String uri = "/myPHPscript.php";
bool connected = false;
void setup() {
esp.begin(9600);
Serial.begin(9600);
reset();
connectWifi();
}
void reset() {
esp.println("AT+RST");
delay(1000);
if(esp.find("OK")) {
Serial.println("Module Reset");
}
}
void connectWifi()
{
WiFi.begin(ssid, ssidpw);
Serial.print("Connecting to: ");
Serial.print(ssid);
Serial.println();
Serial.print("Connected to: ");
Serial.print(ssid);
Serial.println();
Serial.print("Local IP address: ");
Serial.print(WiFi.localIP());
delay(4000);
}
void sendData(int netStat){
HTTPClient http;
//Build call string:
String dataline = server + uri;
dataline += "&var1=" + String(netStat, 1);
bool httpResult = http.begin(dataline);
if(!httpResult)
{
SerialDebug.println("Invalid HTTP request:");
SerialDebug.println(dataline);
}
else
{
int httpCode = http.GET();
if (httpCode > 0)
{ // Request has been made
SerialDebug.printf("HTTP status: %d Message: ", httpCode);
String payload = http.getString();
SerialDebug.println(payload);
}
else
{ // Request could not be made
SerialDebug.printf("HTTP request failed. Error: %s\r\n", http.errorToString(httpCode).c_str());
}
}
http.end();
}
void checkConnection(){
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
connected = false;
}
connected = true;
if (connected == true){
sendData(1);
}
}
void loop() {
checkConnection();
}
Any help would be appreciated.