极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 19563|回复: 8

[求助] 用arduino接受新浪微博提醒

[复制链接]
发表于 2012-4-3 15:29:47 | 显示全部楼层 |阅读模式
我想做一个用arduino来接受新浪微博消息提醒的东西

打算给arduino用wifi连接互联网

不知道有没有人见过或者尝试过类似的,想了解下是否可行
回复

使用道具 举报

发表于 2012-4-3 16:37:29 | 显示全部楼层
同求。。。。。。
回复 支持 反对

使用道具 举报

发表于 2012-4-3 23:00:57 | 显示全部楼层
貌似看过人家做过。。。。。。用网络模块+新浪的api
回复 支持 反对

使用道具 举报

发表于 2012-4-4 22:36:38 | 显示全部楼层
这个我有计划。做完鱼缸控制器后,准备做个类似的应用,信息电子钟。用12864显示个天气预报什么的。
思路先沟通下,用php做个信息抓取工具,输出成arduino能接受的格式。然后arduino 直接读取,显示。
回复 支持 反对

使用道具 举报

发表于 2012-4-6 22:28:55 | 显示全部楼层
thomas 发表于 2012-4-4 22:36
这个我有计划。做完鱼缸控制器后,准备做个类似的应用,信息电子钟。用12864显示个天气预报什么的。
思路先 ...

想法很好嘛,赞一个。
回复 支持 反对

使用道具 举报

发表于 2012-4-8 14:16:53 | 显示全部楼层
新浪微博的app有一个简单的。可以参考一下
回复 支持 反对

使用道具 举报

 楼主| 发表于 2012-4-8 15:32:43 | 显示全部楼层
jackiesue2005 发表于 2012-4-8 14:16
新浪微博的app有一个简单的。可以参考一下

请问具体是哪个?
回复 支持 反对

使用道具 举报

