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