BLE Mesh Light Sensor output to FRDMK64f

Fork of Hexi_Blinky_Example by Hexiwear

Revision:
15:67a7cca7ae06
Parent:
14:02ddfa711646
Child:
16:39e45e59677c
diff -r 02ddfa711646 -r 67a7cca7ae06 main.cpp
--- a/main.cpp	Sun Mar 19 20:17:30 2017 +0000
+++ b/main.cpp	Fri Mar 31 15:03:37 2017 +0000
@@ -2,212 +2,352 @@
 #include "mbed.h"
 #include "ESP8266.h"               // Include header file from Author: Antonio Quevedo
 #include "math.h"
+#include "DHT.h"
 #include <string>
 
-#define APIKEY 45IE5JPN8ZIK2W2M    //Put "Write key" of your channel in thingspeak.com 
+#define APIKEY JJAOBK32WOINKT00    //Put "Write key" of your channel in thingspeak.com 
 #define IP "184.106.153.149"       // IP Address of "api.thingspeak.com\"
-#define WIFI_SSID "Redmi"
-#define WIFI_PASS "akash12345"
+#define WIFI_SSID "Batman2G"
+#define WIFI_PASS "Alien12345"
+
+Serial FRDM_UART_Debug(USBTX,USBRX);
+
+ESP8266 ESP_8266_UART(PTC17, PTC16, 115200); // UART for ESP8266 Wifi module
+// Options are TX-RX - PTB11 - PTB10 , PTC17 - PTC16 , PTC15 - PTC14
 
-Serial pc(USBTX,USBRX);
-SPI spi(PTC6,PTC7,PTC5); // (MOSI MISO CLK)setup SPI interface on pins PTC5,PTC6,PTC7
-ESP8266 esp(PTC17, PTC16, 115200); // baud rate for wifi
+SPI SPI_Bus(PTD2,PTD3,PTD1); // (MOSI MISO CLK)setup SPI interface
+DigitalOut SPI_CS_AMM(PTC3);
+DigitalOut SPI_CS_VOLT(PTC2);
 
-DigitalOut cs1(PTC4);
-DigitalOut cs2(PTC3);
-I2C i2c(PTD9,PTD8);
+I2C I2C_Bus(PTE25,PTE24);
+
+AnalogIn AN_Thermo(PTB3); // Thermocouple Analog Input
+
+DigitalIn DG_Motion(PTB2); // Motion module Digital Input
 
-//AnalogIn check(PTB6);
-const int addr = 0x88;
+DHT DHT_Temp_Hum(PTC11,DHT22); //DHT Sensor
 
-char snd[255],rcv[1000],snd_Data[255];           //snd= string used to send command to ESP 8266 wii and  rcv = string used to receive response from ESP8266 wifi module  
+const int Light_I2C_Addr = 0x88;
+
+char ESP_8266_CMD_Send[255],ESP_8266_CMD_Recv[1000];           //ESP_8266_CMD_Send = string used to send command to ESP8266 &  ESP_8266_CMD_Recv = string used to receive response from ESP8266
 
-int cnt = 0;                       // counter for  number of motion detected
-int cnt1 = 0;                       // counter for  number of motion detected
-int cnt2 = 0;                       // counter for  number of motion detected
+float Amm_Out = 0;
+float Volt_Out = 0;
+float Light_Out = 0;
+float Thermo_Out = 0;
+float Temp_Out = 0;
+float Hum_Out = 0;
+int Motion_Out = 0;
+int Finger_Out = 0;
+float Pres_Out = 0;
 
-float ammeter_out = 0;
-float voltmeter_out = 0;
-float light_out = 0;
 
