arduino code
- CODE: SELECT_ALL_CODE
#include <CytronWiFiShield.h>
#include <CytronWiFiClient.h>
#include <CytronWiFiServer.h>
#include <SoftwareSerial.h>
#define trigPin 13
#define echoPin 12
ESP8266Client client;
//connection to wifi
const char *ssid = "iPhone";
const char *pass = "kasipecah10";
IPAddress ip(172, 20, 10 , 1);
//ultrasonic sensor
void sensorDetection()
{
long duration, distance;
digitalWrite(trigPin, LOW); // Added this line
delayMicroseconds(2); // Added this line
digitalWrite(trigPin, HIGH);
delayMicroseconds(10); // Added this line
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration / 2) / 29.1;
if ((distance <= 7))
{
saveDb();
Serial.print(distance);
Serial.println(" cm");
delay(5000);
}
else
{
Serial.println ("range is to far, try again with short range");
}
delay(1000);
}
void setup() {
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
if (!wifi.begin(2, 3))
{
Serial.println(F("Error talking to shield"));
while (1);
}
Serial.print(F("Mode: "));
Serial.println(wifi.getMode()); // 1- station mode, 2- softap mode, 3- both
wifi.config(ip);
Serial.println(F("Start wifi connection"));
if (!wifi.connectAP(ssid, pass))
{
Serial.println(F("Error connecting to WiFi"));
while (1);
}
Serial.print(F("Connected to ")); Serial.println(wifi.SSID());
Serial.println(F("IP address: "));
Serial.println(wifi.localIP());
wifi.updateStatus();
Serial.println(wifi.status()); //2- wifi connected with ip, 3- got connection with servers or clients, 4- disconnect with clients or servers, 5- no wifi
sensorDetection();
}
void loop()
{
sensorDetection();
}
//connection and save to database
char server[] = "172.20.10.10"; // IMPORTANT: If you are using XAMPP you will have to find out the IP address of your computer and put it here (it is explained in previous article). If you have a web page, enter its address (ie. "www.yourwebpage.com")
void saveDb()
{
// Connect to the server (your computer or web page)
if (client.connect(server, 80))
{
Serial.print("You Have 1 Mail!\n");
client.print("GET /write_data.php?"); // This
client.print("value="); // This
client.println(" HTTP/1.1"); // Part of the GET request
client.println("Host: 172.20.10.10"); // IMPORTANT: If you are using XAMPP you will have to find out the IP address of your computer and put it here (it is explained in previous article). If you have a web page, enter its address (ie.Host: "www.yourwebpage.com")
client.println("Connection: close"); // Part of the GET request telling the server that we are over transmitting the message
client.stop(); // Closing connection to server
}
else
{
// If Arduino can't connect to the server (your computer or web page)
Serial.println("--> connection failed\n");
}
}
php code
- CODE: SELECT_ALL_CODE
<?php
// Prepare variables for database connection
$dbusername = "root"; // enter database username, I used "arduino" in step 2.2
$dbpassword = ""; // enter database password, I used "arduinotest" in step 2.2
$servera = "localhost"; // IMPORTANT: if you are using XAMPP enter "localhost", but if you have an online website enter its address, ie."www.yourwebsite.com"
// Connect to your database
$dbconnect = mysql_pconnect($server, $dbusername, $dbpassword);
$dbselect = mysql_select_db("test",$dbconnect);
// Prepare the SQL statement
$sql = "INSERT INTO history (value) VALUES ('".$_GET["value"]."')";
// Execute SQL statement
mysql_query($sql);
?>