Priyansh Srivastava
Published © GPL3+

Accessing a Database Using NodeMCU

We are going to see how to access a database created on a local server using NodeMCU!

IntermediateProtip1 hour16,480
Accessing a Database Using NodeMCU

Things used in this project

Hardware components

NodeMCU ESP8266 Breakout Board
NodeMCU ESP8266 Breakout Board
×1

Software apps and online services

Arduino IDE
Arduino IDE
Apache Mynewt OS XAMPP

Story

Read more

Schematics

NO CONNECTIONS REQUIRED.

MAKE SURE YOUR LOCAL SERVER AND NODEMCU ARE ON THE SAME NETWORK.

Code

NODE MCU CODE

Processing
THIS CODE WILL CONNECT NODEMCU TO THE DATABASE
//YOUR NODEMCU AND THE DEVICE ON WHICH THE LOCAL SERVER IS RUNNING MUST BE ON THE //SAME WIFI NETWORK

#include<ESP8266WiFi.h>
const char* ssid="";//enter the name of your wifi
const char* password="";//wifi password
const char* host="192.168.43.11";//LOCAL IPv4 ADDRESS...ON CMD WINDOW TYPE ipconfig/all
const uint16_t port=80;//PORT OF THE LOCAL SERVER
int rank=4;//THE RANK WHOSE DATA YOU WANT TO FETCH
void setup() {

 Serial.begin(115200);
 Serial.println("STARTING CONNECTION TO WIFI");
 WiFi.mode(WIFI_STA);
 WiFi.begin(ssid,password);
 while(WiFi.status()!=WL_CONNECTED)//waiting for device to be connected to the network
 {Serial.print(".");
 delay(500);
}
Serial.println("CONNECTED TO WIFI");
Serial.print("IP:");
Serial.println(WiFi.localIP());
}

void loop(){
  String l="";
  String lengthh="";
  Serial.print("Connecting to: ");
  Serial.print(host);
  Serial.print(":");
  Serial.println(port);
  WiFiClient client;
  if(!client.connect(host,port))//IF THIS OCCURS MAKE THEN FIREWALL IS STOPPING THE CONNECTION OR THE IP ADDRESS/PORT OF THE SERVER IS INCORRECT.
  {Serial.println("CONNECTION FAILED");
  delay(4000);
  return;
  }
  else{
   Serial.println("CONNECTED TO THE SERVER");
   client.print(String("GET ")+"/test2.php?RANK="+rank+" HTTP/1.1\r\n"+"Host:"+host+"\r\n"+"Connection:close\r\n\r\n");
    unsigned long timeout=millis();
    while(client.available()==0)
    { 
      //close the connection if it has been connected for more than 5 seconds
      if(millis()-timeout>5000){
      Serial.println(">>> Client Timeout !");
      client.stop();
      delay(5000);
      return;
      }}
       Serial.println("receiving from remote server");
       
//Start reading the response from the server
  while (client.available()) {
    char ch = static_cast<char>(client.read());
  Serial.print(ch);
  }
  // Close the connection
  Serial.println();
  Serial.println("closing connection");
  client.stop();
  delay(6000);
  }
  }

PHP CODE

PHP
SAVE THE CODE WITH EXTENSION .php in htdocs folder
<?php
$server="localhost";
$username="root";//THE DEFAULT USERNAME OF THE DATABASE
$password="";
$dbname="test2";
$con=mysqli_connect($server,$username,$password,$dbname) or die("unable to connect");
$rank=$_GET["RANK"];
$sql="SELECT NAME from class where RANK=$rank";//WE ARE TRYING TO GET THE NAME OF THE STUDENT BY ENTERING THE RANK
$result=mysqli_query($con,$sql);
if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {    
      echo  $row["NAME"];
}
}
?>

Credits

Priyansh Srivastava

Priyansh Srivastava

2 projects • 12 followers

Comments