Php communication with mysql: 3 ways
- MySQL shared module
- MySQL Improved Extension
- PHP Data Objects plus PDO driver for MySQL
Arduino communication with php: 2 ways
- HttpClient->uhttpd->php-cgi
- Process->php-cli
The total method send Yun sensor data to mySql on external server via php is 6 ways.
Sample code (Process->php-cli+MySQL Improved Extension)
At Mysql console:
mysql> CREATE DATABASE sensors;
mysql> USE sensors;
mysql> CREATE TABLE sensor_data (
-> id INT NOT NULL AUTO_INCREMENT,
-> temperature INT,
-> insert_date TIMESTAMP,
-> PRIMARY KEY (id)
-> );
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'password';
mysql> FLUSH PRIVILEGES;
mysql> INSERT INTO sensor_data (temperature) VALUES(20);
mysql> select * from sensor_data;
opkg update
opkg install php5-mod-mysqli
opkg install php5-cli
nano /mnt/sda1/db.php
#!/usr/bin/php-cli
<?php
$temperature = $argv[1];
$DBServer = '192.168.0.20';
$DBUser = 'root';
$DBPass = 'password';
$DBName = 'sensors';
$conn = new mysqli($DBServer, $DBUser, $DBPass, $DBName);
// check connection
if ($conn->connect_error) {
trigger_error('Database connection failed: ' . $conn->connect_error, E_USER_ERROR);
}
$sql="INSERT INTO sensor_data (temperature) VALUES ($temperature)";
if($conn->query($sql) === false) {
trigger_error('Wrong SQL: ' . $sql . ' Error: ' . $conn->error, E_USER_ERROR);
}
?>
chmod 755 /mnt/sda1/db.php
Send test insert data:
/mnt/sda1/db.php 80
Arduino code:
#include <Process.h>
int temperature;
void setup() {
Bridge.begin(); // Initialize Bridge
}
void loop() {
int temperature = random(0, 100);
Process p;
p.begin("/mnt/sda1/db.php");
p.addParameter(String(temperature));
p.run();
delay(5000);
}