A je to! / Mbed OS J_HTTP_Client

Dependencies:   DS1820

Fork of J_HTTP_Client by Jan Kamidra

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002 This fun example was comleted by JohnnyK / odiin@seznam.cz 5/2018
00003 I am an amateur so this program and web pages are not perfect but working.
00004 In folder ForServer you can found .php files what you can use for control 3 GPIO (on/off) pins and show temperature on the page. For more follow Readme.txt
00005 THX Mbed team and Mbed community and all other who want to help other people be a maker :)
00006 
00007 I know Mbed web server is better/faster for gpio control but in my country it is not possible to take advantage of ipv6 without extra cost. Our standart is mixed ipv4/ipv6 but with dynamic and not public IP address.
00008 */
00009 #include "mbed.h"
00010 #include "DS1820.h"
00011 #include "EthernetInterface.h"
00012 
00013 #define SERVER  "server.someweb.com"   // Here you must place your web server address
00014 #define PORT    80                              
00015 #define HOST    "yourweb.com"                   // Here you must place your web domain
00016 #define GPIOPAGE  "gpio.txt"                    // Name of TXT file for GPIO setting
00017 #define TEMPPAGE  "nucleoin.php"                // Name of web page for temperature add/update
00018 
00019 DigitalOut gpio[3] = {D8, LED2, LED3};
00020 DigitalOut myled1(LED1);
00021 InterruptIn button(USER_BUTTON);
00022 Serial pc(SERIAL_TX, SERIAL_RX);
00023 EthernetInterface net;
00024 DS1820  ds1820(A0);
00025 TCPSocket socket;
00026 Timer timer;
00027 Ticker ticker;
00028 
00029 float temperature;
00030 int gpioSet[3];
00031 int i;
00032 
00033 void liveLed();
00034 void justDoIt();
00035 void connectionStop();
00036 void softReset();
00037 
00038 int main() {
00039     
00040     pc.printf("Client running...\n");
00041     // Attach the address of the interrupt handler routine for pushbutton
00042     button.rise(&softReset);
00043     // Live led
00044     ticker.attach(&liveLed, 0.5);
00045     // Bring up the ethernet interface
00046     net.connect();
00047     // Show the network address
00048     const char *ip = net.get_ip_address();
00049     // Check ip
00050     if(ip){ 
00051         pc.printf("IP address is: %s\n", ip);
00052     }else{
00053         pc.printf("No IP");
00054         // Reset when no IP was detected
00055         softReset();
00056     }
00057     // Begin DS1820
00058     if(ds1820.begin()) {
00059         // Start temperature conversion
00060         ds1820.startConversion();
00061         // Ĺet DS1820 complete the temperature conversion
00062         wait(0.5);
00063     } else {
00064         pc.printf("No DS1820 sensor found!\r\n");
00065         // Reset when no sencor was detected
00066         softReset();
00067     }
00068     
00069     timer.start();
00070         
00071     while(1){
00072         wait(0.5); 
00073         if(timer > 10){
00074             pc.printf("Counter: %d\n", i++);
00075             // Read temperature
00076             temperature = ds1820.read();
00077             pc.printf("Temperature = %3.1f\r\n", temperature);
00078             // Start temperature conversion
00079             ds1820.startConversion();
00080             // Call function for temeperature send and request for gpio setting
00081             justDoIt();
00082             // Set gpio state for all gpio ports in array
00083             for(int i = 0; i < 3; i++){
00084                 if (gpio[i].read()!= gpioSet[i]){ 
00085                     // Set gpio from latest recieved meassege      
00086                     gpio[i] = gpioSet[i];
00087                 }      
00088             }
00089             // Reset Timer
00090             timer.reset();
00091         }   
00092     }  
00093 }
00094 
00095 void justDoIt() {
00096     char sbuffer[100];
00097     char rbuffer[250];
00098     char *buffer;
00099     int scount;
00100     int rcount;
00101     //*************************First request*************************  
00102     // Open a socket on the network interface
00103     socket.open(&net);
00104     // Create a TCP connection to server and check of success
00105     if (socket.connect(SERVER, PORT) < 0) {
00106         pc.printf("Failed to connect with server\n\r");
00107         connectionStop();
00108     }
00109     // Fill command string into buffer
00110     sprintf(sbuffer,"GET /%s HTTP/1.1\r\nHost: %s\r\nConnection: close\r\n\r\n", GPIOPAGE, HOST);
00111     // Send http request
00112     scount = socket.send(sbuffer, sizeof sbuffer);
00113     // Print shorter string of sended messeage
00114     pc.printf("sent %d [%.*s]\n", scount, strstr(sbuffer, "\r\n")-sbuffer, sbuffer);
00115     // Recieve simple http response
00116     rcount = socket.recv(rbuffer, sizeof rbuffer);
00117     // Print out the response
00118     pc.printf("recv %d [%.*s]\n", rcount, strstr(rbuffer, "\r\n")-rbuffer, rbuffer);
00119     // Ignore data from server and take only values after "gpio:" like 1 0 1
00120     buffer = strstr(rbuffer, "gpio:")+ 5;
00121     // Extract into int buffer
00122     sscanf(buffer,"%d %d %d", &gpioSet[0], &gpioSet[1], &gpioSet[2]);
00123     // Close the socket to return its memory 
00124     socket.close(); 
00125     wait(1);
00126     //*************************Second request*************************
00127     // Open a socket on the network interface
00128     socket.open(&net);
00129     // Create a TCP connection to server and check of success
00130     if (socket.connect(SERVER, PORT) < 0) {
00131         pc.printf("Failed to connect with server\n\r");
00132         connectionStop();
00133     }
00134     // Fill command string into buffer
00135     sprintf(sbuffer,"GET /%s/?temp=%3.1f HTTP/1.1\r\nHost: %s\r\nConnection: close\r\n\r\n", TEMPPAGE, temperature, HOST );
00136     // Send http request
00137     scount = socket.send(sbuffer, sizeof sbuffer);
00138     pc.printf("sent %d [%.*s]\n", scount, strstr(sbuffer, "\r\n")-sbuffer, sbuffer);
00139     // Recieve http response
00140     rcount = socket.recv(rbuffer, sizeof rbuffer);
00141     // Print out the response line
00142     pc.printf("recv %d [%.*s]\n", rcount, strstr(rbuffer, "\r\n")-rbuffer, rbuffer);
00143     // Close the socket to return its memory 
00144     socket.close();  
00145 }
00146 
00147 void connectionStop(){
00148         // Timer stop
00149         timer.stop();
00150         // Close the socket to return its memory 
00151         socket.close();  
00152         // Bring down the network interface
00153         net.disconnect();
00154         // Mbed reset
00155         softReset();
00156 }
00157 
00158 void softReset(){
00159         // Wait
00160         wait(2);
00161         // Mbed reset
00162         NVIC_SystemReset();
00163 }
00164 
00165 void liveLed(){
00166     // Change state of Led one
00167     myled1 =! myled1;
00168 }