ピックワールド(PIC World)

インフォメーション

この記事は 2013年05月21日 に以下のカテゴリに投稿されました Arduino Blog.

この記事のタグ

, , , , , , , , , , ,


Arduino Ethernet シールド – 超簡単に SD カードにページを格納

こんなにかんたんで良いのだろうか?

サンプルから2つのファイルを参考にさせてもらって、さらっとかいたのがこれ。
Arduino UNO + Arduino Ethernet shield で、主として Ethernet Liblary と SD Liblary を使ってみた。
内容は、DHCP でアドレス取得して、このブログの RSS 情報を取得するという物。
ちょいコードが、まだまだなところあると思うけど、なんと5分ぐらいで動いちゃう。
他にもいろいろやりたかったので、他のライブラリを読み込んだり、これどうやって使うのよ?みたいな記述もあるけど、とりあえずコードのせておきます。

/*
Web client - SD カードに Web データを格納する

This sketch connects to a website (http://www.google.com)
using an Arduino Wiznet Ethernet shield.

Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13

created 18 Dec 2009
modified 9 Apr 2012
by David A. Mellis

*/

#include <SPI.h>
#include <Ethernet.h>
#include <string.h>
#include <WString.h>
#include <SD.h>

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0x00, 0x09, 0x41, 0x24, 0xBB, 0xB9 };
IPAddress server(210,163,10,206); // picworld.jp

boolean isDisconnect = false;

String contents;
String str1;
char c;
int cr = 0;
int cr_max = 10;
int flg1 = 0;
const int chipSelect = 4;

// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;

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

// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore:
for(;;)
;
}
// give the Ethernet shield a second to initialize:
delay(1000);
Serial.println("connecting...");

// if you get a connection, report back via serial:
if (client.connect(server, 80)) {
Serial.println("connected");
// Make a HTTP request:
client.println("GET /feed/");
client.println();
}
else {
// kf you didn't get a connection to the server:
Serial.println("connection failed");
}
Serial.print(F("Initializing SD card..."));

// SSピン(Unoは10番、Megaは53番)は使わない場合でも出力にする必要があります。
// そうしないと、SPIがスレーブモードに移行し、SDライブラリが動作しなくなります。
pinMode(SS, OUTPUT);
// SDライブラリを初期化
if (!SD.begin(chipSelect)) {
Serial.println(F("Card failed, or not present"));
// 失敗、何もしない
while(1);
}
Serial.println(F("ok."));
}

void loop()
{

// if there are incoming bytes available
// from the server, read them and print them:

// ファイルを開く
File dataFile = SD.open("datalog.txt", FILE_WRITE);

// もしファイルが開けたか確認
if (dataFile) {
Serial.println("ファイル (datalog.txt)が正常に開かれました");
}
// ファイルが開けなかったらエラーを出力
else {
Serial.println(F("ファイル(datalog.txt)を開くことができませんでした"));
}

for (;;){

// Web からデータを受信してファイルに書き込む

if (client.available()) {

for (;;){ // 無限ループを作り、Web からのデータを読み込む
c = client.read();
if (c == 'n' || c == 'r'){ // CR または LF だったらループを抜ける
break;
}
if ( c != ' ' ){
str1 += c; // CR または LF 以外の場合でかつスペースでなければ str1 にデータを格納する
}
}
/*
// 必要なデータを抽出する
// title / pubDate / dc:creator / description
// String クラスを使う

if ( str1.indexOf("<title>") >= 0 ){ // title の抽出
//str1 = str1.replace("<title>",""); // タグの消去
//str1 = str1.replace("</title>","");
Serial.print(str1);
str1 = "";
}
if ( str1.indexOf("<pubDate>") >= 0 ){ // pubDate の抽出
//str1 = str1.replace("<pubDate>",""); // タグの消去
//str1 = str1.replace("</pubDate>","");
Serial.print(str1);
str1 = "";
}
if ( str1.indexOf("<dc:creator>") >= 0 ){ // dc:creator の抽出
//str1 = str1.replace("<dc:creator>",""); // タグの消去
//str1 = str1.replace("<.dc:creator>","");
Serial.print(str1);
str1 = "";
}
if ( str1.indexOf("<description>") >= 0 ) { // description の抽出
//str1 = str1.replace("<description>",""); // タグの消去
//str1 = str1.replace("</description>","");
Serial.print(str1);
str1 = "";
}
*/

dataFile.println(str1);
Serial.print(str1);
str1 = "";

// if the server's disconnected, stop the client:
if (!client.connected() && !isDisconnect) {
isDisconnect = true;
dataFile.close();
Serial.println();
Serial.println("disconnecting.");
client.stop();
}
}
}
}

処理が完了して、micro SD カードを PC のカードリーダに差し込み内容を確認すると、ちゃんと RSS が書き込まれてます。


コメントを残す

最近の投稿

最近のコメント

アーカイブ