개발자/Arduino

nano 33 IoT 보드로 구현한 스마트 가든 예제

지구빵집 2020. 9. 21. 16:00
반응형

 

nano 33 IoT 보드로 구현한 스마트 가든 예제 

 

집안에 꽃밭을 꾸미고, 화분을 놓고 심지어 스마트 농장을 설치하는 일들은 언듯 보기엔 좋은 일 같기도 하고 장점이 많아 보인다. 심리 상태나 정서상에 많은 도움이 되기도 한다. 그냥 바라보는 일도 감정을 평온하게 하니 좋은데 문제는 지저분해지는 문제다. 시들고, 벌레가 생기고, 나중에 치우거나 바꿔주는 일 등 사소하고 자질구레한 일이 많아진다. 우리가 만족하고 풍요로움을 느끼는 일들은 대부분 처음만 좋은 일들이 많다.  

 

아래는 dht22 온도 습도 센서를 사용하여 구현한 스마트 가든 예제 회로도와 코드를 나타낸다. 프로젝트에서는 당연히 아두이노 클라우드 서비스를 이용하여 Thing 설정을 하고 진행하는 데 그 과정을 포함하지 않고 연결도면과 nano 33 iot 도면과 아두이노 코드만 참고용으로 첨부한다.

 

도면과 코드 출처는 Smart Garden System with Arduino Nano IoT 임을 표시한다. 

 

 

 

 

 

아두이노 코드

 

#include "arduino_secrets.h"
// SimpleDHT - Version: Latest 
#include <SimpleDHT.h>

#define DHTPIN 2
#define SOILPIN A0
#define FLOWMETERPIN A1
#define VALVEPIN 3

#define UPDATE_RATE 1500 //update sensor values every 1500ms

/* 
  Sketch generated by the Arduino IoT Cloud Thing "SmartGarden"
  https://create.arduino.cc/cloud/things/50892e59-50bc-4a46-afe3-78dc3e0d34ee 

  Arduino IoT Cloud Properties description

  The following variables are automatically generated and updated when changes are made to the Thing properties

  float humidity;
  float temperature;
  float waterFlow;
  int soilMoisture;
  bool valveOpen;

  Properties which are marked as READ/WRITE in the Cloud Thing will also have functions
  which are called when their values are changed from the Dashboard.
  These functions are generated with the Thing and added at the end of this sketch.
*/

#include "thingProperties.h"

#define SOIL_THRESH 300 //given as number between 0-1023

SimpleDHT22 dht22(DHTPIN);

unsigned long latest_millis = 0;
unsigned long flow_millis = 0;

volatile float flow;

void setup() {
  // Initialize serial and wait for port to open:
  Serial.begin(9600);
  // This delay gives the chance to wait for a Serial Monitor without blocking if none is found
  delay(1500); 
  pinMode(SOILPIN, INPUT);
  pinMode(FLOWMETERPIN, INPUT);
  pinMode(VALVEPIN, OUTPUT);
  set_valve(0);

  // Defined in thingProperties.h
  initProperties();

  // Connect to Arduino IoT Cloud
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);
  
  attachInterrupt(FLOWMETERPIN, triggerPulse, RISING);
  
  /*
     The following function allows you to obtain more information
     related to the state of network and IoT Cloud connection and errors
     the higher number the more granular information youll get.
     The default is 0 (only errors).
     Maximum is 4
 */
  setDebugMessageLevel(2);
  ArduinoCloud.printDebugInfo();
  flow = 0.0;
}

void loop() {
  ArduinoCloud.update();
  // Your code here 
  
  if(millis() - latest_millis >= UPDATE_RATE){
    update_vals();
    latest_millis = millis();
  }
  if(millis() - flow_millis >= 1000){
    reset_flow();
    flow_millis = millis();
  }
}

void update_vals(){
  float temperature_raw = 0;
  float humidity_raw = 0;
  int err = SimpleDHTErrSuccess;
  if ((err = dht22.read2(&temperature_raw, &humidity_raw, NULL)) != SimpleDHTErrSuccess) {
    Serial.print("Read DHT22 failed, err="); Serial.println(err);delay(1000);
    return;
  }
  
  temperature = read_temp(temperature_raw);
  humidity = read_hum(humidity_raw);
  waterFlow = get_water_flow();
  soilMoisture = read_soil_moisture();

  if(soilMoisture < SOIL_THRESH){
    valveOpen = 1;
    set_valve(valveOpen);
  }
  else{
    valveOpen = 0;
    set_valve(valveOpen);
  }
}

float read_temp(float temp_raw){
  float temp = (float)temp_raw;
  temp = (1.8 * temp) + 32.0;
  return temp;
}

float read_hum(float hum_raw){
  float hum = (float)hum_raw;
  return hum;
}

float get_water_flow(){ //given in L/s
  float flow_l_s = flow / 450.0;
  Serial.print("Flow: ");
  Serial.println(flow_l_s);
  return flow_l_s;
}

void reset_flow(){
  flow = 0.0;
}

void triggerPulse(){
  //Serial.println("triggered");
  flow += 1.0;
}

int read_soil_moisture(){
  int moisture = analogRead(SOILPIN);
  return moisture;
}

void set_valve(bool open_or_closed){
  digitalWrite(VALVEPIN, !open_or_closed);
}


void onTemperatureChange() {
  // Do something
}


void onHumidityChange() {
  // Do something
}


void onSoilMoistureChange() {
  // Do something
}


void onWaterFlowChange() {
  // Do something
}







void onValveOpenChange() {
  Serial.println(valveOpen);
  set_valve(valveOpen);
}

 

 

 

 

스마트 가든 홍보판(국립백두대간 수목원 내 설치 사례) <자료제공=산림청>

 

 

 

 

반응형