K64F Ethernet server with DS18B20

Dependencies:   DS1820 EthernetInterface mbed-rtos mbed

Fork of eth_lib by Jacek Kolodziej

Files at this revision

API Documentation at this revision

Comitter:
mrozmus
Date:
Mon Jun 11 11:14:19 2018 +0000
Parent:
0:9b72cde90412
Commit message:
Ethernet server with DS18B20 temperature sensor

Changed in this revision

DS1820.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 9b72cde90412 -r 8b553350350e DS1820.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DS1820.lib	Mon Jun 11 11:14:19 2018 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/Sissors/code/DS1820/#c591209285e9
diff -r 9b72cde90412 -r 8b553350350e main.cpp
--- a/main.cpp	Sat Apr 21 08:43:33 2018 +0000
+++ b/main.cpp	Mon Jun 11 11:14:19 2018 +0000
@@ -1,18 +1,42 @@
 #include "mbed.h"
 #include "EthernetInterface.h"
+#include "DS1820.h" 
  
-#define MBED_DEV_IP       "192.168.0.52"
-#define MBED_DEV_MASK     "255.255.255.0"
-#define MBED_DEV_GW       "0.0.0.0"
+#define MBED_DEV_IP       "192.168.144.24"
+#define MBED_DEV_MASK     "255.255.0.0"
+#define MBED_DEV_GW       "192.168.144.25"
 #define ECHO_SERVER_PORT   80
 
-Serial pc(USBTX, USBRX); // Debug via USB port.
+#define DATA_PIN        A1
+
+Ticker timer;
+
 DigitalOut red_led(LED_RED, 1);
 DigitalOut green_led(LED_GREEN, 1);
+Serial pc(USBTX, USBRX); // Debug via USB port.
 
-char find_green[50] = "GET /demo_form.asp?LED_COLOUR=GREEN";
-char find_red[50] = "GET /demo_form.asp?LED_COLOUR=RED";
+
+DS1820 probe(DATA_PIN);
+
+char find_temperature[50] = "GET /demo_form.asp?OPTION=TEMPERATURE";
+char find_lasttemperature[50] = "GET /demo_form.asp?OPTION=LAST";
+
+int counter;
 
