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

Committer:
sherckuith
Date:
2012-10-28
Revision:
0:8df845fb13c7

File content as of revision 0:8df845fb13c7:

// RFID Tweeter
/*
  Update: 21-06-2010
  The basic authentication service for twitter is going down.
  To continue using that program, the code has been updated to use http://supertweet.net which acts as an API proxy.
  Simply visit the website to setup your twitter account for this API.
  See: http://www.supertweet.net/about/documentation
*/

#include "mbed.h"
#include "ID12RFID.h"
#include "EthernetNetIf.h"
#include "HTTPClient.h"

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

//#include <string.h>
// Function declarations
// typedef __w64 unsigned int size_t
//size_t strlen(const char *);
char *strrev(char *);
char *itoa(int, char *, int);



#define TWITTER_USER "xxxx"
#define TWITTER_PASSWORD "xxxx"

#define IDS_COUNT 4
const int ids_list[IDS_COUNT] = {346432,340613,333651,123456};
const char* names_list[IDS_COUNT] = {"Sophia Alava", "Angel Yaguana", "Nicolas Yaguana", "Ivan Sanchez"};

EthernetNetIf eth;
HTTPClient twitter;

ID12RFID rfid(p27);
DigitalOut serching(LED1);
DigitalOut tag_present(LED2);
DigitalOut tweet_ok(LED4);

int main()
{



    printf("Init\n");
    printf("\r\nSetting up...\r\n");
    EthernetErr ethErr = eth.setup();
    if(ethErr) {
        printf("Error %d in setup.\n", ethErr);
        return -1;
    }
    printf("\r\nSetup OK\r\n");

    while (1) {

        //printf("Serching Tag...\n");
        serching = !serching;
        int id = 0;
        int auth = 1;
        if(rfid.readable()) {
            id = rfid.read();
            tag_present = 1;
            printf("Tag Serial is: %d\r\n",id);
            //wait(5);
            for (int i = 0; i < IDS_COUNT; i++) {
                if (ids_list[i] == id) {
                    auth = 0;
                    HTTPMap msg;
                    msg["status"] = names_list[i];
                    printf("Id: %s\r\n",names_list[i]);
                    msg["status"] += " Login ok!";
                    printf("Mensaje HTTP: %s\n\r",msg["status"]);
                    twitter.basicAuth(TWITTER_USER, TWITTER_PASSWORD);
                    HTTPResult r = twitter.post("http://api.supertweet.net/1/statuses/update.xml", msg, NULL);
                    if ( r == HTTP_OK ) {
                        printf("Tweet sent with success!\n");
                    } else {
                        printf("Problem during tweeting, return code %d\n", r);
                    }
                }
            }
            if (auth) {
                printf("Tag no found in list\r\n");
                HTTPMap msg;
                msg["status"] = "Tag strange attempts to access!, Tag is: ";
                int num = id;
                char buf[5];
                // convert id to string [buf]
                itoa(num, buf, 10);
                // print our string
                printf("ID tag strange: %s\n", buf);
                msg["status"] += buf;
                printf("Mensaje HTTP: %s\n\r",msg["status"]);
                twitter.basicAuth(TWITTER_USER, TWITTER_PASSWORD);
                HTTPResult r = twitter.post("http://api.supertweet.net/1/statuses/update.xml", msg, NULL);
                if ( r == HTTP_OK ) {
                    printf("Tweet sent with success!\n");
                } else {
                    printf("Problem during tweeting, return code %d\n", r);

                }
                auth = 1;
            }
            tag_present = 0;
            id = 0;
        }
    }
}

/*
size_t strlen(const char *string) {
    const char *s;
    s = string;
    while (*s)
        s++;
    return s - string;
}
*/

char *strrev(char *str) {
    char *p1, *p2;
    if (!str || !*str)
        return str;
    for (p1 = str, p2 = str + strlen(str) - 1; p2 > p1; ++p1, --p2) {
        *p1 ^= *p2;
        *p2 ^= *p1;
        *p1 ^= *p2;
    }
    return str;
}

char *itoa(int n, char *s, int b) {
    static char digits[] = "0123456789abcdefghijklmnopqrstuvwxyz";
    int i=0, sign;
    
    if ((sign = n) < 0)
        n = -n;
    do {
        s[i++] = digits[n % b];
    } while ((n /= b) > 0);
    if (sign < 0)
        s[i++] = '-';
    s[i] = '\0';
    return strrev(s);
}