Question

So i'm having this function (code below) in c that checks data from my database. If this data equals 255 i want to have a LED to light on one of my GPIO pins (12) on the raspberry pi.

I first tried the code WITHOUT bcm functions, it worked all fine. Now that I have the bcm2835 included, and changed my Makefile (compiling worked fine), I'm getting this "Segmentation fault" error.

I know this means my program is using memory it shouldn't be using, but I have no idea what causes this in the bcm lines.

Here is the function:

void check_pasid(char k[]){

MYSQL *conn;
MYSQL_RES * result;
MYSQL_ROW row;



   char *server = "server";
   char *user = "myusername";
   char *password = "mypassword";
   char *database = "dbname";
   char query1[100];

   // Make the connection to the Mysql-database.
   conn = mysql_init(NULL);

   if (!mysql_real_connect(conn, server, user, password, database, 0, NULL, 0)) {
      fprintf(stderr, "%s\n", mysql_error(conn));
      exit(1);   

   }

sprintf(query1,"SELECT COUNT(*) FROM passcan WHERE UserID = \"%s\";",k);


int result1 = mysql_query(conn, query1);
result = mysql_store_result(conn);
row = mysql_fetch_row(result);

int compare = 1;
compare = strcmp(row[0], "1");

if(compare == 0){
    printf("Led is turning on");
    // Turn it on
    bcm2835_gpio_write(PIN12, HIGH); 
    delay(5000);
    // Turn it off
    bcm2835_gpio_write(PIN12, LOW);
}
else{
    printf("Led is not turning on");
}

}

I hope someone has some information for me.

Was it helpful?

Solution

A correct use of BCM2835
Here's problably can work better:

#include <bcm2835.h>
// Led on RPi Plug P1 pin 11 (which is GPIO pin 17)
#define PIN RPI_GPIO_P1_11
uint8_t status = LOW;
int SetLed()
{
    // Inizialize the library
    if (!bcm2835_init())
        return 1;
    // Set the pin to be an output
    bcm2835_gpio_fsel(PIN, BCM2835_GPIO_FSEL_OUTP);
    // Set the Led:
    bcm2835_gpio_write(PIN, status);
    status=!status;
    /*
    I fetched a flip-flop variable, problably does not work without integer variable, so replace the up line with
    if (status == HIGH)
       status = LOW;
    else
       status = HIGH;
    */
    //Clean-up and return success.
    bcm2835_close();
    return 0;
}

I'm not an expert, the code can be wrong, but the part important are the function bcm2835_init(),bcm2835_gpio_fsel() and bcm2835_close() that must be called almost one time!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top