K64F Ethernet server with DS18B20

Dependencies:   DS1820 EthernetInterface mbed-rtos mbed

Fork of eth_lib by Jacek Kolodziej

Committer:
mrozmus
Date:
Mon Jun 11 11:14:19 2018 +0000
Revision:
1:8b553350350e
Parent:
0:9b72cde90412
Ethernet server with DS18B20 temperature sensor

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jackolo 0:9b72cde90412 1 #include "mbed.h"
jackolo 0:9b72cde90412 2 #include "EthernetInterface.h"
mrozmus 1:8b553350350e 3 #include "DS1820.h"
jackolo 0:9b72cde90412 4
mrozmus 1:8b553350350e 5 #define MBED_DEV_IP "192.168.144.24"
mrozmus 1:8b553350350e 6 #define MBED_DEV_MASK "255.255.0.0"
mrozmus 1:8b553350350e 7 #define MBED_DEV_GW "192.168.144.25"
jackolo 0:9b72cde90412 8 #define ECHO_SERVER_PORT 80
jackolo 0:9b72cde90412 9
mrozmus 1:8b553350350e 10 #define DATA_PIN A1
mrozmus 1:8b553350350e 11
mrozmus 1:8b553350350e 12 Ticker timer;
mrozmus 1:8b553350350e 13
jackolo 0:9b72cde90412 14 DigitalOut red_led(LED_RED, 1);
jackolo 0:9b72cde90412 15 DigitalOut green_led(LED_GREEN, 1);
mrozmus 1:8b553350350e 16 Serial pc(USBTX, USBRX); // Debug via USB port.
jackolo 0:9b72cde90412 17
mrozmus 1:8b553350350e 18
mrozmus 1:8b553350350e 19 DS1820 probe(DATA_PIN);
mrozmus 1:8b553350350e 20
mrozmus 1:8b553350350e 21 char find_temperature[50] = "GET /demo_form.asp?OPTION=TEMPERATURE";
mrozmus 1:8b553350350e 22 char find_lasttemperature[50] = "GET /demo_form.asp?OPTION=LAST";
mrozmus 1:8b553350350e 23
mrozmus 1:8b553350350e 24 int counter;
jackolo 0:9b72cde90412 25
mrozmus 1:8b553350350e 26 void attime()
mrozmus 1:8b553350350e 27 {
mrozmus 1:8b553350350e 28 probe.convertTemperature(true, DS1820::all_devices);
mrozmus 1:8b553350350e 29 if(probe.temperature()>30)
mrozmus 1:8b553350350e 30 {
mrozmus 1:8b553350350e 31 red_led=0;
mrozmus 1:8b553350350e 32 green_led=1;
mrozmus 1:8b553350350e 33 }
mrozmus 1:8b553350350e 34 else
mrozmus 1:8b553350350e 35 {
mrozmus 1:8b553350350e 36 red_led=1;
mrozmus 1:8b553350350e 37 green_led=0;
mrozmus 1:8b553350350e 38 }
mrozmus 1:8b553350350e 39 }
jackolo 0:9b72cde90412 40 int main (void) {
jackolo 0:9b72cde90412 41
jackolo 0:9b72cde90412 42 pc.printf("\nHello!\n\r");
jackolo 0:9b72cde90412 43 //pc.baud(9600);
jackolo 0:9b72cde90412 44 EthernetInterface eth; // Interface using Ethernet to connect to an IP-based network.
jackolo 0:9b72cde90412 45 eth.init(); // Initialize the interface with DHCP.
jackolo 0:9b72cde90412 46 //eth.init(MBED_DEV_IP, MBED_DEV_MASK, MBED_DEV_GW); // Initialize the interface with a static IP address.
jackolo 0:9b72cde90412 47 eth.connect(); // Connect Bring the interface up, start DHCP if needed (Dynamic Host Configuration Protocol).
jackolo 0:9b72cde90412 48
jackolo 0:9b72cde90412 49 pc.printf("\nIP Address is %s\n\r", eth.getIPAddress());
jackolo 0:9b72cde90412 50
jackolo 0:9b72cde90412 51 TCPSocketServer server; // Instantiate a TCP Server.
jackolo 0:9b72cde90412 52 server.bind(ECHO_SERVER_PORT); // Bind a socket to a specific port (80 = HTTP).
jackolo 0:9b72cde90412 53 server.listen(); // Start listening for incoming connections.
jackolo 0:9b72cde90412 54
jackolo 0:9b72cde90412 55 while (true) {
mrozmus 1:8b553350350e 56 timer.attach(&attime,1);
jackolo 0:9b72cde90412 57 pc.printf("\nWait for new connection...\n\r");
jackolo 0:9b72cde90412 58 TCPSocketConnection client; // TCP socket connection.
jackolo 0:9b72cde90412 59 server.accept(client); // Accept a new connection.
jackolo 0:9b72cde90412 60 client.set_blocking(false, 1500); // Timeout after (1.5)s
jackolo 0:9b72cde90412 61
jackolo 0:9b72cde90412 62 pc.printf("\nConnection from: %s\n\r", client.get_address());
jackolo 0:9b72cde90412 63
jackolo 0:9b72cde90412 64 char buffer[600];
jackolo 0:9b72cde90412 65 int ret;
mrozmus 1:8b553350350e 66 char value[5];
mrozmus 1:8b553350350e 67 char last5values[5][5];
jackolo 0:9b72cde90412 68
mrozmus 1:8b553350350e 69 int option =1;
mrozmus 1:8b553350350e 70
jackolo 0:9b72cde90412 71 /////////////GET//////////////
jackolo 0:9b72cde90412 72 while (true) {
mrozmus 1:8b553350350e 73 ret = client.receive(buffer, sizeof(buffer)-1);
mrozmus 1:8b553350350e 74
mrozmus 1:8b553350350e 75 // Receive data from the remote host.
jackolo 0:9b72cde90412 76 if (ret <= 0)break;
jackolo 0:9b72cde90412 77 buffer[ret] = '\0';
mrozmus 1:8b553350350e 78 pc.printf("Received %d chars from server:\n\r%s\n\r", ret, buffer);
jackolo 0:9b72cde90412 79
mrozmus 1:8b553350350e 80 // Option seeking:
mrozmus 1:8b553350350e 81 if(strstr(buffer, find_temperature)>0) {
mrozmus 1:8b553350350e 82 probe.convertTemperature(true, DS1820::all_devices); //Start temperature conversion, wait until ready
mrozmus 1:8b553350350e 83 pc.printf("It is %3.1foC\r\n", probe.temperature());
mrozmus 1:8b553350350e 84 sprintf(value,"%3.1f",probe.temperature());
mrozmus 1:8b553350350e 85 sprintf(last5values[counter],"%3.1f",probe.temperature());
mrozmus 1:8b553350350e 86 if(counter==4)
mrozmus 1:8b553350350e 87 {
mrozmus 1:8b553350350e 88 counter=0;
mrozmus 1:8b553350350e 89 }
mrozmus 1:8b553350350e 90 else
mrozmus 1:8b553350350e 91 {
mrozmus 1:8b553350350e 92 counter++;
mrozmus 1:8b553350350e 93 }
mrozmus 1:8b553350350e 94 option = 1;
jackolo 0:9b72cde90412 95 }
mrozmus 1:8b553350350e 96 if(strstr(buffer,find_lasttemperature)>0){
mrozmus 1:8b553350350e 97
mrozmus 1:8b553350350e 98 option = 2;
mrozmus 1:8b553350350e 99 }
mrozmus 1:8b553350350e 100
mrozmus 1:8b553350350e 101 }
jackolo 0:9b72cde90412 102 // Webpage here:
mrozmus 1:8b553350350e 103 /*
mrozmus 1:8b553350350e 104 <!DOCTYPE html>
mrozmus 1:8b553350350e 105 <html>
mrozmus 1:8b553350350e 106 <head>
jackolo 0:9b72cde90412 107 <title> FRDM-K64F </title>
mrozmus 1:8b553350350e 108 </head>
mrozmus 1:8b553350350e 109 <body>
mrozmus 1:8b553350350e 110
mrozmus 1:8b553350350e 111 <h1>Temperature Sensor</h1>
mrozmus 1:8b553350350e 112 <form action=""demo_form.asp"">
mrozmus 1:8b553350350e 113 <select name=""OPTION"">
mrozmus 1:8b553350350e 114 <option value=""TEMPERATURE"">
mrozmus 1:8b553350350e 115 TEMPERATURE
mrozmus 1:8b553350350e 116 <OPTION VALUE=""LAST"">
mrozmus 1:8b553350350e 117 LAST 5 TEMPERATURES
mrozmus 1:8b553350350e 118 </select>
mrozmus 1:8b553350350e 119 <input type=""submit"" value=""Submit"">
mrozmus 1:8b553350350e 120 </select>
mrozmus 1:8b553350350e 121 </form>
mrozmus 1:8b553350350e 122 </body>
mrozmus 1:8b553350350e 123 </html>
mrozmus 1:8b553350350e 124 */
mrozmus 1:8b553350350e 125 char http_cmd1[] = "<!DOCTYPE html> <html> <head> <title> FRDM-K64F </title></head> <body><h1>Temperature Sensor</h1><form action=""demo_form.asp""><select name=""OPTION""><option value=""TEMPERATURE"">TEMPERATURE<OPTION VALUE=""LAST"">LAST 5 TEMPERATURES<input type=""submit"" value=""Submit""></select></form>";
mrozmus 1:8b553350350e 126 char http_cmd_current[]="<p>Current temperature in oC:</p>";
mrozmus 1:8b553350350e 127 char http_cmd_last5temp[]="<p>Last (max 5) temperatures in oC:</p>";
mrozmus 1:8b553350350e 128 char http_cmd_warning[]="<p><font color=""red"">WARNING! HIGH TEMPERATURE</font></p>";
mrozmus 1:8b553350350e 129 char http_cmd2[]="</body></html>";
mrozmus 1:8b553350350e 130 char http_cmd_newline_start[]="<p>";
mrozmus 1:8b553350350e 131 char http_cmd_newline_end[]="</p>";
jackolo 0:9b72cde90412 132
mrozmus 1:8b553350350e 133 client.send_all(http_cmd1, sizeof(http_cmd1)-1); // Send all the data to the remote host.
mrozmus 1:8b553350350e 134 if(option==1)
mrozmus 1:8b553350350e 135 {
mrozmus 1:8b553350350e 136 probe.convertTemperature(true, DS1820::all_devices);
mrozmus 1:8b553350350e 137 pc.printf(" It's %3.1f oC\r\n",probe.temperature());
mrozmus 1:8b553350350e 138 client.send_all(http_cmd_current, sizeof(http_cmd_current)-1);
mrozmus 1:8b553350350e 139 client.send_all(value, sizeof(value)-1);
mrozmus 1:8b553350350e 140 if(probe.temperature()>25)
mrozmus 1:8b553350350e 141 {
mrozmus 1:8b553350350e 142 client.send_all(http_cmd_warning, sizeof(http_cmd_warning)-1);
mrozmus 1:8b553350350e 143 }
mrozmus 1:8b553350350e 144 }
mrozmus 1:8b553350350e 145 else if(option ==2)
mrozmus 1:8b553350350e 146 {
mrozmus 1:8b553350350e 147 client.send_all(http_cmd_last5temp, sizeof(http_cmd_last5temp)-1);
mrozmus 1:8b553350350e 148 for(int i=0;i<=4;++i)
mrozmus 1:8b553350350e 149 {
mrozmus 1:8b553350350e 150 pc.printf("last %d value: %s",i,last5values[i]);
mrozmus 1:8b553350350e 151 client.send_all(http_cmd_newline_start, sizeof(http_cmd_newline_start)-1);
mrozmus 1:8b553350350e 152 client.send_all(last5values[i],sizeof(last5values[i])-1);
mrozmus 1:8b553350350e 153 client.send_all(http_cmd_newline_end, sizeof(http_cmd_newline_end)-1);
mrozmus 1:8b553350350e 154 }
mrozmus 1:8b553350350e 155 }
mrozmus 1:8b553350350e 156 client.send_all(http_cmd2, sizeof(http_cmd2)-1);
jackolo 0:9b72cde90412 157 client.close(); // Close the socket.
jackolo 0:9b72cde90412 158 pc.printf("\n\nClient closed\n\n\r");
jackolo 0:9b72cde90412 159 }
jackolo 0:9b72cde90412 160 }