I am currently working for my final project, but I have a problem. I want to send my sensor data to MySQL database using XAMPP and phpMyAdmin. I have been using NodeMCU for the WiFi connection. But I am unable to send the value.
Here is the full code of my work.
#include <ESP8266HTTPClient.h>
#include <ESP8266WiFi.h>
#include <WiFiEsp.h>
#include <WiFiEspClient.h>
// Sensor 1
int sensorPin1 = 5;
int ledPin1 = 13;
// Sensor 2
int sensorPin2 = 4;
int ledPin2 = 12;
// Sensor 3
int sensorPin3 = 0;
int ledPin3 = 14;
//Wifi Connection
const char* ssid = "xxxxxx";
const char* password = "xxxxx";
const char* web = "192.168.1.69";
int status = WL_IDLE_STATUS;
WiFiServer server(80);
//Initialize the Wifi werver library
WiFiClient client;
void setup(void) {
//start serial port
Serial.begin(9600);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
// Start the server
server.begin();
Serial.println("Server started");
// Print the IP address
Serial.print("This is your ip address: ");
Serial.print(WiFi.localIP());
}
void loop() {
// Check if a client has connected
WiFiClient client = server.available();
if (!client) {
return;
}
// Wait until the client sends some data
Serial.println("new client");
while(!client.available()){
delay(1);
}
// Read the first line of the request
String request = client.readStringUntil('');
Serial.println(request);
client.flush(); // Match the request
if (client.connect(web, 80)) {
int sensor = digitalRead(sensorPin1);
int sensor1 = digitalRead(sensorPin2);
int sensor2 = digitalRead(sensorPin3);
//connect to the server (your computer or web page)
Serial.println("--> connection ok
");
client.print("POST /smart_parking/sensor.php?"); //This
client.print("sensor=");
client.print(sensor);
client.print("&sensor1=");
client.print(sensor1);
client.print("&sensor2=");
client.print(sensor2);
// client.print("sensor="); //This
// client.print("100");
client.println(" HTTP/1.1");
client.print("Host: ");
client.println(web);
client.println("Connection: close"); //Part of the GET request telling the server that we are over transmitting the message
client.println();//empty line
client.println(); //empty line
client.stop(); //Closing connection to server
Serial.println("--> finished transmission
");
}
else {
// If Arduino can't connect to the server (your computer or web page)
Serial.println("--> connection failed
");
}
}