XBee WiFiを、Webサーバに適用
【サンプルソースコード】
#include "mbed.h" LocalFileSystem local("local"); void atcmd( char *cmd, char *ret ); DigitalOut myled1(LED1); Serial pc(USBTX, USBRX); // tx, rx Serial xbee(p13, p14); // tx, rx DigitalOut xbeeRst(p12); InterruptIn mySw1(p17); InterruptIn mySw2(p18); InterruptIn mySw3(p21); InterruptIn mySw4(p22); char myIP[64]; char myGW[64]; void intrSW1(void) { pc.printf("SW1 - disconnect\n\r"); atcmd( "atnr", NULL ); atcmd( "atcn", NULL ); } void intrSW2(void) { pc.printf("SW2 - IP address\n\r" ); atcmd( "atmy", myIP ); pc.printf("\n\r" ); atcmd( "atcn", NULL ); } void intrSW3(void) { pc.printf("SW3\n\r" ); struct tm *t; time_t seconds; seconds = time(NULL); t = localtime(&seconds); pc.printf( "Now: %d/%02d/%02d %02d:%02d:%02d - %d\n\r", (t->tm_year + 1900), (t->tm_mon + 1), t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec, seconds ); } void intrSW4(void) { int year, mon, mday, hour, min, sec; char buf[10]; struct tm t; time_t seconds; for (;;) { pc.printf("SW4- input year.\n\r" ); scanf( "%d", &year ); pc.printf( "%d\n\r", year ); pc.printf("input month.\n\r" ); scanf( "%d", &mon ); pc.printf( "%d\n\r", mon ); pc.printf("input day.\n\r" ); scanf( "%d", &mday ); pc.printf( "%d\n\r", mday ); pc.printf("input hour.\n\r" ); scanf( "%d", &hour ); pc.printf( "%d\n\r", hour ); pc.printf("input min.\n\r" ); scanf( "%d", &min ); pc.printf( "%d\n\r", min ); pc.printf("input hour.\n\r" ); scanf( "%d", &sec ); pc.printf( "%d\n\r", sec ); t.tm_sec = sec; // 0-59 t.tm_min = min; // 0-59 t.tm_hour = hour; // 0-23 t.tm_mday = mday; // 1-31 t.tm_mon = mon - 1; // 0-11 t.tm_year = year - 1900; // year since 1900 seconds = mktime(&t); pc.printf( "OK? (Y/*): %d/%d/%d %d:%d:%d - %d\n\r", year, mon, mday, hour, min, sec, seconds ); scanf( "%s", buf ); if ( (buf[0] == 'Y') || (buf[0] == 'y') ) break; else { pc.printf( "canceled!\n\r" ); return; } } set_time( seconds ); seconds = time(NULL); pc.printf( "set to %d!\n\r", seconds ); } void atcmd( char *cmd, char *ret ) { char cc1, cc2; int i; for(;;) { xbee.printf( "+++" ); wait( 0.5 ); for (;;) { if ( xbee.readable() ) break; } cc1 = xbee.getc(); for (;;) { if (xbee.readable()) break; } cc2 = xbee.getc(); if ( (cc1 == 'O') && (cc2 == 'K') ) { break; } wait( 1.0 ); } xbee.printf( "%s", cmd ); xbee.putc( 0x0d ); wait( 0.5 ); for (i=0;;i++) { if (xbee.readable()) { pc.putc( cc1 = xbee.getc() ); if (ret != NULL) ret[i] = cc1; } else break; } if (ret != NULL) ret[i] = 0x00; } // 下記の応答するHTMLページのデータは、ユーザは修正して下さい。 char *repBody = "I am a XBee. " "I am a XBee WiFi.
" "
" "responding at %d/%02d/%02d, %02d:%02d:%02d from %s"; int main() { struct tm *t; time_t seconds; char cc; char mySSID[128]; char myPW[128]; char myNI[128]; char buf[256]; myled1 = 0; myIP[0] = 0x00; myGW[0] = 0x00; xbeeRst = 0; wait( 0.1 ); xbeeRst = 1; wait( 1.0 ); FILE *fp; pc.baud(115200); xbee.baud(115200); fp = fopen( "/local/config.ini", "r" ); fscanf( fp, "%s", mySSID ); printf( "SSID: %s\n\r", mySSID ); fscanf( fp, "%s", myPW ); printf( "PW: %s\n\r", myPW ); fscanf( fp, "%s", myNI ); printf( "NI: %s\n\r", myNI ); sprintf( buf, "atni%s", myNI ); atcmd( buf, NULL ); fscanf( fp, "%s", myIP ); if ( (myIP[0] == '*') || (myIP[0] == 0x00) ) { printf( "enable to DHCP\n\r" ); atcmd( "atma0", NULL ); } else { printf( "IP Address: %s\n\r", myIP ); atcmd( "atma1", NULL ); sprintf( buf, "atmy%s", myIP ); atcmd( buf, NULL ); fscanf( fp, "%s", myGW ); sprintf( buf, "atgw%s", myGW ); atcmd( buf, NULL ); } fclose(fp); mySw1.fall( intrSW1 ); mySw2.fall( intrSW2 ); mySw3.fall( intrSW3 ); mySw4.fall( intrSW4 ); atcmd( "atee2", NULL ); sprintf( buf, "atid%s", mySSID ); atcmd( buf, NULL ); sprintf( buf, "atpk%s", myPW ); atcmd( buf, NULL ); atcmd( "atip1", NULL ); atcmd( "atc050", NULL ); atcmd( "atac", NULL ); pc.printf("begin\r"); for (;;) { if (pc.readable()) { cc = pc.getc(); pc.putc( cc ); xbee.putc( cc ); // if (cc == '@' ) xbee.putc( 0x1a ); } if (xbee.readable()) { pc.putc(cc = xbee.getc()); #if 1 if ( cc == 'G' ) { myled1 = 1; seconds = time(NULL); t = localtime(&seconds); pc.printf( "Responce: %d/%02d/%02d %02d:%02d:%02d - %d\n\r", (t->tm_year + 1900), (t->tm_mon + 1), t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec, seconds ); for (;;) { if ( xbee.readable() ) pc.putc( xbee.getc() ); else break; break; } xbee.printf( "HTTP/1.1 200 Ok\n" ); xbee.printf( "Content-Type: text/html\n" ); xbee.printf( "Content-Length=%d\n", strlen(repBody) ); xbee.printf( "Connection: close\n\n" ); xbee.printf( repBody, (t->tm_year + 1900), (t->tm_mon + 1), t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec, myIP ); xbee.printf( "<!--- " ); atcmd( "atc051", NULL); atcmd( "atac", NULL ); wait( 0.5 ); atcmd( "atc050", NULL); atcmd( "atac", NULL ); atcmd( "atcn", NULL ); myled1 = 0; } #endif } } }
トラックバック(0)
このブログ記事を参照しているブログ一覧: XBee WiFiを、Webサーバに適用
このブログ記事に対するトラックバックURL: http://the.nerd.jp/blogs/cgi-bin/mt-tb.cgi/4763