Wednesday, 21 September 2016

Sending the sensor data from NodeMcu to the Remote Database

little NodeMcu board is packed with so much fun.. I recently done project which reads data from Three sensors and sends them via post request to the webpage. a simple php script  which reads the variables sent via post request and stores them in mySql data base. it is more like my Previous project
raspberry pi Local Server but here we are actually posting the data to the web.

In NodeMcu part :

to connect the nodemcu to the wifi we use

    wifi.setmode(wifi.STATION)
    wifi.sta.config(_ssid,_pswd)
    wifi.sta.connect()


and to send the data to database

http.request("http://pamda1ver2.16mb.com/sensorData.php", "POST", "Content-Type: application/x-www-form-urlencoded\r\n",data,
  function(code, data)
    if (code < 0) then
      print("HTTP request failed")
    else
      print(code, data)
    end
  end)

In server side :

//Create Connection
$conn = new mysqli($serverName,$userName,$password,$db);
if($conn ->connect_error){
 die("Connection Failed: ".$conn->connect_error);
 }
echo "Connected successfully";

$sql = "INSERT INTO sensorData(time, ldr, humi,temp,mois)
VALUES ( date('Y/m/d H:i:s'),$ldr,$humi,$temp,$mois)";

is used to read the data and store them in mySQl database


and

//Create Connection
$conn = new mysqli($serverName,$userName,$password,$db);
if($conn ->connect_error){
 die("Connection Failed: ".$conn->connect_error);
 }
echo "Connected successfully";
$retval = mysqli_query($conn,"SELECT * FROM sensorData");

if(!$retval){
 die('Could not get Data : '.mysqli_error());
}

is used to read the values from database and display

you can see the result http://pamda1ver2.16mb.com/padma.php

and get the all source code from https://github.com/anilkunchalaece/lLabBasic


No comments:

Post a Comment