ArduinoでiDigiにアクセスしてみる。

ArduinoでiDigiにアクセスしてみました。
iDigiへhttpプロトコル、basic認証方式で接続可能です。
今回、Arduino + Ethernet Shieldの構成で/ws/DeviceCoreへアクセスした結果をシリアル出力するサンプルスケッチを作りました。


Arduinoのスケッチ

116行目のユーザ名とパスワードの箇所にbase64認証に変換した値を記入します。
1行目に変換サービスを提供しているサイトのリンク先を書きました。

   1 //http://www.functions-online.com/base64_encode.html
   2 //http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1234028200/1
   3 
   4 #include <SPI.h>
   5 #include <Ethernet.h>
   6 #include <EthernetDHCP.h>
   7 #include <EthernetDNS.h>
   8 
   9 // Enter a MAC address and IP address for your controller below.
  10 // The IP address will be dependent on your local network:
  11 byte mac[] = {  0xDE, 0xAD, 0xBE, 0x**, 0x**, 0x** };
  12 byte serveripAddr[4];
  13 // Initialize the Ethernet client library
  14 // with the IP address and port of the server 
  15 // that you want to connect to (port 80 is default for HTTP):
  16 Client client(serveripAddr, 80);
  17 int bytecount = 0;
  18 byte clientstatus;
  19 
  20   
  21 void setup() {
  22 
  23   const char* hostName = "developer.idigi.com";
  24   
  25   Serial.begin(115200);
  26   Serial.println("Attempting to obtain a DHCP lease...");
  27   // give the Ethernet shield a second to initialize:
  28   delay(1000);
  29 
  30   EthernetDHCP.begin(mac);
  31 
  32   const byte* ipAddr = EthernetDHCP.ipAddress();
  33   const byte* gatewayAddr = EthernetDHCP.gatewayIpAddress();
  34   const byte* dnsAddr = EthernetDHCP.dnsIpAddress();
  35 
  36   Serial.println("A DHCP lease has been obtained.");
  37 
  38   Serial.print("My IP address is ");
  39   Serial.println(ip_to_str(ipAddr));
  40 
  41   Serial.print("Gateway IP address is ");
  42   Serial.println(ip_to_str(gatewayAddr));
  43 
  44   Serial.print("DNS IP address is ");
  45   Serial.println(ip_to_str(dnsAddr));
  46   Serial.println();
  47 
  48   EthernetDNS.setDNSServer(dnsAddr);
  49   
  50     Serial.print("Resolving ");
  51     Serial.print(hostName);
  52     Serial.println("...");
  53     
  54     DNSError err = EthernetDNS.resolveHostName(hostName, serveripAddr);
  55    
  56     if (DNSSuccess == err) {
  57       Serial.print("The IP address is ");
  58       Serial.print(ip_to_str(serveripAddr));
  59       Serial.println(".");
  60     } else if (DNSTimedOut == err) {
  61       Serial.println("Timed out.");
  62     } else if (DNSNotFound == err) {
  63       Serial.println("Does not exist.");
  64     } else {
  65       Serial.print("Failed with error code ");
  66       Serial.print((int)err, DEC);
  67       Serial.println(".");
  68     }
  69 
  70 
  71 }
  72 
  73 void loop()
  74 {
  75     byte ret;
  76     
  77     switch(clientstatus){
  78       case 0:
  79         ret = ConnectServer();
  80         clientstatus = 1;
  81       break;
  82       case 1:
  83         GetDataFromServer();
  84         // if the server's disconnected, stop the client:
  85         if (!client.connected()) {
  86           Serial.println();
  87           Serial.println("disconnecting.");
  88           client.stop();
  89           clientstatus = 0;
  90           delay(10000);
  91         }
  92        break;
  93       }
  94 }
  95 
  96 void GetDataFromServer(){
  97   
  98   if (client.available()){
  99     char c = client.read();
 100     Serial.print(c);
 101   }
 102 }
 103 
 104 byte ConnectServer(){
 105 
 106   byte ret;
 107   Serial.print("connecting...");
 108 
 109   // if you get a connection, report back via serial:
 110   if (client.connect()) {
 111     ret = 0;
 112     Serial.println("connected");
 113 //      client.print("GET /ws/sci HTTP/1.1¥r¥n");
 114       client.print("GET /ws/XbeeAttributeCore HTTP/1.1¥r¥n");
 115       client.print("Host: developer.idigi.com¥r¥n");
 116       client.print("Authorization: Basic (ユーザー名:パスワード)");
 117       client.print("¥r¥n");
 118       
 119     client.println();
 120   } 
 121   else {
 122     // kf you didn't get a connection to the server:
 123     Serial.println("connection failed");
 124     ret = 1;
 125   }
 126   
 127   return ret;
 128 }
 129 
 130 // Just a utility function to nicely format an IP address.
 131 const char* ip_to_str(const uint8_t* ipAddr)
 132 {
 133   static char buf[16];
 134   sprintf(buf, "%d.%d.%d.%d¥0", ipAddr[0], ipAddr[1], ipAddr[2], ipAddr[3]);
 135   return buf;
 136 }
 137 
実行結果
   1 connecting...connected
   2 HTTP/1.1 200 OK
   3 Server: Apache-Coyote/1.1
   4 Cache-Control: no-store
   5 Pragma: no-cache
   6 Expires: Thu, 01 Jan 1970 00:00:00 GMT
   7 Set-Cookie: ???????????????????????????????????????????; Path=/ws; Secure
   8 Content-Type: application/xml;charset=ISO-8859-1
   9 Content-Language: en-US
  10 Transfer-Encoding: chunked
  11 Date: Thu, 01 Dec 2011 15:00:00 GMT
  12 Vary: Accept-Encoding, User-Agent
  13 
  14 fb
  15 <?xml version="1.0" encoding="ISO-8859-1"?>
  16 <result>
  17    <resultTotalRows>0</resultTotalRows>
  18    <requestedStartRow>0</requestedStartRow>
  19    <resultSize>0</resultSize>
  20    <requestedSize>1000</requestedSize>
  21    <remainingSize>0</remainingSize>
  22 </result>
  23 0
  24 
コメント
トラックバック
この記事のトラックバックURL