-void esp_initialize(void); // Function used to initialize ESP8266 wifi module 
-void esp_send(void);       // Function used to connect with thingspeak.com and update channel using ESP8266 wifi module 
+void ESP_8266_Init(void); // Function used to initialize ESP8266 wifi module 
+void ESP_8266_TX_Data(void);       // Function used to connect with thingspeak.com and update channel using ESP8266 wifi module 
 
 int main() 
 {
-    
-    pc.baud(115200);           // Baud rate used for communicating with Tera-term on PC
-    
-    pc.printf("START\r\n");  // Starting point
-    
-    esp_initialize();
+    int SPI_High_byte = 0;
+    int SPI_Low_byte = 0; 
+    float Temp_f_1 = 0.00;
+    float Temp_f_2 = 0.00;
+    int Temp_i_1 = 0;
+    int Temp_i_2 = 0;
+    int Temp_i_3 = 0;
+    char I2C_Cmd[3];
+    int loop_count = 0;
     
-    //ammeter + voltmeter code
-    spi.format(8,0);
-    spi.frequency(1000000);
-    pc.printf("Analog read test from spi.\n");
-    pc.printf("\n\r");
+    FRDM_UART_Debug.baud(115200);           // Baud rate used for communicating with Tera-term on PC
+    
+    ESP_8266_Init();
     
-    //light code
-    int exp,exp1,l=1;
-    i2c.frequency(100000); // set required i2c frequency
-    //pc.baud(9600); //set baud rate
-    pc.printf("I2C started!\r\n");
-    pc.printf("\n\r");
-    char cmd[3]; //for byte transfer
-
+    SPI_Bus.format(8,0);
+    SPI_Bus.frequency(1000000);
+    
+    I2C_Bus.frequency(100000); // set required i2c frequency
+    
+    FRDM_UART_Debug.printf("Start sampling data\r\n");  // Starting point
+    
     while (1) 
     {
+        Amm_Out = 0;
+        Volt_Out = 0;
+        Light_Out = 0;
+        Thermo_Out = 0;
+        Temp_Out = 0;
+        Hum_Out = 0;
+        Motion_Out = 0;
+
         wait(15);
-        //ammeter code
-    
-        cs1=0;
-        //spi.write();
-        //pc.printf("%d\r\n",k);
-        int high_byte = spi.write(0);
-        int low_byte = spi.write(0);
-        cs1=1;
-        float m = ((high_byte & 0x1f) << 7) | ((low_byte >> 1));    
-        float k= (float)((m*1)/4096); // show value in volts. 
-        ammeter_out = (float)((k-0.5)*1000);
-        pc.printf("Current value:            %f mA\r\n", ammeter_out);
-        //pc.printf("Analog Value of current : %.2f\n\r",m);
+        
+        // Copy Motion values
+        
+        Motion_Out = DG_Motion;
+        
+        // Ammeter
+        
+        SPI_High_byte = 0;
+        SPI_Low_byte = 0; 
+        Temp_f_1 = 0.00;
+        Temp_f_2 = 0.00;
+        Temp_i_1 = 0;
+        Temp_i_2 = 0;
+        Temp_i_3 = 0;
+        I2C_Cmd[0] = 0;
+        I2C_Cmd[1] = 0;
+        I2C_Cmd[2] = 0;
+        loop_count = 0;
+        
+        SPI_CS_AMM = 0;
+        
+        SPI_High_byte = SPI_Bus.write(0);
+        SPI_Low_byte = SPI_Bus.write(0);
+        
+        SPI_CS_AMM = 1;
+        
+        Temp_f_1 = (( SPI_High_byte & 0x1f ) << 7 ) | (( SPI_Low_byte >> 1 ));
+            
+        Temp_f_2= (float)(( Temp_f_1 * 1.00 ) / 4096.00 ); // Converting to volts
+        
+        Amm_Out = (float)(( Temp_f_2 - 0.50 ) * 1000.00);
+        
+        FRDM_UART_Debug.printf("Current value = %f mA\r\n", Amm_Out);
         wait_ms(100);
         
-        cs2=0;
-        //spi.write();
-        //pc.printf("%d\r\n",k);
-        int hi_byte = spi.write(0);
-        int lo_byte = spi.write(0);
-        cs2=1;
-        float x = ((hi_byte & 0x1f) << 7) | ((lo_byte >> 1));
+        // Voltmeter
+        
+        SPI_High_byte = 0;
+        SPI_Low_byte = 0; 
+        Temp_f_1 = 0.00;
+        Temp_f_2 = 0.00;
+        Temp_i_1 = 0;
+        Temp_i_2 = 0;
+        Temp_i_3 = 0;
+        I2C_Cmd[0] = 0;
+        I2C_Cmd[1] = 0;
+        I2C_Cmd[2] = 0;
+        loop_count = 0;  
+        
+        SPI_CS_VOLT = 0;
+
+        SPI_High_byte = SPI_Bus.write(0);
+        SPI_Low_byte = SPI_Bus.write(0);
+        
+        SPI_CS_VOLT = 1;
+        
+        Temp_f_1 = ((SPI_High_byte & 0x1f) << 7) | ((SPI_Low_byte >> 1));
+        
+        Temp_f_2 = (float)((Temp_f_1 * 33) / 4096); // show value in volts.
+        
+        Volt_Out = (float)(Temp_f_2 - 16.5);
         
-        float r= (float)((x*33)/4096); // show value in volts.
-        voltmeter_out = (float)(r-16.5);
-        pc.printf("AD Voltage channel value: %f V\r\n", voltmeter_out);
-        //pc.printf("Voltage Analog Value: %.2f\n\r",x);
+        FRDM_UART_Debug.printf("Voltage value = %f V\r\n", Volt_Out);
+        wait_ms(100);
+        
+         //ambient light 
+         
+        SPI_High_byte = 0;
+        SPI_Low_byte = 0; 
+        Temp_f_1 = 0.00;
+        Temp_f_2 = 0.00;
+        Temp_i_1 = 0;
+        Temp_i_2 = 0;
+        Temp_i_3 = 0;
+        I2C_Cmd[0] = 0;
+        I2C_Cmd[1] = 0;
+        I2C_Cmd[2] = 0;
+        loop_count = 0;
+        
+        I2C_Cmd[0] = 0x01;   //configuration register
+        I2C_Cmd[1]= 0xCC;    //configuration data
+        I2C_Cmd[2]= 0x01;   //configuration data
+        
+        I2C_Bus.write(Light_I2C_Addr, I2C_Cmd, 3);
+        
+        I2C_Cmd[0] = 0x00; // data register
+        
+        I2C_Bus.write(Light_I2C_Addr, I2C_Cmd, 1);
+        
         wait_ms(100);
         
-         //ambient light sensor code
-        
-        cmd[0] = 0x01;   //configuration register
-        cmd[1]= 0xCC;    //configuration data
-        cmd[2]= 0x01;   //configuration data
-        i2c.write(addr, cmd, 3);
-        cmd[0] = 0x00; // data register
-        i2c.write(addr, cmd, 1);
-        wait_ms(100);
-        i2c.read(addr, cmd, 2);
+        I2C_Bus.read(Light_I2C_Addr, I2C_Cmd, 2);
      
-        exp= cmd[0]>>4;
-        exp1= (cmd[0]-(exp<<4))*256+cmd[1];
-       // pc.printf("exponent = 0x%x\n\r", exp);
-       // pc.printf("fraction = %d\n\r", exp1);
-        l=1;
-        for(int r=0;r<exp;r++){l=l*2;};
-          //  pc.printf("value = %d\n\r", l);
-        light_out= (exp1*l)/100;
-        pc.printf("Lux = %.2f\n\r", light_out);  // printing LUX value
-        pc.printf("\n\r");
+        Temp_i_1= I2C_Cmd[0]>>4;
+        Temp_i_2= (I2C_Cmd[0]-(Temp_i_1<<4))*256+I2C_Cmd[1];
+
+        for(loop_count = 0,Temp_i_3 = 1 ; loop_count < Temp_i_1 ; Temp_i_3*=2,loop_count++);
+        
+        Light_Out= (Temp_i_2 * Temp_i_3) / 100;
+        
+        FRDM_UART_Debug.printf("Lux = %.2f\n\r", Light_Out);  
+        wait_ms(100); 
+        
+        // Thermocouple
+        
+        SPI_High_byte = 0;
+        SPI_Low_byte = 0; 
+        Temp_f_1 = 0.00;
+        Temp_f_2 = 0.00;
+        Temp_i_1 = 0;
+        Temp_i_2 = 0;
+        Temp_i_3 = 0;
+        I2C_Cmd[0] = 0;
+        I2C_Cmd[1] = 0;
+        I2C_Cmd[2] = 0;
+        loop_count = 0;
+        
+        Temp_f_1 = AN_Thermo.read_u16();
+        
+        Temp_f_1 = (( Temp_f_1 / 65536 ) * 330);
+        
+        Thermo_Out = Temp_f_1;
+        
+        FRDM_UART_Debug.printf("Thermocouple volt diff = %.2f C\r\n",Thermo_Out);
         wait_ms(100); 
         
-        pc.printf("Sending this information to thingspeak.com = %d\r\n",cnt);
-        esp_send(); 
+        // Temp and Humidity
+        
+        SPI_High_byte = 0;
+        SPI_Low_byte = 0; 
+        Temp_f_1 = 0.00;
+        Temp_f_2 = 0.00;
+        Temp_i_1 = 0;
+        Temp_i_2 = 0;
+        Temp_i_3 = 0;
+        I2C_Cmd[0] = 0;
+        I2C_Cmd[1] = 0;
+        I2C_Cmd[2] = 0;
+        loop_count = 0;
+        
+        Temp_i_1 = DHT_Temp_Hum.readData();
+        if (Temp_i_1 == 0)  // Read success
+        {
+            Temp_f_1 = DHT_Temp_Hum.ReadTemperature(FARENHEIT);
+            Temp_f_2 = DHT_Temp_Hum.ReadHumidity();
+        }
+        else // Read failure
+        {
+            Temp_f_1 = 0;
+            Temp_f_2 = 0;
+        }
+        
+        Temp_Out = Temp_f_1;
+        Hum_Out = Temp_f_2;
+        
+        FRDM_UART_Debug.printf("Temperature = %4.2f F ,  Humidity = %4.2f \r\n",Temp_Out,Hum_Out);
+        wait_ms(100); 
+        
+        FRDM_UART_Debug.printf("Sending this information to thingspeak.com\r\n");
+        ESP_8266_TX_Data(); 
         
     }
 }
 
 
