LoginSignup
1
0

More than 5 years have passed since last update.

Arduino M0 Pro でマルチタスクプログラミング : NCES IoT Base Shield : Wifi/SD機能

Last updated at Posted at 2016-05-02

はじめに

この記事では,TOPPERS/R2CAによる NCES IoT Base Shield の基本機能(Wifi/SD)の使い方について説明します.

NCES IoT Base Shield について

NCES IoT Base Shield とは,R2CA用に作成したArduinoシールドです.ESP8266によるWifi機能(UART接続)とMicroSDスロット(SPI接続)を備え,Grove Systemと互換のコネクタを持っているため,各種Grove Moduleが使用可能です.

Arduinoと互換のコネクタを持っているボードで使用可能ですが,IOのレベルが3.3Vしかサポートしていないので注意が必要です.そのため,Arduino UNO 等では使用できません.

Grove用のコネクタは,VCCは5Vを供給し信号は3.3Vから5Vに変換しています.

aFullSizeRender.jpg.jpg

Groveコネクタを実施しない場合は小型のブレッドボードを置くことができます(写真はリビジョンが古いボードです).

aFullSizeRender.jpg.jpg

必要なハードウェア

NCES IoT Base Shieldを使用するためには,この記事で説明している機材に加えて,NCES IoT Base Shield が必要です.近日中にここで購入できる予定です.

Wifi

UART接続のESP8266により実現しています.そのため,以下の記事のプログラ
ムをそのまま動作させることが出来ます.

SD

SPI接続でCSは Digital Pin 10 を使用しています.そのため,Arduinoに付属のSDライブラリを使用したサンプルを実行できます.これらのプログラムでは,CSを以下のようにグローバル変数で定義しているので,この定義を10に変更すると動作します.

libraries/SD/examples/DumpFile/DumpFile.ino
// On the Ethernet Shield, CS is pin 4. Note that even if it's not
// used as the CS pin, the hardware CS pin (10 on most Arduino boards,
// 53 on the Mega) must be left as an output or the SD library
// functions will not work.
const int chipSelect = 4;

R2CAのサンプルはBasicサンプルでSD_CARDを有効にすることで実行出来ます.

examples/Basic/r2ca_app.c
//#define BLINK
//#define toneMelody
//#define USBUART
//#define SERIALUSB
//#define SERIAL5
//#define ATTACHINTERRUPT
//#define ANALOGWRITE
//#define ANALOGREAD
//#define RTC_ALARM
/#define SD_CARD

プログラム自体は DumpFile.ino と同等で,FAT32でフォーマットしたmicroSDを差して実行することで,アナログ入力から読み込んだデータを"datalog.txt"とうファイルに書き込みます.

examples/Basic/r2ca_app.c
/*
  SD card datalogger

 This example shows how to log data from three analog sensors
 to an SD card using the SD library.

 The circuit:
 * analog sensors on analog ins 0, 1, and 2
 * SD card attached to SPI bus as follows:
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK - pin 13
 ** CS - pin 4

 created  24 Nov 2010
 modified 9 Apr 2012
 by Tom Igoe

 This example code is in the public domain.

 */

#include <SPI.h>
#include <SD.h>

// On the Ethernet Shield, CS is pin 4. Note that even if it's not
// used as the CS pin, the hardware CS pin (10 on most Arduino boards,
// 53 on the Mega) must be left as an output or the SD library
// functions will not work.
const int chipSelect = 10;

void setup()
{
  // Open serial communications and wait for port to open:
  Serial.begin(115200);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

  Serial.print("Initializing SD card...");
  // make sure that the default chip select pin is set to
  // output, even if you don't use it:
  // pinMode(10, OUTPUT);

  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }
  Serial.println("card initialized.");

  if (SD.remove("datalog.txt")) {
    Serial.println("Delete datalog.txt");
  }    
}

void loop()
{
  // make a string for assembling the data to log:
  String dataString = "";

  // read three sensors and append to the string:
  for (int analogPin = 0; analogPin < 3; analogPin++) {
    int sensor = analogRead(analogPin);
    dataString += String(sensor);
    if (analogPin < 2) {
      dataString += ",";
    }
  }

  File dataFile = SD.open("datalog.txt");
  if (dataFile) {    
    Serial.println("=== data from file ===");
    while (dataFile.available()) {
      Serial.write(dataFile.read());
    }
    dataFile.close();
  }

  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  dataFile = SD.open("datalog.txt", FILE_WRITE);

  // if the file is available, write to it:
  if (dataFile) {
    dataFile.println(dataString);
    dataFile.close();
    // print to the serial port too:
    Serial.println("=== new data to file ===");      
    Serial.println(dataString);
  }
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening datalog.txt");
  }
  delay(4000);  
}

おわりに

TOPPERS/R2CAによる NCES IoT Base Shield の基本機能(Wifi/SD)の使い方について説明しました.Wifi/SDに関しては,通常のArduinoライブラリが使用出来ます.また,R2CAではない素のArduinoでも使用可能です.

1
0
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
1
0