Juan Pablo Renedo
/
RN-XV_simple_server
Simple server using RN-XV. Code under construction, ideas and corrections acepted.
main.cpp@0:776740e9d35d, 2012-06-24 (annotated)
- Committer:
- arhpoon
- Date:
- Sun Jun 24 18:33:55 2012 +0000
- Revision:
- 0:776740e9d35d
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
arhpoon | 0:776740e9d35d | 1 | #include "mbed.h" |
arhpoon | 0:776740e9d35d | 2 | #include "Wifly.h" |
arhpoon | 0:776740e9d35d | 3 | |
arhpoon | 0:776740e9d35d | 4 | Serial pc(USBTX, USBRX); |
arhpoon | 0:776740e9d35d | 5 | |
arhpoon | 0:776740e9d35d | 6 | /* wifly object where: |
arhpoon | 0:776740e9d35d | 7 | * - p13 and p14 are for the serial communication |
arhpoon | 0:776740e9d35d | 8 | * - p21 is for the reset pin (optional, not needed for our test) |
arhpoon | 0:776740e9d35d | 9 | * - "your_ssid" is the ssid of the network |
arhpoon | 0:776740e9d35d | 10 | * - "your_password" is the password |
arhpoon | 0:776740e9d35d | 11 | * - true means that the security of the network is WPA |
arhpoon | 0:776740e9d35d | 12 | */ |
arhpoon | 0:776740e9d35d | 13 | Wifly wifly(p13, p14, p21, "your_ssid", "your_password", true); |
arhpoon | 0:776740e9d35d | 14 | |
arhpoon | 0:776740e9d35d | 15 | void header(void) |
arhpoon | 0:776740e9d35d | 16 | { |
arhpoon | 0:776740e9d35d | 17 | //Very important: set comm remote 0 |
arhpoon | 0:776740e9d35d | 18 | //Otherwise the header will not load correctly |
arhpoon | 0:776740e9d35d | 19 | wifly.send("HTTP/1.1 200 OK\r\n"); |
arhpoon | 0:776740e9d35d | 20 | wifly.send("Content-Type: text/html\r\n\r\n"); |
arhpoon | 0:776740e9d35d | 21 | } |
arhpoon | 0:776740e9d35d | 22 | |
arhpoon | 0:776740e9d35d | 23 | void web_page( ) |
arhpoon | 0:776740e9d35d | 24 | { |
arhpoon | 0:776740e9d35d | 25 | wifly.send("<HTML><BODY><H1>Hi</H1><P>This is very minimal HTML document.</P></BODY></HTML>\r\n","NO"); |
arhpoon | 0:776740e9d35d | 26 | //wifly.send("","NO"); |
arhpoon | 0:776740e9d35d | 27 | wait(0.5); |
arhpoon | 0:776740e9d35d | 28 | } |
arhpoon | 0:776740e9d35d | 29 | |
arhpoon | 0:776740e9d35d | 30 | void close_connection( ) |
arhpoon | 0:776740e9d35d | 31 | { |
arhpoon | 0:776740e9d35d | 32 | wifly.send("$$$","NO"); |
arhpoon | 0:776740e9d35d | 33 | wait(1); |
arhpoon | 0:776740e9d35d | 34 | wifly.send("close\n\r","NO"); |
arhpoon | 0:776740e9d35d | 35 | wait(1); |
arhpoon | 0:776740e9d35d | 36 | wifly.send("exit\n\r","NO"); |
arhpoon | 0:776740e9d35d | 37 | } |
arhpoon | 0:776740e9d35d | 38 | |
arhpoon | 0:776740e9d35d | 39 | |
arhpoon | 0:776740e9d35d | 40 | int main() { |
arhpoon | 0:776740e9d35d | 41 | char recv[129]; |
arhpoon | 0:776740e9d35d | 42 | |
arhpoon | 0:776740e9d35d | 43 | // join the network specified in the constructor |
arhpoon | 0:776740e9d35d | 44 | while (!wifly.join()) { |
arhpoon | 0:776740e9d35d | 45 | printf("cannot to join the network, will retry!\r\n"); |
arhpoon | 0:776740e9d35d | 46 | wifly.reset(); |
arhpoon | 0:776740e9d35d | 47 | } |
arhpoon | 0:776740e9d35d | 48 | |
arhpoon | 0:776740e9d35d | 49 | printf("network joined!\r\n"); |
arhpoon | 0:776740e9d35d | 50 | |
arhpoon | 0:776740e9d35d | 51 | |
arhpoon | 0:776740e9d35d | 52 | while(1) |
arhpoon | 0:776740e9d35d | 53 | { |
arhpoon | 0:776740e9d35d | 54 | //prints all received messages from the RN-XV module |
arhpoon | 0:776740e9d35d | 55 | if(wifly.readable()) |
arhpoon | 0:776740e9d35d | 56 | { |
arhpoon | 0:776740e9d35d | 57 | wifly.read(recv); |
arhpoon | 0:776740e9d35d | 58 | printf("%s\r\n", recv); |
arhpoon | 0:776740e9d35d | 59 | } |
arhpoon | 0:776740e9d35d | 60 | |
arhpoon | 0:776740e9d35d | 61 | wait(0.2); |
arhpoon | 0:776740e9d35d | 62 | |
arhpoon | 0:776740e9d35d | 63 | //sends to the RN-XV module the characteres we type on the console |
arhpoon | 0:776740e9d35d | 64 | if(pc.readable()) |
arhpoon | 0:776740e9d35d | 65 | { |
arhpoon | 0:776740e9d35d | 66 | wifly.putc(pc.getc()); |
arhpoon | 0:776740e9d35d | 67 | } |
arhpoon | 0:776740e9d35d | 68 | |
arhpoon | 0:776740e9d35d | 69 | if(strstr (recv,"alive\r\n\r\n")) |
arhpoon | 0:776740e9d35d | 70 | { |
arhpoon | 0:776740e9d35d | 71 | header( ); |
arhpoon | 0:776740e9d35d | 72 | web_page( ); |
arhpoon | 0:776740e9d35d | 73 | close_connection( ); |
arhpoon | 0:776740e9d35d | 74 | } |
arhpoon | 0:776740e9d35d | 75 | } |
arhpoon | 0:776740e9d35d | 76 | } |