test

Dependencies:   mbed MMA8452Q

Fork of HelloWorld by Simon Ford

Revision:
30:5881c661b0bb
Parent:
29:3acc071af432
Child:
31:13d0d26413d3
Child:
32:bcfe51898e7c
diff -r 3acc071af432 -r 5881c661b0bb main.cpp
--- a/main.cpp	Fri Sep 29 16:34:22 2017 +0000
+++ b/main.cpp	Sat Sep 30 18:43:25 2017 +0000
@@ -3,39 +3,144 @@
 #include "mbed.h"    
 #include "MMA8452Q.h"
 
-Serial xbee1(p13,p14);
+Serial xbee_routeur(p13,p14);
 Serial pc(USBTX, USBRX); // tx, rx
 DigitalOut rst1(p8); //Digital reset for the XBee, 200ns for reset
+MMA8452Q mma8452(p9,p10,0x1d);
+DigitalIn btn(p15);
+
+LocalFileSystem local("local");               // Create the local filesystem under the name "local"
+
+uint16_t panId;  // panId
+uint8_t panId_LSB;
+uint8_t panId_MSB;
+
+char*   url;    // url
+char URL[10];
+
+void seperatePanId(void);
+void ReadFile (void);
+void setPanId(void);
+void sauvegarder(void);
 
 int main() { 
  
-    rst1 = 0; //Set reset pin to 0
-    wait_ms(400);//Wait at least one millisecond
-    rst1 = 1;//Set reset pin to 1
-    //wait_ms(400);//Wait another millisecond
-    int Y;
-     
-    while (1) //Neverending Loop
+    ReadFile();// when you run first time without this line, the setup file is on created
+    
+    wait(1);
+    
+    seperatePanId(); // séparé msb et lsb du panId
+    
+    pc.printf("panId lsb : %x\n\r",panId_LSB);
+    pc.printf("panId msb : %x\n\r",panId_MSB);
+    pc.printf("panId : %x\n\r",panId);
+    pc.printf("url: %s\n\r",URL);
+    
+    setPanId();     // set panId to Xbee
+    
+    wait_ms(500);
+    
+    sauvegarder();
+    
+    while(1)
+    {   
+    
+    if(xbee_routeur.readable())
     {
+     pc.putc(xbee_routeur.getc());   
+    }
+    
+    if(pc.readable())
+    {
+     xbee_routeur.putc(pc.getc());   
+    }
+    
+    /*
+        xbee_routeur.putc(0x7E);                     // start delimiter
+        xbee_routeur.putc(0x00);                     // length 1/2
+        xbee_routeur.putc(0x10);                     // legth 2/2
+        xbee_routeur.putc(0x10);                     // Frame type
+        xbee_routeur.putc(0x01);                     // Frame id
+        xbee_routeur.putc(0x00);       // 64-bit adress (broadcast)  
+        xbee_routeur.putc(0x00);       // 64-bit adress (broadcast) 
+        xbee_routeur.putc(0x00);       // 64-bit adress (broadcast) 
+        xbee_routeur.putc(0x00);       // 64-bit adress (broadcast) 
+        xbee_routeur.putc(0x00);       // 64-bit adress (broadcast) 
+        xbee_routeur.putc(0x00);       // 64-bit adress (broadcast) 
+        xbee_routeur.putc(0x00);       // 64-bit adress (broadcast) 
+        xbee_routeur.putc(0x00);       // 64-bit adress (broadcast)   
+        xbee_routeur.putc(0xFF);                   // 16-bit adress (broadcast)
+        xbee_routeur.putc(0xFE);                   // 16-bit adress (broadcast) 
+        xbee_routeur.putc(0x00);                     // Broadcast radius
+        xbee_routeur.putc(0x00);                     // options
         
-     //uart vers pc
-     
-        if(xbee1.readable())
-        { 
-        pc.putc(xbee1.getc());
-        }
+        xbee_routeur.putc(0x23);
+        xbee_routeur.putc(0x45);
+ 
+        long sum = 0xFF - ((0x10 + 0x01 + 0xFF + 0xFF + 0xFE + 0x23 + 0x45 + 1) & 0xFF); // calcul du checksum
+        xbee_routeur.printf("%x \n", sum);
+        xbee_routeur.putc(sum);           // checksum
+        
+        wait(1);
+        */
+    }
+    
+}
+
+void seperatePanId(void)
+{
+    panId_LSB = (panId & 0x00FF);
+    panId_MSB = ((panId >> 8) & 0x00FF);
+}
 
-        if(pc.readable())
-        { 
-        xbee1.putc(pc.getc());
-        }
+void ReadFile (void) 
+{
+    FILE *set = fopen("/local/config.txt", "r");  // Open "setup.txt" on the local file system for read
+    fscanf(set,"%x",&panId); // read offset
+    fscanf(set,"%s %f",URL,&url);   // read gain
+    fclose(set);
+}
 
-    }
-    }
+void sauvegarder(void)
+{
+    xbee_routeur.putc(0x7E); // start delimiter
+    xbee_routeur.putc(0x00); // MSB length
+    xbee_routeur.putc(0x04); // LSB length
+    xbee_routeur.putc(0x09); // AT COMMAND queue
+    xbee_routeur.putc(0x41); // Frame Id
+    xbee_routeur.putc(0x57); // 'W' Two ASCII characters that identify the AT Command (ID)
+    xbee_routeur.putc(0x52); // 'R'
+    
+    uint8_t checksum = (0xFF - ((0x09 + 0x41 + 0x57 + 0x52) & 0xFF));
+    pc.printf("checksum : %x\n\r",checksum);
+    xbee_routeur.putc(checksum); // checksum
+}
 
 
-
-
+void setPanId(void)
+{
+    xbee_routeur.baud(9600); //set baud rate
+    rst1 = 0;   // xbee reset
+    wait_ms(400);
+    rst1 = 1;
+    
+    xbee_routeur.putc(0x2B); // +
+    xbee_routeur.putc(0x2B); // +
+    xbee_routeur.putc(0x2B); // +
+    xbee_routeur.putc(0x7E); // start delimiter
+    xbee_routeur.putc(0x00); // MSB length
+    xbee_routeur.putc(0x06); // LSB length
+    xbee_routeur.putc(0x08); // AT command
+    xbee_routeur.putc(0x01); // Frame Id
+    xbee_routeur.putc(0x49); // 'I' Two ASCII characters that identify the AT Command (ID)
+    xbee_routeur.putc(0x44); // 'D'
+    xbee_routeur.putc(panId_MSB); // MSB du panId
+    xbee_routeur.putc(panId_LSB); // LSB du panId
+    
+    uint8_t checksum = (0xFF - ((0x08 + 0x01 + 0x49 + 0x44 + panId_MSB + panId_LSB) & 0xFF));
+    pc.printf("checksum : %x\n\r",checksum);
+    xbee_routeur.putc(checksum); // checksum
+}
 
 // SPI COMMUNICATION
 /*