I was using Blynk.cc for many years. It is still the best (for me) in IoT world. After a while, they changed all system. Upgraded, got new fascinating specs...
...and payment.
If you are living in middle east country, It is really hard to pay money in dollars for your hobbies. Such a shame... 1 USD = 14TL
This story brings me here. I use simple get method to call link on ESP8266 to get and post data.
It is not super fast( pretty slow), not super easy to use but almost free to log data. You can use free hosting services like 000webhost.com. They provide MySQL, PHP, ftp services and something more.
Also you can control pins, read sensors etc.
ESP Board makes web request with GET method. After successful request, board makes new request and pulls body data from PHP file. That data comes from MySQL.
I am updating interface with Bootstrap 5
Just by dividing values into the separate information with substring function. After substring, we can process this info in if else conditions.
This project is still processing. I will add some features for me so I am waiting for your comments
To Do List- Translate filenames to English - Done
- Improve code quality - Still continue
- mysqli_real_escape_string - Done
- Add device name and filter device option - Done
- Data page numbers - Not started yet
- Sleep Mode - Done (Don't forget to connect rst and D0)
- Excel export - Done (I used php5 code and converted It to php 7 with mysqli)
- Logging attemp count - Done
- Google Chart - Done
- Fancy Notifications - Done
- Auto Refresh - Done
- ASC / DESC Data order - Done
- Save the World
-Make a video instructions - Done
PHP Files - Processing...
This is espget.php file to get information from board.
<?php
include 'db.php';
if(!empty($_GET['temp']) && !empty($_GET['hum']) && !empty($_GET['probe']) && !empty($_GET['charge']) && !empty($_GET['device']))
{
$temp = mysqli_real_escape_string($conn, $_GET['temp']);
$hum = mysqli_real_escape_string($conn, $_GET['hum']);
$probe = mysqli_real_escape_string($conn, $_GET['probe']);
$charge = mysqli_real_escape_string($conn, $_GET['charge']);
$device = mysqli_real_escape_string($conn, $_GET['device']);
$sql = "INSERT INTO espdata (temp, hum, probe, charge, device)
VALUES ('".$temp."', '".$hum."', '".$probe."', '".$charge."', '".$device."')";
if ($conn->query($sql) === TRUE) {
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
$sql = "SELECT * FROM espset WHERE id='1'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo $row["slp"];
echo $row["pin1"];
echo $row["pin2"];
echo $row["pin3"];
echo $row["interv"];
}
} else {
echo "0 results";
}
$conn->close();
?>
This is index.php file to show data and control board
<?php include "db.php";
require "logincheck.php";
if ($_SESSION["active"])
{
if (isset($_SESSION["device"]))
{
$device = $_SESSION["device"];
}
else
{
$device = 1;
}
if (isset($_SESSION["records"]))
{
$records = $_SESSION["records"];
}
else
{
$records = 20;
}
if (isset($_SESSION["ascdesc"]))
{
$ascdesc = $_SESSION["ascdesc"];
}
else
{
$ascdesc = "ASC";
}
//------------------------------------------
$sql = "SELECT * FROM espset WHERE id='1'";
$result = $conn->query($sql);
if ($result->num_rows > 0)
{
while ($row = $result->fetch_assoc())
{
$pin1Stat = $row["pin1"];
$pin2Stat = $row["pin2"];
$pin3Stat = $row["pin3"];
$pageint = $row["interv"];
if ($row["slp"] == 1)
{
$sleepStat = "Enabled";
}
else
{
$sleepStat = "Disabled";
}
}
}
else
{
echo "0 results";
}
//---------------------------------//
?>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.1/dist/css/bootstrap.min.css">
<script src="https://cdn.jsdelivr.net/npm/jquery@3.6.0/dist/jquery.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.1/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>
<div class="container mt-3">
<div class='alert alert-success alert-dismissible sticky-top'><span id="timer"></span> -
<a href="index.php" style="text-decoration:none" aria-label="close">Refresh Now</a>
<a href="" class="close" data-dismiss='alert' aria-label="close">×</a>
</div>
<h1><a href="index.php" style="text-decoration:none">ESP Data Logger</h1></a>
<div id="selects" class="collapse">
<form action="index.php" method="post" autocomplete="off" >
<div class="input-group mb-3">
<div class="input-group-prepend">
<select class="custom-select" id="pin" name="pin">
<option selected>Select Pin</option>
<option value="pin1">Pin 1</option>
<option value="pin2">Pin 2</option>
<option value="pin3">Pin 3</option>
</select>
</div>
<select class="custom-select" id="val" name="val">
<option selected>Choose State</option>
<option value="1">On</option>
<option value="0">Off</option>
</select>
<div class="input-group-append">
<button class="btn btn-outline-secondary" type="submit" name="submit">Update</button>
</div>
</div>
<div class='alert alert-success alert-dismissible'>
<strong>Pin 1</strong> State: <?php echo $pin1Stat; ?> <br>
<strong>Pin 2</strong> State: <?php echo $pin2Stat; ?> <br>
<strong>Pin 3</strong> State: <?php echo $pin3Stat; ?> <br>
</div>
<br>
</div>
<div id="settings" class="collapse">
<div class="input-group mb-3">
<select class="custom-select" id="device" name="device">
<option value="" selected disabled hidden>Device <?php echo $device; ?></option>
<option value="1">Device 1</option>
<option value="2">Device 2</option>
</select>
<div class="input-group-append">
<button class="btn btn-outline-secondary" type="submit" name="submit5">Update</button>
</div>
</div>
<div class="input-group mb-3">
<select class="custom-select" id="int" name="int">
<option value="" selected disabled hidden><?php echo $pageint; ?> Min. Refresh</option>
<option value="1">1 Min</option>
<option value="2">2 Min</option>
<option value="5">5 Min</option>
<option value="10">10 Min</option>
<option value="20">20 Min</option>
</select>
<div class="input-group-append">
<button class="btn btn-outline-secondary" type="submit" name="submit2">Update</button>
</div>
</div>
<div class="input-group mb-3">
<select class="custom-select" id="slp" name="slp">
<option value="" selected disabled hidden>Sleep <?php echo $sleepStat; ?></option>
<option value="1">Sleep Enable</option>
<option value="0">Sleep Disable</option>
</select>
<div class="input-group-append">
<button class="btn btn-outline-secondary" type="submit" name="submit4">Update</button>
</div>
</div>
<div class="input-group mb-3">
<select class="custom-select" name="records" id="records">
<option value="" selected disabled hidden><?php echo $records; ?> Records</option>
<option value="20">20 Records</option>
<option value="50">50 Records</option>
<option value="100">100 Records</option>
<option value="150">150 Records</option>
<option value="200">200 Records</option>
</select>
<div class="input-group-append">
<button class="btn btn-outline-secondary" type="submit" name="submit6">Update</button>
</div>
</div>
<div class="input-group mb-3">
<select class="custom-select" name="ascdesc" id="ascdesc">
<option value="" selected disabled hidden><?php echo $ascdesc; ?></option>
<option value="ASC">Ascending</option>
<option value="DESC">Descending</option>
</select>
<div class="input-group-append">
<button class="btn btn-outline-secondary" type="submit" name="submit7">Update</button>
</div>
</div>
</div>
<button type="button" class="btn btn-outline-primary" data-toggle="collapse" data-target="#selects">Pin Controls</button> |
<button type="button" class="btn btn-outline-info " data-toggle="collapse" data-target="#settings">Settings</button> |
<a href="chart.php" class="btn btn-outline-info" role="button" >Chart</a> |
<input type="submit" onclick="return confirm('Are your sure?')" class="btn btn-outline-warning " name="submit3" value="Clear data" /> |
<a href="excel.php?device=<?php echo $device; ?>" class="btn btn-outline-warning " role="button"> Download Data</a> |
<a href="logout.php" onclick="return confirm('Are your sure?')" class="btn btn-outline-danger " role="button"> Logout</a>
</form>
<br><br>
<?php
if (isset($_POST["submit"]))
{
$pin = addslashes($_POST["pin"]);
$val = addslashes($_POST["val"]);
$sql = "UPDATE espset SET $pin='$val' WHERE id=1";
if ($conn->query($sql) === true)
{
echo "<div class='alert alert-success alert-dismissible'>";
echo "<a href='index.php' class='close' aria-label='close'>×</a>";
echo "<strong>Success!</strong> " . $pin . " successfully updated to " . $val . " </div>";
}
else
{
echo "Error updating record: " . $conn->error;
}
}
//---------------------------------------------------------------------
if (isset($_POST["submit2"]))
{
$interv = addslashes($_POST["int"]);
$sql = "UPDATE espset SET interv='$interv' WHERE id=1";
if ($conn->query($sql) === true)
{
echo "<div class='alert alert-success alert-dismissible'>";
echo "<a href='index.php' class='close' aria-label='close'>×</a>";
echo "<strong>Success! </strong>Interval updated successfully to " . $interv . " minutes </div>";
}
else
{
echo "Error updating record: " . $conn->error;
}
}
//---------------------------------------------------------------------
if (isset($_POST["submit3"]))
{
//SQL Command
$sql_command = "TRUNCATE TABLE espdata";
if (mysqli_query($conn, $sql_command))
{
echo "<div class='alert alert-success alert-dismissible'>";
echo "<a href='index.php' class='close' data-dismiss='alert' aria-label='close'>×</a>";
echo "<strong>Success!</strong> All data successfully deleted. </div>";
}
else
{
echo "SQL Error " . mysqli_error($connection);
}
}
//-----------------------------------------
if (isset($_POST["submit4"]))
{
$slp = addslashes($_POST["slp"]);
$sql = "UPDATE espset SET slp='$slp' WHERE id=1";
if ($conn->query($sql) === true)
{
echo "<div class='alert alert-success alert-dismissible'>";
echo "<a href='index.php' class='close' aria-label='close'>×</a>";
echo "<strong>Success!</strong> Sleep settings successfully updated to " . $sleepStat . "</div>";
}
else
{
echo "Error updating record: " . $conn->error;
}
}
//---------------------------------------------------------------------
if (isset($_POST["submit5"]))
{
$device = addslashes($_POST["device"]);
$_SESSION["device"] = $device;
if (($_SESSION["device"]) == $device)
{
echo "<div class='alert alert-success alert-dismissible'>";
echo "<a href='index.php' class='close' aria-label='close'>×</a>";
echo "<strong>Success!</strong> Device settings successfully updated to " . $device . "</div>";
}
else
{
echo "Error";
}
}
//---------------------------------------------------------------------
if (isset($_POST["submit6"]))
{
$records = addslashes($_POST["records"]);
$_SESSION["records"] = $records;
if (($_SESSION["records"]) == $records)
{
echo "<div class='alert alert-success alert-dismissible'>";
echo "<a href='index.php' class='close' aria-label='close'>×</a>";
echo "<strong>Success!</strong> Record settings successfully updated to " . $records . "</div>";
}
else
{
echo "Error";
}
}
//---------------------------------------------------------------------
if (isset($_POST["submit7"]))
{
$ascdesc = addslashes($_POST["ascdesc"]);
$_SESSION["ascdesc"] = $ascdesc;
if (($_SESSION["ascdesc"]) == $ascdesc)
{
echo "<div class='alert alert-success alert-dismissible'>";
echo "<a href='index.php' class='close' aria-label='close'>×</a>";
echo "<strong>Success!</strong> Data ordering settings successfully updated to " . $ascdesc . "</div>";
}
else
{
echo "Error";
}
}
//---------------------------------------------------------------------
$sql = "SELECT * FROM espdata WHERE device = '$device' ORDER BY id $ascdesc LIMIT $records";
if ($result = mysqli_query($conn, $sql))
{ ?>
<table class="table table-striped">
<thead>
<tr>
<th>ID</th>
<th>Temperature</th>
<th>Humidity</th>
<th>Probe</th>
<th>Charge</th>
<th>Device ID</th>
<th>Time</th>
</tr>
</thead>
<tbody>
<?php
//while ($row = mysqli_fetch_row($result))
while ($row = $result->fetch_assoc())
{
echo "<TR>";
echo "<TD>" . $row["id"] . "</TD>";
echo "<TD>" . $row["temp"] . "</TD>";
echo "<TD>" . $row["hum"] . "</TD>";
echo "<TD>" . $row["probe"] . "</TD>";
echo "<TD>" . $row["charge"] . "</TD>";
echo "<TD>" . $row["device"] . "</TD>";
echo "<TD>" . $row["TimeStamp"] . "</TD>";
echo "</TR>";
}
echo "</TABLE>";
mysqli_free_result($result);
}
mysqli_close($conn);
?>
<script type='text/javascript'>
title = "ESP Data Logger - #<?php echo $pageint; ?> min auto refresh - ";
position = 0;
function scrolltitle() {
document.title = title.substring(position, title.length) + title.substring(0, position);
position++;
if (position > title.length) position = 0;
titleScroll = window.setTimeout(scrolltitle,200);
}
scrolltitle();
function checklength(i) {
'use strict';
if (i < 10) {
i = "0" + i;
}
return i;
}
var minutes, seconds, count, counter, timer;
count = <?php echo $pageint * 60; ?>; //seconds
counter = setInterval(timer, 1000);
function timer() {
'use strict';
count = count - 1;
minutes = checklength(Math.floor(count / 60));
seconds = checklength(count - minutes * 60);
if (count < 0) {
clearInterval(counter);
return;
}
document.getElementById("timer").innerHTML = 'Next refresh in ' + minutes + ':' + seconds + ' ';
if (count === 0) {
location.reload();
}
}
</script>
</body>
</html>
<?php
} ?>
login page
login.php
<?php
session_start();
include "db.php";
?>
<html>
<head>
<title>ESP Mobile Data Station</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.1/dist/css/bootstrap.min.css">
<script src="https://cdn.jsdelivr.net/npm/jquery@3.6.0/dist/jquery.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.1/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>
<div class="container mt-3">
<center>
<p><h1>Login Page</h1><br>
<form action="login.php" method="post" autocomplete="off">
<input type="text" name="uid" class="form-control" required="required" placeholder="User" value=""><br><br>
<input type="password" name="pass" class="form-control" placeholder="Pass" value=""><br><br>
<input type="submit" class="btn btn-outline-success btn-lg" name="submit" value="Login" />
</form><br>
<?php if (isset($_POST["submit"])) {
$uid = mysqli_real_escape_string($conn, $_POST["uid"]);
$passa = mysqli_real_escape_string($conn, $_POST["pass"]);
$pass = md5($passa);
$sql = "SELECT * FROM user_data WHERE uid='$uid'";
if ($result = mysqli_query($conn, $sql)) {
while ($row = mysqli_fetch_row($result)) {
$count = $row[3];
$idx = $row[1];
$passx = $row[2];
if ($idx == $uid && $passx == $pass && $count < 3) {
$count = 0;
$logged = 1;
} else {
$logged = 0;
$count++;
}
//------------------------------------------------
$sql = "UPDATE user_data SET attempCount='$count' WHERE uid='$uid'";
if ($conn->query($sql) === true) {
} else {
echo "Error updating record: " . $conn->error;
}
//----------------------------
}
mysqli_free_result($result);
}
mysqli_close($conn);
if ($logged == 1) {
$_SESSION["active"] = $pass;
echo "<div class='alert alert-success alert-dismissible'>";
echo "<strong>Success!</strong> You are logged in. Redirecting... </div>";
echo "<meta http-equiv='refresh' content='2;url=index.php'>";
} else {
$remain = 3-$count;
echo "<div class='alert alert-danger alert-dismissible'>";
echo "<a href='login.php' class='close' data-dismiss='alert' aria-label='close'>×</a>";
echo "Username or password is wrong<br>";
if ($remain > 0){
echo "<strong>Warning!</strong> You have ".$remain." attemps</div>";
} else {
echo "Account suspended<br>";
}
}
}
?>
excel export page. excel.php
<?php
include 'db.php';
$device = mysqli_real_escape_string($conn, $_GET['device']);
$table_name = "espdata"; //MySQL Table Name
$date = date("Y.m.d.h.i.sa"); //File Name
$filename = $date."Device_".$device;
//create MySQL connection
$sql = "SELECT * FROM espdata WHERE device = '$device'";
$result = $conn->query($sql);
$file_ending = "xls";
//header info for browser
header("Content-Type: application/xls");
header("Content-Disposition: attachment; filename=$filename.xls");
header("Pragma: no-cache");
header("Expires: 0");
/*******Start of Formatting for Excel*******/
$sep = "\t"; //tabbed character
for ($i = 0; $i < mysqli_num_fields($result); $i++) {
$finfo = mysqli_fetch_field($result);
echo $finfo->name . "\t";
}
print("\n");
//end of printing column names
//start while loop to get data
while($row = mysqli_fetch_row($result))
{
$schema_insert = "";
for($j=0; $j<mysqli_num_fields($result);$j++)
{
if(!isset($row[$j]))
$schema_insert .= "NULL".$sep;
elseif ($row[$j] != "")
$schema_insert .= "$row[$j]".$sep;
else
$schema_insert .= "".$sep;
}
$schema_insert = str_replace($sep."$", "", $schema_insert);
$schema_insert = preg_replace("/\r\n|\n\r|\n|\r/", " ", $schema_insert);
$schema_insert .= "\t";
print(trim($schema_insert));
print "\n";
}
?>
Logout page logout.php
<?php
session_start();
unset($_SESSION["active"]);
header("Location:login.php");
?>
<?php
session_start();
if($_SESSION["active"])
{
}
else
{
echo"<meta http-equiv='refresh' content='0;url=login.php'>";
}
?>
<?php
$servername = "localhost";
$username = "...";
$password = "....";
$dbname = "...";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Database Connection failed: " . $conn->connect_error);
}
?>
You
can use this PHP file to set up your database or you can do it manually.
<html>
<head>
<title>ESP Data Logger - Setup</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>
<div class="container mt-3">
<?php
if (isset($_POST["submit"]))
{
include 'db.php';
echo "<h1>Setup started...</h1><br><hr>";
$sql = "CREATE TABLE espdata (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
temp FLOAT(5),
hum FLOAT(5),
probe FLOAT(5),
charge INT(5),
device INT(5),
`TimeStamp` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
)";
if ($conn->query($sql) === true)
{
echo "<h1>Data tables are ready</h1>";
}
else
{
echo "Error creating table: " . $conn->error;
}
echo "<br><hr><br>";
$sql = "CREATE TABLE espset (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
pin1 INT(3),
pin2 INT(5),
pin3 INT(5),
interv INT(5),
`TimeStamp` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
)";
if ($conn->query($sql) === true)
{
echo "<h1>Setting tables are ready</h1>";
}
else
{
echo "Error creating table: " . $conn->error;
}
echo "<br><hr><br>";
$sql = "CREATE TABLE user_data (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
uid VARCHAR(30),
pass VARCHAR(50)
)";
if ($conn->query($sql) === true)
{
echo "<h1>User tables are ready</h1>";
}
else
{
echo "Error creating table: " . $conn->error;
}
echo "<br><hr><br>";
$uid = mysqli_real_escape_string($conn, $_POST["uid"]);
$passa = mysqli_real_escape_string($conn, $_POST["pass"]);
$pass = md5($passa);
$sql = "INSERT INTO user_data (uid, pass)
VALUES ('" . $uid . "', '" . $pass . "')";
if ($conn->query($sql) === true)
{
echo "<b>Username: $uid <br><br>";
echo "Password: $passa</b><br><hr>";
}
else
{
echo "Error updating record: " . $conn->error;
}
$sql = "INSERT INTO espset (pin1, pin2, pin3, interv)
VALUES ('0', '0', '0', '1')";
if ($conn->query($sql) === true)
{
echo "<b>Pin settings are done.<br><br>";
}
else
{
echo "Error updating record: " . $conn->error;
}
$conn->close();
?>
<a href="index.php" onclick="return confirm('Rename or delete this file')" class="btn btn-success btn-sm" role="button"> Main Page</a><br><br>
<?php
}
else
{
?>
<div class="container mt-3">
<h1> Setup Database </h1>
<form action="setup.php" method="post" autocomplete="off">
<input type="text" name="uid" class="form-control" placeholder="Username" value=""><br><br>
<input type="text" name="pass" class="form-control" placeholder="Password" value=""><br><br>
<input type="submit" class="btn btn-success btn-lg" name="submit" value="Setup" />
</form>
<?php
} ?>
Comments