Akash Gaikwad / Mbed 2 deprecated ATT_Cellular_IOT_Using_ATT_Flow_and_M2X_Cloud

Dependencies:   DHT11 FXOS8700CQ MODSERIAL mbed

Fork of Avnet_ATT_Cellular_IOT by Avnet

Files at this revision

API Documentation at this revision

Comitter:
agaikwad
Date:
Tue Apr 17 21:29:20 2018 +0000
Parent:
81:f236ef0ee0c5
Commit message:
Remote I/O Sensor bus with AT&T flow and M2X cloud

Changed in this revision

DHT.lib Show annotated file Show diff for this revision Revisions of this file
config_me.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
sensors.cpp Show annotated file Show diff for this revision Revisions of this file
sensors.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DHT.lib	Tue Apr 17 21:29:20 2018 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/agaikwad/code/DHT11/#ad3ac9fb885d
--- a/config_me.h	Thu Nov 17 18:21:52 2016 +0000
+++ b/config_me.h	Tue Apr 17 21:29:20 2018 +0000
@@ -26,10 +26,15 @@
 // This is the server's base URL name.  Example "www.google.com"
 // Note that when you Fork a FLOW, it will typically assign  either
 // "run-east.att.io" or "run-west.att.io", so be sure to check this.
+
+//static const char * MY_SERVER_URL       = "run-east.att.io";
 static const char * MY_SERVER_URL       = "run-west.att.io";
 
 // These are FLOW fields from the Endpoints tab:
-#define FLOW_BASE_URL                   "/1e464b19cdcde/774c88d68202/86694923d5bf28a/in/flow"
+//#define FLOW_BASE_URL                   "/f77eac636bc3c/0938084ba00d/02023ad4d9819fc/in/flow"
+
+#define FLOW_BASE_URL                   "/e7029bd26faf8/9acef64eda2d/63a0820330f9994/in/flow"
+//https://run-west.att.io/e7029bd26faf8/9acef64eda2d/63a0820330f9994/in/flow
 #define FLOW_INPUT_NAME                 "/climate"
 
 // Unless you want to use a different protocol, this field should be left as is:
@@ -39,7 +44,7 @@
 // If you only have one devive there then you can just leave this as is.
 // Once your FLOW device has been initialized (Virtual Device Initialize clicked),
 // the Virtual Device will show up in M2X.  This is its "DEVICE SERIAL" field
-#define FLOW_DEVICE_NAME                "vstarterkit001"
+#define FLOW_DEVICE_NAME                "starterkit001"
 
 // This constant defines how often sensors are read and sent up to FLOW
 #define SENSOR_UPDATE_INTERVAL_MS       5000; //5 seconds
@@ -56,7 +61,7 @@
 #define TEMP_HUMIDITY_ACCELEROMETER_GPS                         3
 #define TEMP_HUMIDITY_ACCELEROMETER_PMODSENSORS                 4
 #define TEMP_HUMIDITY_ACCELEROMETER_PMODSENSORS_VIRTUALSENSORS  5
-static int iSensorsToReport = TEMP_HUMIDITY_ACCELEROMETER; //modify this to change your selection
+static int iSensorsToReport = TEMP_HUMIDITY_ONLY; //modify this to change your selection
 
 // This is the APN name for the cellular network, you will need to change this, check the instructions included with your SIM card kit:
 static const char * MY_APN_STR          = "m2m.com.attz";
--- a/main.cpp	Thu Nov 17 18:21:52 2016 +0000
+++ b/main.cpp	Tue Apr 17 21:29:20 2018 +0000
@@ -22,6 +22,10 @@
 #include "sensors.h"
 #include "cell_modem.h"
 #include "hardware.h"
+#include "DHT.h"
+#include<stdio.h>
+#include<math.h>
+
 
 I2C i2c(PTC11, PTC10);    //SDA, SCL -- define the I2C pins being used
 MODSERIAL pc(USBTX, USBRX, 256, 256); // tx, rx with default tx, rx buffer sizes
@@ -30,6 +34,16 @@
 DigitalOut led_red(LED_RED);
 DigitalOut led_blue(LED_BLUE);
 
+//UART Code
+Serial device(PTB11,PTB10);  // tx, rx
+char temp[10];
+char humid[10];
+char data[100];
+void reverse(char *str, int len);
+int intToStr(int x, char str[], int d);
+void ftoa(float n, char *res, int afterpoint); 
+
+
 
 //********************************************************************************************************************************************
 //* Create string with sensor readings that can be sent to flow as an HTTP get
