위즈네트 아카데미

TUTORIAL

튜토리얼

Home  > 튜토리얼

Wireless WizFi250 Twitter Example for Arudino

WIZnet Academy 2014.11.13 11:29 조회 수 : 19

Step 1 : 구성도


Arduino에서 Reference 하는 예제는 Twitter Server에 직접 접속하는 구조가 아닌, 별도의 Proxy Server를 이용하여 Twitter Server에 접속하여 Message를 Post 합니다. 참고로 본 포스팅에서 사용하는 Proxy Server는 arduino-tweet.appspot.com 이며 Post는 1분에 한번 씩 가능 합니다.

본 포스팅 예제의 구성도는 아래와 같습니다.
WizFi250_Twitter_Example9

 

Step 2 : Get a token to post a message using OAuth


http://arduino-tweet.appspot.com/oauth/twitter/login


WizFi250_Twitter_Example1

WizFi250_Twitter_Example2
WizFi250_Twitter_Example3

 

Step 3 : Hardware


Arduino Mega와 WizFi250 입니다.
WizFi250_Twitter_Example8

 


    • WIZnet에서는 SPI 인터페이스 기반의 Arduino Library 를 제공하며, 아래의 경로에서 다운로드가 가능합니다.



https://github.com/Wiznet/Arduino_WizFi250


    • WizFi250을 Aruduino와 사용하기 위해서는 아래 포스팅을 참고하세요.



WizFi250-EVB(Arduino Shield) 이용 시, Check 사항

Step 4: Software


아래 예제는 https://github.com/Wiznet/Arduino_WizFi250 의 Software/WizFi250/Example/WizFi250_Twitter 폴더에서 다운로드가 가능합니다.

[sourcecode language="plain"]
#include <Arduino.h>
#include <SPI.h>
#include <IPAddress.h>
#include "WizFi250.h"
#include "WizFi250_tcp_client.h"

#define SSID "WiznetKaizen"
#define KEY "qazxswedc"
#define AUTH ""

#define REMOTE_PORT 80
#define TOKEN "Your-Token" // For Wiznet_Maker

#define LIB_DOMAIN "arduino-tweet.appspot.com"

char server[] = LIB_DOMAIN;
unsigned long lastConnectionTime = 0;
const unsigned long postingInterval = 60000;

boolean Wifi_setup = false;
boolean lastConnected = false;
boolean isFirst = true;

WizFi250 wizfi250;
WizFi250_TCP_Client myClient(server, REMOTE_PORT);

//The setup function is called once at startup of the sketch
void setup()
{
// Add your initialization code here
String str;
char ssid[50],key[50];

Serial.begin(9600);
Serial.println("rnSerial Init");

wizfi250.begin();
wizfi250.setDebugPrint(4);
wizfi250.hw_reset();

wizfi250.sync();
wizfi250.setDhcp();

Serial.println("Input SSID");
str = getStr();
str.toCharArray(ssid,50);
Serial.println("Input KEY");
str = getStr();
str.toCharArray(key,50);

for(int i=0; i<10; i++) // Try to join 30 times
{
if( wizfi250.join(ssid,key,AUTH) == RET_OK )
{
Wifi_setup = true;
break;
}
}
}

void loop()
{
if( Wifi_setup )
{
wizfi250.RcvPacket();

if( myClient.available() )
{
char c = myClient.recv();
if( c != NULL )
Serial.print(c);
}
else
{
if( !myClient.getIsConnected() && lastConnected )
{
Serial.println();
Serial.println("disconnecting.");
myClient.stop();
}


if( isFirst || (((millis() - lastConnectionTime) > postingInterval) && (!myClient.getIsConnected())) )
{
Serial.println("Ready to transmit message");
Twitter_Post( inputSerialMessage() );
isFirst = false;
}

lastConnected = myClient.getIsConnected();
}
}
}

String inputSerialMessage()
{
String SendingMessage="";

Serial.print("Input Your Name :");
SendingMessage = "Name : ";
SendingMessage += getStr();
SendingMessage += "rn";

Serial.print("Input Your Comment :");
SendingMessage += "Comment : ";
SendingMessage += getStr();
SendingMessage += "rn";

return SendingMessage;
}


String getStr()
{
String str="";

while(1)
{
if( Serial.available() )
{
char inChar = (char)Serial.read();
Serial.print(inChar);

if(inChar == 'r') break;
str += inChar;
}
}
return str;
}


bool Twitter_Post(String thisData)
{
uint8_t content_len[6]={0};
String TxData;
uint16_t length;

Serial.println("Connecting..");
if(myClient.connect() == RET_OK)
{
TxData = "POST http://";
TxData += LIB_DOMAIN;
TxData += "/update HTTP/1.0rn";
TxData += "Content-Length: ";

length = thisData.length() + strlen(TOKEN) + 14;
itoa(length, (char*)content_len, 10);

TxData += (char*)content_len;
TxData += "rnrn";
TxData += "token=";
TxData += TOKEN;
TxData += "&status=";
TxData += thisData;
TxData += "rn";

Serial.print(TxData);
myClient.send((String)TxData);

lastConnectionTime = millis();
}
}
[/sourcecode]

5:Demo


1. 환경설정
Linux Terminal 설정

  • Minicom 실행
    WizFi250_Twitter_Example4

  • Serial Setup

  • dmesg|grp tty를 수행하면 Linux 상에서 사용중인 Serial Port 를 확인할 수 있다.WizFi250_Twitter_Example6WizFi250_Twitter_Example5


2. 실행화면

[sourcecode language="plain"]
AT+WNET=1
[OK]
Input SSID

Input KEY
qazxswedc
<Send AT Command>
AT+WLEAVE
[OK]
<Send AT Command>
AT+WSET=0,WiznetKaizen
[OK]
<Send AT Command>
AT+WSEC=0,,qazxswedc
[OK]
<Send AT Command>
AT+WJOIN
Joining : WiznetKaizen
Successfully joined : WiznetKaizen
[Link-Up Event]
IP Addr : 192.168.201.12
Gateway : 192.168.201.1
[OK]Ready to transmit message
Input Your Name : WizFi250
Comment : Hi I am WizFi250
Connecting..

<Send AT Command>
AT+FDNS=arduino-tweet.appspot.com,1000
74.125.203.141
[OK]
<Send AT Command>
AT+SCON=O,TCN,74.125.203.141,80,,0
[OK]
[CONNECT 0]POST http://arduino-tweet.appspot.com/update HTTP/1.0
Content-Length: 111

token=2815954261-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX:status=Name : WizFi250
Comment : Hi I am WizFi250


<Send AT Command>
AT+SSEND=0,0.0.0.0,80,191
[0,,,191]
[OK]HTTP/1.0 200 OK
Content-Type: text/html; charset=utf-8
Cache-Control: no-cache
Vary: Accept-Encoding
Date: Tue, 23 Sep 2014 04:03:08 GMT
Server: Google Frontend
Alternate-Protocol: 80:quic,p=0.002

OK<Disassociate>

disconnecting.
[/sourcecode]

3. Twitter 계정에서 메시지 확인

WizFi250_Twitter_Example7

 

아래는 데모 동영상입니다.