DFRobotのセンサーを使うためにArduino UNO Wifi Rev2を使ってみる

DFRobotのセンサーなどを使う場合は主にArduino UNOを使っていて、スケッチもArduino UNO用のスケッチなのでESP32やESP8266だとうまく動かない(試した限りでは動かなかった)。動くかもしれませんが、自分は詳しくなく分からないのでArduino UNOを使っています。

が、UNOはWifiが無いんですよねぇ・・・

UNOクローンのUNO+WiFi R3 ATmega328P+ESP8266というボードを買ってみてUNOとして使えると思っていましたがESP8266として使うようで、DFRobotのセンサーは使えず・・・

なので純正Arduino UNO WiFi Rev2を買ってみました。純正のArduino UNOだったら問題なくDFRobotのセンサーが使えるのでついでにWifiも使えるようになると嬉しい。

Arduino UNO Wifi買った

MOUSERで買いました。

こちら50ドル以上は送料無料でUPSで送ってくれます。

https://www.mouser.jp/ProductDetail/arduino/abx00021/?qs=%252bEew9%252b0nqrBypi1Yoqvaaw==&countrycode=JP¤cycode=JPY

すぐに発送してくれて4日ほどで日本に到着。

Arduino UNO Wifi Rev2 本体

箱はカワイイイラスト付き。

Arduino UNO Wifi Rev2

本体のベースが透明プラ付きだった。↓

Arduino UNO Wifi Rev2

ご親切にPINに全て番号とAnalogとかデジタルとかの表記があってわかりやすい。

裏表にPINの番号が書いてある。親切です。

Arduino UNO Wifi Rev2

しかし!今どき(Arduino UNO Wifi Rev2の発売古い?2018年だったような)USB  Type-Bポートというのはかなり困ります・・・せめてMicroUSBだったらよかった。別途プラグを買わないと無いですヨ。

Arduino UNO Wifi Rev2

これでAruduino UNOが2台とArduino UNOクローンが1個(↓右側)で3つになった。

Arduino UNO Wifi Rev2

本当はESP32を使いたいところだけど、自分の技術力が足りず、Arduino UNOでないとうまくセンサーを動かせないのが困った。精進します。

USBシリアルデバイスになる

まずは適当に接続してスケッチを書いてみます。

Arduino UNO Wifi

ポートはUSBシリアル デバイスと表示されました。

試しにArduino UNO Wifiを選んでスケッチを書き込む

使い方が分からないので適当にそれらしいボード「Arduino UNO WiFi」を選んでスケッチを書き込んでみます。

Arduino UNO Wifi

以下のエラーが出た。このボード違うみたいです・・・

avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 1 of 10: not in sync: resp=0x5b
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 2 of 10: not in sync: resp=0x5b
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 3 of 10: not in sync: resp=0x5b
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 4 of 10: not in sync: resp=0x5b
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 5 of 10: not in sync: resp=0x5b
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 6 of 10: not in sync: resp=0x5b
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 7 of 10: not in sync: resp=0x5b
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 8 of 10: not in sync: resp=0x5b
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 9 of 10: not in sync: resp=0x5b
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 10 of 10: not in sync: resp=0x5b
マイコンボードに書き込もうとしましたが、エラーが発生しました。
このページを参考にしてください。
ERROR: The request could not be satisfied

適当にやってもダメですね・・・使い方が書いてあるのでそのとおりにやってみます。

Arduino UNO WiFi Rev2を正しく使う

ボードマネージャでArduino UNO WiFi Rev2で検索

以下のページに詳しく書いてありました。

UNO WiFi Rev2 | Arduino Documentation
The Arduino UNO WiFi Rev2 is the easiest point of entry to basic IoT with the standard form factor of the UNO family. Whether you are looking at building a sens...

まずはArduino IDEでツール > ボード >ボードマネージャで「Arduino UNO WiFi Rev2」と検索すると、「Arduino megaAVR Boards」が出てくるのでコレをインストールする。

https://www.arduino.cc/en/Guide/ArduinoUnoWiFiRev2

WARNING出たけど気にしなくていいらしい

Arduino IDEを再起動してスケッチを書き込んだら以下のようなWARNINGになった。

avrdude: WARNING: invalid value for unused bits in fuse "fuse5", should be set to 1 according to datasheet
This behaviour is deprecated and will result in an error in future version
You probably want to use 0xcd instead of 0xc9 (double check with your datasheet first).

気にしなくていいらしい。

UNO WiFi Rev 2
when i upload a Sketch to an UNO WiFi Rev 2, i get the following error: "avrdude:WARNIG:invalid value for unused bits in fuse "Fuse5", should be set to 1 acco...

スケッチを書き終わったとか何も表示されず、速いので分からなかったけど、ちゃんと書き込めてる。

Arduino UNO Wifi Rev2でWifi接続

Arduino UNO Wifi Rev2のWifiについて書いてある。

WiFiNINA - Arduino Reference
The Arduino programming language Reference, organized into Functions, Variable and Constant, and Structure keywords.

WiFiNINAライブラリをインストール

まずはWifiを使えるようにツール >ライブラリを管理を選んでライブラリマネージャを起動して、「WiFiNINA」と検索します。WiFiNINA by Arduinoをインストール

