Student program for Xbee Clicker System

Dependencies:   MODSERIAL mbed

Committer:
adhithyan15
Date:
Wed Mar 11 20:20:42 2015 +0000
Revision:
0:7b5c57924414
Base Student Program Commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
adhithyan15 0:7b5c57924414 1 /**
adhithyan15 0:7b5c57924414 2 * XBee Example Test
adhithyan15 0:7b5c57924414 3 * A test application that demonstrates the ability
adhithyan15 0:7b5c57924414 4 * of transmitting serial data via an XBee module with
adhithyan15 0:7b5c57924414 5 * an mbed microprocesor.
adhithyan15 0:7b5c57924414 6 * By: Vlad Cazan
adhithyan15 0:7b5c57924414 7 * Date: Tuesday, September 29th 2009
adhithyan15 0:7b5c57924414 8 */
adhithyan15 0:7b5c57924414 9
adhithyan15 0:7b5c57924414 10 #include "mbed.h"
adhithyan15 0:7b5c57924414 11 #include <string>
adhithyan15 0:7b5c57924414 12 #include "MODSERIAL.h"
adhithyan15 0:7b5c57924414 13
adhithyan15 0:7b5c57924414 14 Serial xbee1(p9, p10); //Creates a variable for serial comunication through pin 9 and 10
adhithyan15 0:7b5c57924414 15 MODSERIAL xbee(p9,p10);
adhithyan15 0:7b5c57924414 16 DigitalOut rst1(p11); //Digital reset for the XBee, 200ns for reset
adhithyan15 0:7b5c57924414 17 DigitalOut myled(LED3);//Create variable for Led 3 on the mbed
adhithyan15 0:7b5c57924414 18 DigitalOut myled2(LED4);//Create variable for Led 4 on the mbed
adhithyan15 0:7b5c57924414 19 int char_count;
adhithyan15 0:7b5c57924414 20 Serial pc(USBTX, USBRX);//Opens up serial communication through the USB port via the computer
adhithyan15 0:7b5c57924414 21 char c;
adhithyan15 0:7b5c57924414 22 PinName _tx = p9;
adhithyan15 0:7b5c57924414 23 PinName _rx = p10;
adhithyan15 0:7b5c57924414 24
adhithyan15 0:7b5c57924414 25 int ConfigMode()
adhithyan15 0:7b5c57924414 26 {
adhithyan15 0:7b5c57924414 27 int a;
adhithyan15 0:7b5c57924414 28 Serial DATA(_tx,_rx);
adhithyan15 0:7b5c57924414 29 wait(2);
adhithyan15 0:7b5c57924414 30 DATA.printf("+++");
adhithyan15 0:7b5c57924414 31 while (a != 75) {
adhithyan15 0:7b5c57924414 32 if (DATA.readable()) {
adhithyan15 0:7b5c57924414 33 a = DATA.getc();
adhithyan15 0:7b5c57924414 34 }
adhithyan15 0:7b5c57924414 35 }
adhithyan15 0:7b5c57924414 36 wait(1);
adhithyan15 0:7b5c57924414 37 printf("Configuration Complete!\n");
adhithyan15 0:7b5c57924414 38 return 1;
adhithyan15 0:7b5c57924414 39 }
adhithyan15 0:7b5c57924414 40
adhithyan15 0:7b5c57924414 41 int GetSerial(int *serial_no)
adhithyan15 0:7b5c57924414 42 {
adhithyan15 0:7b5c57924414 43 int sh1,sh2,sh3,sl1,sl2,sl3,sl4;
adhithyan15 0:7b5c57924414 44 Serial DATA(_tx,_rx);
adhithyan15 0:7b5c57924414 45 wait_ms(50);
adhithyan15 0:7b5c57924414 46 DATA.printf("ATSL \r");
adhithyan15 0:7b5c57924414 47 DATA.scanf ("%2x%2x%2x%2x",&sl1,&sl2,&sl3,&sl4);
adhithyan15 0:7b5c57924414 48 wait_ms(500);
adhithyan15 0:7b5c57924414 49 DATA.printf("ATSH \r");
adhithyan15 0:7b5c57924414 50 DATA.scanf ("%2x%2x%2x",&sh1,&sh2,&sh3);
adhithyan15 0:7b5c57924414 51
adhithyan15 0:7b5c57924414 52 serial_no[0] = sh1;
adhithyan15 0:7b5c57924414 53 serial_no[1] = sh2;
adhithyan15 0:7b5c57924414 54 serial_no[2] = sh3;
adhithyan15 0:7b5c57924414 55 serial_no[3] = sl1;
adhithyan15 0:7b5c57924414 56 serial_no[4] = sl2;
adhithyan15 0:7b5c57924414 57 serial_no[5] = sl3;
adhithyan15 0:7b5c57924414 58 serial_no[6] = sl4;
adhithyan15 0:7b5c57924414 59
adhithyan15 0:7b5c57924414 60 return 1;
adhithyan15 0:7b5c57924414 61 }
adhithyan15 0:7b5c57924414 62
adhithyan15 0:7b5c57924414 63 int SetKey(char* key)
adhithyan15 0:7b5c57924414 64 {
adhithyan15 0:7b5c57924414 65 Serial DATA(_tx,_rx);
adhithyan15 0:7b5c57924414 66 DATA.printf("ATEE 0 \r");
adhithyan15 0:7b5c57924414 67
adhithyan15 0:7b5c57924414 68 DATA.scanf ("%*s");
adhithyan15 0:7b5c57924414 69 wait_ms(1);
adhithyan15 0:7b5c57924414 70 DATA.printf("ATKY %s \r",key);
adhithyan15 0:7b5c57924414 71 DATA.scanf ("%*s");
adhithyan15 0:7b5c57924414 72 return 1;
adhithyan15 0:7b5c57924414 73 }
adhithyan15 0:7b5c57924414 74
adhithyan15 0:7b5c57924414 75 int SetPanId(int pan_id)
adhithyan15 0:7b5c57924414 76 {
adhithyan15 0:7b5c57924414 77 Serial DATA(_tx,_rx);
adhithyan15 0:7b5c57924414 78 wait_ms(5);
adhithyan15 0:7b5c57924414 79 DATA.printf("ATID %i\r",pan_id);
adhithyan15 0:7b5c57924414 80 DATA.scanf ("%*s");
adhithyan15 0:7b5c57924414 81 return 1;
adhithyan15 0:7b5c57924414 82 }
adhithyan15 0:7b5c57924414 83
adhithyan15 0:7b5c57924414 84 int WriteSettings()
adhithyan15 0:7b5c57924414 85 {
adhithyan15 0:7b5c57924414 86 Serial DATA(_tx,_rx);
adhithyan15 0:7b5c57924414 87 wait_ms(5);
adhithyan15 0:7b5c57924414 88 DATA.printf("ATWR \r");
adhithyan15 0:7b5c57924414 89 DATA.scanf ("%*s");
adhithyan15 0:7b5c57924414 90 return 1;
adhithyan15 0:7b5c57924414 91 }
adhithyan15 0:7b5c57924414 92
adhithyan15 0:7b5c57924414 93 int ExitConfigMode()
adhithyan15 0:7b5c57924414 94 {
adhithyan15 0:7b5c57924414 95 Serial DATA(_tx,_rx);
adhithyan15 0:7b5c57924414 96 wait_ms(5);
adhithyan15 0:7b5c57924414 97 DATA.printf("ATCN \r");
adhithyan15 0:7b5c57924414 98 DATA.scanf ("%*s");
adhithyan15 0:7b5c57924414 99 return 1;
adhithyan15 0:7b5c57924414 100 }
adhithyan15 0:7b5c57924414 101
adhithyan15 0:7b5c57924414 102 void Reset()
adhithyan15 0:7b5c57924414 103 {
adhithyan15 0:7b5c57924414 104 rst1 = 0;
adhithyan15 0:7b5c57924414 105 wait_ms(10);
adhithyan15 0:7b5c57924414 106 rst1 = 1;
adhithyan15 0:7b5c57924414 107 wait_ms(1);
adhithyan15 0:7b5c57924414 108 }
adhithyan15 0:7b5c57924414 109
adhithyan15 0:7b5c57924414 110 int read_sentence(char* input){
adhithyan15 0:7b5c57924414 111
adhithyan15 0:7b5c57924414 112 char tmp;
adhithyan15 0:7b5c57924414 113 while(!pc.readable());
adhithyan15 0:7b5c57924414 114 tmp = pc.getc();
adhithyan15 0:7b5c57924414 115 pc.putc(tmp);
adhithyan15 0:7b5c57924414 116 int counter = 0;
adhithyan15 0:7b5c57924414 117 while(tmp != '\n'){
adhithyan15 0:7b5c57924414 118 input[counter] = tmp;
adhithyan15 0:7b5c57924414 119 if(pc.readable()){
adhithyan15 0:7b5c57924414 120 tmp = pc.getc();
adhithyan15 0:7b5c57924414 121 pc.putc(tmp);
adhithyan15 0:7b5c57924414 122 counter++;
adhithyan15 0:7b5c57924414 123 }
adhithyan15 0:7b5c57924414 124 }
adhithyan15 0:7b5c57924414 125
adhithyan15 0:7b5c57924414 126 pc.printf("\n");
adhithyan15 0:7b5c57924414 127 return counter-1;
adhithyan15 0:7b5c57924414 128 }
adhithyan15 0:7b5c57924414 129
adhithyan15 0:7b5c57924414 130 int read_xbee_sentence(char* input){
adhithyan15 0:7b5c57924414 131 char tmp;
adhithyan15 0:7b5c57924414 132 while(!xbee1.readable());
adhithyan15 0:7b5c57924414 133 tmp = xbee1.getc();
adhithyan15 0:7b5c57924414 134 int counter = 0;
adhithyan15 0:7b5c57924414 135 while(tmp != '\n'){
adhithyan15 0:7b5c57924414 136 input[counter] = tmp;
adhithyan15 0:7b5c57924414 137 if(xbee1.readable()){
adhithyan15 0:7b5c57924414 138 tmp = xbee1.getc();
adhithyan15 0:7b5c57924414 139 counter++;
adhithyan15 0:7b5c57924414 140 }
adhithyan15 0:7b5c57924414 141 }
adhithyan15 0:7b5c57924414 142 return counter;
adhithyan15 0:7b5c57924414 143 }
adhithyan15 0:7b5c57924414 144
adhithyan15 0:7b5c57924414 145 int main() {
adhithyan15 0:7b5c57924414 146 rst1 = 0; //Set reset pin to 0
adhithyan15 0:7b5c57924414 147 myled = 0;//Set LED3 to 0
adhithyan15 0:7b5c57924414 148 myled2= 0;//Set LED4 to 0
adhithyan15 0:7b5c57924414 149 wait_ms(1);//Wait at least one millisecond
adhithyan15 0:7b5c57924414 150 rst1 = 1;//Set reset pin to 1
adhithyan15 0:7b5c57924414 151 wait_ms(1);//Wait another millisecond
adhithyan15 0:7b5c57924414 152 pc.printf("Student Machine\n");
adhithyan15 0:7b5c57924414 153 int device_serial[8];
adhithyan15 0:7b5c57924414 154 char security_key[] = {'h','e','l','l','o','m','o','b'};
adhithyan15 0:7b5c57924414 155 int pan_id = 5;
adhithyan15 0:7b5c57924414 156 ConfigMode();
adhithyan15 0:7b5c57924414 157 GetSerial(device_serial);
adhithyan15 0:7b5c57924414 158 SetKey(security_key);
adhithyan15 0:7b5c57924414 159 SetPanId(pan_id);
adhithyan15 0:7b5c57924414 160 WriteSettings();
adhithyan15 0:7b5c57924414 161 ExitConfigMode();
adhithyan15 0:7b5c57924414 162 Reset();
adhithyan15 0:7b5c57924414 163 char name[255];
adhithyan15 0:7b5c57924414 164 char id[255];
adhithyan15 0:7b5c57924414 165 pc.printf("Your Serial No: %d\n",device_serial);
adhithyan15 0:7b5c57924414 166 pc.printf("Please enter your name: \n");
adhithyan15 0:7b5c57924414 167 char_count = read_sentence(name);
adhithyan15 0:7b5c57924414 168 char set_name[char_count];
adhithyan15 0:7b5c57924414 169 for(int i = 0;i < char_count; i++){
adhithyan15 0:7b5c57924414 170 set_name[i] = name[i];
adhithyan15 0:7b5c57924414 171 }
adhithyan15 0:7b5c57924414 172 pc.printf("Please enter your ID: \n");
adhithyan15 0:7b5c57924414 173 char_count = read_sentence(id);
adhithyan15 0:7b5c57924414 174 char set_id[char_count];
adhithyan15 0:7b5c57924414 175 for(int i = 0;i < char_count; i++){
adhithyan15 0:7b5c57924414 176 set_id[i] = id[i];
adhithyan15 0:7b5c57924414 177 }
adhithyan15 0:7b5c57924414 178 pc.printf("Polling for available assessments!\n");
adhithyan15 0:7b5c57924414 179 string t;
adhithyan15 0:7b5c57924414 180 bool assessment_available = false;
adhithyan15 0:7b5c57924414 181 pc.printf("AA-\n");
adhithyan15 0:7b5c57924414 182 xbee1.printf("AA-\n");
adhithyan15 0:7b5c57924414 183 while(!assessment_available){
adhithyan15 0:7b5c57924414 184 wait(3);
adhithyan15 0:7b5c57924414 185 if(xbee1.readable()){
adhithyan15 0:7b5c57924414 186 char poll[30];
adhithyan15 0:7b5c57924414 187 read_xbee_sentence(poll);
adhithyan15 0:7b5c57924414 188 t = string(poll);
adhithyan15 0:7b5c57924414 189 }
adhithyan15 0:7b5c57924414 190 if(t.find("Available") != -1){
adhithyan15 0:7b5c57924414 191 assessment_available = true;
adhithyan15 0:7b5c57924414 192 }else{
adhithyan15 0:7b5c57924414 193 xbee1.printf("AA-\n");
adhithyan15 0:7b5c57924414 194 }
adhithyan15 0:7b5c57924414 195 }
adhithyan15 0:7b5c57924414 196 pc.printf("A new assessment is available\n");
adhithyan15 0:7b5c57924414 197 pc.printf("Connecting to the Teacher Machine\n");
adhithyan15 0:7b5c57924414 198 pc.printf("%d\n",device_serial);
adhithyan15 0:7b5c57924414 199 pc.printf("C-%d\n",device_serial);
adhithyan15 0:7b5c57924414 200 xbee1.printf("C-%d\n",device_serial);
adhithyan15 0:7b5c57924414 201 wait(2);
adhithyan15 0:7b5c57924414 202 string serialKey = "";
adhithyan15 0:7b5c57924414 203 for(int j = 0; j < 8; j++){
adhithyan15 0:7b5c57924414 204 serialKey += device_serial[j];
adhithyan15 0:7b5c57924414 205 }
adhithyan15 0:7b5c57924414 206 while(true){
adhithyan15 0:7b5c57924414 207 if(xbee1.readable()){
adhithyan15 0:7b5c57924414 208 string input;
adhithyan15 0:7b5c57924414 209 char sentence_store[255];
adhithyan15 0:7b5c57924414 210 char_count = read_xbee_sentence(sentence_store);
adhithyan15 0:7b5c57924414 211 char set_sentence_store[char_count];
adhithyan15 0:7b5c57924414 212 for(int i = 0; i < char_count; i++){
adhithyan15 0:7b5c57924414 213 set_sentence_store[i] = sentence_store[i];
adhithyan15 0:7b5c57924414 214 }
adhithyan15 0:7b5c57924414 215 input = string(set_sentence_store);
adhithyan15 0:7b5c57924414 216 pc.printf("\n");
adhithyan15 0:7b5c57924414 217 if(input.find("NReq-") != -1){
adhithyan15 0:7b5c57924414 218 pc.printf("Name requested by the teacher machine. Sending your name in response\n");
adhithyan15 0:7b5c57924414 219 pc.printf("NRes-%d-%s\n",device_serial,set_name);
adhithyan15 0:7b5c57924414 220 xbee1.printf("NRes-%d-%s\n",device_serial,set_name);
adhithyan15 0:7b5c57924414 221 }else if(input.find("IDReq-" + string(set_name)) != -1){
adhithyan15 0:7b5c57924414 222 pc.printf("ID number has been requested by the teacher machine. Sending your ID number in response\n");
adhithyan15 0:7b5c57924414 223 pc.printf("IDRes-%d-%s",device_serial,set_id);
adhithyan15 0:7b5c57924414 224 xbee1.printf("IDRes-%s\n",set_id);
adhithyan15 0:7b5c57924414 225 }else if(input.find("QC-") != -1){
adhithyan15 0:7b5c57924414 226 int second_dash = input.find_last_of("-");
adhithyan15 0:7b5c57924414 227 string question_count = input.substr(second_dash+1,input.length()-1);
adhithyan15 0:7b5c57924414 228 int q_count = atoi (question_count.c_str());
adhithyan15 0:7b5c57924414 229 pc.printf("The # of questions in this assessment is %d\n",q_count);
adhithyan15 0:7b5c57924414 230 xbee1.printf("QA-\n");
adhithyan15 0:7b5c57924414 231 char answer_store[q_count];
adhithyan15 0:7b5c57924414 232 char c[10];
adhithyan15 0:7b5c57924414 233 pc.printf("Please enter your answers below\n\n");
adhithyan15 0:7b5c57924414 234 for(int i = 0; i < q_count; i++){
adhithyan15 0:7b5c57924414 235 pc.printf("Question %d - ",i+1);
adhithyan15 0:7b5c57924414 236 read_sentence(c);
adhithyan15 0:7b5c57924414 237 answer_store[i] = c[0];
adhithyan15 0:7b5c57924414 238 }
adhithyan15 0:7b5c57924414 239 xbee1.printf("ANS-%s\n",answer_store);
adhithyan15 0:7b5c57924414 240 }
adhithyan15 0:7b5c57924414 241
adhithyan15 0:7b5c57924414 242 }
adhithyan15 0:7b5c57924414 243 }
adhithyan15 0:7b5c57924414 244 }