+void attime()
+{
+probe.convertTemperature(true, DS1820::all_devices); 
+            if(probe.temperature()>30)
+            {
+            red_led=0;
+            green_led=1;    
+            }
+            else
+            {
+            red_led=1;
+            green_led=0;
+            }
+}
 int main (void) {
     
     pc.printf("\nHello!\n\r");
@@ -29,6 +53,7 @@
     server.listen(); // Start listening for incoming connections.
     
     while (true) {
+        timer.attach(&attime,1);
         pc.printf("\nWait for new connection...\n\r");
         TCPSocketConnection client; //  TCP socket connection.
         server.accept(client); // Accept a new connection.
@@ -38,55 +63,97 @@
         
         char buffer[600];
         int ret;
+        char value[5];
+        char last5values[5][5];
         
+        int option =1;
+     
         /////////////GET//////////////
         while (true) {
-            ret = client.receive(buffer, sizeof(buffer)-1); // Receive data from the remote host. 
+            ret = client.receive(buffer, sizeof(buffer)-1); 
+            
+            // Receive data from the remote host. 
             if (ret <= 0)break;
             buffer[ret] = '\0';
-            printf("Received %d chars from server:\n\r%s\n\r", ret, buffer);
+            pc.printf("Received %d chars from server:\n\r%s\n\r", ret, buffer);
             
-            // LED colour seeking:
-            if(strstr(buffer, find_red)>0) {
-                red_led=0;
-                green_led=1;
+            // Option seeking:
+            if(strstr(buffer, find_temperature)>0) {
+                probe.convertTemperature(true, DS1820::all_devices);         //Start temperature conversion, wait until ready
+                pc.printf("It is %3.1foC\r\n", probe.temperature());
+                sprintf(value,"%3.1f",probe.temperature());
+                sprintf(last5values[counter],"%3.1f",probe.temperature());
+                if(counter==4)
+                {
+                    counter=0;
+                }
+                else
+                {
+                    counter++;
+                }
+                option = 1;
             }
-            if(strstr(buffer, find_green)>0) {
-                red_led=1;
-                green_led=0;
-            }
-        }        
-        /////////////SET//////////////
+            if(strstr(buffer,find_lasttemperature)>0){
+                
+                option = 2;
+                }
+
+        }      
         // Webpage here:
-        
-        char http_cmd[] = "<!DOCTYPE html> <html> <head> <title> FRDM-K64F </title> </head> <body> <h1> Hello World! </h1> <p> LED </p><form action=""demo_form.asp""><SELECT NAME=""LED_COLOUR""> <OPTION VALUE=""""> <OPTION VALUE=""GREEN"">GREEN <OPTION VALUE=""RED"">RED <input type=""submit"" value=""Submit""> </SELECT></form></body> </html>"; 
-/*
-<!DOCTYPE html>
-<html>
-    <head>
+        /*
+        <!DOCTYPE html> 
+<html> 
+    <head> 
         <title> FRDM-K64F </title>
-    </head>
-    <body> 
-    
-        <h1> Hello World! </h1> 
-        <p> LED </p>
+        </head>
+        <body>
+        
+        <h1>Temperature Sensor</h1>
+        <form action=""demo_form.asp"">
+        <select name=""OPTION"">
+        <option value=""TEMPERATURE"">
+        TEMPERATURE
+        <OPTION VALUE=""LAST"">
+        LAST 5 TEMPERATURES
+        </select>
+        <input type=""submit"" value=""Submit"">
+        </select>
+        </form>
+    </body>
+</html>
+        */
+        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>";
+        char http_cmd_current[]="<p>Current temperature in oC:</p>";
+        char http_cmd_last5temp[]="<p>Last (max 5) temperatures in oC:</p>";
+        char http_cmd_warning[]="<p><font color=""red"">WARNING! HIGH TEMPERATURE</font></p>";
+        char http_cmd2[]="</body></html>";
+        char http_cmd_newline_start[]="<p>";
+        char http_cmd_newline_end[]="</p>";
         
-        <form action="demo_form.asp">
-            <SELECT NAME="LED_COLOUR"> 
-            <OPTION VALUE="">
-            <OPTION VALUE="GREEN">
-            GREEN
-            <OPTION VALUE="RED">
-            RED 
-            <input type="submit" value="Submit"> 
-            </SELECT>
-        </form>
-    
-    </body>
-</html>"; 
-*/
-        client.send_all(http_cmd, sizeof(http_cmd)-1); // Send all the data to the remote host. 
-                
+        client.send_all(http_cmd1, sizeof(http_cmd1)-1); // Send all the data to the remote host. 
+        if(option==1)
+        {
+        probe.convertTemperature(true, DS1820::all_devices); 
+        pc.printf(" It's %3.1f oC\r\n",probe.temperature()); 
+        client.send_all(http_cmd_current, sizeof(http_cmd_current)-1);
+        client.send_all(value, sizeof(value)-1);
+        if(probe.temperature()>25)
+        {
+            client.send_all(http_cmd_warning, sizeof(http_cmd_warning)-1);
+        }  
+        }
+        else if(option ==2)
+        {
+            client.send_all(http_cmd_last5temp, sizeof(http_cmd_last5temp)-1);
+            for(int i=0;i<=4;++i)
+            {
+                pc.printf("last %d value: %s",i,last5values[i]);
+                client.send_all(http_cmd_newline_start, sizeof(http_cmd_newline_start)-1);
+                client.send_all(last5values[i],sizeof(last5values[i])-1);
+                client.send_all(http_cmd_newline_end, sizeof(http_cmd_newline_end)-1);
+            }
+        }
+        client.send_all(http_cmd2, sizeof(http_cmd2)-1);        
         client.close(); //  Close the socket.
         pc.printf("\n\nClient closed\n\n\r");
      }