source: rtos_arduino/trunk/arduino_lib/libraries/Ethernet2/src/Twitter.cpp@ 136

Last change on this file since 136 was 136, checked in by ertl-honda, 8 years ago

ライブラリとOS及びベーシックなサンプルの追加.

File size: 1.9 KB
Line 
1/*
2 Twitter.cpp - Arduino library to Post messages to Twitter using OAuth.
3 Copyright (c) NeoCat 2010-2011. All right reserved.
4
5 This library is distributed in the hope that it will be useful,
6 but WITHOUT ANY WARRANTY; without even the implied warranty of
7 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
8 */
9
10// ver1.2 - Use <string.h>
11// ver1.3 - Support IDE 1.0
12
13#include <string.h>
14#include "Twitter.h"
15
16#define LIB_DOMAIN "arduino-tweet.appspot.com"
17
18#if defined(ARDUINO) && ARDUINO < 100
19static uint8_t server[] = {0,0,0,0}; // IP address of LIB_DOMAIN
20Twitter::Twitter(const char *token) : client(server, 80), token(token)
21{
22}
23#else
24Twitter::Twitter(const char *token) : token(token)
25{
26}
27#endif
28
29bool Twitter::post(const char *msg)
30{
31#if defined(ARDUINO) && ARDUINO < 100
32 DNSError err = EthernetDNS.resolveHostName(LIB_DOMAIN, server);
33 if (err != DNSSuccess) {
34 return false;
35 }
36#endif
37 parseStatus = 0;
38 statusCode = 0;
39#if defined(ARDUINO) && ARDUINO < 100
40 if (client.connect()) {
41#else
42 if (client.connect(LIB_DOMAIN, 80)) {
43#endif
44 client.println("POST http://" LIB_DOMAIN "/update HTTP/1.0");
45 client.print("Content-Length: ");
46 client.println(strlen(msg)+strlen(token)+14);
47 client.println();
48 client.print("token=");
49 client.print(token);
50 client.print("&status=");
51 client.println(msg);
52 } else {
53 return false;
54 }
55 return true;
56}
57
58bool Twitter::checkStatus(Print *debug)
59{
60 if (!client.connected()) {
61 if (debug)
62 while(client.available())
63 debug->print((char)client.read());
64 client.flush();
65 client.stop();
66 return false;
67 }
68 if (!client.available())
69 return true;
70 char c = client.read();
71 if (debug)
72 debug->print(c);
73 switch(parseStatus) {
74 case 0:
75 if (c == ' ') parseStatus++; break; // skip "HTTP/1.1 "
76 case 1:
77 if (c >= '0' && c <= '9') {
78 statusCode *= 10;
79 statusCode += c - '0';
80 } else {
81 parseStatus++;
82 }
83 }
84 return true;
85}
86
87int Twitter::wait(Print *debug)
88{
89 while (checkStatus(debug));
90 return statusCode;
91}
Note: See TracBrowser for help on using the repository browser.