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);
?>