水分測定センサ付き給水ポンプユニット Watering UnitをM5StickCで使う
2021年9月5日
給水ポンプ、それを駆動するモーターとコントローラー、静電容量式土壌水分センサーで構成される「Watering Unit」を、M5StickCで使ってみます。
ポンプの吐出量は、高低差がない場合は約0.5L/minで、家庭用レベルのプランター園芸には十分な量です。
例えば、トマトを栽培する場合、灌水量は定植後に40〜50ccを10回程度なので、ポンプの総稼働時間は1日あたり1分程度で済みます。もちろん、植物の成長に合わせて灌水量を増やす必要はありますが...。大容量のモバイルバッテリーがあれば、数日間は稼働できるでしょう。
商品について
M5StickCでの使い方
1.下記のURLを参考にCanaspad-IoTに登録してライブラリをインストールする
2.サンプルコードをM5StickCに書き込む
ファイル→スケッチ例→Canaspad ESP lib→M5StickC→Canaspad_WATERINGを選択してサンプルコードを開きます。
3.サンプルコードを適宜書き換える
サンプルコードの以下の部分は書き換えが必要です。
const char ssid[] = "WiFi_ssid";
const char password[] = "WiFi_pass";
const char* api_username = "user@mail.com";
const char* api_password = "password";
4.GroveでユニットとM5StickCを接続する
Groveポートに接続してください。
土壌水分センサの校正
静電容量式の土壌水分センサは、土壌の種類やEC、温度、指で触ったかどうかなどで値が変動するようです。あくまでも家庭用なので、今回は大まかに校正しています。
WateringUnitのセンサ出力電圧は、空気中で1.2V、水中で1.6Vとなっており、参考にしたサイトで使用されているものとそれほど変わりません。
また、土が乾いた時の閾値を1.4V(50%)に設定していますが、実際に計測しながら調整した方が良いと思います。
おわりに
このチュートリアルでは、水分測定センサーを搭載したポンプユニット「Watering Unit」のCANASPAD-IoTチュートリアルを作成しました。
CANASPAD-IoTは、マイコンを使ったIoTのプロトタイピングや、センシングデータの相関関係を見つけるのに便利なサービスです。 無料でご利用いただけますので、ぜひご活用ください。
サンプルコード
M5StickC用のサンプルコードを作成しました。
#include <M5StickC.h>
#include <Canaspad.h>
#define INPUT_PIN 33
#define PUMP_PIN 32
const char ssid[] = "WiFi_ssid";
const char password[] = "WiFi_pass";
const char* api_username = "user@mail.com";
const char* api_password = "password";
Canaspad api;
String sensor_moisture;
float vol, moisture, threshold_moisture;
void setup() {
M5.begin();
Serial.begin(115200);
if(not api.begin(ssid, password, 9, api_username, api_password)){
Serial.println("Api Connection Faild");
Serial.println(api.httpCode);
}
pinMode(INPUT_PIN, INPUT);
pinMode(PUMP_PIN,OUTPUT);
//Get the uuid
sensor_moisture = api.set("moisture", "watering", "number", 3, true);
//control_threshold = api.set("moisture", "control", "number", 3, true);//You can also fetch the thresholds recorded in the cloud.
}
void loop() {
if (api.gettimestamp() % 60 == 0){// interval
Serial.println();
Serial.println("---------------------------------------------");
//Get the measured value
float adc = analogRead(INPUT_PIN);
adc += analogRead(INPUT_PIN);
adc += analogRead(INPUT_PIN);
adc += analogRead(INPUT_PIN);
adc += analogRead(INPUT_PIN);
adc = adc / 5;
vol = (adc + 1) * 3.3 / (4095 + 1);//0-3.3V,12bit(0-4095)
moisture = 100 * (1.65 - vol) / (1.65 - 1.2);//100 * (dry_voltage - now_voltage) / (dry_voltage - full_of_water_voltage)
Serial.printf("Voltage: %2.2fV Moisture: %0.2f%%\r\n", vol, moisture);
//Add the measured values to JSON
api.add(moisture, sensor_moisture);
//Send JSON to API
if (api.send()) {
Serial.print("Now : ");
Serial.println(api.gettime());//current time
}
else {
int err_num = api.httpCode;
Serial.print("Error on HTTP request! HttpCode : ");
Serial.println(err_num);
}
//Getting values from API
float res_moisture = api.get(sensor_moisture);
Serial.printf("Moisture: %0.2f%%(Received from the API)\r\n", res_moisture);
//Judge the watering
threshold_moisture = 50.0;//Turn on when 50% or less
//threshold_moisture = api.get(control_threshold);//You can also fetch the thresholds recorded in the cloud.
if (moisture <= threshold_moisture){
Serial.println("Needs watering");
digitalWrite(PUMP_PIN,HIGH);
delay(1000*1);//pump discharge:8.3 ml/s (without height difference)
digitalWrite(PUMP_PIN,LOW);
}
Serial.println("---------------------------------------------");
delay(10*1000);
}
}