final

Dependencies:   KS0108 WIZnet_Library mbed

Fork of bigthingRec by rohan gala

main.cpp

Committer:
rohangala
Date:
2016-03-15
Revision:
3:0ee289161a2a
Parent:
2:015a5a76bb82
Child:
4:a4ec8ea99d70

File content as of revision 3:0ee289161a2a:

#include "mbed.h"
#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;
   /*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 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 == 0x01)
        {      
          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);
        
        }

        else if(val == 0x02)
        {      
           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);
        
       }
       
      } 
       
    sock.close();
    
    ethernet.disconnect();
    printf("Socket Closed");
    }       
 }