final

Dependencies:   KS0108 WIZnet_Library mbed

Fork of bigthingRec by rohan gala

Revision:
3:0ee289161a2a
Parent:
2:015a5a76bb82
Child:
4:a4ec8ea99d70
--- a/main.cpp	Tue Mar 15 09:40:07 2016 +0000
+++ b/main.cpp	Tue Mar 15 11:38:05 2016 +0000
@@ -1,65 +1,136 @@
 #include "mbed.h"
-#include "stdio.h"        
-DigitalOut myled(LED1);
+#include "stdio.h"
+#include "WIZnetInterface.h"
+
+unsigned char MAC_Addr[6] = {0x90,0x08,0xDC,0x12,0x07,0x07};
+
+char* Update_Key = "L055HRYFCBL1LTM2";
+char* ServerIP = "api.thingspeak.com";
+int Count = 15;
+
+SPI spi(PTD2,PTD3,PTD1);
+WIZnetInterface ethernet(&spi,PTD0,PTA1);
+
 Serial pc(USBTX, USBRX);
 Serial micro(PTE0, PTE1); // tx, rx
+
 char buff[]={'\0'};
+
 AnalogIn ax(PTC1);
 
 float luxm(void)
 {
 AnalogIn ain(PTC2);
 
-float vr=0,lr=0;
-   
+float vr=0;
+   /*float vr=0,lr=0;
 float vol[12]={0.36,0.61,0.8,0.9,1.23,1.45,1.81,2.06,2.62,2.81,3.02,3.25};
 float lux[12]={27000,20000,16000,11000,4000,1500,833,578,201,104,40,23};
 
 int i;
 float m=0;
-
+*/
 vr=ain*3.3;
+/*
 for(i=0; vr> vol[i];i++);
 m = ((lux[i]-lux[i+1])/(vol[i]-vol[i+1]));
 lr=lux[i+1]+(m*(vr-(vol[i+1])));
-
-return lr;
+*/
+return vr;
 }
 
-
-
-
 uint8_t val;
  
 int main() 
 {
-
      while(1) 
      {
+    pc.printf("Start\r\n");
+      val=0x00;   
+      int ret = ethernet.init(MAC_Addr);
+        
+        if (!ret) 
+        {
+            pc.printf("Initialized, MAC: %s\r\n", ethernet.getMACAddress());
+            ret = ethernet.connect();
+            if (!ret) 
+            {
+                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);
+                val=0xff;
+            }
+        
+        } 
+        else 
+        {
+            pc.printf("Error ethernet.init() - ret = %d\r\n", ret);
+            val=0xff;;
+        }  
+        
+
+        TCPSocketConnection sock;
+        sock.connect("api.thingspeak.com", 80);
+        if(sock.is_connected())
+            pc.printf("Socket Connected\n\r");
+        else
+            pc.printf("Socket NoT Connected\n\r");
+            
+            
+     
+     char buffer[300];
+    int ret_t;
+    char http_cmd[256]; 
+    while(val==0x00)
+    {
       if(micro.readable()) 
       {
         val=(micro.getc());
-        
-
-        if(val == 0x00)
+        if(val == 0x01)
         {      
-            
-            
-            pc.printf("%.2f ",luxm());
-            
-            
+          pc.printf("%.2f ",luxm());
+          
+    
+    sprintf(http_cmd,"GET /update?key=L055HRYFCBL1LTM2&field1= %.2f HTTP/1.0\n\n",luxm());
+    printf("Running - %s\r\n",http_cmd);
+    sock.send_all(http_cmd, sizeof(http_cmd)-1);
+        
+    ret_t = sock.receive(buffer, sizeof(buffer)-1);
+    buffer[ret_t] = '\0';
+    printf("Received %d chars from server:\n%s\r\n", ret_t, buffer);
+        
         }
 
-        if(val == 0x01)
+        else if(val == 0x02)
         {      
+           pc.printf("%.2f ",ax*3.3);
            
-            pc.printf("%.2f ",ax*3.3);
+    
+    sprintf(http_cmd,"GET /update?key=L055HRYFCBL1LTM2&field2= %.2f HTTP/1.0\n\n",ax*3.3);
+    printf("Running - %s\r\n",http_cmd);
+    sock.send_all(http_cmd, sizeof(http_cmd)-1);
+        
+    ret_t = sock.receive(buffer, sizeof(buffer)-1);
+    buffer[ret_t] = '\0';
+    printf("Received %d chars from server:\n%s\r\n", ret_t, buffer);
+     
         }
   
+        else
         val=0x00;
         
         wait(2);
-         myled=1;
+        
        }
+       
+      } 
+       
+    sock.close();
+    
+    ethernet.disconnect();
+    printf("Socket Closed");
     }       
  }  
\ No newline at end of file