Example Get requests using Ethernet

Dependencies:   EthernetInterface mbed-rtos mbed

Fork of TCPSocket_HelloWorld by mbed official

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "EthernetInterface.h"
00003 #include <string>
00004 
00005 Serial pc(USBTX,USBRX);
00006 
00007 extern "C" void mbed_mac_address(char * mac) {
00008  
00009 // define your own MAC Address ba:d7:05:47:1f:c6
00010   mac[0] = 0xba;  
00011   mac[1] = 0xd7;  
00012   mac[2] = 0x05;  
00013   mac[3] = 0x47;  
00014   mac[4] = 0x1f;  
00015   mac[5] = 0xc6;           
00016   
00017 };
00018 
00019 int main() {
00020     pc.baud(9600);
00021     pc.printf("Running\n");
00022     EthernetInterface eth;
00023     eth.init(); //Use DHCP
00024     wait(15);
00025     eth.connect();
00026     wait(15);
00027     pc.printf("MAC is %s\n", eth.getMACAddress());
00028     pc.printf("IP Address is %s\n", eth.getIPAddress());
00029     
00030     TCPSocketConnection sock;
00031     sock.connect("192.184.82.3", 5000);
00032     
00033     char http_cmd[300] = "GET /create_board?playerNum=1&gameNum=144&board=1111000000000000000000000000000000000000000000000000000000000000 HTTP/1.0\n\n";
00034     sock.send_all(http_cmd, sizeof(http_cmd)-1);
00035     
00036     //Instead of printing here, do what you did before to convert the string, and just append buffer to a string (below the break),
00037     //    replacing pc.printf
00038     //Then we can do string find to snipe the values we want (have to do math to grab the right amount)
00039     //Implemented example here, testStr now holds the received text (so you can search it)
00040     string testStr = "";
00041     char buffer[600];
00042     int ret;
00043     while (true) {
00044         ret = sock.receive(buffer, sizeof(buffer)-1);
00045         if (ret <= 0)
00046             break;
00047         buffer[ret] = '\0';
00048         pc.printf("Received %d chars from server:\n%s\n", ret, buffer);
00049         string conv(buffer);
00050         testStr = testStr + conv;
00051     }
00052     pc.printf("Stringified %s\n", testStr);
00053     sock.close();
00054     wait(0.2);
00055     sock.connect("192.184.82.3", 5000);
00056     wait(0.2);
00057     strcpy(http_cmd, "GET /create_board?playerNum=2&gameNum=144&board=1111000000000000000000000000000000000000000000000000000000000000 HTTP/1.0\n\n");
00058     pc.printf("Command is %s", http_cmd);
00059     sock.send_all(http_cmd, sizeof(http_cmd)-1);
00060     
00061     while (true) {
00062         ret = sock.receive(buffer, sizeof(buffer)-1);
00063         if (ret <= 0)
00064             break;
00065         buffer[ret] = '\0';
00066         pc.printf("Received %d chars from server:\n%s\n", ret, buffer);
00067     }
00068     
00069     sock.close();
00070     wait(0.2);
00071     sock.connect("192.184.82.3", 5000);
00072     wait(0.2);
00073     strcpy(http_cmd, "GET /polling?gameNum=144&playerNum=1 HTTP/1.0\n\n");
00074     pc.printf("Command is %s", http_cmd);
00075     sock.send_all(http_cmd, sizeof(http_cmd)-1);
00076     
00077     while (true) {
00078         ret = sock.receive(buffer, sizeof(buffer)-1);
00079         if (ret <= 0)
00080             break;
00081         buffer[ret] = '\0';
00082         pc.printf("Received %d chars from server:\n%s\n", ret, buffer);
00083     }
00084       
00085     sock.close();
00086     wait(0.2);
00087     sock.connect("192.184.82.3", 5000);
00088     wait(0.2);
00089     strcpy(http_cmd, "GET /fire?playerNum=1&gameNum=144&x=1&y=0 HTTP/1.0\n\n");
00090     pc.printf("Command is %s", http_cmd);
00091     sock.send_all(http_cmd, sizeof(http_cmd)-1);
00092     
00093     while (true) {
00094         ret = sock.receive(buffer, sizeof(buffer)-1);
00095         if (ret <= 0)
00096             break;
00097         buffer[ret] = '\0';
00098         pc.printf("Received %d chars from server:\n%s\n", ret, buffer);
00099     }
00100     
00101     sock.close();
00102     eth.disconnect();
00103     
00104     while(1) {}
00105 }