Modificacion de https://mbed.org/cookbook/RFID-Tweeter para soporte de un tag no enlistado y registro del mismo mediante twiter
Dependencies: EthernetNetIf HTTPClient_ToBeRemoved mbed
main.cpp@0:8df845fb13c7, 2012-10-28 (annotated)
- Committer:
- sherckuith
- Date:
- Sun Oct 28 04:49:16 2012 +0000
- Revision:
- 0:8df845fb13c7
Modificacion de https://mbed.org/cookbook/RFID-Tweeter para soporte de un tag no valido y registro en twiter.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sherckuith | 0:8df845fb13c7 | 1 | // RFID Tweeter |
sherckuith | 0:8df845fb13c7 | 2 | /* |
sherckuith | 0:8df845fb13c7 | 3 | Update: 21-06-2010 |
sherckuith | 0:8df845fb13c7 | 4 | The basic authentication service for twitter is going down. |
sherckuith | 0:8df845fb13c7 | 5 | To continue using that program, the code has been updated to use http://supertweet.net which acts as an API proxy. |
sherckuith | 0:8df845fb13c7 | 6 | Simply visit the website to setup your twitter account for this API. |
sherckuith | 0:8df845fb13c7 | 7 | See: http://www.supertweet.net/about/documentation |
sherckuith | 0:8df845fb13c7 | 8 | */ |
sherckuith | 0:8df845fb13c7 | 9 | |
sherckuith | 0:8df845fb13c7 | 10 | #include "mbed.h" |
sherckuith | 0:8df845fb13c7 | 11 | #include "ID12RFID.h" |
sherckuith | 0:8df845fb13c7 | 12 | #include "EthernetNetIf.h" |
sherckuith | 0:8df845fb13c7 | 13 | #include "HTTPClient.h" |
sherckuith | 0:8df845fb13c7 | 14 | |
sherckuith | 0:8df845fb13c7 | 15 | #include <stdio.h> |
sherckuith | 0:8df845fb13c7 | 16 | #include <stdlib.h> |
sherckuith | 0:8df845fb13c7 | 17 | #include <math.h> |
sherckuith | 0:8df845fb13c7 | 18 | |
sherckuith | 0:8df845fb13c7 | 19 | //#include <string.h> |
sherckuith | 0:8df845fb13c7 | 20 | // Function declarations |
sherckuith | 0:8df845fb13c7 | 21 | // typedef __w64 unsigned int size_t |
sherckuith | 0:8df845fb13c7 | 22 | //size_t strlen(const char *); |
sherckuith | 0:8df845fb13c7 | 23 | char *strrev(char *); |
sherckuith | 0:8df845fb13c7 | 24 | char *itoa(int, char *, int); |
sherckuith | 0:8df845fb13c7 | 25 | |
sherckuith | 0:8df845fb13c7 | 26 | |
sherckuith | 0:8df845fb13c7 | 27 | |
sherckuith | 0:8df845fb13c7 | 28 | #define TWITTER_USER "xxxx" |
sherckuith | 0:8df845fb13c7 | 29 | #define TWITTER_PASSWORD "xxxx" |
sherckuith | 0:8df845fb13c7 | 30 | |
sherckuith | 0:8df845fb13c7 | 31 | #define IDS_COUNT 4 |
sherckuith | 0:8df845fb13c7 | 32 | const int ids_list[IDS_COUNT] = {346432,340613,333651,123456}; |
sherckuith | 0:8df845fb13c7 | 33 | const char* names_list[IDS_COUNT] = {"Sophia Alava", "Angel Yaguana", "Nicolas Yaguana", "Ivan Sanchez"}; |
sherckuith | 0:8df845fb13c7 | 34 | |
sherckuith | 0:8df845fb13c7 | 35 | EthernetNetIf eth; |
sherckuith | 0:8df845fb13c7 | 36 | HTTPClient twitter; |
sherckuith | 0:8df845fb13c7 | 37 | |
sherckuith | 0:8df845fb13c7 | 38 | ID12RFID rfid(p27); |
sherckuith | 0:8df845fb13c7 | 39 | DigitalOut serching(LED1); |
sherckuith | 0:8df845fb13c7 | 40 | DigitalOut tag_present(LED2); |
sherckuith | 0:8df845fb13c7 | 41 | DigitalOut tweet_ok(LED4); |
sherckuith | 0:8df845fb13c7 | 42 | |
sherckuith | 0:8df845fb13c7 | 43 | int main() |
sherckuith | 0:8df845fb13c7 | 44 | { |
sherckuith | 0:8df845fb13c7 | 45 | |
sherckuith | 0:8df845fb13c7 | 46 | |
sherckuith | 0:8df845fb13c7 | 47 | |
sherckuith | 0:8df845fb13c7 | 48 | printf("Init\n"); |
sherckuith | 0:8df845fb13c7 | 49 | printf("\r\nSetting up...\r\n"); |
sherckuith | 0:8df845fb13c7 | 50 | EthernetErr ethErr = eth.setup(); |
sherckuith | 0:8df845fb13c7 | 51 | if(ethErr) { |
sherckuith | 0:8df845fb13c7 | 52 | printf("Error %d in setup.\n", ethErr); |
sherckuith | 0:8df845fb13c7 | 53 | return -1; |
sherckuith | 0:8df845fb13c7 | 54 | } |
sherckuith | 0:8df845fb13c7 | 55 | printf("\r\nSetup OK\r\n"); |
sherckuith | 0:8df845fb13c7 | 56 | |
sherckuith | 0:8df845fb13c7 | 57 | while (1) { |
sherckuith | 0:8df845fb13c7 | 58 | |
sherckuith | 0:8df845fb13c7 | 59 | //printf("Serching Tag...\n"); |
sherckuith | 0:8df845fb13c7 | 60 | serching = !serching; |
sherckuith | 0:8df845fb13c7 | 61 | int id = 0; |
sherckuith | 0:8df845fb13c7 | 62 | int auth = 1; |
sherckuith | 0:8df845fb13c7 | 63 | if(rfid.readable()) { |
sherckuith | 0:8df845fb13c7 | 64 | id = rfid.read(); |
sherckuith | 0:8df845fb13c7 | 65 | tag_present = 1; |
sherckuith | 0:8df845fb13c7 | 66 | printf("Tag Serial is: %d\r\n",id); |
sherckuith | 0:8df845fb13c7 | 67 | //wait(5); |
sherckuith | 0:8df845fb13c7 | 68 | for (int i = 0; i < IDS_COUNT; i++) { |
sherckuith | 0:8df845fb13c7 | 69 | if (ids_list[i] == id) { |
sherckuith | 0:8df845fb13c7 | 70 | auth = 0; |
sherckuith | 0:8df845fb13c7 | 71 | HTTPMap msg; |
sherckuith | 0:8df845fb13c7 | 72 | msg["status"] = names_list[i]; |
sherckuith | 0:8df845fb13c7 | 73 | printf("Id: %s\r\n",names_list[i]); |
sherckuith | 0:8df845fb13c7 | 74 | msg["status"] += " Login ok!"; |
sherckuith | 0:8df845fb13c7 | 75 | printf("Mensaje HTTP: %s\n\r",msg["status"]); |
sherckuith | 0:8df845fb13c7 | 76 | twitter.basicAuth(TWITTER_USER, TWITTER_PASSWORD); |
sherckuith | 0:8df845fb13c7 | 77 | HTTPResult r = twitter.post("http://api.supertweet.net/1/statuses/update.xml", msg, NULL); |
sherckuith | 0:8df845fb13c7 | 78 | if ( r == HTTP_OK ) { |
sherckuith | 0:8df845fb13c7 | 79 | printf("Tweet sent with success!\n"); |
sherckuith | 0:8df845fb13c7 | 80 | } else { |
sherckuith | 0:8df845fb13c7 | 81 | printf("Problem during tweeting, return code %d\n", r); |
sherckuith | 0:8df845fb13c7 | 82 | } |
sherckuith | 0:8df845fb13c7 | 83 | } |
sherckuith | 0:8df845fb13c7 | 84 | } |
sherckuith | 0:8df845fb13c7 | 85 | if (auth) { |
sherckuith | 0:8df845fb13c7 | 86 | printf("Tag no found in list\r\n"); |
sherckuith | 0:8df845fb13c7 | 87 | HTTPMap msg; |
sherckuith | 0:8df845fb13c7 | 88 | msg["status"] = "Tag strange attempts to access!, Tag is: "; |
sherckuith | 0:8df845fb13c7 | 89 | int num = id; |
sherckuith | 0:8df845fb13c7 | 90 | char buf[5]; |
sherckuith | 0:8df845fb13c7 | 91 | // convert id to string [buf] |
sherckuith | 0:8df845fb13c7 | 92 | itoa(num, buf, 10); |
sherckuith | 0:8df845fb13c7 | 93 | // print our string |
sherckuith | 0:8df845fb13c7 | 94 | printf("ID tag strange: %s\n", buf); |
sherckuith | 0:8df845fb13c7 | 95 | msg["status"] += buf; |
sherckuith | 0:8df845fb13c7 | 96 | printf("Mensaje HTTP: %s\n\r",msg["status"]); |
sherckuith | 0:8df845fb13c7 | 97 | twitter.basicAuth(TWITTER_USER, TWITTER_PASSWORD); |
sherckuith | 0:8df845fb13c7 | 98 | HTTPResult r = twitter.post("http://api.supertweet.net/1/statuses/update.xml", msg, NULL); |
sherckuith | 0:8df845fb13c7 | 99 | if ( r == HTTP_OK ) { |
sherckuith | 0:8df845fb13c7 | 100 | printf("Tweet sent with success!\n"); |
sherckuith | 0:8df845fb13c7 | 101 | } else { |
sherckuith | 0:8df845fb13c7 | 102 | printf("Problem during tweeting, return code %d\n", r); |
sherckuith | 0:8df845fb13c7 | 103 | |
sherckuith | 0:8df845fb13c7 | 104 | } |
sherckuith | 0:8df845fb13c7 | 105 | auth = 1; |
sherckuith | 0:8df845fb13c7 | 106 | } |
sherckuith | 0:8df845fb13c7 | 107 | tag_present = 0; |
sherckuith | 0:8df845fb13c7 | 108 | id = 0; |
sherckuith | 0:8df845fb13c7 | 109 | } |
sherckuith | 0:8df845fb13c7 | 110 | } |
sherckuith | 0:8df845fb13c7 | 111 | } |
sherckuith | 0:8df845fb13c7 | 112 | |
sherckuith | 0:8df845fb13c7 | 113 | /* |
sherckuith | 0:8df845fb13c7 | 114 | size_t strlen(const char *string) { |
sherckuith | 0:8df845fb13c7 | 115 | const char *s; |
sherckuith | 0:8df845fb13c7 | 116 | s = string; |
sherckuith | 0:8df845fb13c7 | 117 | while (*s) |
sherckuith | 0:8df845fb13c7 | 118 | s++; |
sherckuith | 0:8df845fb13c7 | 119 | return s - string; |
sherckuith | 0:8df845fb13c7 | 120 | } |
sherckuith | 0:8df845fb13c7 | 121 | */ |
sherckuith | 0:8df845fb13c7 | 122 | |
sherckuith | 0:8df845fb13c7 | 123 | char *strrev(char *str) { |
sherckuith | 0:8df845fb13c7 | 124 | char *p1, *p2; |
sherckuith | 0:8df845fb13c7 | 125 | if (!str || !*str) |
sherckuith | 0:8df845fb13c7 | 126 | return str; |
sherckuith | 0:8df845fb13c7 | 127 | for (p1 = str, p2 = str + strlen(str) - 1; p2 > p1; ++p1, --p2) { |
sherckuith | 0:8df845fb13c7 | 128 | *p1 ^= *p2; |
sherckuith | 0:8df845fb13c7 | 129 | *p2 ^= *p1; |
sherckuith | 0:8df845fb13c7 | 130 | *p1 ^= *p2; |
sherckuith | 0:8df845fb13c7 | 131 | } |
sherckuith | 0:8df845fb13c7 | 132 | return str; |
sherckuith | 0:8df845fb13c7 | 133 | } |
sherckuith | 0:8df845fb13c7 | 134 | |
sherckuith | 0:8df845fb13c7 | 135 | char *itoa(int n, char *s, int b) { |
sherckuith | 0:8df845fb13c7 | 136 | static char digits[] = "0123456789abcdefghijklmnopqrstuvwxyz"; |
sherckuith | 0:8df845fb13c7 | 137 | int i=0, sign; |
sherckuith | 0:8df845fb13c7 | 138 | |
sherckuith | 0:8df845fb13c7 | 139 | if ((sign = n) < 0) |
sherckuith | 0:8df845fb13c7 | 140 | n = -n; |
sherckuith | 0:8df845fb13c7 | 141 | do { |
sherckuith | 0:8df845fb13c7 | 142 | s[i++] = digits[n % b]; |
sherckuith | 0:8df845fb13c7 | 143 | } while ((n /= b) > 0); |
sherckuith | 0:8df845fb13c7 | 144 | if (sign < 0) |
sherckuith | 0:8df845fb13c7 | 145 | s[i++] = '-'; |
sherckuith | 0:8df845fb13c7 | 146 | s[i] = '\0'; |
sherckuith | 0:8df845fb13c7 | 147 | return strrev(s); |
sherckuith | 0:8df845fb13c7 | 148 | } |
sherckuith | 0:8df845fb13c7 | 149 |