Arduino UNO Wifi

WEPのスケッチを実行してみる

ここにWifi接続のスケッチがありました。

WiFiNINA Library Examples | Arduino Documentation
The WiFi library for boards with a NINA WiFi module. Works with the MKR WiFi 1010, MKR Vidor 4000, Uno WiFi Rev2, Nano 33 IoT and the Nano RP2040 Connect.

テスト的にWifi接続してみます。

arduino_secretsが無いと言われる場合

上記のスケッチを書き込もうとしたところエラーになりました。

以下のようにarduino_secrets.hが無いと言われた場合は空のarduino_secrets.hを作れば良いそうです。

UNO-WIFI-REV2-Wifi:3:10: error: arduino_secrets.h: No such file or directory
 #include "arduino_secrets.h" ;

まずは右上↓▼ボタンを押して、

arduino_secrets.h

「新規タブ」を選択↓

arduino_secrets.h

以下のように新規ファイルの名前に「arduino_secrets.h」と入力してOKボタンを押す。

Arduino UNO Wifi

その後、arduino_secrets.hタブを選択してファイル > 保存を押すと以下のようにスケッチファイルと同じフォルダにarduino_secrets.hファイルができました。

arduino_secrets.h

うまくWifi接続できた

上記エラーを解決したらちゃんとWifi接続できました♪

Arduino UNO Wifi

「Please upgrade the firmware」というのが気になる。

 

ArduinoHttpClientでPost送信

WiFiNINAでWifiにログイン後、ArduinoHttpClientを使ってサーバー(ラズパイ)のInfluxDBにセンサーで計測したデータを送ります。

ArduinoHttpClientライブラリをインストール

ツール >ライブラリを管理でArduinoHttpClientをインストールします。

これを使ってサーバーのInfluxDBにデータを投げるようにスケッチを書きます。

Arduino UNO Wifi Rev2

Arduino IDE スケッチ

センサーは今回はTDS(Total Dissolved Solid=総溶解固形分)で主に水に溶け込んでいる肥料の濃度を計測するものです。

以下のようにすると、InfluxDBに約10分おきにTDS計測データを送ることができました。

#include <SPI.h>
//Wifi接続
#include <WiFiNINA.h>
#include "arduino_secrets.h"
char ssid[] = "Xiaomi_wifi";//Wifi SSIDを変更する
char pass[] = "xxxxxxxx";   //Wifiパスワード

//サーバにアクセスPOST送信
#include <ArduinoHttpClient.h>

//InfluxDB
#define PLACE "reef-TDS"
#define MEASUREMENT "air5"
#define HOST "UNO-Wifi-Rev2"
String influxUrl = "/write?db=sensor";
String influxUser = "root";
String influxPassword = "xxxxxxxx";

const char serverName[] = "192.168.31.53";
int port = 8086;
WiFiClient wifi;
HttpClient client = HttpClient(wifi, serverName, port);
int status = WL_IDLE_STATUS;

//サーバー ラズパイに接続
void postToInfluxDB(int tdsValue) {
  String influxData  = MEASUREMENT;
  influxData += ",place=" PLACE ",host=" HOST;
  influxData += " test=";
  influxData += tdsValue;
   
  String contentType = "application/x-www-form-urlencoded";
  client.beginRequest();
  client.sendBasicAuth(influxUser, influxPassword);//Basic認証
  client.post(influxUrl, contentType, influxData);
  
  int statusCode = client.responseStatusCode();
  Serial.print("Status code: ");
  Serial.println(statusCode);
  String response = client.responseBody();
  Serial.print("Response: ");
  Serial.println(response);
}

//TDS計測
#include <EEPROM.h>
#include "GravityTDS.h"
#define TdsSensorPin A1
GravityTDS gravityTds;
//温度そのうち入れる
float temperature = 25,tdsValue = 0;

void setup() {
  Serial.begin(115200);
  while (status != WL_CONNECTED) {
    Serial.println(ssid);
    status = WiFi.begin(ssid, pass);
    delay(10000);
  }
  
  //TDS計測
  gravityTds.setPin(TdsSensorPin);
  gravityTds.setAref(5);  //reference voltage on ADC, default 5.0V on Arduino UNO
  gravityTds.setAdcRange(1024);  //1024 for 10bit ADC;4096 for 12bit ADC
  gravityTds.begin();  //initialization
}

void loop() {
  //temperature = readTemperature();  
  gravityTds.setTemperature(temperature);
  gravityTds.update();
  tdsValue = gravityTds.getTdsValue();
  Serial.print(tdsValue,0);
  Serial.println("ppm");
  
  if (WiFi.status() == WL_CONNECTED) {
    delay(600000);
    postToInfluxDB(tdsValue);
  }
  delay(1000);
}

これで、Arduino UNO Wifi Rev2単体でWifiに繋げてサーバーに計測データを送ることができました。

一安心です。本当はESP32で計測してサーバーにデータを送信したかったのですが、今の自分の技術では無理そうなのでしばらくはArduino UNO Wifi Rev2を使用してしのぎます。

コメント

タイトルとURLをコピーしました