MBED HTTP client with web pages for server

Dependencies:   DS1820

Fork of J_HTTP_Client by Jan Kamidra

Committer:
JohnnyK
Date:
Wed May 23 06:47:56 2018 +0000
Revision:
1:920482bc63f4
Parent:
0:d90e1f7bda1f
Repair

Who changed what in which revision?

UserRevisionLine numberNew contents of line
JohnnyK 0:d90e1f7bda1f 1 /*
JohnnyK 0:d90e1f7bda1f 2 This fun example was comleted by JohnnyK / odiin@seznam.cz 5/2018
JohnnyK 0:d90e1f7bda1f 3 I am an amateur so this program and web pages are not perfect but working.
JohnnyK 0:d90e1f7bda1f 4 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
JohnnyK 0:d90e1f7bda1f 5 THX Mbed team and Mbed community and all other who want to help other people be a maker :)
JohnnyK 0:d90e1f7bda1f 6
JohnnyK 0:d90e1f7bda1f 7 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.
JohnnyK 0:d90e1f7bda1f 8 */
JohnnyK 0:d90e1f7bda1f 9 #include "mbed.h"
JohnnyK 0:d90e1f7bda1f 10 #include "DS1820.h"
JohnnyK 0:d90e1f7bda1f 11 #include "EthernetInterface.h"
JohnnyK 0:d90e1f7bda1f 12
JohnnyK 0:d90e1f7bda1f 13 #define SERVER "server.someweb.com" // Here you must place your web server address
JohnnyK 0:d90e1f7bda1f 14 #define PORT 80
JohnnyK 0:d90e1f7bda1f 15 #define HOST "yourweb.com" // Here you must place your web domain
JohnnyK 0:d90e1f7bda1f 16 #define GPIOPAGE "gpio.txt" // Name of TXT file for GPIO setting
JohnnyK 0:d90e1f7bda1f 17 #define TEMPPAGE "nucleoin.php" // Name of web page for temperature add/update
JohnnyK 0:d90e1f7bda1f 18
JohnnyK 0:d90e1f7bda1f 19 DigitalOut gpio[3] = {D8, LED2, LED3};
JohnnyK 0:d90e1f7bda1f 20 DigitalOut myled1(LED1);
JohnnyK 0:d90e1f7bda1f 21 InterruptIn button(USER_BUTTON);
JohnnyK 0:d90e1f7bda1f 22 Serial pc(SERIAL_TX, SERIAL_RX);
JohnnyK 0:d90e1f7bda1f 23 EthernetInterface net;
JohnnyK 0:d90e1f7bda1f 24 DS1820 ds1820(A0);
JohnnyK 0:d90e1f7bda1f 25 TCPSocket socket;
JohnnyK 0:d90e1f7bda1f 26 Timer timer;
JohnnyK 0:d90e1f7bda1f 27 Ticker ticker;
JohnnyK 0:d90e1f7bda1f 28
JohnnyK 0:d90e1f7bda1f 29 float temperature;
JohnnyK 0:d90e1f7bda1f 30 int gpioSet[3];
JohnnyK 0:d90e1f7bda1f 31 int i;
JohnnyK 0:d90e1f7bda1f 32
JohnnyK 0:d90e1f7bda1f 33 void liveLed();
JohnnyK 0:d90e1f7bda1f 34 void justDoIt();
JohnnyK 0:d90e1f7bda1f 35 void connectionStop();
JohnnyK 0:d90e1f7bda1f 36 void softReset();
JohnnyK 0:d90e1f7bda1f 37
JohnnyK 0:d90e1f7bda1f 38 int main() {
JohnnyK 0:d90e1f7bda1f 39
JohnnyK 0:d90e1f7bda1f 40 pc.printf("Client running...\n");
JohnnyK 0:d90e1f7bda1f 41 // Attach the address of the interrupt handler routine for pushbutton
JohnnyK 0:d90e1f7bda1f 42 button.rise(&softReset);
JohnnyK 0:d90e1f7bda1f 43 // Live led
JohnnyK 0:d90e1f7bda1f 44 ticker.attach(&liveLed, 0.5);
JohnnyK 0:d90e1f7bda1f 45 // Bring up the ethernet interface
JohnnyK 0:d90e1f7bda1f 46 net.connect();
JohnnyK 0:d90e1f7bda1f 47 // Show the network address
JohnnyK 0:d90e1f7bda1f 48 const char *ip = net.get_ip_address();
JohnnyK 0:d90e1f7bda1f 49 // Check ip
JohnnyK 0:d90e1f7bda1f 50 if(ip){
JohnnyK 0:d90e1f7bda1f 51 pc.printf("IP address is: %s\n", ip);
JohnnyK 0:d90e1f7bda1f 52 }else{
JohnnyK 0:d90e1f7bda1f 53 pc.printf("No IP");
JohnnyK 0:d90e1f7bda1f 54 // Reset when no IP was detected
JohnnyK 0:d90e1f7bda1f 55 softReset();
JohnnyK 0:d90e1f7bda1f 56 }
JohnnyK 0:d90e1f7bda1f 57 // Begin DS1820
JohnnyK 0:d90e1f7bda1f 58 if(ds1820.begin()) {
JohnnyK 0:d90e1f7bda1f 59 // Start temperature conversion
JohnnyK 0:d90e1f7bda1f 60 ds1820.startConversion();
JohnnyK 0:d90e1f7bda1f 61 // Ĺet DS1820 complete the temperature conversion
JohnnyK 0:d90e1f7bda1f 62 wait(0.5);
JohnnyK 0:d90e1f7bda1f 63 } else {
JohnnyK 0:d90e1f7bda1f 64 pc.printf("No DS1820 sensor found!\r\n");
JohnnyK 0:d90e1f7bda1f 65 // Reset when no sencor was detected
JohnnyK 0:d90e1f7bda1f 66 softReset();
JohnnyK 0:d90e1f7bda1f 67 }
JohnnyK 0:d90e1f7bda1f 68
JohnnyK 0:d90e1f7bda1f 69 timer.start();
JohnnyK 0:d90e1f7bda1f 70
JohnnyK 0:d90e1f7bda1f 71 while(1){
JohnnyK 0:d90e1f7bda1f 72 wait(0.5);
JohnnyK 0:d90e1f7bda1f 73 if(timer > 10){
JohnnyK 0:d90e1f7bda1f 74 pc.printf("Counter: %d\n", i++);
JohnnyK 0:d90e1f7bda1f 75 // Read temperature
JohnnyK 0:d90e1f7bda1f 76 temperature = ds1820.read();
JohnnyK 0:d90e1f7bda1f 77 pc.printf("Temperature = %3.1f\r\n", temperature);
JohnnyK 0:d90e1f7bda1f 78 // Start temperature conversion
JohnnyK 0:d90e1f7bda1f 79 ds1820.startConversion();
JohnnyK 0:d90e1f7bda1f 80 // Call function for temeperature send and request for gpio setting
JohnnyK 0:d90e1f7bda1f 81 justDoIt();
JohnnyK 0:d90e1f7bda1f 82 // Set gpio state for all gpio ports in array
JohnnyK 0:d90e1f7bda1f 83 for(int i = 0; i < 3; i++){
JohnnyK 0:d90e1f7bda1f 84 if (gpio[i].read()!= gpioSet[i]){
JohnnyK 0:d90e1f7bda1f 85 // Set gpio from latest recieved meassege
JohnnyK 0:d90e1f7bda1f 86 gpio[i] = gpioSet[i];
JohnnyK 0:d90e1f7bda1f 87 }
JohnnyK 0:d90e1f7bda1f 88 }
JohnnyK 0:d90e1f7bda1f 89 // Reset Timer
JohnnyK 0:d90e1f7bda1f 90 timer.reset();
JohnnyK 0:d90e1f7bda1f 91 }
JohnnyK 0:d90e1f7bda1f 92 }
JohnnyK 0:d90e1f7bda1f 93 }
JohnnyK 0:d90e1f7bda1f 94
JohnnyK 0:d90e1f7bda1f 95 void justDoIt() {
JohnnyK 0:d90e1f7bda1f 96 char sbuffer[100];
JohnnyK 0:d90e1f7bda1f 97 char rbuffer[250];
JohnnyK 0:d90e1f7bda1f 98 char *buffer;
JohnnyK 0:d90e1f7bda1f 99 int scount;
JohnnyK 0:d90e1f7bda1f 100 int rcount;
JohnnyK 0:d90e1f7bda1f 101 //*************************First request*************************
JohnnyK 0:d90e1f7bda1f 102 // Open a socket on the network interface
JohnnyK 0:d90e1f7bda1f 103 socket.open(&net);
JohnnyK 0:d90e1f7bda1f 104 // Create a TCP connection to server and check of success
JohnnyK 0:d90e1f7bda1f 105 if (socket.connect(SERVER, PORT) < 0) {
JohnnyK 0:d90e1f7bda1f 106 pc.printf("Failed to connect with server\n\r");
JohnnyK 0:d90e1f7bda1f 107 connectionStop();
JohnnyK 0:d90e1f7bda1f 108 }
JohnnyK 0:d90e1f7bda1f 109 // Fill command string into buffer
JohnnyK 0:d90e1f7bda1f 110 sprintf(sbuffer,"GET /%s HTTP/1.1\r\nHost: %s\r\nConnection: close\r\n\r\n", GPIOPAGE, HOST);
JohnnyK 0:d90e1f7bda1f 111 // Send http request
JohnnyK 0:d90e1f7bda1f 112 scount = socket.send(sbuffer, sizeof sbuffer);
JohnnyK 0:d90e1f7bda1f 113 // Print shorter string of sended messeage
JohnnyK 0:d90e1f7bda1f 114 pc.printf("sent %d [%.*s]\n", scount, strstr(sbuffer, "\r\n")-sbuffer, sbuffer);
JohnnyK 0:d90e1f7bda1f 115 // Recieve simple http response
JohnnyK 0:d90e1f7bda1f 116 rcount = socket.recv(rbuffer, sizeof rbuffer);
JohnnyK 0:d90e1f7bda1f 117 // Print out the response
JohnnyK 0:d90e1f7bda1f 118 pc.printf("recv %d [%.*s]\n", rcount, strstr(rbuffer, "\r\n")-rbuffer, rbuffer);
JohnnyK 0:d90e1f7bda1f 119 // Ignore data from server and take only values after "gpio:" like 1 0 1
JohnnyK 0:d90e1f7bda1f 120 buffer = strstr(rbuffer, "gpio:")+ 5;
JohnnyK 0:d90e1f7bda1f 121 // Extract into int buffer
JohnnyK 0:d90e1f7bda1f 122 sscanf(buffer,"%d %d %d", &gpioSet[0], &gpioSet[1], &gpioSet[2]);
JohnnyK 0:d90e1f7bda1f 123 // Close the socket to return its memory
JohnnyK 0:d90e1f7bda1f 124 socket.close();
JohnnyK 0:d90e1f7bda1f 125 wait(1);
JohnnyK 0:d90e1f7bda1f 126 //*************************Second request*************************
JohnnyK 0:d90e1f7bda1f 127 // Open a socket on the network interface
JohnnyK 0:d90e1f7bda1f 128 socket.open(&net);
JohnnyK 0:d90e1f7bda1f 129 // Create a TCP connection to server and check of success
JohnnyK 0:d90e1f7bda1f 130 if (socket.connect(SERVER, PORT) < 0) {
JohnnyK 0:d90e1f7bda1f 131 pc.printf("Failed to connect with server\n\r");
JohnnyK 0:d90e1f7bda1f 132 connectionStop();
JohnnyK 0:d90e1f7bda1f 133 }
JohnnyK 0:d90e1f7bda1f 134 // Fill command string into buffer
JohnnyK 0:d90e1f7bda1f 135 sprintf(sbuffer,"GET /%s/?temp=%3.1f HTTP/1.1\r\nHost: %s\r\nConnection: close\r\n\r\n", TEMPPAGE, temperature, HOST );
JohnnyK 0:d90e1f7bda1f 136 // Send http request
JohnnyK 0:d90e1f7bda1f 137 scount = socket.send(sbuffer, sizeof sbuffer);
JohnnyK 0:d90e1f7bda1f 138 pc.printf("sent %d [%.*s]\n", scount, strstr(sbuffer, "\r\n")-sbuffer, sbuffer);
JohnnyK 0:d90e1f7bda1f 139 // Recieve http response
JohnnyK 0:d90e1f7bda1f 140 rcount = socket.recv(rbuffer, sizeof rbuffer);
JohnnyK 0:d90e1f7bda1f 141 // Print out the response line
JohnnyK 0:d90e1f7bda1f 142 pc.printf("recv %d [%.*s]\n", rcount, strstr(rbuffer, "\r\n")-rbuffer, rbuffer);
JohnnyK 0:d90e1f7bda1f 143 // Close the socket to return its memory
JohnnyK 0:d90e1f7bda1f 144 socket.close();
JohnnyK 0:d90e1f7bda1f 145 }
JohnnyK 0:d90e1f7bda1f 146
JohnnyK 0:d90e1f7bda1f 147 void connectionStop(){
JohnnyK 0:d90e1f7bda1f 148 // Timer stop
JohnnyK 0:d90e1f7bda1f 149 timer.stop();
JohnnyK 0:d90e1f7bda1f 150 // Close the socket to return its memory
JohnnyK 0:d90e1f7bda1f 151 socket.close();
JohnnyK 0:d90e1f7bda1f 152 // Bring down the network interface
JohnnyK 0:d90e1f7bda1f 153 net.disconnect();
JohnnyK 0:d90e1f7bda1f 154 // Mbed reset
JohnnyK 0:d90e1f7bda1f 155 softReset();
JohnnyK 0:d90e1f7bda1f 156 }
JohnnyK 0:d90e1f7bda1f 157
JohnnyK 0:d90e1f7bda1f 158 void softReset(){
JohnnyK 0:d90e1f7bda1f 159 // Wait
JohnnyK 0:d90e1f7bda1f 160 wait(2);
JohnnyK 0:d90e1f7bda1f 161 // Mbed reset
JohnnyK 0:d90e1f7bda1f 162 NVIC_SystemReset();
JohnnyK 0:d90e1f7bda1f 163 }
JohnnyK 0:d90e1f7bda1f 164
JohnnyK 0:d90e1f7bda1f 165 void liveLed(){
JohnnyK 0:d90e1f7bda1f 166 // Change state of Led one
JohnnyK 0:d90e1f7bda1f 167 myled1 =! myled1;
JohnnyK 0:d90e1f7bda1f 168 }