---
title: "Arduino i MySQL"
url: "https://maker.wiznet.io/jaden/projects/arduino-i-mysql/"
markdown_url: "https://maker.wiznet.io/jaden/projects/arduino-i-mysql/md"
type: "UCC: User Created Content"
author: "Jack"
author_url: "https://homeguard24.pl/arduino-i-mysql/"
editor: "WIZnet"
editor_url: "https://maker.wiznet.io/"
original_author: "Jack"
original_url: "https://homeguard24.pl/arduino-i-mysql/"
published: "2022-11-01"
language: "en"
hardware: ["Arduino UNO", "Arduino Ethernet Shield", "ShenzenAV HC-SR501 PIR motion detector"]
likes: 3
views: 1033
comments: 0
source: "WIZnet Makers (https://maker.wiznet.io/)"
---

# Arduino i MySQL

> upload data to DB(MySQL)

Original author: Jack (source: https://homeguard24.pl/arduino-i-mysql/)

## Components

- **Arduino UNO** x 1 ([docs](https://www.farnell.com/datasheets/1682209.pdf))
- **Arduino Ethernet Shield** x 1 ([docs](https://docs.arduino.cc/retired/shields/arduino-ethernet-shield-without-poe-module))
- **ShenzenAV HC-SR501 PIR motion detector** x 1

## Article

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)

![](https://maker.wiznet.io/upload/ckeditor5/1010957345%5F1667330646%2Epng)

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 &amp;lt;SPI.h&amp;gt; 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&gt;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](https://homeguard24.pl/wp-content/uploads/2015/08/uk%C5%82ad-1024x613.png)](https://homeguard24.pl/wp-content/uploads/2015/08/uk%C5%82ad.png)

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 | &lt;?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; } ?&gt; |
| --- | --- |

![](https://maker.wiznet.io/upload/ckeditor5/151033983%5F1667403388%2Epng)

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.

---

Source: https://maker.wiznet.io/jaden/projects/arduino-i-mysql/
