Skip to content

Arduino i MySQL

upload data to DB(MySQL)

jaden

Published November 01, 2022

Original author: Jack · PolandOriginal source (new tab)

Arduino i MySQL

Components

Hardware components

x 1


Project description

 

In this project,author show to upload the read data to DB and then display it in a website.

He used PIR motion sensor(HC-SR501)

 

 

Arduino i MySQL
  1. Arduino monitors the state of the pin to which the motion sensor is connected, the sensor works digitally, so it sends the status 0 or 1.
  2. If motion is detected, pin 6 displays high or "1". This is a prerequisite for establishing connection with the server.
  3. If the status on pin 6 is low - we do not connect to the server. Thanks to this, we limit the bandwidth and the amount of data sent.
  4. Arduino, after establishing a connection with the server, runs the PHP script and passes a variable informing about the detection of motion.
  5. The script takes a variable and saves it to the database. In addition, the CURRENT_TIMESTAMP  function was used in the MySQL database  , which automatically adds the date and time of the measurement.
  6. wait a few seconds not to enter the same detection into the database.

 

Arduino code

#include <SPI.h>
 
byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x01 };
EthernetClient client;
 
int czujnik =6;
int val = 0;
 
String conf = "tak";
String data;
 
void setup() {
pinMode(6, INPUT);
pinMode(7, OUTPUT);
Serial.begin(9600);
 
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
}
data = "";
}
 
void loop(){
digitalRead(czujnik);
val=digitalRead(czujnik);
Serial.println(val);
if (digitalRead(czujnik)==1)
{
digitalWrite(7, HIGH);
}
else
{
digitalWrite(7, LOW);
}
data = "ruch1"+ conf;
if(val>0){
if (client.connect("www.xxxxx.xxx",80)) { // REPLACE WITH YOUR SERVER ADDRESS
client.println("GET add2.php HTTP/1.1");
client.println("Host: xxx.xxx.xxx"); // SERVER ADDRESS HERE TOO
client.println("Content-Type: application/x-www-form-urlencoded");
client.print("Content-Length: ");
client.println(data.length());
client.println();
client.print(data);
}
if (client.connected()) {
client.stop(); // DISCONNECT FROM THE SERVER
}
delay(3000); // WAIT FIVE MINUTES BEFORE SENDING AGAIN
}
else{
Serial.println("brak danych do wyslania");
delay(300);
}
}

System diagram:

system

LED is just check motion sensor work well.

and he created DB and table to store measurements.

this is PHP script to server. 

write the address, username, and password according to the DB information

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php
function Connection(){
$server="localhost";
$user="xxxxxxxxxxx";
$pass="xxxxxxxxxxx";
$db="xxxxxxxxxxxxx";
$connection = mysql_connect($server, $user, $pass);
if (!$connection) {
die('MySQL ERROR: ' . mysql_error());
}
mysql_select_db($db) or die( 'MySQL ERROR: '. mysql_error() );
return $connection;
}
?>
Arduino i MySQL

user can entering servername / index.php path and check motion detected time.

This UCC used DB and can show data in web.

So I thought this UCC was a special, interesting UCC.

Comments

Comments