Sending sensor data to localhost using IoT development board esp8266-12

Sending  sensor data to localhost  using IoT development board esp8266-12

In this article, I am going to show you how to send sensor sensor data to a local webserver using esp8266.

Before delving into this article , let me explain the term localhost. Localhost refers to the local computer that a program is running on. For example, if you are running a Web browser on your computer, your computer is considered to be the "localhost".

The deliverables for this project are:

Xamp webserver download it here

Arduino IDE download here

ESP8266 official website

Smartphone used as a router.

Project requirement: Create a folder in the xamp/htdocs named esp8266. In this folder you are going to save the postData.php and databaseConfig.php

To send the sensor data to a database, we need to write an arduino sketch using a TCP protocol to communicate wirelessly to the local webserver.

Let's get it done now!

Step 1: Install esp8266 board the link to the procedure can be found here

Step 2: The arduino sketch is shown below. I create two function connectWifi() and sendSensorData() which I invoke respectively in the void setup and void loop. If you are confused please look for a tutorial on basic arduino code.

#include <WiFiClient.h> 
#include <ESP8266WebServer.h>
#include <ESP8266HTTPClient.h>
const char *ssid = "MelleB";  //ENTER YOUR WIFI ssid
const char *password = "refj4497";  //ENTER YOUR WIFI password

void setup() {

void loop() {
//function to connect to wifi
void connectWifi(){
  WiFi.mode(WIFI_OFF);        //Prevents reconnection issue (taking too long to connect)
  WiFi.mode(WIFI_STA);        //This line hides the viewing of ESP as wifi hotspot
  WiFi.begin(ssid, password);     //Connect to your WiFi router
  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
  //If connection successful show IP address in serial monitor 
  Serial.print("Connected to ");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());  //IP address assigned to your ESP

//function to send sensor data 
void SendSensorData() {
  HTTPClient http;    //Declare object of class HTTPClient
  String sensorData1,sensorData2,sensorData3,sensorData4,sensorData5,sensorData6,sensorData7, postData;
  //Post Data
  postData = "sensor1=" +  sensorData1 + "&sensor2=" + sensorData2+ "&sensor3=" + sensorData3+  "&sensor4=" + sensorData4+ "&sensor5=" + sensorData5+ "&sensor6=" + sensorData6+  "&sensor7=" + sensorData7;
  http.begin("");              //change the ip to your computer ip address
  http.addHeader("Content-Type", "application/x-www-form-urlencoded");    //Specify content-type header
  int httpCode = http.POST(postData);   //Send the request
  String payload = http.getString();    //Get the response payload
  Serial.println(httpCode);   //Print HTTP return code
  Serial.println(payload);    //Print request response payload
  http.end();  //Close connection
  delay(5000);  //Post Data at every 5 seconds

Step 3: Create the database sensor and the table name logs and save it in the esp866 folder as databaseConfig.php

//connect to localhost if not exists
    $servername = "localhost";
    $username = "root";
    $password = "";

    // Create connection
    $conn = new mysqli($servername, $username, $password);
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);

    // Create database
    $sql = "CREATE DATABASE sensor";

        echo "Database created successfully";
    } else {
        echo "Error creating database: " . $conn->error;


    echo "<br>";
//Connect to database and create table
    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "sensor";

    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);

    $sql = "CREATE TABLE logs (
    sensor1 VARCHAR(30),
    sensor2 VARCHAR(30),
    sensor3 VARCHAR(30),
    sensor4 VARCHAR(50),
    sensor5 VARCHAR(50),
    sensor6 VARCHAR(50),
    sensor7 VARCHAR(50),
    \`Date\` DATE NULL,
    \`Time\` TIME NULL, 

    if ($conn->query($sql) === TRUE) {
        echo "Table logs created successfully";
    } else {
        echo "Error creating table: " . $conn->error;


Step 4: Create postData.php in the esp866 folder and paste the code below in it.

//Creates new record as per request
    //Connect to database
    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "sensor";

    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
        die("Database Connection failed: " . $conn->connect_error);

    //Get current date and time
    $d = date("Y-m-d");
    //echo " Date:".$d."<BR>";
    $t = date("H:i:s");

    if(!empty($\_POST\['sensor1'\]) || !empty($\_POST\['sensor2'\]))
        $sensorData1 = $_POST\['sensor1'\];
        $sensorData2 = $_POST\['sensor2'\];
        $sensorData3 = $_POST\['sensor3'\];
        $sensorData4 = $_POST\['sensor4'\];
        $sensorData5 = $_POST\['sensor5'\];
        $sensorData6 = $_POST\['sensor6'\];
        $sensorData7 = $_POST\['sensor7'\];

        $sql = "INSERT INTO logs (sensor1, sensor2,sensor3,sensor4,sensor5,sensor6,sensor7, Date, Time) VALUES ('".$sensorData1."', '".$sensorData2."', '".$sensorData3."', '".$sensorData4."', '".$sensorData5."', '".$sensorData6."', '".$sensorData7."', '".$d."', '".$t."')";

        if ($conn->query($sql) === TRUE) {
            echo "OK";
        } else {
            echo "Error: " . $sql . "<br>" . $conn->error;


Step 5: software testing

  • Open your web browser and type localhost/esp8266. Click on databaseConfig.php you should get a message data base sensor created successfully and do the same with postData.php. If you do not get any error message then you are good to go.
  • Open the serial monitor of your arduino and set the baud rate to 115200
  • The wifi ssid and ip address should display to the serial monitor
  • You should get a response 200 from the server and ok message
  • Check your database table you will get all the sensor table high, the date and time.
  • ** The output is shown below**

Please let me know in case of any errors.

Angular 9 Tutorial: Learn to Build a CRUD Angular App Quickly

What's new in Bootstrap 5 and when Bootstrap 5 release date?

Brave, Chrome, Firefox, Opera or Edge: Which is Better and Faster?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

PHP / MySQL - Count how many table in database contain Specific name

I'm building an admin to control an interactive game, and I'm using PHP/MySql to store all the data for all the rounds. Each of the tables that I want to load into the game via PHP are titled "Puzzle*" with the * being an incremental index number. There are a few other tables in my database for global game data and bonus rounds.

PhP MySQL Projects

Simpliv LLC, a platform for learning and teaching online courses. We basically focus on online learning which helps to learn business concepts, software technology to develop personal and professional goals through video library by recognized industry experts and trainers.

Inserting session array into mysql database

I am still learning bits of PHP and have been stuck on this issue regarding the POST data from radio buttons within a form being inserted into a mysql database from a session array. Currently, I have 2 questions within my survey and they consist of radio button options for a user to select from. The relevant answers can be selected and they are stored in a session array: