Charles Dejardin
/
xbeerx
Wireless data sending
Fork of xberx by
main.cpp@1:27646760b7ac, 2014-06-03 (annotated)
- Committer:
- CharlesDej
- Date:
- Tue Jun 03 08:59:19 2014 +0000
- Revision:
- 1:27646760b7ac
- Parent:
- 0:3d9570678c6d
Wireless data receiving
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
CharlesDej | 1:27646760b7ac | 1 | /* LIBRARIES */ |
CharlesDej | 1:27646760b7ac | 2 | #include "mbed.h" |
CharlesDej | 1:27646760b7ac | 3 | #include "C12832.h" |
CharlesDej | 1:27646760b7ac | 4 | #include "string.h" |
CharlesDej | 1:27646760b7ac | 5 | /*--------------------*/ |
CharlesDej | 1:27646760b7ac | 6 | |
CharlesDej | 1:27646760b7ac | 7 | /*OUTPUT AND INPUT CONFIGURATION*/ |
CharlesDej | 1:27646760b7ac | 8 | Serial xbee1(p9, p10); //pin declaration for the xbee module on the board |
CharlesDej | 1:27646760b7ac | 9 | Serial cinterion(p13,p14); //pin declaration for the cinterion module on the board |
CharlesDej | 1:27646760b7ac | 10 | DigitalOut myled(LED1); //pin declaration for the blinking LED on the board |
CharlesDej | 1:27646760b7ac | 11 | DigitalOut myled2 (LED2); |
CharlesDej | 1:27646760b7ac | 12 | DigitalOut myled3 (LED3); |
CharlesDej | 1:27646760b7ac | 13 | DigitalOut myled4 (LED4); |
CharlesDej | 1:27646760b7ac | 14 | C12832 lcd(p5, p7, p6, p8, p11); //pin declaration for the LCD screen on the board |
CharlesDej | 1:27646760b7ac | 15 | /*--------------------------*/ |
CharlesDej | 1:27646760b7ac | 16 | |
CharlesDej | 1:27646760b7ac | 17 | /*FUNCTION PROTOTYPE*/ |
CharlesDej | 1:27646760b7ac | 18 | float decryption (float sens2, char sens); |
CharlesDej | 1:27646760b7ac | 19 | /*----------------------*/ |
CharlesDej | 1:27646760b7ac | 20 | |
CharlesDej | 1:27646760b7ac | 21 | /*VARIABLES DEFINITION*/ |
CharlesDej | 1:27646760b7ac | 22 | char X; //definition of a character |
CharlesDej | 1:27646760b7ac | 23 | char temp,volt,current,lumi; //definition of four variables matching the 4 differents type of data that the board receive |
CharlesDej | 1:27646760b7ac | 24 | float temp2, current2,volt2, lumi2; // which are temperature, voltage, current, and luminosity |
CharlesDej | 1:27646760b7ac | 25 | char NumTel[] = "07404054906"; //Mobile number to which the values will be sent |
CharlesDej | 1:27646760b7ac | 26 | char inchar,inchar1,inchar2,inchar3,inchar4,inchar5; // variables used for the SMS |
CharlesDej | 1:27646760b7ac | 27 | /*-----------------------------------------*/ |
CharlesDej | 1:27646760b7ac | 28 | |
CharlesDej | 1:27646760b7ac | 29 | |
CharlesDej | 1:27646760b7ac | 30 | /*------------*/ |
CharlesDej | 1:27646760b7ac | 31 | /*MAIN PROGRAM*/ |
CharlesDej | 1:27646760b7ac | 32 | /*------------*/ |
CharlesDej | 1:27646760b7ac | 33 | |
CharlesDej | 1:27646760b7ac | 34 | |
CharlesDej | 1:27646760b7ac | 35 | void FlushGSM(void) { char char1 = 0; while (cinterion.readable()) { char1 = cinterion.getc(); } return; } //function that clean out the reception buffer of the SIM card |
CharlesDej | 1:27646760b7ac | 36 | |
anoop1728 | 0:3d9570678c6d | 37 | |
CharlesDej | 1:27646760b7ac | 38 | //----CALLBACK FUNCTIONS----// |
CharlesDej | 1:27646760b7ac | 39 | //This one will interrupt the program when the cinterion will be receiving a SMS// |
CharlesDej | 1:27646760b7ac | 40 | void callback2() |
CharlesDej | 1:27646760b7ac | 41 | { |
CharlesDej | 1:27646760b7ac | 42 | inchar = cinterion.getc(); // I am copying several value to be sure that this is the right command |
CharlesDej | 1:27646760b7ac | 43 | inchar1 = cinterion.getc(); |
CharlesDej | 1:27646760b7ac | 44 | inchar2 = cinterion.getc(); |
CharlesDej | 1:27646760b7ac | 45 | inchar3 = cinterion.getc(); |
CharlesDej | 1:27646760b7ac | 46 | inchar4 = cinterion.getc(); |
CharlesDej | 1:27646760b7ac | 47 | inchar5 = cinterion.getc(); |
CharlesDej | 1:27646760b7ac | 48 | if ((inchar == 0x0D)&&(inchar1 == 0x0A)&&(inchar2 == '+')&&(inchar3 == 'C')&&(inchar4 == 'M')&& (inchar5 == 'T')) // if the incoming command is "+CMTI", a new SMS has been received |
CharlesDej | 1:27646760b7ac | 49 | { |
CharlesDej | 1:27646760b7ac | 50 | myled4=1; |
CharlesDej | 1:27646760b7ac | 51 | cinterion.printf("AT+CMGD=1"); //AT command to delete the last SMS, to never be out of space |
CharlesDej | 1:27646760b7ac | 52 | cinterion.putc(0x0D); //"enter" |
CharlesDej | 1:27646760b7ac | 53 | wait(1); |
CharlesDej | 1:27646760b7ac | 54 | FlushGSM(); //clear the reception buffer |
CharlesDej | 1:27646760b7ac | 55 | wait(0.5); |
CharlesDej | 1:27646760b7ac | 56 | cinterion.printf("AT+CMGF=1"); //Text mode activated for the SMS |
CharlesDej | 1:27646760b7ac | 57 | cinterion.putc(0x0D); //"enter" |
CharlesDej | 1:27646760b7ac | 58 | wait(1); |
CharlesDej | 1:27646760b7ac | 59 | myled2=1; |
CharlesDej | 1:27646760b7ac | 60 | cinterion.printf("AT+CMGS=%s", NumTel); //AT command to send a SMS to the mobile number set |
CharlesDej | 1:27646760b7ac | 61 | cinterion.putc(0x0D); //"enter" |
CharlesDej | 1:27646760b7ac | 62 | wait(1) ; |
CharlesDej | 1:27646760b7ac | 63 | cinterion.printf("Temperature: %.2f, Current: %.2f,Voltage: %.2f, Luminosity: %.2f" , temp2, current2, volt2, lumi2); //the results are send by SMS |
CharlesDej | 1:27646760b7ac | 64 | cinterion.putc(0x1A); //ASCII code for Ctrl+Z |
CharlesDej | 1:27646760b7ac | 65 | wait(5); //there are a lot of delays, to be sure that the command are sent |
CharlesDej | 1:27646760b7ac | 66 | myled2=0; |
CharlesDej | 1:27646760b7ac | 67 | FlushGSM(); //clear the reception buffer |
CharlesDej | 1:27646760b7ac | 68 | myled4=0; |
CharlesDej | 1:27646760b7ac | 69 | |
CharlesDej | 1:27646760b7ac | 70 | } |
CharlesDej | 1:27646760b7ac | 71 | } |
CharlesDej | 1:27646760b7ac | 72 | |
CharlesDej | 1:27646760b7ac | 73 | |
anoop1728 | 0:3d9570678c6d | 74 | |
CharlesDej | 1:27646760b7ac | 75 | //This one will interrupt the program when the xbee will be receiving data// |
CharlesDej | 1:27646760b7ac | 76 | void callback() |
CharlesDej | 1:27646760b7ac | 77 | { |
CharlesDej | 1:27646760b7ac | 78 | X = xbee1.getc(); //the values received are put into the X variable |
CharlesDej | 1:27646760b7ac | 79 | if (X == 0xEE) //if X equals the start bit |
CharlesDej | 1:27646760b7ac | 80 | { myled = 1; |
CharlesDej | 1:27646760b7ac | 81 | temp = xbee1.getc(); //the next value that the xbee receive is the temperature, then the current, the voltage and finally the luminosity |
CharlesDej | 1:27646760b7ac | 82 | current = xbee1.getc(); |
CharlesDej | 1:27646760b7ac | 83 | volt = xbee1.getc(); |
CharlesDej | 1:27646760b7ac | 84 | lumi = xbee1.getc(); |
CharlesDej | 1:27646760b7ac | 85 | |
CharlesDej | 1:27646760b7ac | 86 | temp2 = decryption(temp2, temp); //decryption of the values received |
CharlesDej | 1:27646760b7ac | 87 | current2 = decryption(current2, current); |
CharlesDej | 1:27646760b7ac | 88 | volt2 = decryption (volt2, volt) ; |
CharlesDej | 1:27646760b7ac | 89 | lumi2 = decryption(lumi2, lumi); |
CharlesDej | 1:27646760b7ac | 90 | } |
CharlesDej | 1:27646760b7ac | 91 | |
CharlesDej | 1:27646760b7ac | 92 | if (X == 0xFF) //if X equals the final bit |
CharlesDej | 1:27646760b7ac | 93 | { |
CharlesDej | 1:27646760b7ac | 94 | myled = 0; //LED is off when the string stops |
CharlesDej | 1:27646760b7ac | 95 | lcd.cls(); |
CharlesDej | 1:27646760b7ac | 96 | lcd.locate(0,0); |
CharlesDej | 1:27646760b7ac | 97 | lcd.printf("Temperature: %.2f, Current: %.2f,Voltage: %.2f, Luminosity: %.2f" , temp2, current2, volt2, lumi2); //the board monitor the results on the LCD Screen |
CharlesDej | 1:27646760b7ac | 98 | xbee1.printf("Temperature : %f , Current : %f , Voltage : %f , Luminosity : %f " , temp2, current2, volt2, lumi2); //the board sends the results to the PC thanks to the xbee |
CharlesDej | 1:27646760b7ac | 99 | } |
CharlesDej | 1:27646760b7ac | 100 | } |
CharlesDej | 1:27646760b7ac | 101 | |
CharlesDej | 1:27646760b7ac | 102 | |
CharlesDej | 1:27646760b7ac | 103 | |
CharlesDej | 1:27646760b7ac | 104 | float decryption(float sens2, char sens) // decryption function |
CharlesDej | 1:27646760b7ac | 105 | { |
CharlesDej | 1:27646760b7ac | 106 | sens >>=2; //shift bit to the right |
CharlesDej | 1:27646760b7ac | 107 | sens2 = (float)sens/50 ; //division |
CharlesDej | 1:27646760b7ac | 108 | return sens2; |
CharlesDej | 1:27646760b7ac | 109 | } |
CharlesDej | 1:27646760b7ac | 110 | |
CharlesDej | 1:27646760b7ac | 111 | |
CharlesDej | 1:27646760b7ac | 112 | |
CharlesDej | 1:27646760b7ac | 113 | //----MAIN FUNCTION----// |
CharlesDej | 1:27646760b7ac | 114 | int main() |
CharlesDej | 1:27646760b7ac | 115 | { |
CharlesDej | 1:27646760b7ac | 116 | cinterion.printf("AT+CNMI=1,1,0,0,1"); //configure the cinterion board to be warned when a new SMS is received |
CharlesDej | 1:27646760b7ac | 117 | cinterion.putc(0x0D); // ASCII code for the "enter" key |
CharlesDej | 1:27646760b7ac | 118 | FlushGSM(); // clear the reception buffer |
CharlesDej | 1:27646760b7ac | 119 | |
CharlesDej | 1:27646760b7ac | 120 | while (1) //Neverending loop |
CharlesDej | 1:27646760b7ac | 121 | { |
CharlesDej | 1:27646760b7ac | 122 | xbee1.attach(&callback); //call of the xbee callback function |
CharlesDej | 1:27646760b7ac | 123 | cinterion.attach(&callback2); //call of the cinterion callback function |
CharlesDej | 1:27646760b7ac | 124 | wait(1); |
CharlesDej | 1:27646760b7ac | 125 | } |
anoop1728 | 0:3d9570678c6d | 126 | } |
anoop1728 | 0:3d9570678c6d | 127 | |
anoop1728 | 0:3d9570678c6d | 128 | |
CharlesDej | 1:27646760b7ac | 129 | |
CharlesDej | 1:27646760b7ac | 130 |