LoginSignup
7
6

More than 5 years have passed since last update.

ArduinoをEthernetでWR702Nモバイルルーターに接続してRESTクライアントにする

Posted at

Arduino Uno用のEthernetシールドにWiznet W5100 チップを搭載したW5100が安く買えました。セットアップしたWR702Nモバイルルーターと一緒に使いArduinoをRESTクライアントとして使うサンプルを書いてみます。Ethernet libraryを使ったサンプルはたくさん見つかるのでArduinoをネットワーク接続する勉強にはちょうど良いです。

MACアドレスの生成

W5100にはMACアドレスが付いていません。ArduinoのスケッチではMACアドレスを指定する必要があるため別途用意します。
Random MAC address generator for Arduino Ethernet Shield (static MAC generator)の記事にhttps://ssl.crox.net/arduinomacというMACアドレスを生成してくれるサイトの紹介がありました。以下のようにMACアドレスを生成してコードのサンプルまで書いてくれます。

// 90:A2:DA:89:C8:50
// MAC address generated by https://ssl.crox.net/arduinomac/
// see http://blog.crox.net/archives/91-MAC-generator.html

byte mac[6] = { 0x90, 0xA2, 0xDA, 0x89, 0xC8, 0x50 };

HTTPクライアントライブラリ

Arduino用のHTTPクライアントライブラリはいくつか見つかりました。

REST APIを実行することが主な用途のためcsquared/arduino-restclientを使うことにします。

csquared/arduino-restclient

ライブラリをリポジトリからlibrariesディレクトリにgit cloneします。

$ cd ~/Documents/Arduino/libraries
$ git clone https://github.com/csquared/arduino-restclient.git RestClient

arduino-restclientのリポジトリにあるsimple_GET.inoを例にしてMeshbluからstatusを取得するスケッチを書きます。

~/Documents/Arduino/MesubluStatus/MesubluStatus.ino
#include <Ethernet.h>
#include <SPI.h>
#include "RestClient.h"

RestClient client = RestClient("meshblu.octoblu.com");

void setup() {
  Serial.begin(9600);
  byte mac[6] = { 0x90, 0xA2, 0xDA, 0x11, 0x34, 0x7E };
  Ethernet.begin(mac);
  Serial.println("Setup!");
  String response;
  int statusCode = client.get("/status", &response);
  Serial.println(response);
}

void loop() {
}

コンパイルしてArduinoに書き込みます。シリアルモニタを実行するとMesubluのステータスを取得します。

Setup!
{"meshblu":"online"}

mesublu-status.png

7
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
6