Ch4_7 RFID 데이터를 트위터에 올리기

학습 내용

RFID 태크에서 인식된 신호를 트위터에 나타낼수있는 임베디드 클라우드 컴퓨팅( Embedded Cloud Computing: "Internet of Things")을 간단히 구현 해볼수 있습니다.

500

배선도 & 회로도

_하드웨어 구성요소_

필요한 하드웨어 구성 요소는:

_하드웨어 셋업_

하드웨어 셋업사진:

http://mbed.org/media/uploads/donatien/schematic.png

먼저 브레드보드에 mbed를 설치하고 접지를 (GND)핀과 브레드보드의 접지라인을 연결한 후 USB 파워핀(VU) 파워라인에 연결합니다.

RFID Reader

  • 5V (pin 11) 과 RST (pin 2) 핀을 파워라인에 연결
  • GND (pin 1) 과 FS (pin 7) 핀을 접지라인에 연결
  • 마지막으로 D0 (pin 9) 을 mbed 의p14 (시리얼 입력 핀)에 연결

Ethernet Socket

  • 소켓의 pin 3 을 mbed의 RD- pin에 연결
  • 소켓의 pin 1 을 mbed의 RD+ pin에 연결
  • 소켓의 pin 4 를 mbed의 TD- pin에 연결
  • 소켓의 pin 6 을 mbed의 TD+ pin에 연결

Warning

소켓의 핀 출력은 단지 이 부품에서만 유효합니다.만약 다른 부품을 사용하면 핀 출력 포트를 재 확인해야 합니다.

배선 사진

RFID Tweeter Bradboard

마지막으로 네트워크 케이블을 소켓에 연결하면 mbed는 인터넷과 연결됩니다!

소프트웨어 설치

하드웨어를 작동하고 트위터에 연결하려면 다음의 라이브러리를 설치해야합니다:

_트위터 계정과 태그 설정_

500

Information

The basic authentication service for twitter is going down at the end of the week. To continue using that program, the code has been updated to use http://supertweet.net which acts as an API proxy. Simply visit the website to setup your twitter account for this API. See: http://www.supertweet.net/about/documentation

_Supertweet 계정 만들기_

500 500 500 500 500 500

_Supertweet 테스트 코드_

300

#include "mbed.h"
#include "EthernetNetIf.h"
#include "SuperTweetV1XML.h"

#define YOUR_ACCOUNT    "EuiSeokHong"
#define YOUR_PASSWORD   "110787"

extern "C" void mbed_reset();

EthernetNetIf net;
SuperTweetV1XML st(YOUR_ACCOUNT, YOUR_PASSWORD);

/**
 * Callback function for postStatusesUpdate.
 *
 * @param buf A pointer to a buffer.
 * @param siz A size of the buffer.
 */
void func(char *buf, size_t siz) {
#if 0
    /*
     * This is for checking a response.
     */
    for (int i = 0; i < siz; i++) {
        printf("%c", buf[i]);
    }
#endif
}

/**
 * Entry point.
 */
int main() {
    char text[BUFSIZ];
    int cnt = 0;

    EthernetErr err = net.setup();
    if (err) {
        error("Network setup failed.\n");
    }

    while (1) {
        snprintf(text, sizeof(text), "Hi! I'm mbed from ARM. Message number is %d", cnt++);
        HTTPResult r = st.postStatusesUpdate(std::string(text), func);
        printf("r=%d\n", (int)r);

        /*
         * Note:
         * I don't know why sometime it get a error.
         * I think it a bug in a mbed library.
         */
        if (r != 0) {
            printf("Resetting...\n");
            mbed_reset();
        }

        wait(5);
    }
}

_Tweet 테스트하기_

300

#include "EthernetNetIf.h"
#include "HTTPClient.h"

EthernetNetIf eth; 

int main() {

  printf("Init\n");

  printf("\r\nSetting up...\r\n");
  EthernetErr ethErr = eth.setup();
  if(ethErr)
  {
    printf("Error %d in setup.\n", ethErr);
    return -1;
  }
  printf("\r\nSetup OK\r\n");

  HTTPClient twitter;
  
  HTTPMap msg;
  msg["status"] = "I am tweeting from my mbed!"; //A good example of Key/Value pair use with Web APIs

  twitter.basicAuth("EuiSeokHong", "110787"); //We use basic authentication, replace with you account's parameters
  
  //No need to retieve data sent back by the server
  HTTPResult r = twitter.post("http://api.supertweet.net/1/statuses/update.xml", msg, NULL); 
  if( r == HTTP_OK )
  {
    printf("Tweet sent with success!\n");
  }
  else
  {
    printf("Problem during tweeting, return code %d\n", r);
  }
  
  return 0;

}

