Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: AD128160_kazushi_branch HTTPClient mbed PowerControl WiflyInterface
Fork of FontTest3 by

Please see at http://kazushi-lab.c.fun.ac.jp/pukiwiki/index.php?TwitterMbed
Diff: main.cpp
- Revision:
- 10:94f45d37174f
- Parent:
- 9:86dd65b94dca
--- a/main.cpp Mon Feb 25 03:26:07 2013 +0000
+++ b/main.cpp Sun Apr 28 10:05:28 2013 +0000
@@ -1,42 +1,43 @@
-// Twitter Mbed with Gain Span WiFi
+// Twitter Mbed with WiFly RN-XV
//
// coded by Kazushi Mukaiyama (http://www.kazushi.info/)
-// refer to gs fan's notebook (http://mbed.org/users/gsfan/notebook/gainspan_wifi/)
+// refer to cookbook of WiFly(https://mbed.org/cookbook/wifly)
// refer to GingaX's notebook (http://www31.atwiki.jp/gingax/pages/63.html)
#include "mbed.h"
#include "PowerControl/PowerControl.h"
#include "PowerControl/EthernetPowerControl.h"
-#include "GSwifi.h"
+#include "WiflyInterface.h"
+#include "HTTPClient.h"
#include "sjis_utf16.h"
#include "s_Lcd.h"
#define countof(x) ( sizeof(x) / sizeof(x[0]) )
-#define SECURE GSSEC_WPA_PSK
-#define SSID "WIFISSID"
-#define PASS "abcdef1234"
+#define SSID "your_ssid"
+#define PASS "your_pass"
-#define HTTP_HOST "api.supertweet.net" // SuperTweet.net
-//#define HTTP_URI "/1.1/statuses/show.json?id=281639966776369152"
-#define HTTP_URI "/1.1/statuses/user_timeline.json?screen_name=pla3c&count=1"
-#define HTTP_USER "user"
-#define HTTP_PASS "pass"
-#define INTERVAL_SEC 60.0
+#define HTTP_URI "http://api.supertweet.net/1.1/statuses/user_timeline.json?screen_name=pla3c&count=1"
+#define HTTP_USER "your_id"
+#define HTTP_PASS "your_pass"
+
+#define INTERVAL_SEC 90.0
//#define _DEBUG
-GSwifi gs(p13, p14); // TX, RX (no flow control)
+WiflyInterface wifly(p13, p14, p15, p16, SSID, PASS, WPA); // TX, RX, reset, connection_status
+HTTPClient http;
DigitalOut led1(LED1), led2(LED2);
#ifdef _DEBUG
Serial pc(USBTX, USBRX);
#endif
+char buf[512];
+
unsigned char kstatus = 0;
bool estate = false;
unsigned char unibuf[4];
unsigned char codes[512];
-bool emergency = false;
unsigned short convert(unsigned short utf){
unsigned short sjis = NULL;
@@ -49,11 +50,11 @@
return sjis;
}
-void parseCode(const unsigned char* s){
+bool parseCode(const unsigned char* s){
unsigned int i = 0;
unsigned char c;
- emergency = false;
+ bool em = false;
while((c = *s++) != '\0') {
if(estate && kstatus==4){ // 1st digit of utf-16
unibuf[3] = c;
@@ -63,7 +64,7 @@
i++;
codes[i] = code & 0xFF;
i++;
- if(code==0x8149) emergency = true;
+ if(code==0x8149) em = true;
#ifdef _DEBUG
pc.printf("%s, %x\r\n", unibuf, code);
#endif
@@ -89,11 +90,13 @@
} else { // 4x8font
codes[i] = c;
i++;
- if(c=='!') emergency = true;
+ if(c=='!') em = true;
}
}
codes[i]= '\0';
+
+ return em;
}
void drawString(const unsigned char *s){
@@ -102,55 +105,28 @@
}
int connect(){
- IpAddr ipaddr, netmask, gateway, nameserver;
-
- //gs.command("AT&F", GSRES_NORMAL);
+ char localip[20];
- if (gs.connect(SECURE, SSID, PASS, 1, 5)) {
- drawString("WiFi connection error"); newline();
-#ifdef _DEBUG
- pc.printf("WiFi connection error\r\n");
-#endif
- return -1;
- }
+ wifly.init(); //Use DHCP
+ while (!wifly.connect());
- gs.getAddress(ipaddr, netmask, gateway, nameserver);
- if(nameserver.isNull()) {
- nameserver = IpAddr(192,168,1,1);
- gs.setAddress(ipaddr, netmask, gateway, nameserver);
- gs.getAddress(ipaddr, netmask, gateway, nameserver);
- }
#ifdef _DEBUG
- pc.printf("ip %d.%d.%d.%d\r\n", ipaddr[0], ipaddr[1], ipaddr[2], ipaddr[3]);
- pc.printf("gw %d.%d.%d.%d\r\n", gateway[0], gateway[1], gateway[2], gateway[3]);
- pc.printf("ns %d.%d.%d.%d\r\n", nameserver[0], nameserver[1], nameserver[2], nameserver[3]);
+ pc.printf("ip %s\r\n", wifly.getIPAddress());
#endif
- char localip[20];
- sprintf(localip, "ip %d.%d.%d.%d", ipaddr[0], ipaddr[1], ipaddr[2], ipaddr[3]);
- drawString((unsigned char*)localip); newline();
- sprintf(localip, "gw %d.%d.%d.%d", gateway[0], gateway[1], gateway[2], gateway[3]);
+
+ sprintf(localip, "ip %s", wifly.getIPAddress());
drawString((unsigned char*)localip); newline();
- sprintf(localip, "ns %d.%d.%d.%d", nameserver[0], nameserver[1], nameserver[2], nameserver[3]);
- drawString((unsigned char*)localip); newline();
-
- delete [] localip;
-
+
return 0;
}
-void onGsReceive (int cid, int len) {
- int i;
- char buf[GS_DATA_SIZE + 1];
-
+void parseBuffer () {
led2 = 1;
- i = gs.recv(cid, buf, len);
- buf[i] = 0;
// parse buffer
- char* c = strtok(buf, "\r\n");
- if(strcmp(c, "200 OK")==0){
- while(c!=NULL){
- c = strtok(NULL, "\"");
+ char* c = strtok(buf, "\"");
+ while(c!=NULL){
+ char* c = strtok(NULL, "\"");
char key[4];
sprintf(key, "%.4s", c);
if(strcmp(key, "text")==0){
@@ -159,35 +135,29 @@
#ifdef _DEBUG
pc.printf("%s\r\n", c);
#endif
- //c = strtok(c, "@");
- parseCode((unsigned char*)c);
+ bool emergency = parseCode((unsigned char*)c);
if(emergency) color(0xf800); else color(0x001f);
cls();
color(0xffff);
drawString(codes); newline();
- //c = strtok(NULL, "@");
- //drawString((unsigned char*)c); newline();
+ if(emergency) bmp(0,80,1); else bmp(0,80,0); // comment out if you don't use on-memory images
break;
}
}
- }
led2 = 0;
-
- delete [] buf;
}
int main() {
int r;
char* msg;
- Host host;
led1 = 0;
PHY_PowerDown();
Peripheral_PowerDown(0x7D7F6FE1);
- gs.setBaud(115200);
+
#ifdef _DEBUG
- pc.baud(115200);
+ pc.baud(9600);
#endif
s_Lcdinit();
@@ -203,27 +173,45 @@
NVIC_SystemReset();
}
+ wait_ms(1000);
+
msg = "start twitter";
drawString((unsigned char*)msg); newline();
+
+ http.basicAuth(HTTP_USER, HTTP_PASS);
+
+ wait_ms(1000);
- host.setName(HTTP_HOST);
-
while(1) {
led1 = !led1; wait_ms(500); led1 = !led1; // blink
- r = gs.httpGet(host, HTTP_URI, HTTP_USER, HTTP_PASS, 0, &onGsReceive);
- gs.poll();
- if (!gs.isConnected(r)) {
+ int ret = http.get(HTTP_URI, buf, 351); // out put only 40 zenkaku characters
+ if(!ret){
+ parseBuffer();
+ }else{
+ sprintf((char*)codes, "Error - ret = %d - HTTP return code = %d", ret, http.getHTTPResponseCode());
color(0x001f);
cls();
- msg = "reconnect";
- drawString((unsigned char*)msg); newline();
- wait_ms(5);
- r = gs.disconnect();
- r = connect();
+ color(0xffff);
+ drawString(codes); newline();
+ if(ret==8){
+ drawString("disconnect..."); newline();
+ wifly.disconnect();
+ wifly.init();
+ drawString("reconnect..."); newline();
+ while (!wifly.connect());
+ drawString("done"); newline();
+ }
}
-
+
+#ifdef _DEBUG
+ if(!ret){
+ pc.printf("Page fetched successfully - read %d characters\r\n", strlen(buf));
+ pc.printf("Result: %s\r\n", buf);
+ }else{
+ pc.printf("Error - ret = %d - HTTP return code = %d\r\n", ret, http.getHTTPResponseCode());
+ }
+#endif
wait(INTERVAL_SEC);
}
-
}
