code

Dependencies:   MPU6050_SIM5320_TEST SDFileSystem WakeUp

Fork of Nucleo_L476RG_SDCard_WorkingSample by M J.

Files at this revision

API Documentation at this revision

Comitter:
suads
Date:
Wed Sep 13 16:07:18 2017 +0000
Parent:
1:d5774258d18b
Commit message:
first commit

Changed in this revision

SIM5320.cpp Show annotated file Show diff for this revision Revisions of this file
SIM5320.h Show annotated file Show diff for this revision Revisions of this file
SensorBoards.cpp Show annotated file Show diff for this revision Revisions of this file
SensorBoards.h Show annotated file Show diff for this revision Revisions of this file
WakeUp.lib 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
--- a/SIM5320.cpp	Sat Sep 09 14:36:17 2017 +0000
+++ b/SIM5320.cpp	Wed Sep 13 16:07:18 2017 +0000
@@ -25,7 +25,6 @@
     SIM5320Serial.printf("%s\r\n", command);
     wait(timeout);
     string ret = readBuffer();
-    rx_response=ret;
     const char* debugResponse = rx_buffer;
     printDebug(debugResponse);
     flushBuffer();
@@ -113,19 +112,4 @@
 
     return out2;
 
-}
-
-bool SIM5320::getGPS(){
-    enableGPS(true);
-    wait(30);
-    if ((sendCommandAndCheck("AT+CGPSINFO","N",2))){
-        enableGPS(false);
-        return 1;
-    }   
-    else {
-        enableGPS(false);
-        return 0;
-    }
-  }      
-    
-    
+}
\ No newline at end of file
--- a/SIM5320.h	Sat Sep 09 14:36:17 2017 +0000
+++ b/SIM5320.h	Wed Sep 13 16:07:18 2017 +0000
@@ -18,10 +18,8 @@
   Serial SIM5320Serial; 
   Serial debugSerial; 
   char rx_buffer[BUFFER_SIZE];
-  string rx_response;
   void flushBuffer();
   int rx_in; 
-  bool getGPS();
 
   bool connect(const char *apn, const char *userName, const char *passPhrase);
   bool disconnect();
--- a/SensorBoards.cpp	Sat Sep 09 14:36:17 2017 +0000
+++ b/SensorBoards.cpp	Wed Sep 13 16:07:18 2017 +0000
@@ -1,39 +1,120 @@
 #include "SensorBoards.h"
-
-uint8_t SensorBoards::getSensorNumbers(uint8_t address)
-{
-    //return boards[0].numberOfSensors;
-}
-uint8_t SensorBoards::getSensorIDs(uint8_t address, uint8_t *IDBuffer)
-{
+#include "mbed.h"
 
 
-}
-void SensorBoards::getSensorReadings(uint8_t address, uint8_t ID, uint8_t* IDMeasure)
+uint8_t SensorBoards::getSensorNumbers()
 {
+    I2C i2c(D14, D15);
+    char cmd[1];
+    for(int i=0; i<numberOfBoards; i++) {
+        uint8_t write_adress=(boards[i].I2CAddress << 1) & 0xFE;
+        uint8_t read_adress=(boards[i].I2CAddress << 1) | 0x01;
+        i2c.start();
+        i2c.write(write_adress);      //WRITE_ADRESS
+        i2c.write(SENSOR_NUMBERS);   //CMD   0XA1
+        wait(0.5);
+        i2c.stop();
+        i2c.start();
+        i2c.read(read_adress,cmd,1);
+        i2c.stop();
+        boards[i].numberOfSensors=cmd[0];
+        //printf("beoj senzora %d\n",boards[i].numberOfSensors);
+    }
+    return 1;
+
+
 
 
+    //return boards[0].numberOfSensors;
 }
