This shows you the differences between two versions of the page.
user:asowers:mysqldaemon [2013/04/21 23:27] – created asowers | user:asowers:mysqldaemon [2013/04/21 23:28] (current) – [C code] asowers | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====Welcome==== | ||
+ | |||
+ | ====Purpose==== | ||
+ | The purpose of this project was to create a simple daemon type utility that checks the state of a MYSQL database. Based on the state of certain data the program will execute certain operations. | ||
+ | |||
+ | ====The GPIO==== | ||
+ | Using the wringPi library means you can use your RPi's GPIO like an Arduino' | ||
+ | |||
+ | the workhorse of wiringPi is: " | ||
+ | |||
+ | ====PHP and MYSQL==== | ||
+ | Using a PHP frontend and MYSQL as mediator we can have users interface with the RPi's GPIO via a web GUI. Let's abstract that away for now and focus on the daemon. | ||
+ | |||
+ | ====C code==== | ||
+ | Here's some code that establishes a local MYSQL connection and checks the database based on an interval of seconds chosen as an initial argument. | ||
+ | |||
+ | <code c> | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include </ | ||
+ | |||
+ | int main(int argc, char *argv[]){ | ||
+ | char user[7] = " | ||
+ | char pass[6] = " | ||
+ | |||
+ | MYSQL *connection; | ||
+ | MYSQL_RES *result; | ||
+ | MYSQL_ROW row; | ||
+ | int num_fields; | ||
+ | int i; | ||
+ | |||
+ | if (wiringPiSetup () == -1) | ||
+ | exit (1) ; | ||
+ | |||
+ | |||
+ | connection = mysql_init(NULL); | ||
+ | if (connection == NULL) { | ||
+ | printf(" | ||
+ | exit(1); | ||
+ | }else{ | ||
+ | printf(" | ||
+ | } | ||
+ | |||
+ | printf(" | ||
+ | |||
+ | mysql_real_connect(connection, | ||
+ | int repeatFlag = atoi(argv[1]); | ||
+ | char test[4]; | ||
+ | for(;;){ | ||
+ | |||
+ | mysql_query(connection, | ||
+ | result = mysql_store_result(connection); | ||
+ | num_fields = mysql_num_fields(result); | ||
+ | while ((row = mysql_fetch_row(result))) | ||
+ | { | ||
+ | for(i = 0; i < num_fields; i++) | ||
+ | { | ||
+ | char* tester = row[i] ? row[i] : " | ||
+ | if (tester[0] == ' | ||
+ | printf(" | ||
+ | pinMode(0, | ||
+ | mysql_query(connection, | ||
+ | result = mysql_store_result(connection); | ||
+ | num_fields = mysql_num_fields(result); | ||
+ | tester = NULL; | ||
+ | while ((row = mysql_fetch_row(result))){ | ||
+ | for(i = 0; i < num_fields; i++){ | ||
+ | char *tester = row[i] ? row[i] : " | ||
+ | printf(" | ||
+ | if (tester[0] == ' | ||
+ | digitalWrite(0, | ||
+ | }else digitalWrite(0, | ||
+ | } | ||
+ | } | ||
+ | }else if(tester[0] == ' | ||
+ | printf(" | ||
+ | pinMode(0, | ||
+ | mysql_query(connection, | ||
+ | result = mysql_store_result(connection); | ||
+ | num_fields = mysql_num_fields(result); | ||
+ | tester = NULL; | ||
+ | while ((row = mysql_fetch_row(result))){ | ||
+ | for(i = 0; i < num_fields; i++){ | ||
+ | char *tester = row[i] ? row[i] : " | ||
+ | printf(" | ||
+ | if (tester[0] == ' | ||
+ | digitalWrite(0, | ||
+ | }else digitalWrite(0, | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | printf(" | ||
+ | } | ||
+ | sleep(repeatFlag); | ||
+ | } | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | mysql_free_result(result); | ||
+ | mysql_close(connection); | ||
+ | |||
+ | |||
+ | return 0; | ||
+ | } | ||
+ | </ | ||
+ | |||