Data Collection Service - Arduino & RaspberryPi
21 Jan 2019 | IoT이 프로젝트는 전남대학교 김경백 교수님 주관하에 만들어졌던 프로잭트임을 알립니다.
1.1 아두이노와 라즈베리파이에서 센싱 및 수집
아두이노에서 수집되는 센서 값들을 라즈베리파이의 Flume을 통해 InfluxDB로 보내려면 우선 라즈베리파이와 아두이노 간의 $Serial Communication$이 필요하다.Arduino 센서 Matrix
알코올가스 센서에서의 $Alcohol Gas$ 값, 일산화탄소가스 센서에서의 $Carbon Monoxide Gas$ 값, 온도센서에서 나오는 $Temperature$ 값, 조도센서에서 나오는 $Light$ 값, 먼지센서에서 나오는 $Dust$값No. | Name | Type | Range | Error Value | M/O | Desc. |
---|---|---|---|---|---|---|
$1$ | $DeviceID$ | $String$ | $m$ | 디바이스 고유 id | ||
$2$ | $Status$ | $Integer$ | [0,1,2] | $m$ | 0: Success 1: Sensing Error(일부에서 에러나 누락 발생시에도 1로 표시하고, 해당 센서 측정치에 Error Value ) 2: Comm, Error | |
$3$ | $Time$ | $Datetime$ | $m$ | 저장된 시간 | ||
$4$ | $AlcoholGas$ | $Integer$ | 0~1023 | $-1$ | $O$ | 알코올 가스량(ppm) |
$5$ | $CoGas$ | $Integer$ | 0~1023 | $-1$ | $O$ | 일산화탄소량 (ppm) |
$6$ | $Temp$ | $Float$ | -40.0~125.0 | $-999$ | $O$ | 온도 |
$7$ | $Light$ | $Integer$ | 0~1023 | $-1$ | $O$ | 조도 |
$8$ | $Dust$ | $Float$ | 0.0~750.0 | $O$ | 먼지(ug/m3) |
Arduino 센서 Example
#define R0 10000
#define T0 25 //room temperature
#define B 4200 //the coefficient of the thermistor
#define SERISR 10000 //seris resistor 10K
int LledPin = 13;
int TledPin = 9;
int CledPin = 7;
int AledPin = 2;
long previousMillis1 = 0;
long previousMillis2 = 0;
long previousMillis3 = 0;
long previousMillis4 = 0;
long interval = 1000;
void CheckOut(long currentMillis, int ledPin, long previousMillis, float data, int value)
{
if((data > value) && ((currentMillis - previousMillis) > interval))
{
previousMillis = currentMillis;
digitalWrite(ledPin, HIGH);
}
else if((data <= value) && ((currentMillis - previousMillis) > interval))
{
previousMillis = currentMillis;
digitalWrite(ledPin, LOW);
}
}
void setup() {
Serial.begin(9600);
// 9번 핀을 출력으로 설정
pinMode(LledPin, OUTPUT);
pinMode(TledPin, OUTPUT);
pinMode(CledPin, OUTPUT);
pinMode(AledPin, OUTPUT);
}
void loop() {
// 조도 센서 값
int Ldata = analogRead(A0);
// 온도 센서 값
float thermistorRValue = 0;
thermistorRValue = 1023.0 / analogRead(A1) - 1;
thermistorRValue = SERISR / thermistorRValue;
float temperature = 0;
temperature = thermistorRValue / R0; // R/R0
temperature = log(temperature);
temperature = temperature / B;
temperature = temperature + 1.0 / (T0 + 273.15);
temperature = 1.0 / temperature;
temperature -= 273.15 ;// kelvins to C
float Tdata = temperature;
// MQ-7 일산화 탄소 센서 값
int MQ7data = analogRead(A2);
// MQ-3 알코올 센서 값
int MQ3data = analogRead(A3);
unsigned long currentMillis = millis();
CheckOut(currentMillis,LledPin,previousMillis1,Ldata,800);
CheckOut(currentMillis,TledPin,previousMillis2,Tdata,27);
CheckOut(currentMillis,CledPin,previousMillis3,MQ7data,2);
CheckOut(currentMillis,AledPin,previousMillis4,MQ3data,-1);
}
Arduio RaspberryPi Serial 통신
<내용>
위에서 4개의 센서를 동시에 돌리는 프로세스를 하나 만들어 시리얼 프린트를 해주고 라즈베리파이와 직렬 연결(Serial)을 하여 데이터를 보내준다. 라즈베리파이 측에서는 프로세스를 하나 돌려 한꺼번에 들어오는 데이터들을 분리하는 작업을 해준다.RaspberryPi Python Code
import serial
import time
NullStr = -1
Id = '"RaspberryPi"'
Light = NullStr
Temp = NullStr
AlcoholGas = NullStr
CoGas = NullStr
ser = serial.Serial('/dev/ttyACM0', 9600)
if(ser.isOpen() == False):
ser.open()
ser.readline()
time.sleep(2)
while 1:
#print ser.readline()
response = ser.readline()
if len(response.split(';')) == 5 :
Light = response.split(';')[0]
Temp = response.split(';')[1]
AlcoholGas = response.split(';')[2]
CoGas = response.split(';')[3]
print Light, Temp, AlcoholGas, CoGas
가져온곳: URL