Twitter client, with TextLCD, Ethernet Connection, and USB Host for keyboard.

Dependencies:   EthernetNetIf TextLCD mbed

Committer:
blmarket
Date:
Thu Apr 21 05:54:26 2011 +0000
Revision:
0:70571fd24107
Initial Release

Who changed what in which revision?

UserRevisionLine numberNew contents of line
blmarket 0:70571fd24107 1 /*
blmarket 0:70571fd24107 2 Copyright (c) 2011 blmarket.net
blmarket 0:70571fd24107 3 */
blmarket 0:70571fd24107 4 /*
blmarket 0:70571fd24107 5 Copyright (c) 2010 Peter Barrett
blmarket 0:70571fd24107 6
blmarket 0:70571fd24107 7 Permission is hereby granted, free of charge, to any person obtaining a copy
blmarket 0:70571fd24107 8 of this software and associated documentation files (the "Software"), to deal
blmarket 0:70571fd24107 9 in the Software without restriction, including without limitation the rights
blmarket 0:70571fd24107 10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
blmarket 0:70571fd24107 11 copies of the Software, and to permit persons to whom the Software is
blmarket 0:70571fd24107 12 furnished to do so, subject to the following conditions:
blmarket 0:70571fd24107 13
blmarket 0:70571fd24107 14 The above copyright notice and this permission notice shall be included in
blmarket 0:70571fd24107 15 all copies or substantial portions of the Software.
blmarket 0:70571fd24107 16
blmarket 0:70571fd24107 17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
blmarket 0:70571fd24107 18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
blmarket 0:70571fd24107 19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
blmarket 0:70571fd24107 20 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
blmarket 0:70571fd24107 21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
blmarket 0:70571fd24107 22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
blmarket 0:70571fd24107 23 THE SOFTWARE.
blmarket 0:70571fd24107 24 */
blmarket 0:70571fd24107 25
blmarket 0:70571fd24107 26 #include "mbed.h"
blmarket 0:70571fd24107 27 #include "USBHost.h"
blmarket 0:70571fd24107 28 #include "Utils.h"
blmarket 0:70571fd24107 29 #include "FATFileSystem.h"
blmarket 0:70571fd24107 30 #include "EthernetNetIf.h"
blmarket 0:70571fd24107 31 #include "HTTPClient.h"
blmarket 0:70571fd24107 32 #include "TextLCD.h"
blmarket 0:70571fd24107 33
blmarket 0:70571fd24107 34 Serial pc(USBTX, USBRX);
blmarket 0:70571fd24107 35 TextLCD lcd(p21, p23, p22, p27, p28, p29, p30); // rs, rw, e, d0-d3
blmarket 0:70571fd24107 36 EthernetNetIf eth(
blmarket 0:70571fd24107 37 IpAddr(110,76,72,244), //IP Address
blmarket 0:70571fd24107 38 IpAddr(255,255,255,0), //Network Mask
blmarket 0:70571fd24107 39 IpAddr(110,76,72,1), //Gateway
blmarket 0:70571fd24107 40 IpAddr(143,248,1,177) //DNS
blmarket 0:70571fd24107 41 );
blmarket 0:70571fd24107 42 HTTPClient *twitterclient = NULL; // will be set by initialization func.
blmarket 0:70571fd24107 43
blmarket 0:70571fd24107 44 u8 prevkey[8] = {0};
blmarket 0:70571fd24107 45 char keyMap[256] = {0,0,0,0,'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',0,0,0,0,0,0,0,0,0,0,0/*return*/,0,0,0,' '};
blmarket 0:70571fd24107 46
blmarket 0:70571fd24107 47 int msglen = 0;
blmarket 0:70571fd24107 48 char msgtxt[256];
blmarket 0:70571fd24107 49 bool needcls = false;
blmarket 0:70571fd24107 50
blmarket 0:70571fd24107 51 void sendTweet(void)
blmarket 0:70571fd24107 52 {
blmarket 0:70571fd24107 53 if(msglen == 0) return;
blmarket 0:70571fd24107 54 if(twitterclient == NULL) return;
blmarket 0:70571fd24107 55 HTTPMap msg;
blmarket 0:70571fd24107 56 msg["status"] = msgtxt;
blmarket 0:70571fd24107 57
blmarket 0:70571fd24107 58 twitterclient->basicAuth("blmarketmbed", "1324"); //We use basic authentication, replace with you account's parameters
blmarket 0:70571fd24107 59
blmarket 0:70571fd24107 60 //No need to retieve data sent back by the server
blmarket 0:70571fd24107 61 HTTPResult r = twitterclient->post("http://api.supertweet.net/1/statuses/update.xml", msg, NULL);
blmarket 0:70571fd24107 62 if( r == HTTP_OK )
blmarket 0:70571fd24107 63 {
blmarket 0:70571fd24107 64 printf("Tweet sent with success!\n");
blmarket 0:70571fd24107 65 lcd.cls();
blmarket 0:70571fd24107 66 lcd.printf("Tweet success!\nType Tweet msg\n");
blmarket 0:70571fd24107 67 }
blmarket 0:70571fd24107 68 else
blmarket 0:70571fd24107 69 {
blmarket 0:70571fd24107 70 printf("Problem during tweeting, return code %d\n", r);
blmarket 0:70571fd24107 71 lcd.cls();
blmarket 0:70571fd24107 72 lcd.printf("Tweet failure\n");
blmarket 0:70571fd24107 73 }
blmarket 0:70571fd24107 74 needcls = true;
blmarket 0:70571fd24107 75
blmarket 0:70571fd24107 76 msglen = 0;
blmarket 0:70571fd24107 77 msgtxt[msglen]=0;
blmarket 0:70571fd24107 78 }
blmarket 0:70571fd24107 79
blmarket 0:70571fd24107 80 void onPressedKey(u8 value)
blmarket 0:70571fd24107 81 {
blmarket 0:70571fd24107 82 if(value == 40)
blmarket 0:70571fd24107 83 {
blmarket 0:70571fd24107 84 sendTweet();
blmarket 0:70571fd24107 85 return;
blmarket 0:70571fd24107 86 }
blmarket 0:70571fd24107 87
blmarket 0:70571fd24107 88 if(keyMap[value] != 0)
blmarket 0:70571fd24107 89 {
blmarket 0:70571fd24107 90 if(msglen >= 32) return;
blmarket 0:70571fd24107 91 if(needcls)
blmarket 0:70571fd24107 92 {
blmarket 0:70571fd24107 93 lcd.cls();
blmarket 0:70571fd24107 94 needcls = false;
blmarket 0:70571fd24107 95 }
blmarket 0:70571fd24107 96 lcd.printf("%c",keyMap[value]);
blmarket 0:70571fd24107 97 msgtxt[msglen++] = keyMap[value];
blmarket 0:70571fd24107 98 msgtxt[msglen] = 0;
blmarket 0:70571fd24107 99 }
blmarket 0:70571fd24107 100 else
blmarket 0:70571fd24107 101 {
blmarket 0:70571fd24107 102 printf("Unknown keycode %d\n",value);
blmarket 0:70571fd24107 103 }
blmarket 0:70571fd24107 104 }
blmarket 0:70571fd24107 105
blmarket 0:70571fd24107 106 void HandleKeyboardCallback(u8 *data, int len)
blmarket 0:70571fd24107 107 {
blmarket 0:70571fd24107 108 if(len != 8)
blmarket 0:70571fd24107 109 {
blmarket 0:70571fd24107 110 printf("Wrong KeyboardCallback : len should be 8\n");
blmarket 0:70571fd24107 111 return;
blmarket 0:70571fd24107 112 }
blmarket 0:70571fd24107 113
blmarket 0:70571fd24107 114 for(int i=2;i<len;i++) if(data[i] != 0)
blmarket 0:70571fd24107 115 {
blmarket 0:70571fd24107 116 bool omit = false;
blmarket 0:70571fd24107 117 for(int j=2;j<len;j++)
blmarket 0:70571fd24107 118 {
blmarket 0:70571fd24107 119 if(prevkey[j] == data[i])
blmarket 0:70571fd24107 120 {
blmarket 0:70571fd24107 121 omit = true;
blmarket 0:70571fd24107 122 break;
blmarket 0:70571fd24107 123 }
blmarket 0:70571fd24107 124 }
blmarket 0:70571fd24107 125 if(omit) continue;
blmarket 0:70571fd24107 126 onPressedKey(data[i]);
blmarket 0:70571fd24107 127 }
blmarket 0:70571fd24107 128 memcpy(prevkey, data, sizeof(u8)*len);
blmarket 0:70571fd24107 129 }
blmarket 0:70571fd24107 130
blmarket 0:70571fd24107 131 int main()
blmarket 0:70571fd24107 132 {
blmarket 0:70571fd24107 133 printf("Init\n");
blmarket 0:70571fd24107 134 lcd.printf("TwitterClient\nLoading...\n");
blmarket 0:70571fd24107 135
blmarket 0:70571fd24107 136 EthernetErr ethErr = eth.setup();
blmarket 0:70571fd24107 137 if(ethErr)
blmarket 0:70571fd24107 138 {
blmarket 0:70571fd24107 139 printf("Error %d in setup.\n", ethErr);
blmarket 0:70571fd24107 140 return -1;
blmarket 0:70571fd24107 141 }
blmarket 0:70571fd24107 142 printf("\r\nSetup OK\r\n");
blmarket 0:70571fd24107 143
blmarket 0:70571fd24107 144 pc.baud(9600);
blmarket 0:70571fd24107 145 USBInit();
blmarket 0:70571fd24107 146
blmarket 0:70571fd24107 147 twitterclient = new HTTPClient();
blmarket 0:70571fd24107 148
blmarket 0:70571fd24107 149 lcd.cls();
blmarket 0:70571fd24107 150 lcd.printf("Type Tweet msg\n");
blmarket 0:70571fd24107 151 needcls = true;
blmarket 0:70571fd24107 152
blmarket 0:70571fd24107 153 while(1)
blmarket 0:70571fd24107 154 {
blmarket 0:70571fd24107 155 USBLoop();
blmarket 0:70571fd24107 156 }
blmarket 0:70571fd24107 157
blmarket 0:70571fd24107 158 /*
blmarket 0:70571fd24107 159 printf("\r\nSetting up...\r\n");
blmarket 0:70571fd24107 160
blmarket 0:70571fd24107 161 HTTPClient twitter;
blmarket 0:70571fd24107 162
blmarket 0:70571fd24107 163 HTTPMap msg;
blmarket 0:70571fd24107 164 msg["status"] = "Now I need to attach my USB keyboard to mbed"; //A good example of Key/Value pair use with Web APIs
blmarket 0:70571fd24107 165
blmarket 0:70571fd24107 166 twitter.basicAuth("blmarketmbed", "1324"); //We use basic authentication, replace with you account's parameters
blmarket 0:70571fd24107 167
blmarket 0:70571fd24107 168 //No need to retieve data sent back by the server
blmarket 0:70571fd24107 169 HTTPResult r = twitter.post("http://api.supertweet.net/1/statuses/update.xml", msg, NULL);
blmarket 0:70571fd24107 170 if( r == HTTP_OK )
blmarket 0:70571fd24107 171 {
blmarket 0:70571fd24107 172 printf("Tweet sent with success!\n");
blmarket 0:70571fd24107 173 lcd.printf("Tweet success!\n");
blmarket 0:70571fd24107 174 }
blmarket 0:70571fd24107 175 else
blmarket 0:70571fd24107 176 {
blmarket 0:70571fd24107 177 printf("Problem during tweeting, return code %d\n", r);
blmarket 0:70571fd24107 178 lcd.printf("Tweet failure\n");
blmarket 0:70571fd24107 179 }
blmarket 0:70571fd24107 180 */
blmarket 0:70571fd24107 181
blmarket 0:70571fd24107 182 return 0;
blmarket 0:70571fd24107 183 }
blmarket 0:70571fd24107 184