@@ -77,7 +91,7 @@
     {
         case TEMP_HUMIDITY_ONLY:
         {
-            sprintf(modem_string, "GET %s%s?serial=%s&temp=%s&humidity=%s %s%s\r\n\r\n", FLOW_BASE_URL, FLOW_INPUT_NAME, FLOW_DEVICE_NAME, SENSOR_DATA.Temperature, SENSOR_DATA.Humidity, FLOW_URL_TYPE, MY_SERVER_URL);
+            sprintf(modem_string, "GET %s%s?serial=%s&temp=%s&humidity=%s&ammeter=%s&voltmeter=%s&light=%s %s%s\r\n\r\n", FLOW_BASE_URL, FLOW_INPUT_NAME, FLOW_DEVICE_NAME, SENSOR_DATA.Temperature, SENSOR_DATA.Humidity,  SENSOR_DATA.Ammeter,  SENSOR_DATA.Voltmeter, SENSOR_DATA.Light, FLOW_URL_TYPE, MY_SERVER_URL);
             break;
         }
         case TEMP_HUMIDITY_ACCELEROMETER:
@@ -235,14 +249,24 @@
 
     // Send and receive data perpetually
     while(1) {
+    
+        
         #ifdef USE_VIRTUAL_SENSORS
         ProcessUsbInterface();
         #endif
+        
         if  (bTimerExpiredFlag)
         {
             bTimerExpiredFlag = false;
             read_sensors(); //read available external sensors from a PMOD and the on-board motion sensor
+            
+            //UART code
+            sprintf(data,"%s&%s&%s&%s&%s",SENSOR_DATA.Temperature, SENSOR_DATA.Humidity, SENSOR_DATA.Light, SENSOR_DATA.Voltmeter, SENSOR_DATA.Ammeter);
             char modem_string[512];
+            PRINTF("%s", data);
+            device.printf("%s", data);
+            //UART Code end
+            
             GenerateModemString(&modem_string[0]);
             char myJsonResponse[512];
             if (cell_modem_Sendreceive(&modem_string[0], &myJsonResponse[0]))
@@ -257,3 +281,4 @@
         } //bTimerExpiredFlag
     } //forever loop
 }
+
--- a/sensors.cpp	Thu Nov 17 18:21:52 2016 +0000
+++ b/sensors.cpp	Tue Apr 17 21:29:20 2018 +0000
@@ -23,11 +23,42 @@
 #include "HTS221.h"
 #include "xadow_gps.h"
 #include <string>
+#include "DHT.h"
+#include<stdio.h>
+#include<math.h>
 
 //I2C for pmod sensors:
 #define Si1145_PMOD_I2C_ADDR   0xC0 //this is for 7-bit addr 0x60 for the Si7020
 #define Si7020_PMOD_I2C_ADDR   0x80 //this is for 7-bit addr 0x4 for the Si7020
 
+//for light code
+I2C akash(PTE25,PTE24);
+const int addr = 0x88;
+ // light code end
+ 
+//DHT code  
+DHT sensor(PTC10,DHT11);
+
+
+//For Ammeter and Voltmeter
+SPI SPI_Bus(PTC6,PTC7,PTC5); // (MOSI MISO CLK)setup SPI interface
+DigitalOut SPI_CS_AMM(PTC8);
+DigitalOut SPI_CS_VOLT(PTC9);
+
+
+
+float Amm_Out = 0;
+float Volt_Out = 0;
+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;
+int Temp_i_4 = 0;
+char I2C_Cmd[3];
+
 // Storage for the data from the motion sensor
 SRAWDATA accel_data;
 SRAWDATA magn_data;
@@ -412,13 +443,184 @@
     }
 } //Init_HTS221()
 
