WizFi250 Twitter Example

details

WizFi250 Twitter Example

 Arduino provides the twitter example code. This code sends the twitting message to the server via proxy server (arduino-tweet.appsopt.com). In this posting, we are going to explain how to send the twitting message using Arduino twitter example and WizFi250 (the Wi-Fi module).

Step 1 : System Configuration

WizFi250 Twitter 01

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

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

WizFi250 Twitter 02

 

WizFi250 Twitter 03

WizFi250 Twitter 04

Step 3 : Hardware

We connect the WizFi250-EVT with Arduino Mega board as below picture.

WizFi250 Twitter 05

 

Step 4: Software

You can download below source code from https://github.com/Wiznet/Arduino_WizFi250. The directory is located in ‘Software/WizFi250/Example/WizFi250_Twitter’

 

#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(“\r\nSerial 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 += “\r\n”;

 

Serial.print(“Input Your Comment :”);

SendingMessage += “Comment : “;

SendingMessage += getStr();

SendingMessage += “\r\n”;

 

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.0\r\n”;

TxData += “Content-Length: “;

 

length = thisData.length() + strlen(TOKEN) + 14;

itoa(length, (char*)content_len, 10);

 

TxData += (char*)content_len;

TxData += “\r\n\r\n”;

TxData += “token=”;

TxData += TOKEN;

TxData += “&status=”;

TxData += thisData;

TxData += “\r\n”;

 

Serial.print(TxData);

myClient.send((String)TxData);

 

lastConnectionTime = millis();

}

}

Step 5: Demonstration

1) Configuration To configure Linux Terminal

  • Execute Minicom

WizFi250 Twitter 06

  • Serial Setup

 

  • If you execute ‘dmesg|grp tty’, you can check the serial port running on Linux

WizFi250 Twitter 07

WizFi250 Twitter 08

 

 

2) Execution Screen

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.

 

 

  1. To check the message at the Twitter

WizFi250 Twitter 09
Below is the demonstration video

 

WizFi250 Twitter Example

 Arduino provides the twitter example code. This code sends the twitting message to the server via proxy server (arduino-tweet.appsopt.com). In this posting, we are going to explain how to send the twitting message using Arduino twitter example and WizFi250 (the Wi-Fi module).

Step 1 : System Configuration

WizFi250 Twitter 01

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

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

WizFi250 Twitter 02

 

WizFi250 Twitter 03

WizFi250 Twitter 04

Step 3 : Hardware

We connect the WizFi250-EVT with Arduino Mega board as below picture.

WizFi250 Twitter 05

 

Step 4: Software

You can download below source code from https://github.com/Wiznet/Arduino_WizFi250. The directory is located in ‘Software/WizFi250/Example/WizFi250_Twitter’

 

#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(“\r\nSerial 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 += “\r\n”;

 

Serial.print(“Input Your Comment :”);

SendingMessage += “Comment : “;

SendingMessage += getStr();

SendingMessage += “\r\n”;

 

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.0\r\n”;

TxData += “Content-Length: “;

 

length = thisData.length() + strlen(TOKEN) + 14;

itoa(length, (char*)content_len, 10);

 

TxData += (char*)content_len;

TxData += “\r\n\r\n”;

TxData += “token=”;

TxData += TOKEN;

TxData += “&status=”;

TxData += thisData;

TxData += “\r\n”;

 

Serial.print(TxData);

myClient.send((String)TxData);

 

lastConnectionTime = millis();

}

}

Step 5: Demonstration

1) Configuration To configure Linux Terminal

  • Execute Minicom

WizFi250 Twitter 06

  • Serial Setup

 

  • If you execute ‘dmesg|grp tty’, you can check the serial port running on Linux

WizFi250 Twitter 07

WizFi250 Twitter 08

 

 

2) Execution Screen

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.

 

 

  1. To check the message at the Twitter

WizFi250 Twitter 09
Below is the demonstration video

 

COMMENTS

Please Login to comment
  Subscribe  
Notify of