Kompiliavimo eilutė:
gcc -o spi2mysqlx100.o -L/usr/lib/test1 -lmysqlclient -I/usr/local/include -L/usr/local/lib -lwiringPi spi2mysqlx100.c
rpi c kodas: spi2mysql1kx.c
/*********************************************************
SPI_Hello_Arduino
Configures an Raspberry Pi as an SPI master and demonstrates bidirectional communication with an
Arduino Slave by 1000 times repeat request Arduino with AR_command string and ger response in result string
Compile String: gcc -o spi2mysqlx100.o -L/usr/lib/test1 -lmysqlclient -I/usr/local/include -L/usr/local/lib -lwiringPi spi2mysqlx100.c
***********************************************************/
#include <mysql/mysql.h> //used by mysql
#include <stdlib.h> //used by mysql
#include <stdio.h>
#include <sys/ioctl.h>
#include <linux/spi/spidev.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h> //used by string
/**********************************************************
Declare Global Variables
***********************************************************/
int spiTxRx(unsigned char txDat);
int fd;
unsigned char AR_command[] = {0x81, 0xA8, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC7};
unsigned char spi_bitsPerWord;
unsigned char spi_mode;
struct connection_details{char *server;char *user;char *password;char *database;};
MYSQL* mysql_connection_setup(struct connection_details mysql_details){// first of all create a mysql instance and initialize the variables within
MYSQL *connection = mysql_init(NULL); // connect to the database with the details attached.
if (!mysql_real_connect(connection,mysql_details.server, mysql_details.user, mysql_details.password, mysql_details.database, 0, NULL, 0)) {
printf(„Conection error : %s\n”, mysql_error(connection));exit(1);
}
return connection;
}
MYSQL_RES* mysql_perform_query(MYSQL *connection, char *sql_query){
if (mysql_query(connection, sql_query)) {// send the query to the database
printf(„MySQL query error : %s\n”, mysql_error(connection));
exit(1);
}
return mysql_use_result(connection);
}
struct connection_details mysqlD;
MYSQL *conn; // the connection
MYSQL_RES *res; // the results
MYSQL_ROW row; // the results row (line by line)
/**********************************************************
Main
Setup SPI
Open file spidev0.0 (chip enable 0) for read/write access with the file descriptor „fd”
Configure transfer speed (1MkHz)
***********************************************************/
int main (void){
mysqlD.server = „localhost”; // where the mysql database is
mysqlD.user = „vejojegaines”; // the root user of mysql
mysqlD.password = „vejojegaines”; // the password of the root user in mysql
mysqlD.database = „vejojegaines”; // the databse to pick
conn = mysql_connection_setup(mysqlD); // connect to the mysql database
int i, qilgis, j;
int status_value = -1;
unsigned char result[20];
unsigned char buffH[1];
unsigned char buffL[1];
fd = open(„/dev/spidev0.0″, O_RDWR);
unsigned int speed =1000000;
ioctl (fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed);
j=0;
while (j<1000) { //while (1) {
unsigned char qu[255]=”insert into vejojegaines.spi values (\”\”,”;
for (i=0; i<sizeof(AR_command); i++) {
result[i] = spiTxRx(AR_command[i]);
if (i>4 && i<20) {
if (i%2==0) {
sprintf(buffL, „%02X”, result[i-1]);
sprintf(buffH, „%02X”, result[i]);
strcat(qu, „X'”);
strcat(qu, buffH);
strcat(qu, buffL);
strcat(qu, „‘,”);
}
}
}
strcat(qu, ” null);”);
//printf(„%s\n”, qu);
res=mysql_perform_query(conn, qu); // assign the results return to the MYSQL_RES pointer
mysql_free_result(res); //clean up the database result set
j++;
}
exit(1);
}
/**********************************************************
spiTxRx
Transmits one byte via the SPI device, and returns one byte as the result.
Establishes a data structure, spi_ioc_transfer as defined by spidev.h and loads the various members to pass the data
and configuration parameters to the SPI device via IOCTL
Local variables txDat and rxDat are defined and passed by reference.
***********************************************************/
int spiTxRx(unsigned char txDat){
unsigned char rxDat;
struct spi_ioc_transfer spi;
memset (&spi, 0, sizeof (spi));
spi.tx_buf = (unsigned long)&txDat;
spi.rx_buf = (unsigned long)&rxDat;
spi.len = 1;
ioctl (fd, SPI_IOC_MESSAGE(1), &spi);
return rxDat;
}
void ToMySQL(char qu[]) {
res=mysql_perform_query(conn, qu); // assign the results return to the MYSQL_RES pointer
mysql_free_result(res); //clean up the database result set
}
Arduino kodas: AnalogRead_on_rpi_request_using_spi
#include <SPI.h>
const int markeris = 13;
String msg=”deb:”;
byte buf[20];
int dat[]={0,11,22,33,0×55,0x66,0x77,0x88,0x99,0x00,0x19,0x28,0x37,0x46,0x55,0x64,0x73,0x82,0x91,0x00};
int checksumreal,komanda;
int komilgis=0;
int i=0;
boolean beginas=false;
boolean process_error=false;
volatile byte pos;
volatile boolean process_it;
void setup (void){
Serial.begin (115200); // debugging
pinMode(markeris, OUTPUT);
pinMode(MISO, OUTPUT); // have to send on master in, *slave out*
pinMode(8, OUTPUT); // have to send on master in, *slave out*
SPCR |= _BV(SPE); // turn on SPI in slave mode, get ready for an interrupt
pos = 0; // buffer empty
process_it = false;
SPI.attachInterrupt(); // now turn on interrupts
}
ISR (SPI_STC_vect){ // SPI interrupt routine
digitalWrite(markeris, HIGH);
byte c = SPDR; // grab byte from SPI Data Register
if (c==0x81 and beginas==false) {//msg+=micros();msg+=”.”;
beginas=true;checksumreal=0;komilgis=0; //0X81 hex’E – PAKETO PIRMAS BAITAS
}
if (beginas==true) {
buf[pos]=c;
checksumreal+=c; if(checksumreal>255)checksumreal=checksumreal-256;
switch (pos) {
case 1: komanda=c;
break;
case 2: komilgis=c;
break;
default:
if (pos<komilgis+3 and pos>1) SPDR=dat[pos-2];
if (pos==komilgis+3) {
if (checksumreal==0) {
process_it=true;
process_error=false;//msg+=micros();msg+=”.”;
} else {
process_error=true;
}
beginas=false;
}
break;
}
if (pos < sizeof(buf)) pos++; else {pos=0;beginas=false; msg+=”buffer overflow”;}
}
digitalWrite(markeris, LOW);
}
void loop (void){
int x;
if (process_it) {
pos = 0;
process_it = false;
}
x=analogRead(A0); dat[0]=lowByte(x); dat[1]=highByte(x);
x=analogRead(A1); dat[2]=lowByte(x); dat[3]=highByte(x);
x=analogRead(A2); dat[4]=lowByte(x); dat[5]=highByte(x);
x=analogRead(A3); dat[6]=lowByte(x); dat[6]=highByte(x);
x=analogRead(A4); dat[8]=lowByte(x); dat[9]=highByte(x);
x=analogRead(A5); dat[10]=lowByte(x); dat[11]=highByte(x);
x=millis(); dat[12]=lowByte(x); dat[13]=highByte(x);
}
Programos komentarai:
Pusė reikšmių MySQL’e nulinės. Perjungus Arduino maitinimą – vėl gerai
Markeris – LED’as arduino plokštėje neveikia.