-void esp_initialize(void)
+void ESP_8266_Init(void)
 {    
-    pc.printf("Initializing ESP\r\n"); 
+    FRDM_UART_Debug.printf("Initializing and Reset ESP\r\n"); 
       
-    pc.printf("Reset ESP\r\n"); 
-    esp.Reset();                   //RESET ESP
-    esp.RcvReply(rcv, 400);        //receive a response from ESP
-    //pc.printf(rcv);          //Print the response onscreen 
+    ESP_8266_UART.Reset();                   //RESET ESP
+    ESP_8266_UART.RcvReply(ESP_8266_CMD_Recv, 400);        //receive a response from ESP
+    //FRDM_UART_Debug.printf(ESP_8266_CMD_Recv);          //Print the response onscreen 
     wait(2);
     
-    strcpy(snd,"AT");
-    esp.SendCMD(snd);
-    pc.printf(snd);
+    strcpy(ESP_8266_CMD_Send,"AT");
+    ESP_8266_UART.SendCMD(ESP_8266_CMD_Send);
+    FRDM_UART_Debug.printf(ESP_8266_CMD_Send);
     //wait(2);
-    esp.RcvReply(rcv, 400);       
-    pc.printf(rcv);      
+    ESP_8266_UART.RcvReply(ESP_8266_CMD_Recv, 400);       
+    FRDM_UART_Debug.printf(ESP_8266_CMD_Recv);      
     wait(0.1);
     
-    strcpy(snd,"AT+CWMODE=1");
-    esp.SendCMD(snd);
-    pc.printf(snd);
+    strcpy(ESP_8266_CMD_Send,"AT+CWMODE=1");
+    ESP_8266_UART.SendCMD(ESP_8266_CMD_Send);
+    FRDM_UART_Debug.printf(ESP_8266_CMD_Send);
     wait(2);
-
-    strcpy(snd,"AT+CWJAP=\"");
-    strcat(snd,WIFI_SSID);
-    strcat(snd,"\",\"");
-    strcat(snd,WIFI_PASS);
-    strcat(snd,"\"");
+    
+    if(!strcmp(ESP_8266_CMD_Recv,"WIFI CONNECTED"))
+    {
+        strcpy(ESP_8266_CMD_Send,"AT+CWJAP=\"");
+        strcat(ESP_8266_CMD_Send,WIFI_SSID);
+        strcat(ESP_8266_CMD_Send,"\",\"");
+        strcat(ESP_8266_CMD_Send,WIFI_PASS);
+        strcat(ESP_8266_CMD_Send,"\"");
     
-    esp.SendCMD(snd);
-    pc.printf(snd);
-    wait(5);
-    esp.RcvReply(rcv, 400);       
-    pc.printf("\n %s \n", rcv); 
-    
-    strcpy(snd,"AT+CIPMUX=0");
-    esp.SendCMD(snd);
-    pc.printf(snd);
+        ESP_8266_UART.SendCMD(ESP_8266_CMD_Send);
+        FRDM_UART_Debug.printf(ESP_8266_CMD_Send);
+        wait(5);
+        ESP_8266_UART.RcvReply(ESP_8266_CMD_Recv, 400);       
+        FRDM_UART_Debug.printf(ESP_8266_CMD_Recv); 
+    }
+    else 
+        FRDM_UART_Debug.printf("Wifi was preconfigured\r\n"); 
+        
+    strcpy(ESP_8266_CMD_Send,"AT+CIPMUX=0");
+    ESP_8266_UART.SendCMD(ESP_8266_CMD_Send);
+    FRDM_UART_Debug.printf(ESP_8266_CMD_Send);
     //wait(2);
-    esp.RcvReply(rcv, 400);       
-    pc.printf("\n %s \n", rcv); 
+    ESP_8266_UART.RcvReply(ESP_8266_CMD_Recv, 400);       
+    FRDM_UART_Debug.printf(ESP_8266_CMD_Recv); 
 
 }
 
 