500

500

_TweetRFID 코딩_

프로그램 연결사이트:

실제로 프로그램을 작동하기 위해서는 다음의 셋업이 필요합니다.

  • TWITTER_USERTWITTER_PASSWORD 자기의 계정 파라메터를 입력한다.
  • ids_list and names_list 테이블에 등록된 ID와 이름을 입력한다.
    • IDS_COUNT에 등록된수로 업데이트 한다.
  • (msg["status"])에 본인 나타내려는 문장을 입력한다...

코딩

main.cpp

// RFID Tweeter

#include "mbed.h"
#include "ID12RFID.h"
#include "EthernetNetIf.h"
#include "HTTPClient.h"

#define TWITTER_USER "donatiengarnier"
#define TWITTER_PASSWORD "myverysecurepassword"

#define IDS_COUNT 3
const int ids_list[IDS_COUNT] = {89481809, 89481810, 89481811};
const char* names_list[IDS_COUNT] = {"Donatien", "Simon", "Dan"};

EthernetNetIf ethernet;
HTTPClient twitter;

ID12RFID rfid(p14);
DigitalOut tag_present(LED1);
DigitalOut tweet_ok(LED4);

int main() {
  ethernet.setup();
  twitter.basicAuth(TWITTER_USER, TWITTER_PASSWORD);

  while(true) {
    int id = rfid.read();
    tag_present = 1;
    for(int i = 0; i < IDS_COUNT; i++) {
      if (ids_list[i] == id) {
        HTTPMap msg;
        msg["status"] = names_list[i];
        msg["status"] += " just arrived home!";
        HTTPResult r = twitter.post("http://api.supertweet.net/1/statuses/update.xml", msg, NULL);
        tweet_ok = !r;
      }
    }
    tag_present = 0;
  }
}

라이브러리

Import libraryEthernetNetIf

This library is deprecated.

Import library

api_msg This struct contains a function to execute in another thread context and a struct api_msg_msg that serves as an argument for this function
api_msg_msg This struct includes everything that is necessary to execute a function for a netconn in another thread context (mainly used to process netconns in the tcpip_thread context to be thread safe)
dhcp_msg Minimum set of fields of any DHCP message
dns_api_msg As do_gethostbyname requires more arguments but doesn't require a netconn, it has its own struct (to avoid struct api_msg getting bigger than necessary)
DNSRequest This is a simple DNS Request class
EthernetNetIf Ethernet network interface
Host Host information container
igmp_group Igmp group structure - there is a list of groups for each interface these should really be linked from the interface, but if we keep them separate we will not affect the lwip original code too much
IpAddr IP Address container
local_hostlist_entry Struct used for local host-list
memp_malloc_helper This structure is used to save the pool one element came from
mib_array_node Derived node, points to a fixed size const array of sub-identifiers plus a 'child' pointer
mib_external_node Derived node, has access functions for mib object in external memory or device using 'tree_level' and 'idx', with a range 0
mib_list_rootnode Derived node, points to a doubly linked list of sub-identifiers plus a 'child' pointer
mib_node Node "base class" layout, the mandatory fields for a node
mib_ram_array_node Derived node, points to a fixed size mem_malloced array of sub-identifiers plus a 'child' pointer
netconn A netconn descriptor
NetService Net Service base class
obj_def Object definition returned by (get_object_def)()
pbuf_custom_ref A custom pbuf that holds a reference to another pbuf, which is freed when this custom pbuf is freed
snmp_obj_id Internal object identifier representation
snmp_resp_header_lengths Output response message header length fields
snmp_trap_header_lengths Output response message header length fields
TCPSocket This is a simple TCP Socket class
UDPSocket This is a simple UDP Socket class

Import libraryHTTPClient_ToBeRemoved

HTTP Client library

Import library

HTTPClient A simple HTTP Client The HTTPClient is composed of:

  • The actual client ( HTTPClient )
  • Classes that act as a data repository, each of which deriving from the HTTPData class ( HTTPText for short text content, HTTPFile for file I/O, HTTPMap for key/value pairs, and HTTPStream for streaming purposes)
HTTPMap Map of key/value pairs Used to transmit POST data using the application/x-www-form-urlencoded encoding
HTTPText A data endopint to store text
IHTTPDataIn This is a simple interface for HTTP data storage (impl examples are Key/Value Pairs, File, etc...)
IHTTPDataOut This is a simple interface for HTTP data storage (impl examples are Key/Value Pairs, File, etc...)

학습 참고


Please log in to post comments.