+void Read_temp_humid()
+{
+int error = 0;
+    float h = 0.0f, c = 0.0f, f = 0.0f, k = 0.0f, dp = 0.0f, dpf = 0.0f;
+
+    
+        wait(2.0f);
+        error = sensor.readData();
+        PRINTF("Error: %d\n", error);
+        if (0 == error) {
+            c   = sensor.ReadTemperature(CELCIUS);
+            f   = sensor.ReadTemperature(FARENHEIT);
+            k   = sensor.ReadTemperature(KELVIN);
+            h   = sensor.ReadHumidity();
+            dp  = sensor.CalcdewPoint(c, h);
+            dpf = sensor.CalcdewPointFast(c, h);
+            PRINTF("Temperature in Celcius is %4.2f \n\r", c);
+            PRINTF("Humidity is %4.2f\n\r", h);
+            
+            sprintf(SENSOR_DATA.Temperature, "%.2f", f);
+            sprintf(SENSOR_DATA.Humidity, "%.2f", h);
+            
+       } else {
+            PRINTF("Error: %d\n", error);
+        }
+    
+}
+
+
+//Read_Ammeter
+
+void Read_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;
+      
+        
+        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);
+            PRINTF("Current value = %f mA\r\n", Amm_Out);
+            sprintf(SENSOR_DATA.Ammeter, "%0.2f", Amm_Out);
+            
+        wait_ms(100);
+}
+
+
+
+void Read_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;
+       
+        
+        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.3405) / 4096); // show value in volts.
+        
+        Volt_Out = (float)(Temp_f_2 - 16.5)*2;
+        
+       
+            PRINTF("Voltage value = %f V\r\n", Volt_Out);
+            sprintf(SENSOR_DATA.Voltmeter, "%0.2f", Volt_Out);
+        wait_ms(100);  */
+        
+ 
+    
+        SPI_CS_VOLT=0;
+        //spi.write();
+        //pc.printf("%d\r\n",k);
+        int high_byte = SPI_Bus.write(0);
+        int low_byte = SPI_Bus.write(0);
+        SPI_CS_VOLT=1;
+        
+        float x = ((high_byte & 0x1f) << 7) | ((low_byte >> 1));
+        pc.printf("x= %f \r\n", x);
+        
+        float r= (float)((x * 33.3405) / 4096); // show value in volts.
+        float Volt_Out = (float)(r - 16.5)*2; 
+ 
+                PRINTF("Voltage value = %f V\r\n", Volt_Out);
+            sprintf(SENSOR_DATA.Voltmeter, "%0.2f", Volt_Out);
+     
+       
+    
+        
+         
+}
+
+
+
+//Read_light Code Start 
+
+void Read_light()
+{
+    
+// light code
+int exp,exp1,l=1;
+float z;
+float x;
+  
+    akash.frequency(100000); // set required i2c frequency
+    //pc.baud(9600); //set baud rate
+   // pc.printf("I2C started!\r\n");
+    char cmd[3]; //for byte transfer
+    
+//ambient light sensor code
+   cmd[0] = 0x01;   //configuration register
+   cmd[1]= 0xCC;    //configuration data
+    cmd[2]= 0x01;   //configuration data
+  akash.write(addr, cmd, 3);
+         cmd[0] = 0x00; // data register
+    akash.write(addr, cmd, 1);
+       wait(0.5);
+       akash.read(addr, cmd, 2);
+ 
+        exp= cmd[0]>>4;
+        exp1= (cmd[0]-(exp<<4))*256+cmd[1];
+        l=1;
+        for(int r=0;r<exp;r++){l=l*2;};
+        z= (exp1*l)/100;
+        //pc.printf("Lux = %.2f\n\r", z);  // printing LUX value
+        
+sprintf(SENSOR_DATA.Light, "%.2f", z);
+  
+  /*
+  
+        //sprintf(SENSOR_DATA.Temperature, "%0.2f", CTOF(hts221.readTemperature()));
+        sprintf(SENSOR_DATA.Temperature, "36.33");
+        //sprintf(SENSOR_DATA.Humidity, "%02d", hts221.readHumidity());
+        sprintf(SENSOR_DATA.Humidity, "99");
+    */    
+        
+} //Read_light()
+
+
+//Read_light Code Start 
+
+
 void Read_HTS221()
 {
-    if (bHTS221_present)
-    {
-        sprintf(SENSOR_DATA.Temperature, "%0.2f", CTOF(hts221.readTemperature()));
-        sprintf(SENSOR_DATA.Humidity, "%02d", hts221.readHumidity());
-    } //bHTS221_present
+
+
 } //Read_HTS221()
 
 bool bGPS_present = false;
@@ -652,18 +854,23 @@
 #ifdef USE_VIRTUAL_SENSORS
    pc.attach(&UsbUartRxCallback, MODSERIAL::RxIrq);
 #endif
-    Init_HTS221();
-    Init_Si7020();
-    Init_Si1145();
-    Init_motion_sensor();
-    Init_GPS();
+    //Init_HTS221();
+    //Init_Si7020();
+   // Init_Si1145();
+    //Init_motion_sensor();
+   // Init_GPS();
 } //sensors_init
 
 void read_sensors(void)
 {
-    Read_HTS221();
-    Read_Si7020();
-    Read_Si1145();
-    Read_motion_sensor();
-    Read_GPS();
+    Read_light();
+    Read_temp_humid();
+    Read_Ammeter();
+    Read_Voltmeter();
+    
+    //Read_HTS221();
+    //Read_Si7020();
+    //Read_Si1145();
+    //Read_motion_sensor();
+    //Read_GPS();
 } //read_sensors
--- a/sensors.h	Thu Nov 17 18:21:52 2016 +0000
+++ b/sensors.h	Tue Apr 17 21:29:20 2018 +0000
@@ -24,7 +24,10 @@
 
 #define SENSOR_FIELD_LEN_LIMIT  32
 typedef struct
-{
+{   
+    char  Ammeter[SENSOR_FIELD_LEN_LIMIT];
+    char  Voltmeter[SENSOR_FIELD_LEN_LIMIT];
+    char  Light[SENSOR_FIELD_LEN_LIMIT];
     char  Temperature[SENSOR_FIELD_LEN_LIMIT];
     char  Humidity[SENSOR_FIELD_LEN_LIMIT];
     char  AccelX[SENSOR_FIELD_LEN_LIMIT];