아두이노의 센서값을 시리얼 통신으로 라즈베리파이로 받아와 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
  • (•́ ✖ •̀)
    알 수 없는 사용자
  • 데이터베이스를 공부해야 합니다. 글 몇줄로 될 부분이 아닙니다. 데이터베이스를 하나 선택후 설치하고 제공되는 c sdk를 이용하여 데이터베이스와 통신할 수 있습니다. 정영훈 2019.9.26 02:40
  • 잘알지는 못하나 전공자라 리눅스에 MySQL기초적인 SQL문 정도는 실행 해봤습니다. 제가 직접 쿼리문을 이용해 테이블을 생성하고 데이터를 입력은 해보았습니다. 이게 외부로 받아온 데이터를 저장하려면 아파지나 php같은 서버를 통해서만 올려야하는지 궁금합니다... 제가 부족한 지식수준이지만 큰 틀을 좀 알려주신다면 많은 도움이 될것 같습니다. 그리고 c sdk에서 어떤 기능을 이용해야 하는지 잘모르겠습니다ㅠㅠ.!!! 감사합니다. 알 수 없는 사용자 2019.9.26 10:00
  • mysql 도 서버이므로 ip와 port 로 접속 할 수 있습니다. 우선 c/c++ 에 자신 있다면 https://github.com/ChuckBell/MySQL_Connector_Arduino 커넥터를 이용할 수 있겠고 없다면 외부 서버(http)를 이용해야 할 것 입니다. 정영훈 2019.9.26 20:28
  • 정영훈님 감사합니다!! 알 수 없는 사용자 2019.9.28 17:29

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)