Arduino yun --> Mysql

I need help with a project that i am doing, I cant figure out how to connect arduino yun to a database in mysql. My supervisor requested that used python to upload to mysql database, no other methods( PHP..etc) Please advise, a written code will be good. FYI the data i am uploading is temperature moisture and humidity about every 10 mins or so.

When you mention supervisor, in combination with which languages you can and cannot use, this makes me think it's a school assignment. We don't do a student's work for them on this forum: figuring out the details is part of your learning process. But I can give you some hints, and searching for key concepts from those hints should help you along and fill in the details.

Step 1: on the Linux side, using an SSH connection, develop a Python script that can accept a sample of temperature and humidity data as a command line parameter, and which writes that data to your MySQL database. Test it by running it from the command line, and make sure that part works before moving on.

Step 2: write a sketch to collect your data from your sensor(s) and which uses the Bridge Library's Process class to run your Python script from step 1, passing it the sample data.

It's important to get step 1 working first. If you try to go straight to step 2 without proving that step 1 works, you will be making it more difficult on yourself: if it doesn't work, is the problem in the sketch or the Linux side? If you already know that the Linux side is working, then you can be rather sure that the problem is in the sketch.

Arduino Yun support A to Z Languages

http://www.ibuyopenwrt.com/

Use Python is no challenge fun.

Python code:

#!/usr/bin/python
# -*- coding: utf-8 -*-
import _mysql
import sys, string, os
try:
    	con = _mysql.connect('192.168.0.20', 'root', 'password', 'ARDUINO')
	sqlstr="INSERT INTO temperature(sensor1, sensor2) VALUES( '" + sys.argv[1] +"', '" + sys.argv[2] + "')"
	#print sqlstr
	con.query(sqlstr)   
except _mysql.Error, e:
    	print "Error %d: %s" % (e.args[0], e.args[1])
    	sys.exit(1)

finally:    
    	if con:
		con.close()

Arduino code:

#include <Process.h>
int temperature;
void setup() {
 Bridge.begin();  // Initialize Bridge
}
void loop() {
 int sensor1 = random(0, 100);
 int sensor2 = random(0, 10);
 Process p;              
 p.begin("/mnt/sda1/mysql.py");      
 p.addParameter(String(sensor1)); 
 p.addParameter(String(sensor2)); 
 p.run();
 delay(5000); 
}

http://www.ibuyopenwrt.com/index.php/2-uncategorised/206-python-mysql-for-process-class

sonnyyu: Arduino Yun support A to Z Languages

http://www.ibuyopenwrt.com/

Use Python is no challenge fun.

Python code:


http://www.ibuyopenwrt.com/index.php/2-uncategorised/206-python-mysql-for-process-class

Thanks sonnyyu for the extensive help, but whats does the "int sensor1 = random(0,100)" do? is a random function required?

Can you please elaborate how to do this part? i couldnt understand this line:

chmod 755 /mnt/sda1/mysql.py /mnt/sda1/mysql.py 'test1' 'test2'

for those who dont know what i am talking abt, refer to this: http://www.ibuyopenwrt.com/index.php/2-uncategorised/206-python-mysql-for-process-class

int sensor1 = random(0,100);

emulate sensor output, provide sample data.

chmod 755  /mnt/sda1/mysql.py

Changing File Permissions

https://codex.wordpress.org/Changing_File_Permissions

/mnt/sda1/mysql.py    'test1' 'test2'

Python Command Line Arguments

https://www.tutorialspoint.com/python/python_command_line_arguments.htm

sonnyyu:

int sensor1 = random(0,100);

emulate sensor output, provide sample data.

chmod 755  /mnt/sda1/mysql.py

Changing File Permissions

Changing File Permissions « WordPress Codex

/mnt/sda1/mysql.py    'test1' 'test2'

Python Command Line Arguments

Python - Command Line Arguments - Tutorialspoint

sorry i still dont get how you execute “chmod755 /mnt/sda1/mysql.py” i ran that on python shell and they gave me a syntax error.
Also when i ran the python code, everything works except this :sqlstr=“INSERT INTO temperature(sensor1, sensor2) VALUES( '” + sys.argv[1] +"’, ‘" + sys.argv[2] + "’)"
Also can someone explain what does the “nano” in : nano /mnt/sda1/mysql.py means? i keep seeing this but idk what it means…