Sending date from arduino to local server

I am trying to send data from arduino to my local server (php myadmin). This code is working fine for online web server but not sending data to local server.

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>

char ssid[] = "dlink-1880";         // your SSID
char pass[] = "Mohammad1";     // your SSID Password

void setup () {

Serial.begin(115200);
WiFi.begin(ssid, pass);

Serial.write(12);

while (WiFi.status() != WL_CONNECTED) {

delay(1000);
Serial.print("Connecting..");

}

Serial.println("WIFI Local IP"); 
Serial.print(WiFi.localIP());
}

void loop() {
float humidity1, temprature1;
humidity1 = 19.5;
temprature1 = 25.5;

if (WiFi.status() == WL_CONNECTED) { //Check WiFi connection status

HTTPClient http;  //Declare an object of class HTTPClient



http.begin("http://192.168.1.151/Project2/dht_new.php?humidity=30&temperature=96");  //Specify request destination
delay(1500);
int httpCode = http.GET();                                                                  //Send the request
Serial.println("check db ");  
if (httpCode > 0) { //Check the returning code

String payload = http.getString();   //Get the request response payload
Serial.println(payload);                     //Print the response payload

}

http.end();   //Close connection

}



delay(8000);    //Send a request every 30 seconds

}
one += String(humidity1);
one += String(temprature1);

What is shown on your serial connection?

What is your ip address

Does your local server log anything?

If not what is the address of your server?

You would be better off putting your code in a code section - read the forum rules.

I am using the below code, but its not connecting to the local server while it can connect to web server.

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <SPI.h>
#include <ESP8266HTTPClient.h>


char ssid[] = "dlink-1880";         // your SSID
char pass[] = "Mohammad1";     // your SSID Password

char server[] = "192.168.1.151";
//IPAddress server(192,168,1,151);  

// Initialize the client library
WiFiClient client;


void setup() {
 Serial.begin(115200);
 WiFi.begin(ssid, pass);

 
 
 while (WiFi.status() != WL_CONNECTED) {

 delay(1000);
 Serial.println("Connecting...");

 }
 Serial.println();
 Serial.print("WIFI Local IP: "); 
 Serial.print(WiFi.localIP());

if (WiFi.status() == WL_CONNECTED) {
   Serial.println();
   Serial.println("Connected to wifi");
   Serial.println("\nStarting connection...");
   // if you get a connection, report back via serial:
  

 }
}

void loop() {

if (client.connect(server, 81)) {
     Serial.println("connected to server");
     // Make a HTTP request:
     Serial.print("GET /Project2/dht_new.php?humidity=33&temperature=66");
     client.println("GET /Project2/dht_new.php?humidity=33&temperature=66 HTTP/1.1");
     client.println();
   client.println("Host: 192.168.1.151");
   client.println("Connection: close");
   client.println();
 } else {
   // if you didn't get a connection to the server:
   Serial.println("connection failed");
 }
}

Use ... tags around your code rather than just pasting into the mesage - read this: Read this before posting a programming question ... - Programming Questions - Arduino Forum

What is shown on your serial connection?

What is your ip address

Does your local server log anything? If so what?

  • This is what i'm getting on serial monitor:

Connecting...
Connecting...
Connecting...
Connecting...
Connecting...
Connecting...

WIFI Local IP: 192.168.1.146
Connected to wifi

Starting connection...
connection failed

  • My ip address is:192.168.1.151

  • I m logging values from arduino(temperature and humidity) in php myadmin on my local server. Which i send from arduino manually.

What software are you running on your local server that the arduino is trying to connect to?

Is the PC you are using for the serial connection the server that the arduino is trying to connect to,or do you have more than one PC involved?

What online webserver do you find works?

I m using xamp for local server. Then I am using bracket to run the php file. While running the php file and putting values for temperature and humidity manually it send those values to local server but when sending values from arduino it doesnt send. probably arduino doesn't connect to local the server.
I'm using my pc as local server.

Below is the php code, which works fine for both local and online server.

<?php
class dht11{
 public $link='';
 function __construct($temperature, $humidity){
  $this->connect();
  $this->storeInDB($temperature, $humidity);
 }
 
 function connect(){
  $this->link = mysqli_connect('localhost', 'ayazdb', '1234') or die('Cannot connect to the DB');
  mysqli_select_db($this->link,'ayazdb') or die('Cannot select the DB');
 }
 
