So I've got my Yun and now i'm trying to send sensor data to a MySQL database on an external server.
I have the database set up but as somewhat of a novice I need help with how I can insert the data to a MySQL database preferably using PHP. Whether to have the processing done by a script on the linino or server side I'm open to advice.
I know how to send sensor information to serial and to a local webpage (web server example) and I can submit to the database from a html and php form but need help on this next step.
Start writing a PHP script that you can invoke from the command line and that can connect and insert data into your mysql database. This is a pure PHP question, so this forum is not the best place for finding answers.
Once you have it, get back here if you need support in using Process for calling your script from the yun
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;
Are the first 3 blocks of code just for setting up the database and installing PHP on the Linux or do they form part of some code I have to use constantly?
Is the. 4th section the file called db.php?
Sorry for being such a n00b I'm just trying to work out where to begin with this.
mortonc:
Are the first 3 blocks of code just for setting up the database and installing PHP on the Linux or do they form part of some code I have to use constantly?
Hi sonnyyu
Thanks for your help and contribution of your knowledge with us.
I tried the tut but i get no values into the db. I was not able to send some testdata (/mnt/sda1/db.php 80)
What make me suspitios is if i check the SD card there is the db.php not in the www-folder. Should it not be into the www folder to process php?
Sorry for boudering you with my issues but the data transfair to a external mysql db is essential to my project and examples to do such tasks are extremely valuable.
I was able to write to the database from the Yun via Putty by invoking "php-cli /mnt/sda1/db.php 856"
root@Arduino:~# php-cli /mnt/sda1/db.php 856
but wasn't able to get the Yun script to do it itself without a few code changes:
added #include <Bridge.h>
and changed the p.begin and p.addParameter strings to mimic what worked at the command prompt.
#include <Process.h>
#include <Bridge.h>
int temperature;
void setup() {
Bridge.begin(); // Initialize Bridge
}
void loop() {
int temperature = random(0, 100);
Process p;
p.begin("php-cli");
p.addParameter("/mnt/sda1/db.php");
p.addParameter(String(temperature));
p.run();
delay(5000);
}
I don't know if all the changes were needed, but it works for me.
Other than that, I followed your directions verbatim.
Thanks again!
OP has solved this question himself by removing an existing cygwin install and mysql install and then subsequently reinstalling the mysql and cygwin again.