-uint8_t SensorBoards::sensorBoardScanner()
+uint8_t SensorBoards::getSensorIDs()
 {
-    char ucdata_write[2];
-    short count=0;
-    I2C i2c(D14, D15); // sda, scl
-    for (int Device_Adress=0; Device_Adress<=0xFE; Device_Adress+=2) { //Stepping in 2 Because Read/Write use LSB
-        if (!i2c.write((Device_Adress|WRITE), ucdata_write, 1, 0)) { // Check for ACK from i2c Device NB I am 'ORing' the Write Bit
-            printf("ACK from the Device at Address %#4x\n\r",Device_Adress>>1);
-            //lcd.printf("ACK at:%#4x\n",Device_Adress);
-            boards[0].I2CAddress=Device_Adress>>1;
-            count=count+1;
+    //wait(3);
+    char cmd[2];
+    //printf("a1=%02x",boards[0].I2CAddress);
+    I2C i2c(D14, D15);
+    printf("Get sensor id's\n");
+  //  uint8_t length=2;//get_sensor_numbers(adress);
+   // uint8_t i=0;
+   // uint8_t value=0;
+   // printf("adresa= %02x",boards[0].I2CAddress);
+    for(int i=0; i<numberOfBoards; i++) {
+        //for(int k=0; k<boards[i].numberOfSensors; k++) {
+            //printf("__%02x",boards[0].I2CAddress);
+           // uint8_t write_adress=(0x4a << 1) & 0xFE;
+            //uint8_t read_adress=(0x4a << 1) | 0x01;
+            uint8_t write_adress=(boards[i].I2CAddress << 1) & 0xFE;
+            uint8_t read_adress=(boards[i].I2CAddress << 1) | 0x01;
+            i2c.start();//SD_CARD_WRITE
+            i2c.write(write_adress);
+            i2c.write(SENSORS_IDS);  //A2
+            wait(0.5);
+            i2c.stop();
+            wait(0.5);
+            i2c.start();
+            i2c.read(read_adress,cmd,2);
+            i2c.stop();
+          //  boards[i].sensorIDs[i]=
+           // IDBuffer[0]=cmd[0];
+           
+           for(int k=0; k<boards[i].numberOfSensors; k++) {
+               boards[i].sensorIDs[k]=cmd[k];
+              // printf("ove %d\n",boards[i].sensorIDs[k]);
         }
     }
-    printf("\n\r %d Devices have been detected!\n\r",count);
-    if (count!=0)
-    return 1;
-else
-    return 0;
-}
-uint8_t SensorBoards::returnBoardAdd()
-{
-    return boards[0].I2CAddress;
-}
\ No newline at end of file
+    
+      // printf("prosoooo sammmmm\n");
+        //printf("i=%02x  \n",IDBuffer[1]);
+        return 1;
+    }
+    void SensorBoards::getSensorReadings(uint8_t address, uint8_t ID, uint8_t *IDMeasure) {
+        I2C i2c(D14, D15);
+        char cmd[4];
+        float value;
+        uint8_t write_adress=(address << 1) & 0xFE;
+        uint8_t read_adress=(address << 1) | 0x01;
+        i2c.start();//SD_CARD_WRITE
+        i2c.write(write_adress);
+        i2c.write(SENSOR_READINGS); //A3
+        wait(0.2);
+        i2c.write(ID);  //id
+        i2c.stop();
+        wait(2);
+        i2c.start();
+        i2c.read(read_adress,cmd,4);
+        i2c.stop();
+        memcpy(&value,&cmd,4);
+        //*val=value;
+        for(int i=0;i<4;i++)
+        IDMeasure[i]=cmd[i];
+        printf("value=%f \n",value);
+// return 1;
+
+
+
+
+    }
+    uint8_t SensorBoards::sensorBoardScanner() {
+        char ucdata_write[2];
+        short count=0;
+        I2C i2c(D14, D15); // sda, scl
+        for (int Device_Adress=0; Device_Adress<=0xFE; Device_Adress+=2) { //Stepping in 2 Because Read/Write use LSB
+            if (!i2c.write((Device_Adress|WRITE), ucdata_write, 1, 0)) { // Check for ACK from i2c Device NB I am 'ORing' the Write Bit
+                if(Device_Adress>>1!=0x68) {
+                    //printf("ACK from the Device at Address %#4x\n\r",Device_Adress>>1);
+                    //lcd.printf("ACK at:%#4x\n",Device_Adress);
+                    boards[count].I2CAddress=Device_Adress>>1;
+                    count++;
+                    numberOfBoards++;
+                }
+            }
+        }
+        //printf("\n\r %d Devices have been detected!\n\r",count);
+        if (numberOfBoards!=0)
+            return 1;
+        else
+            return 0;
+    }
+    uint8_t SensorBoards::returnBoardAdd() {
+        return boards[0].I2CAddress;
+    }
\ No newline at end of file
--- a/SensorBoards.h	Sat Sep 09 14:36:17 2017 +0000
+++ b/SensorBoards.h	Wed Sep 13 16:07:18 2017 +0000
@@ -20,14 +20,16 @@
 class SensorBoards
 {
 public:
+	SensorBoards() : numberOfBoards(0) {} 
 	uint8_t sensorBoardScanner();
 	uint8_t sensorIDScanner();
-	uint8_t getSensorNumbers(uint8_t address);
-	uint8_t getSensorIDs(uint8_t address, uint8_t *IDBuffer);
-	void getSensorReadings(uint8_t address, uint8_t ID, uint8_t* IDMeasure);
+	uint8_t getSensorNumbers();
+	uint8_t getSensorIDs();
+	void getSensorReadings(uint8_t address, uint8_t ID, uint8_t *IDMeasure);
 	uint8_t returnBoardAdd();
-private:
-	Board boards[1];
+//private:
+	Board boards[8];
+	int numberOfBoards;
 };
 
 #endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/WakeUp.lib	Wed Sep 13 16:07:18 2017 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/Sissors/code/WakeUp/#65c04a02ad45
--- a/main.cpp	Sat Sep 09 14:36:17 2017 +0000
+++ b/main.cpp	Wed Sep 13 16:07:18 2017 +0000
@@ -3,48 +3,65 @@
 #include "SIM5320.h"
 #include "MPU6050.h"
 #include "SensorBoards.h"
+#include "WakeUp.h"
 
-#define APN "active.bhmobile.ba"
-#define USERNAME ""
-#define PASSWORD ""
-SIM5320 sim5320(PA_9, PA_10);
+
+DigitalOut myled(D13);
+
+
+void create_sensor_data(uint8_t *write_sd_buffer, uint8_t *sensor_board_readings, uint8_t b_id, uint8_t s_id);
+//SIM5320 sim5320(PA_9,PA_10);
 DigitalOut SIM5320_PWR(PA_1);
 DigitalOut SIM_PWR_KEY(PA_15);
-
+InterruptIn mpuInterrupt(PB_6);
+uint8_t sensor_board_readings[4];
+bool test = false;
 //              MOSI, MISO, SCLK, CS, name
-//SDFileSystem sd(PA_7, PA_6, PA_5, PA_4, "sd");
+SDFileSystem sd(PA_7, PA_6, PA_5, PA_4, "sd");
+
+
+uint8_t write_sd_buffer[12];
+
+void mpuInterruptCallback()
+{
+    test=!test;
+
+}
+MPU6050 mpu;
+int main()
+{
+    wait(2);
+    printf("Pocetak\n");
 
-int main() {
- wait(1);
- 
- printf("POCETAK...\r\n");
- SIM5320_PWR = 1;
- SIM_PWR_KEY = 1;
- wait(1);
- SIM_PWR_KEY = 0;
- printf("Start\n");
- wait(10);
- printf("Starting...\r\n");
- sim5320.sendCommand("AT+CNUM",2);
- printf("Checking credit balance...\r\n");
- sim5320.sendCommand("AT+CUSD=1, \"*100#\",15",5);
- sim5320.connect(APN,USERNAME,PASSWORD);
- 
- 
- 
- 
- 
- 
- 
-// sim5320.enableGPS(true);
-//wait(30);
-//****************************Code for GPS***********************************
-/*if(sim5320.getGPS())
-  printf("Imam koordinate a=%s  krahj\n",sim5320.rx_response);
-else
-  printf("Nemam koordinate\n");
+//////////////////////////////////////////////CODE FOR MPU////////////////////////////////////////////////////////////////
+    /*
+    mpuInterrupt.fall(mpuInterruptCallback);
+    wait(2);
+    mpu.calibrate(accelBias, gyroBias);
+    mpu.initialize();
+    mpu.setSleepEnabled(0);
+    wait(2);
+    mpu.setIntFreefallEnabled(1);
+    mpu.setIntZeroMotionEnabled(0);
+    mpu.setIntMotionEnabled(1);
+    //printf("Setting mpu parameters...\r\n");
+    mpu.setMotionDetectionThreshold(1);
+    mpu.setMotionDetectionDuration(15);
 
-printf("\nWait for new connection...\n");*/
+
+    while(1){
+        if(test){
+            printf("Desio se motion\r\n");
+            test = 0;
+            }
+        }
+    */
+
+
+
+
+
+
 
 
 
@@ -52,38 +69,155 @@
 
 
 
-//*************************Code for SD card read and write************************************
+///////////////////////////////////////////////////////////////CODE_FOR_GPS///////////////////////////////////////////////////////////
+    /*
+        wait(3);
+        printf("Star...\r\n");
+        SIM5320_PWR = 1;
+        SIM_PWR_KEY = 1;
+        wait(1);
+        SIM_PWR_KEY = 0;
+        wait(25);
+        wait(2);
+        printf("Starting...\r\n");
+        printf("Starting...\r\n");
+        printf("Starting...\r\n");
+        printf("Starting...\r\n");
+        sim5320.sendCommand("AT+CNUM",2);
+        printf("Checking credit balance...\r\n");
+        sim5320.sendCommand("AT+CUSD=1, \"*100#\",15",5);
+        //Testing GPS
+
+        sim5320.enableGPS(true);
+        wait(30);
+
+        //Gettting GPS location
+        sim5320.sendCommand("AT+CGPSINFO",2);
+        sim5320.sendCommand("AT+CGPSINFO",2);
+        sim5320.sendCommand("AT+CGPSINFO",2);
+
+      */
+
+
+
+
+
+///////////////////////////////////////CODE FOR GPRS/////////////////////////////////////////////////////////////////////////////////
+    /*
+        wait(20);
+        //Testing network connection and disconnection
+        sim5320.connect("active.bhmobile.ba","","");
+        wait(2);
+        if(sim5320.disconnect()) {
+            sim5320.printDebug("Disconnected\r\n");
+        }
+
+        else {
+            sim5320.printDebug("Still connected or error occured!\r\n");
+        }
+
+    */
+
+
+
+
+
+
+
+
+
+
+
+///////////////////////////////////////////CODE FOR SD CARD//////////////////////////////////////////////
+
+    /*
+    mkdir("/sd/mydir3", 0777);
+    printf("\nDirectory created\n");
+    char buf[9];
+    sd.writeSD("/sd/mydir3/sdtest2.txt", "1111111\n");
+    sd.writeSD("/sd/mydir3/sdtest2.txt", "2222222\n");
+    sd.writeSD("/sd/mydir3/sdtest2.txt", "3333333\n");
+    printf("Upisao 1,2 i3\n");
+    sd.readSD("/sd/mydir3/sdtest2.txt",buf);
+    printf("line=%s\n",buf);
+    sd.readSD("/sd/mydir3/sdtest2.txt",buf);
+    printf("line=%s\n",buf);
+    sd.readSD("/sd/mydir3/sdtest2.txt",buf);
+    printf("line=%s\n",buf);
+    */
+
+
+
+//////////////////////////////////////////////CODE FOR SENDOR_BOARDS////////////////////////////////////////////////////////////////
 /*
-mkdir("/sd/mydir8", 0777);
-printf("\nDirectory created\n");
-sd.writeSD("/sd/mydir3/sdtest.txt", "1111111\n");
-sd.writeSD("/sd/mydir3/sdtest.txt", "2222222\n");
-sd.writeSD("/sd/mydir3/sdtest.txt", "3333333\n");
-sd.writeSD("/sd/mydir3/sdtest.txt", "4444444\n");
-sd.writeSD("/sd/mydir3/sdtest.txt", "5555555\n");
-char buf[9];
-sd.writeSD("/sd/mydir3/sdtest2.txt", "1111111\n");
-sd.writeSD("/sd/mydir3/sdtest2.txt", "2111111\n");
-sd.writeSD("/sd/mydir3/sdtest2.txt", "3111111\n");
-printf("Upisao 1,2 i3\n");
-sd.readSD("/sd/mydir3/sdtest2.txt",buf);
-printf("line=%s\n",buf);
-sd.readSD("/sd/mydir3/sdtest2.txt",buf);
-printf("line=%s\n",buf);
-sd.readSD("/sd/mydir3/sdtest2.txt",buf);
-printf("line=%s\n",buf);
+    uint8_t IDBuffer[8];
+    uint8_t IDMeasure[4];
+    float measure=0.0;
+    float measure2=0.0;
+
+    SensorBoards B1;
+    B1.sensorBoardScanner();
+    B1.getSensorNumbers();
+    B1.getSensorIDs();
+    for(int k=0; k<B1.numberOfBoards; k++) {
+       // printf("broj senzora %d \n",B1.boards[k].numberOfSensors);
+        for(int i=0; i<B1.boards[k].numberOfSensors; i++){
+            B1.getSensorReadings(B1.boards[k].I2CAddress,B1.boards[k].sensorIDs[i],IDMeasure);
+            printf("measure=%d %d %d %d keaj\n",IDMeasure[0],IDMeasure[1],IDMeasure[2],IDMeasure[3]);
+            create_sensor_data(write_sd_buffer,IDMeasure,B1.boards[k].I2CAddress,B1.boards[k].sensorIDs[i]);
+            }
+    }
+
+for(int i=0;i<12;i++)
+printf("_%d\n",write_sd_buffer[i]);
 */
 
 
 
 
 
-/*********************Code for scanning I2C Boards*************************
-SensorBoards b1;
-    uint8_t flag=b1.sensorBoardScanner();
-    if (flag) {
-        printf("nadjena ploca sa adresom %d\n", b1.returnBoardAdd());
+
+
+////////////////////////////////////////////////////////////////////////////////////////////////////////////
+//set_time(1505233738);  // Set RTC time to Wed, 28 Oct 2009 11:35:37
+WakeUp::calibrate();
+myled = 0;
+    while (true) {
+//        time_t seconds = time(NULL);
+//        printf("T= %d\n", seconds);
+//        printf("Time = %s", ctime(&seconds));
+//        wait(1);
+
+WakeUp::set_ms(7000);
+printf("sleep\n");
+wait(2);
+deepsleep();
+wait(2);
+//myled = !myled;
+printf("wAKE_UP\n");
+
     }
-    else
-        printf("Nije nadjena ploca\n");*/
+
+
+
+
+}
+void create_sensor_data(uint8_t *write_sd_buffer, uint8_t *sensor_board_readings, uint8_t b_id, uint8_t s_id)
+{
+    //pcf8563_read(&rtc);
+    
+    write_sd_buffer[0]=0;
+    write_sd_buffer[1]=1;
+    write_sd_buffer[2]=2;
+    write_sd_buffer[3]=3;
+    write_sd_buffer[4]=4;
+    write_sd_buffer[5] =b_id;       //sensor boaard id
+    write_sd_buffer[6] =s_id;    //sensor id
+    write_sd_buffer[7] =sensor_board_readings[3];          //sensor reading
+    write_sd_buffer[8] =sensor_board_readings[2];          //sensor reading
+    write_sd_buffer[9] =sensor_board_readings[1];          //sensor reading
+    write_sd_buffer[10]=sensor_board_readings[0];         //sensor reading
+    write_sd_buffer[11]=0xFF;
+    write_sd_buffer[12]=0xFF;
+    
 }
\ No newline at end of file