아두이노의 센서값을 시리얼 통신으로 라즈베리파이로 받아와 DB에 저장하는법
조회수 2459회
제가 C코딩을 통해서 라즈베리파이 터미널창에 아두이노 센서값을 출력을 하는것은 성공했습니다. 저는 그 센서값을 텍스트 파일이나 DB에 저장하고 싶은데 이 시리얼 통신으로 받은 값을 어떻게 처리를 해서 DB에 올릴 수 있는지 잘 모르겠습니다. 도와주세요ㅠㅠ. 센서값을 변수 혹은 DB에 저장할 수 있는 타입으로 받을 수 있는 방법이 없을까요????
#ifdef RaspberryPi
//include system librarys
#include <stdio.h> //for printf
#include <stdint.h> //uint8_t definitions
#include <stdlib.h> //for exit(int);
#include <string.h> //for errno
#include <errno.h> //error output
//wiring Pi
#include <wiringPi.h>
#include <wiringSerial.h>
// Find Serial device on Raspberry with ~ls /dev/tty*
// ARDUINO_UNO "/dev/ttyACM0"
// FTDI_PROGRAMMER "/dev/ttyUSB0"
// HARDWARE_UART "/dev/ttyAMA0"
char device[]= "/dev/ttyACM0";
// filedescriptor
int fd;
unsigned long baud = 9600;
unsigned long time=0;
//prototypes
int main(void);
void loop(void);
void setup(void);
void setup(){
printf("%s \n", "Raspberry Startup!");
fflush(stdout);
//get filedescriptor
if ((fd = serialOpen (device, baud)) < 0){
fprintf (stderr, "Unable to open serial device: %s\n", strerror (errno)) ;
exit(1); //error
}
//setup GPIO in wiringPi mode
if (wiringPiSetup () == -1){
fprintf (stdout, "Unable to start wiringPi: %s\n", strerror (errno)) ;
exit(1); //error
}
}
void loop(){
// Pong every 3 seconds
if(millis()-time>=3000){
serialPuts (fd, "Pong!\n");
// you can also write data from 0-255
// 65 is in ASCII 'A'
serialPutchar (fd, 65);
time=millis();
}
// read signal
if(serialDataAvail (fd)){
char newChar = serialGetchar (fd);
printf("%c", newChar);
fflush(stdout);
}
}
// main function for normal c++ programs on Raspberry
int main(){
setup();
while(1) loop();
return 0;
}
#endif //#ifdef RaspberryPi
-
(•́ ✖ •̀)
알 수 없는 사용자
댓글 입력