2020년 2월 1일 토요일

기상관측소 만들기 - MQTT, Node-RED, Google Chart

이 글은 기상관측소를 만들기 위해 어떻게 MQTT, Node-RED, Google Chart 를 서로 연동하는 지에 대한 내용을 간단히 다루어 본다.
Node-RED 디자인

개요
이 예제는 다음 도구를 사용한다.
Microcontroller
  Cactus Micro Rev2 – Arduino w/ ESP8266 WiFi built-in
Display
  LCD 16×2
Sensors
  DHT11- Humidity and Temperature
Photoresistor – Light
  Messaging
MQTT
  Mosca – MQTT broker
Data Storage
  MongoDB
Data Analysis
  Google Chart
Programming
  Arduino Sketch (C++)
  Node-RED
Gallery
  Pictures
Bonus: Real-time Graphs
  ThingSpeak

개발 순서
이 예제는 ESP8266을 사용한다. 다음과 같이 펌웨어를 준비한다.
git clone https://github.com/tuanpmt/espduino
cd espduino
./esptool.py -p /dev/tty.usbmodem1421 write_flash 0x00000 esp8266/release/0x00000.bin 0x40000 esp8266/release/0x40000.bin

다음과 같이 코딩한다.
// ESP8266 WiFi 
#include <espduino.h>
#define PIN_ENABLE_ESP 13
#define SSID "yourSSID"
#define PASS "yourPassword"

void wifiCb(void* response)
{
 uint32_t status;
 RESPONSE res(response);

 if(res.getArgc() == 1) {
 res.popArgs((uint8_t*)&status, 4);
 if(status == STATION_GOT_IP) {
 Serial.println("WIFI CONNECTED");
 wifiConnected = true;
 } else {
 Serial.println("WIFI OFFLINE");
 wifiConnected = false;
 } 
 }
}

void setup() {

 // Write to ESP8266
 Serial1.begin(19200);
 // Write to Console
 Serial.begin(19200);

 // Enable ESP8266
 esp.enable();
 delay(500);
 esp.reset();
 delay(500);
 while(!esp.ready());

 // Setup WiFi
 esp.wifiCb.attach(&wifiCb);
 esp.wifiConnect(SSID, PASS);

}

void loop() {
 esp.process();
 if(wifiConnected) {
 // Do Something 
 }
}

다음과 같이 회로를 개발한다.

다음과 같이 통신 시스템을 개발한다. IoT 사용 프로토콜은 MQTT, AMQP, øMQ 등이 있다. 또한, 웹 기반 REST, WebSocket 통신도 있다. 이는 PubNub, Pusher, HiveMQ, OctoBlu 등과 같은 도구에서 지원한다.

여기서는 MQTT를 사용한다. 브로커는 Mosca, Mosquito가 가장 유명하다. HiveMQ는 SLA과 확장성을 지원하는 엔터프라이즈 플랫폼이다. 여기서는 Mosca를 선택했으며 이는 NodeJS를 사용한다.

MQTT 클라이언트는 Arduino, RPi 등을 선택할 수 있다.

데이터 저장은 SQL, NoSQL DB를 사용한다. NoSQL은 MongoDB나 DynamoDB 등이 있다. 저장은 key:value 페어 형식이다. 이 형식은 보통 JSON 포맷으로 데이터 주고 받는다. 여기서는 MongoDB를 사용한다.

메시징 흐름은 다음과 같다.
데이터 분석은 Google Chart node를 Node-RED에 설치해 사용한다.

아두이노 코딩은 여기를 참고한다. 노드 레드는 다음과 같이 디자인한다.

실시간 그래프는 ThingSpeak를 사용한다.

마무리
이 글은 간단히 IoT 기반 weather station을 개발하는 예제를 설명하였다. 요즘 많은 도구가 오픈소스로 나와 있어, 저비용으로 손쉽게 개발이 가능하다. 

댓글 없음:

댓글 쓰기