-void esp_send(void)
+void ESP_8266_TX_Data(void)
 {
    
     //ESP updates the Status of Thingspeak channel//
     
-    strcpy(snd,"AT+CIPSTART=");
-    strcat(snd,"\"TCP\",\"");
-    strcat(snd,IP);
-    strcat(snd,"\",80");
+    strcpy(ESP_8266_CMD_Send,"AT+CIPSTART=");
+    strcat(ESP_8266_CMD_Send,"\"TCP\",\"");
+    strcat(ESP_8266_CMD_Send,IP);
+    strcat(ESP_8266_CMD_Send,"\",80");
     
-    esp.SendCMD(snd); 
-    pc.printf("S\r\n%s",snd);
+    ESP_8266_UART.SendCMD(ESP_8266_CMD_Send); 
+    FRDM_UART_Debug.printf("S\r\n%s",ESP_8266_CMD_Send);
     //wait(2);                                                    
-    esp.RcvReply(rcv, 1000);
-    pc.printf("R\r\n%s",rcv);
+    ESP_8266_UART.RcvReply(ESP_8266_CMD_Recv, 1000);
+    FRDM_UART_Debug.printf("R\r\n%s",ESP_8266_CMD_Recv);
     wait(1);
     
-    sprintf(snd,"GET https://api.thingspeak.com/update?key=45IE5JPN8ZIK2W2M&field1=%f&field2=%f&field3=%f\r\n",ammeter_out,voltmeter_out,light_out);
+    /*
+        float Amm_Out = 0;
+        float Volt_Out = 0;
+        float Light_Out = 0;
+        float Thermo_Out = 0;
+        float Temp_Out = 0;
+        float Hum_Out = 0;
+        int Motion_Out = 0;
+    */
+    sprintf(ESP_8266_CMD_Send,"GET https://api.thingspeak.com/update?key=JJAOBK32WOINKT00&field1=%d&field2=%d&field3=%f&field4=%f&field5=%f&field6=%f&field7=%f&field8=%f\r\n"
+            ,Motion_Out,Finger_Out,Amm_Out,Volt_Out,Light_Out,Thermo_Out,Temp_Out,Hum_Out);
     
     int i=0;
-    for(i=0;snd[i]!='\0';i++);
+    for(i=0;ESP_8266_CMD_Send[i]!='\0';i++);
     i++;
     char cmd[255];
     
     sprintf(cmd,"AT+CIPSEND=%d",i);                                       //Send Number of open connection and Characters to send 
-    esp.SendCMD(cmd);
-    pc.printf("S\r\n%s",cmd);
-    while(i<=20 || rcv == ">")
+    ESP_8266_UART.SendCMD(cmd);
+    FRDM_UART_Debug.printf("S\r\n%s",cmd);
+    while(i<=20 || ESP_8266_CMD_Recv == ">")
     {
-        esp.RcvReply(rcv, 1000);
+        ESP_8266_UART.RcvReply(ESP_8266_CMD_Recv, 1000);
         wait(100);
         i++;
     }
-    pc.printf("R\r\n%s",rcv);
+    FRDM_UART_Debug.printf("R\r\n%s",ESP_8266_CMD_Recv);
     
-    esp.SendCMD(snd);                                                      //Post value to thingspeak channel
-    pc.printf("S\r\n%s",snd);
+    ESP_8266_UART.SendCMD(ESP_8266_CMD_Send);                                                      //Post value to thingspeak channel
+    FRDM_UART_Debug.printf("S\r\n%s",ESP_8266_CMD_Send);
     
-    while(i<=20 || rcv == "OK")
+    while(i<=20 || ESP_8266_CMD_Recv == "OK")
     {
-        esp.RcvReply(rcv, 1000);
+        ESP_8266_UART.RcvReply(ESP_8266_CMD_Recv, 1000);
         wait(100);
         i++;
     }
-    pc.printf("R\r\n%s",rcv);
+    FRDM_UART_Debug.printf("R\r\n%s",ESP_8266_CMD_Recv);
     
 }