Water level sensing with simulation using potentiometer. The value uploads on the local phant server using HTTP GET method.

Dependencies:   WIZnet_Library mbed

Revision:
0:2c1f983dd2cc
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Tue Aug 02 07:57:32 2016 +0000
@@ -0,0 +1,103 @@
+#include "mbed.h"
+#include "WIZnetInterface.h"
+
+//MAC address of sheild
+unsigned char MAC_Addr[6] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
+
+//server ip were server is set
+char* ServerIP = "192.168.0.101"; 
+
+//public and private key of stream
+char* public_key = "9Ojdd3Vk2aCqvx1Jg0yLsx2ParXq"; 
+char* private_key = "KzprrvRqKeC5neDN7GRbuQbExAG4"; 
+
+int Count = 15;
+int x;
+
+Serial pc(USBTX, USBRX);
+SPI spi(PTD2,PTD3,PTD1);
+
+//wiznet object
+WIZnetInterface ethernet(&spi,PTD0,PTA20);
+
+//analog input
+AnalogIn waterLevel(PTC1);
+int writtenLen,eth;
+
+int main()
+{
+    //Set serial port baudrate speed: 9600
+    pc.baud(9600);
+    pc.printf("Start\r\n");
+    
+    while(1) 
+    {
+        x=10*waterLevel.read();
+        int ret = ethernet.init(MAC_Addr);
+        
+        if (!ret) 
+        {
+            //print MAC address of device
+            pc.printf("Initialized, MAC: %s\r\n", ethernet.getMACAddress());
+            ret = ethernet.connect();
+            if (!ret)
+            {
+                //print IP alloted and details obtianed from network
+                pc.printf("IP: %s, MASK: %s, GW: %s\r\n",
+                          ethernet.getIPAddress(), ethernet.getNetworkMask(), ethernet.getGateway());
+            }
+            else
+            {
+                pc.printf("Error ethernet.connect() - ret = %d\r\n", ret);
+                exit(0);
+            }
+        }
+        else
+        {
+            pc.printf("Error ethernet.init() - ret = %d\r\n", ret);
+            exit(0);
+        }  
+        
+        //create TCP socket
+        TCPSocketConnection sock;
+        
+        while(1){
+            
+        //connect the server to port 8080
+            sock.connect(ServerIP, 8080);
+            if(sock.is_connected())
+                pc.printf("Socket Connected\n\r");
+            else
+                pc.printf("Socket NoT Connected\n\r");
+        
+            //create character buffer for URL
+            char buffer[300];
+            int ret_t;
+        
+            char http_cmd[100];
+        
+            //make URL
+            sprintf(http_cmd,"GET /input/%s?private_key=%s&pot=%d HTTP/1.0\n\n",public_key,private_key,x);
+            printf("sending %d characters",strlen(http_cmd));
+            printf("Running - %s\r\n",http_cmd);
+            writtenLen = sock.send_all(http_cmd, strlen(http_cmd));
+            
+            //print the result of URL GET method
+            if(writtenLen == -1)
+                printf("sending failed\r\n");
+            else    
+                printf("sending success\r\n");
+                
+            eth=ret_t = sock.receive(buffer, strlen(buffer));
+            buffer[ret_t] = '\0';
+            printf("Received %d chars from server:\r\n%s\r\n", ret_t, buffer);
+        
+            sock.close();
+        
+            ethernet.disconnect();
+            pc.printf("Socket Closed\r\n");
+            wait(1);
+            
+            }
+    }
+}