[136] | 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
|
---|
| 19 | static uint8_t server[] = {0,0,0,0}; // IP address of LIB_DOMAIN
|
---|
| 20 | Twitter::Twitter(const char *token) : client(server, 80), token(token)
|
---|
| 21 | {
|
---|
| 22 | }
|
---|
| 23 | #else
|
---|
| 24 | Twitter::Twitter(const char *token) : token(token)
|
---|
| 25 | {
|
---|
| 26 | }
|
---|
| 27 | #endif
|
---|
| 28 |
|
---|
| 29 | bool 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 |
|
---|
| 58 | bool 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 |
|
---|
| 87 | int Twitter::wait(Print *debug)
|
---|
| 88 | {
|
---|
| 89 | while (checkStatus(debug));
|
---|
| 90 | return statusCode;
|
---|
| 91 | }
|
---|