final

Dependencies:   KS0108 WIZnet_Library mbed

Fork of bigthingRec by rohan gala

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "stdio.h"
00003 #include "WIZnetInterface.h"
00004 
00005 unsigned char MAC_Addr[6] = {0x90,0x08,0xDC,0x12,0x07,0x07};
00006 
00007 char* Update_Key = "L055HRYFCBL1LTM2";
00008 char* ServerIP = "api.thingspeak.com";
00009 int Count = 15;
00010 
00011 SPI spi(PTD2,PTD3,PTD1);
00012 WIZnetInterface ethernet(&spi,PTD0,PTA1);
00013 
00014 Serial pc(USBTX, USBRX);
00015 Serial micro(PTE0, PTE1); // tx, rx
00016 
00017 char buff[]={'\0'};
00018 
00019 AnalogIn ax(PTC1);
00020 AnalogIn ain(PTC2);
00021 
00022 
00023 BusOut SevSeg1(PTD4,PTA12,PTA4,PTA5);
00024 BusOut SevSeg2(PTC8,PTC9,PTA13,PTD5); 
00025 
00026 //DigitalOut toutb(PTA1);
00027 PwmOut ledout(LED1);
00028 
00029 float luxm(void)
00030 {
00031     float vr=0;
00032     float lr=0;
00033     float vol[14]={0,1.1,1.23,1.3,1.45,1.6,1.81,2.06,2.31,2.62,2.81,3.02,3.1,3.3};
00034     float lux[14]={27000,20000,16000,11000,7000,4000,1500,833,578,201,104,40,23,0};
00035 
00036     int i;
00037     float m=0;
00038 
00039     vr=ain*3.3;
00040 
00041     for(i=0; vr> vol[i];i++);
00042     m = ((lux[i]-lux[i-1])/(vol[i]-vol[i-1]));
00043     lr=lux[i-1]+(m*(vr-(vol[i-1])));
00044 
00045     return lr;
00046 }
00047 
00048 
00049 uint8_t val,val1;
00050 
00051 int main() 
00052 {
00053     pc.printf("Start\r\n");
00054         
00055     do
00056     {
00057         int ret = ethernet.init(MAC_Addr);
00058         
00059         if (!ret) 
00060         {
00061             pc.printf("Initialized, MAC: %s\r\n", ethernet.getMACAddress());
00062             ret = ethernet.connect();
00063             if (!ret) 
00064             {
00065                 pc.printf("IP: %s, MASK: %s, GW: %s\r\n",
00066                           ethernet.getIPAddress(), ethernet.getNetworkMask(), ethernet.getGateway());
00067                           val=0x00;
00068             } 
00069             else 
00070             {
00071                 pc.printf("Error ethernet.connect() - ret = %d\r\n", ret);
00072                 val=0xff;
00073             }
00074         
00075         } 
00076         else 
00077         {
00078             pc.printf("Error ethernet.init() - ret = %d\r\n", ret);
00079             val=0xff;;
00080         }
00081     }while(val==0xff);
00082     
00083     TCPSocketConnection sock;       
00084     do
00085     {   
00086         sock.connect("api.thingspeak.com", 80);
00087         if(sock.is_connected())
00088             {
00089                 pc.printf("Socket Connected\n\r");
00090                 val=0x00;
00091             }
00092         else
00093             {
00094                 pc.printf("Socket NoT Connected\n\r");
00095                 val=0xff;
00096             }
00097             
00098     }while(val==0xff);
00099         
00100     char buffer[300];
00101     int ret_t;
00102     char http_cmd[256]; 
00103     
00104     int i;
00105     int j=0;
00106     while(1) 
00107     { 
00108                 
00109     
00110         do
00111         {   
00112             for(i=j;i<60;i++)
00113             {
00114                 float l=ain;
00115                 float t=ax;
00116                 
00117                 ledout=1.0-l;
00118        
00119                 if(l>=0 && l<0.25)
00120                     SevSeg1 = 0x00;  
00121                 else if (l>=0.25 && l<0.50)
00122                     SevSeg1 = 0x01          ;  
00123                 else if (l>=0.50 && l<0.75)
00124                     SevSeg1 = 0x03;  
00125                 else if (l>=0.75 && l<=1.0)
00126                     SevSeg1 = 0x07;  
00127                 
00128                 if(t<0.5050)
00129                     SevSeg2 = 0x00;  
00130                 else if (t>=0.5050 && t<0.5070)
00131                     SevSeg2 = 0x01          ;  
00132                 else if (t>=0.5070)
00133                     SevSeg2 = 0x03;  
00134 
00135                     
00136                         
00137                 if(micro.readable()) 
00138                 {
00139                    val=(micro.getc());
00140                    j=i;
00141                    i=25;                   
00142                 }
00143            
00144                 else
00145                     wait(1);
00146             }
00147         
00148             TCPSocketConnection sock;       
00149             do
00150             {   
00151             sock.connect("api.thingspeak.com", 80);
00152             if(sock.is_connected())
00153             {
00154                 pc.printf("Socket Connected\n\r");
00155                 val1=0x00;
00156             }
00157             else
00158             {
00159                 pc.printf("Socket NoT Connected\n\r");
00160                 val1=0xff;
00161             }
00162             
00163             }while(val1==0xff);
00164     
00165             if(val == 0x00)
00166             {
00167                 pc.printf("%.2f ",luxm());
00168                 
00169                 sprintf(http_cmd,"GET /update?key=L055HRYFCBL1LTM2&field1= %.2f HTTP/1.0\n\n",luxm());
00170                 printf("Running - %s\r\n",http_cmd);
00171                 sock.send_all(http_cmd, sizeof(http_cmd)-1);
00172         
00173                 ret_t = sock.receive(buffer, sizeof(buffer)-1);
00174                 buffer[ret_t] = '\0';
00175                 printf("Received %d chars from server:\n%s\r\n", ret_t, buffer);
00176 
00177                 
00178                 pc.printf("%.2f ",ax*3.3*17);
00179     
00180                 sprintf(http_cmd,"GET /update?key=L055HRYFCBL1LTM2&field2= %.4f HTTP/1.0\n\n",ax*3.3*17/2);
00181                 printf("Running - %s\r\n",http_cmd);
00182                 sock.send_all(http_cmd, sizeof(http_cmd)-1);
00183         
00184                 ret_t = sock.receive(buffer, sizeof(buffer)-1);
00185                 buffer[ret_t] = '\0';
00186                 printf("Received %d chars from server:\n%s\r\n", ret_t, buffer);
00187                 val=0x00;
00188                 j=0;
00189             }
00190             else if(val == 0x01)
00191             {  
00192                 pc.printf("%.2f ",luxm());
00193           
00194                 sprintf(http_cmd,"GET /update?key=L055HRYFCBL1LTM2&field1= %.2f HTTP/1.0\n\n",luxm());
00195                 printf("Running - %s\r\n",http_cmd);
00196                 sock.send_all(http_cmd, sizeof(http_cmd)-1);
00197         
00198                 ret_t = sock.receive(buffer, sizeof(buffer)-1);
00199                 buffer[ret_t] = '\0';
00200                 printf("Received %d chars from server:\n%s\r\n", ret_t, buffer);
00201                 val=0x00;
00202             }
00203 
00204             else if(val == 0x02)
00205             {      
00206                 pc.printf("%.2f ",ax*3.3*17);
00207     
00208                 sprintf(http_cmd,"GET /update?key=L055HRYFCBL1LTM2&field2= %.4f HTTP/1.0\n\n",ax*3.3*17/2);
00209                 printf("Running - %s\r\n",http_cmd);
00210                 sock.send_all(http_cmd, sizeof(http_cmd)-1);
00211         
00212                 ret_t = sock.receive(buffer, sizeof(buffer)-1);
00213                 buffer[ret_t] = '\0';
00214                 printf("Received %d chars from server:\n%s\r\n", ret_t, buffer);
00215                 val=0x00;
00216             }
00217         }while(val==0x00);
00218     
00219     } 
00220        
00221     //sock.close();
00222     
00223     //ethernet.disconnect();
00224     //printf("Socket Closed");
00225 }       
00226   
00227  //        float x=ain;
00228         //led=x;