User Tools

Site Tools


user:asowers:mysqldaemon

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's. This means easy access to the GPIO in C/C++ programs.

the workhorse of wiringPi is: “pinMode(pinNumber, OUTPUT/INPUT);” and “digitalWrite(pinNumber, state);”

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.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <wiringPi.h>
#include </usr/include/mysql/mysql.h>
 
int main(int argc, char *argv[]){
char user[7] = "gpio";
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("Error %u: %s\n", mysql_errno(connection), mysql_error(connection));
      exit(1);
}else{
printf("connection established\n");
}
 
printf("MySQL client version: %s\n", mysql_get_client_info());
 
mysql_real_connect(connection, "localhost", user, pass, "gpio", 0, NULL, 0);
int repeatFlag = atoi(argv[1]);
char test[4];
for(;;){
 
	mysql_query(connection, "SELECT pinDirection FROM pinDirection WHERE pinNumber ='17'");
	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] : "NULL";
			if (tester[0] == 'o' && tester[1] == 'u' && tester[2] == 't'){
				printf("%s\n",tester);
				pinMode(0, OUTPUT);
				mysql_query(connection, "SELECT pinStatus FROM pinStatus WHERE pinNumber ='17'");
				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] : "NULL";
						printf("State: %s\n",tester);
						if (tester[0] == '0'){
							digitalWrite(0, 0);
						}else digitalWrite(0, 1);
					}
				}
			}else if(tester[0] == 'i' && tester[1] == 'n'){
				printf("%s\n",tester);
				pinMode(0, INPUT);
				mysql_query(connection, "SELECT pinStatus FROM pinStatus WHERE pinNumber ='17'");
                                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] : "NULL";
                                                printf("State%s\n",tester);
                                                if (tester[0] == '0'){
                                                        digitalWrite(0, 0);
                                                }else digitalWrite(0, 1);
                                        }
                                }
			}
      		}
      		printf("\n");
  	}
	sleep(repeatFlag);
}
 
 
 
 
mysql_free_result(result);
mysql_close(connection);
 
 
return 0;
}
user/asowers/mysqldaemon.txt · Last modified: 2013/04/21 19:28 by asowers