Communication between esp8266 nodemcu and PHP script

Hello there, I've previously asked a question regarding how to start off with communication between an esp8266 nodemcu and a php script running on a hosted server.

After sadly not getting any help, I continued the journey and build up a somewhat understandable application, that in theory works and worked once in practise as well. Although, after getting off for around 24 hours, the application doesn't seem to work anymore.

I'm basically requesting a certain index from my PHP, which I'm then acquiring by using the POST method on my arduino.

My website is hosted on webhosting.dk & my arduino is coded using the latest arduino IDE.

I am wondering if my host might have blocked the communication because of a certain reason that I'm unaware of, and I'd like to know if anyone has an idea to solve this problem, alternatively another host that might work?

Arduino Code:

#include "SoftwareSerial.h"
#include <WiFiClientSecure.h>
#include <ESP8266WiFi.h>
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266HTTPClient.h>

#include <WiFiClient.h>

const char* ssid = "wifi ssid";
const char* ssidpw = "password";

WiFiClient client;

SoftwareSerial esp(2, 3);// RX, TX


const char server[] = "example.com"; // www.example.com
//String uri = "/php.php";

bool connected = false;

String data;

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.println();
  Serial.print("Connecting to: ");
  Serial.print(ssid);
  checkConnection();

  if (connected == true){
      Serial.println();
      Serial.print("Connected to: ");
      Serial.print(ssid);
      Serial.println();
      Serial.print("Local IP address: ");
      Serial.print(WiFi.localIP());
  }

  delay(4000);
}

void sendData(){
  if(client.connect(server, 8080)){
     String datainf = "NStatus=" + (String) "Connected";
     client.println("POST /php.php HTTP/1.1"); 
     //change URL below if using your Domain
     client.print("Host: example.com\r\n");                 
     client.println("User-Agent: ESP8266/1.0");
     client.println("Connection: close"); 
     client.println("Content-Type: application/x-www-form-urlencoded");
     client.print("Content-Length: ");
     client.print(datainf.length());
     client.print("\r\n");
     client.print(datainf);
     client.stop();
     data = datainf;
  }
}

void checkConnection(){
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
    connected = false;
  }

  connected = true;

}

void loop() {
  if (connected == true){
    Serial.println();
    Serial.print("Sending data: ");
    Serial.print(data);
    sendData();
    delay(5000);
  }
}

PHP Code:

<?php
$var1 = $_REQUEST["NStatus"];
$fileText = "Esp8266 connection: ";
//NStatus=Connected
if ($var1 == "NStatus=Connected"){
$servStatus = True;
}else{
$servStatus = False;
}

//Decides the connection status
if($servStatus === True){
$fileSt = "Connected";
echo "Connected";
}else{
$fileSt = "Disconnected";
echo "Disconnected\r\n";

echo "Data: ".$var1;
}

//Clears content, so it doesnt duplicate the connection status output
$myFile = @fopen('myFile.txt','r+');
@ftruncate($myFile, 0);

//Appends the appropriate output selection to the connection status
$fileContent = $fileText . $fileSt;
$fileStatus = file_put_contents('myFile.txt',$fileContent,FILE_APPEND);
?>

Why do you need to use a POST request to tell the script that the device is connected? A GET request would certainly be a lot simpler.

     String datainf = "NStatus=" + (String) "Connected";

This just looks like a clueless newbie typed it.

const char *datainf = "NStatus=Connected";

Doesn't piss away near as many resources, either.

And, then:

     client.print("Content-Length: ");
     client.println(strlen(datainf));
     client.stop();

Doesn't matter what the server replied with, huh?

     data = datainf;

Why on earth do you (think you) need to do THAT?

Hey Paul.

Thanks for the suggestions.

Although I don’t see how these suggestions might help regarding my question :slight_smile:

I do appreciate the unneccesery critique that I’ll look into.

I had looked at your code before (I think..) And when i saw it the first thing that sprung to mind was: "Why are you using the ESP8266 AT commands ?? Are you aware that you can program the ESP directly using the arduino IDE... Then i figured, but you have these libraries that are a part of that scheme included and are calling their functions, so do you have a second esp connected to the swSerial on pins 2 & 3. I am confused about your setup.

I do appreciate the unneccesery critique that I'll look into.

Unnecessary? THe server just MIGHT be trying to whack you with a clue by four, if you were paying attention to it's reply. But, since you seem to be in favor of ignoring replies, don't expect any more from me.

PaulS:
Unnecessary? THe server just MIGHT be trying to whack you with a clue by four, if you were paying attention to it's reply. But, since you seem to be in favor of ignoring replies, don't expect any more from me.

Whatever pleases you =) Thanks for the suggestions though!

Deva_Rishi:
I had looked at your code before (I think..) And when i saw it the first thing that sprung to mind was: "Why are you using the ESP8266 AT commands ?? Are you aware that you can program the ESP directly using the arduino IDE... Then i figured, but you have these libraries that are a part of that scheme included and are calling their functions, so do you have a second esp connected to the swSerial on pins 2 & 3. I am confused about your setup.

I'm essentially just trying to send a string to the PHP script from my ESP8266 NodeMCU using the POST method through wifi of course.

If the PHP script recognizes this string, it should output a certain text message to the .txt file.

emilo0212:
Whatever pleases you =) Thanks for the suggestions though!

I'm essentially just trying to send a string to the PHP script from my ESP8266 NodeMCU using the POST method through wifi of course.

If the PHP script recognizes this string, it should output a certain text message to the .txt file.

Then what are these lines of code doing in your sketch ?

#include "SoftwareSerial.h"   // 

#include <Arduino.h>  // it is a nodeMCU !

SoftwareSerial esp(2, 3);// RX, TX         // connecting using swSerial toan ESP ???

void setup() {
  esp.begin(9600);
  Serial.begin(9600);
  reset();
  connectWifi();
}

void reset() {
  esp.println("AT+RST");   // that recieves AT commands ?
  delay(1000);
 
  if(esp.find("OK")) {
    Serial.println("Module Reset");
  }
}

Anyway i figure you got this from some online example.. and it would help if you'd post the link to that. Though actually the technique with 'bool connected' is redundant (you could just test for WL_CONNECTED" anywhere it is not causing any issues as it is with "String data" the same, no need for yet another 'String" but wouldn't kill the functionality. But i am wondering how the PHP-server can determine if the "ESP" == connected or actually, that i can figure, but if it's not, how is it going to receive anything, and send something back ?