 function storeInDB($temperature, $humidity){
  $query = "insert into arduino_tbl set humidity='".$humidity."', temperature='".$temperature."'";
  $result = mysqli_query($this->link,$query) or die('Errant query:  '.$query);
 }
 
}
if($_GET['temperature'] != '' and  $_GET['humidity'] != ''){
 $dht11=new dht11($_GET['temperature'],$_GET['humidity']);
}


?>

It certainly loooks like the Arduio is not connecting to your server. Your log suggests that the line:
if (client.connect(server, 81))

is failing for some reason. it would be worth putting another line of information such as:

Serial.println("connection failed");
Serial.println(server);

Just to ensure that the server address used has not been corrupted.

Does the xamp server allow different levels of logging (I am not familiar with xamp), if so can you get it to log the reason for the connect failure? Are you sure it is on port 81?

You also said the code works fine for an online server, but you have not specified a net mask or gateway when starting the Wifi - I can't see how it would therefore connect unless the code was different.

I have connected local server(laptop) and esp8266 node mcu to same wifi network. I tried port 80 also but no luck.

I checked local ip address also (as attached) and its 192.168.1.151

wifi ip.png

Just realised you have changed from using an IPAddress to a character string in the call to client.connect Using a character string might require DNS resolution and not sure if that will work correctly. Will be simpler to use an IPAddress then we know it has the right server address - can you change it back to using IPAddress?
When using google.com or another named server you will need to use a character string.

Did you check on whether the server is logging anythingor can be made to?

The issue has been resolved. Thanks :slight_smile:

The port was blocked in windows firewall, So I opened it.

Below is the code to send values to local server.

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266HTTPClient.h>

char server[] = "192.168.1.151"; // Your local pc IP address , Go to CMD and type IPCONFIG
char ssid[] = "dlink-1880";         // your SSID
char pass[] = "Mohammad1";     // your SSID Password

// Initialize the client library
WiFiClient client;
float humidityData, temperatureData;

void setup() {
  Serial.begin(115200);
  WiFi.begin(ssid, pass);

  while (WiFi.status() != WL_CONNECTED) {
 
  delay(1000);
  Serial.println("Connecting...");
 
  }
  Serial.println();
  Serial.print("WIFI Local IP: "); 
  Serial.print(WiFi.localIP());
 
 if (WiFi.status() == WL_CONNECTED) {
    Serial.println();
    Serial.println("Connected to wifi");
    Serial.println("\nStarting connection...");
    // if you get a connection, report back via serial:
   

  }
}
//------------------------------------------------------------------------------


/* Infinite Loop */
void loop(){
  humidityData = 12.88;
  temperatureData = 98.4;
  Sending_To_phpmyadmindatabase(); 
  delay(2000); // interval
}


  void Sending_To_phpmyadmindatabase()   //CONNECTING WITH MYSQL
 {
   if (client.connect(server, 80)) {
    Serial.println("connected");
    // Make a HTTP request:
    Serial.print("GET /Project2/dht_new.php?humidity=");
    client.print("GET /Project2/dht_new.php?humidity=");     //YOUR URL
    client.print(humidityData);
    client.print("&temperature=");
    Serial.println("&temperature=");
    client.print(temperatureData);
    client.print(" ");      //SPACE BEFORE HTTP/1.1
    client.print("HTTP/1.1");
    client.println();
    client.println("Host: 192.168.1.151");
    client.println("Connection: close");
    client.println();
  } else {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }
 }

This is the PHP code:
use the link in browser (use correct path to your php file) to put values manually for temperature and humidity

http://localhost/Project2/dht_new.php?humidity=98&temperature=34

<?php
class dht11{
 public $link='';
 function __construct($temperature, $humidity){
  $this->connect();
  $this->storeInDB($temperature, $humidity);
 }
 
 function connect(){
  $this->link = mysqli_connect('localhost', 'YOUR DATA BASE', 'PASSWORD') or die('Cannot connect to the DB');
  mysqli_select_db($this->link,'YOUR DATA BASE') or die('Cannot select the DB');
 }
 
 function storeInDB($temperature, $humidity){
  $query = "insert into arduino_tbl set humidity='".$humidity."', temperature='".$temperature."'";
  $result = mysqli_query($this->link,$query) or die('Errant query:  '.$query);
 }
 
}
if($_GET['temperature'] != '' and  $_GET['humidity'] != ''){
 $dht11=new dht11($_GET['temperature'],$_GET['humidity']);
}


?>

Well done & congradulations on getting it working.