发表于 2012-4-12 18:18:58 | 显示全部楼层
找到一个tt的

  1. /*
  2. * Arduino ENC28J60 Ethernet shield twitter client
  3. */

  4. #include "etherShield.h"

  5. // A pin to use as input to trigger tweets
  6. #define INPUT_PIN 0

  7. // Note: This software implements a web server and a web browser.
  8. // The web server is at "myip"
  9. //
  10. // Please modify the following lines. mac and ip have to be unique
  11. // in your local area network. You can not have the same numbers in
  12. // two devices:
  13. // how did I get the mac addr? Translate the first 3 numbers into ascii is: TUX
  14. static uint8_t mymac[6] = {
  15.   0x54,0x55,0x58,0x10,0x00,0x25};

  16. static uint8_t myip[4] = {
  17.   10,0,0,25};

  18. // IP address of the twitter server to contact (IP of the first portion of the URL):
  19. // DNS look-up is a feature which we still need to add.
  20. static uint8_t websrvip[4] = {
  21.   128,121,146,228};

  22. // The name of the virtual host which you want to contact at websrvip (hostname of the first portion of the URL):
  23. #define WEBSERVER_VHOST "twitter.com"

  24. // The BLOGGACCOUNT Authorization code can be generated from
  25. // username and password of your twitter account
  26. // by using this encoder: [url]http://tuxgraphics.org/~guido/javascript/base64-javascript.html[/url]

  27. #define BLOGGACCOUNT "Authorization: Basic xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

  28. // Default gateway. The ip address of your DSL router. It can be set to the same as
  29. // websrvip the case where there is no default GW to access the
  30. // web server (=web server is on the same lan as this host)
  31. static uint8_t gwip[4] = {
  32.   10,0,0,1};
  33. //
  34. // global string buffer for twitter message:
  35. static char statusstr[150];

  36. // listen port for tcp/www:
  37. #define MYWWWPORT 80
  38. //
  39. // the password string, (only a-z,0-9,_ characters):
  40. static char password[]="secret";

  41. static volatile uint8_t start_web_client=0;  // 0=off but enabled, 1=send tweet, 2=sending initiated, 3=twitter was sent OK, 4=diable twitter notify
  42. static uint8_t contact_onoff_cnt=0;
  43. static uint8_t web_client_attempts=0;
  44. static uint8_t web_client_sendok=0;
  45. static uint8_t resend=0;

  46. int buttonState;             // the current reading from the input pin
  47. int lastButtonState = LOW;   // the previous reading from the input pin

  48. // the follow variables are longs because the time, measured in milliseconds,
  49. // will quickly become a bigger number than can be stored in an int.
  50. long lastDebounceTime = 0;  // the last time the output pin was toggled
  51. long debounceDelay = 200;   // the debounce time, increase if the output flickers

  52. #define BUFFER_SIZE 650
  53. #define DATE_BUFFER_SIZE 30
  54. static char datebuf[DATE_BUFFER_SIZE]="none";
  55. static uint8_t buf[BUFFER_SIZE+1];

  56. EtherShield es=EtherShield();

  57. uint8_t verify_password(char *str)
  58. {
  59.   // the first characters of the received string are
  60.   // a simple password/cookie:
  61.   if (strncmp(password,str,strlen(password))==0){
  62.     return(1);
  63.   }
  64.   return(0);
  65. }

  66. // analyse the url given
  67. // return values: -1 invalid password
  68. //                -2 no command given
  69. //                0 switch off
  70. //                1 switch on
  71. //
  72. //                The string passed to this function will look like this:
  73. //                /?mn=1&pw=secret HTTP/1.....
  74. //                / HTTP/1.....
  75. int8_t analyse_get_url(char *str)
  76. {
  77.   uint8_t mn=0;
  78.   char kvalstrbuf[10];
  79.   // the first slash:
  80.   if (str[0] == '/' && str[1] == ' '){
  81.     // end of url, display just the web page
  82.     return(2);
  83.   }
  84.   // str is now something like ?pw=secret&mn=0 or just end of url
  85.   if (es.ES_find_key_val(str,kvalstrbuf,10,"mn")){
  86.     if (kvalstrbuf[0]=='1'){
  87.       mn=1;
  88.     }
  89.     // to change the mail notification one needs also a valid passw:
  90.     if (es.ES_find_key_val(str,kvalstrbuf,10,"pw")){
  91.       if (verify_password(kvalstrbuf)){
  92.         return(mn);
  93.       }
  94.       else{
  95.         return(-1);
  96.       }
  97.     }
  98.   }
  99.   // browsers looking for /favion.ico, non existing pages etc...
  100.   return(-1);
  101. }
  102. uint16_t http200ok(void)
  103. {
  104.   return(es.ES_fill_tcp_data_p(buf,0,PSTR("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nPragma: no-cache\r\n\r\n")));
  105. }


  106. // prepare the webpage by writing the data to the tcp send buffer
  107. uint16_t print_webpage(uint8_t *buf)
  108. {
  109.   uint16_t plen;
  110.   char vstr[5];
  111.   plen=http200ok();
  112.   plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<h2>Twitter notification status</h2>\n<pre>\n"));
  113.   plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("Number of INPUT_PIN to GND connections: "));
  114.   // convert number to string:
  115.   itoa(contact_onoff_cnt,vstr,10);
  116.   plen=es.ES_fill_tcp_data(buf,plen,vstr);
  117.   plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("\nNumber of twitter attempts: "));
  118.   // convert number to string:
  119.   itoa(web_client_attempts,vstr,10);
  120.   plen=es.ES_fill_tcp_data(buf,plen,vstr);
  121.   plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("\nNumber successful twitter: "));
  122.   // convert number to string:
  123.   itoa(web_client_sendok,vstr,10);
  124.   plen=es.ES_fill_tcp_data(buf,plen,vstr);
  125.   plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("\nDate of last twitter: "));
  126.   plen=es.ES_fill_tcp_data(buf,plen,datebuf);
  127.   plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("\nTwitter notify is: "));
  128.   if (start_web_client==4){
  129.     plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("OFF"));
  130.   }
  131.   else{
  132.     plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("ON"));
  133.   }
  134.   plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("\n<form action=/ method=get>\npassw: <input type=password size=8 name=pw><input type=hidden name=mn "));
  135.   if (start_web_client==4){
  136.     plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("value=1><input type=submit value="enable twitter msg">"));
  137.   }
  138.   else{
  139.     plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("value=0><input type=submit value="disable twitter msg">"));
  140.   }
  141.   plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("</form>\n</pre><br><hr>tuxgraphics.org/blog.thiseldo.co.uk"));
  142.   return(plen);
  143. }

  144. void store_date_if_found(char *str)
  145. {
  146.   uint8_t i=100; // search the first 100 char
  147.   uint8_t j=0;
  148.   char datekeyword[]="Date: "; // not any repeating characters, search does not need to resume at partial match
  149.   while(i && *str){
  150.     if (j && datekeyword[j]=='\0'){
  151.       // found date
  152.       i=0;
  153.       while(*str && *str!='\r' && *str!='\n' && i< DATE_BUFFER_SIZE-1){
  154.         datebuf[i]=*str;
  155.         str++;
  156.         i++;
  157.       }
  158.       datebuf[i]='\0';
  159.       return;
  160.     }
  161.     if (*str==datekeyword[j]){
  162.       j++;
  163.     }
  164.     else{
  165.       j=0;
  166.     }
  167.     str++;
  168.     i--;
  169.   }
  170. }

  171. void browserresult_callback(uint8_t statuscode,uint16_t datapos){
  172.   if (statuscode==0){
  173.     web_client_sendok++;
  174.     //    LEDOFF;
  175.     // copy the "Date: ...." as returned by the server
  176.     store_date_if_found((char *)&(buf[datapos]));
  177.   }
  178.   // clear pending state at sucessful contact with the
  179.   // web server even if account is expired:
  180.   if (start_web_client==2) start_web_client=3;
  181. }


  182. void setup(){

  183.   //  pinMode( LED_PIN, OUTPUT );
  184.   pinMode( INPUT_PIN, INPUT );
  185.   digitalWrite( INPUT_PIN, HIGH );    // Set internal pullup

  186.   /*initialize enc28j60*/
  187.   es.ES_enc28j60Init(mymac);

  188.   //init the ethernet/ip layer:
  189.   es.ES_init_ip_arp_udp_tcp(mymac,myip, MYWWWPORT);

  190.   // init the web client:
  191.   es.ES_client_set_gwip(gwip);  // e.g internal IP of dsl router
  192.   es.ES_client_set_wwwip(websrvip);

  193. }

  194. void loop(){
  195.   uint16_t dat_p;
  196.   int8_t cmd;
  197.   start_web_client=1;


  198.   while(1){
  199.     // handle ping and wait for a tcp packet
  200.     dat_p=es.ES_packetloop_icmp_tcp(buf,es.ES_enc28j60PacketReceive(BUFFER_SIZE, buf));
  201.     if(dat_p==0){
  202.       if (start_web_client==1){
  203.         start_web_client=2;
  204.         web_client_attempts++;
  205.         // the twitter line is status=Url_encoded_string
  206.         strcat(statusstr,"status=");
  207.         // the text to send to twitter (append after status=):
  208.         //urlencode("Meals are ready",&(statusstr[7]));
  209.         es.ES_urlencode("Test of Arduino and ENC28J60 Ethernet Shield twitter client 1234567890",&(statusstr[7]));
  210.         // The BLOGGACCOUNT Authorization code can be generated from
  211.         // username and password of your twitter account
  212.         // by using this encoder: [url]http://tuxgraphics.org/~guido/javascript/base64-javascript.html[/url]
  213.         es.ES_client_http_post(PSTR("/statuses/update.xml"),PSTR(WEBSERVER_VHOST),PSTR(BLOGGACCOUNT),NULL,statusstr,&browserresult_callback);
  214.       }
  215.       buttonState = digitalRead(INPUT_PIN);

  216.       // check to see if you just pressed the button
  217.       // (i.e. the input went from HIGH to LOW),  and you've waited
  218.       // long enough since the last press to ignore any noise:  
  219.       if ((buttonState == LOW) &&
  220.         (lastButtonState == HIGH) &&
  221.         (millis() - lastDebounceTime) > debounceDelay) {
  222.         contact_onoff_cnt++;
  223.         // ... and store the time of the last button press
  224.         // in a variable:
  225.         lastDebounceTime = millis();

  226.         // Trigger a tweet        
  227.         resend=1; // resend once if it failed
  228.         start_web_client=1;

  229.       }

  230.       // save the buttonState.  Next time through the loop,
  231.       // it'll be the lastButtonState:
  232.       lastButtonState = buttonState;

  233.       continue;
  234.     }

  235.     if (strncmp("GET ",(char *)&(buf[dat_p]),4)!=0){
  236.       // head, post and other methods:
  237.       //
  238.       // for possible status codes see:
  239.       // [url]http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html[/url]
  240.       dat_p=http200ok();
  241.       dat_p=es.ES_fill_tcp_data_p(buf,dat_p,PSTR("<h1>200 OK</h1>"));
  242.       goto SENDTCP;
  243.     }
  244.     cmd=analyse_get_url((char *)&(buf[dat_p+4]));
  245.     // for possible status codes see:
  246.     // [url]http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html[/url]
  247.     if (cmd==-1){
  248.       dat_p=es.ES_fill_tcp_data_p(buf,0,PSTR("HTTP/1.0 401 Unauthorized\r\nContent-Type: text/html\r\n\r\n<h1>401 Unauthorized</h1>"));
  249.       goto SENDTCP;
  250.     }
  251.     if (cmd==1 && start_web_client==4){
  252.       // twitter was off, switch on
  253.       start_web_client=0;
  254.     }
  255.     if (cmd==0 ){
  256.       start_web_client=4; // twitter off
  257.     }
  258.     dat_p=http200ok();
  259.     dat_p=print_webpage(buf);
  260.     //
  261. SENDTCP:
  262.     es.ES_www_server_reply(buf,dat_p); // send data

  263.   }

  264. }

复制代码
回复 支持 反对

使用道具 举报

发表于 2012-4-13 11:56:06 | 显示全部楼层
我最近也在计划做这个,核心应该就是用arduino来做WebClient,然后取得服务器端反回的信息。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-3-29 01:25 , Processed in 0.044700 second(s), 24 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表