Base teacher program for Xbee clicker

Dependencies:   mbed-rtos mbed

Committer:
adhithyan15
Date:
Wed Mar 11 20:24:51 2015 +0000
Revision:
0:3a4635fed714
Base teacher program for Xbee clicker

Who changed what in which revision?

UserRevisionLine numberNew contents of line
adhithyan15 0:3a4635fed714 1 /**
adhithyan15 0:3a4635fed714 2 * XBee Example Test
adhithyan15 0:3a4635fed714 3 * A test application that demonstrates the ability
adhithyan15 0:3a4635fed714 4 * of transmitting serial data via an XBee module with
adhithyan15 0:3a4635fed714 5 * an mbed microprocesor.
adhithyan15 0:3a4635fed714 6 * By: Vlad Cazan
adhithyan15 0:3a4635fed714 7 * Date: Tuesday, September 29th 2009
adhithyan15 0:3a4635fed714 8 */
adhithyan15 0:3a4635fed714 9
adhithyan15 0:3a4635fed714 10 #include "mbed.h"
adhithyan15 0:3a4635fed714 11 #include "rtos.h"
adhithyan15 0:3a4635fed714 12 #include <string>
adhithyan15 0:3a4635fed714 13 #include <vector>
adhithyan15 0:3a4635fed714 14
adhithyan15 0:3a4635fed714 15 Serial xbee1(p9, p10); //Creates a variable for serial comunication through pin 9 and 10
adhithyan15 0:3a4635fed714 16 DigitalOut rst1(p11); //Digital reset for the XBee, 200ns for reset
adhithyan15 0:3a4635fed714 17 DigitalOut myled(LED3);//Create variable for Led 3 on the mbed
adhithyan15 0:3a4635fed714 18 DigitalOut myled2(LED4);//Create variable for Led 4 on the mbed
adhithyan15 0:3a4635fed714 19 vector<string> studentNames;
adhithyan15 0:3a4635fed714 20 vector<string> serialNumbers;
adhithyan15 0:3a4635fed714 21 vector<string> studentIds;
adhithyan15 0:3a4635fed714 22 Serial pc(USBTX, USBRX);//Opens up serial communication through the USB port via the computer
adhithyan15 0:3a4635fed714 23 char c;
adhithyan15 0:3a4635fed714 24 PinName _tx = p9;
adhithyan15 0:3a4635fed714 25 PinName _rx = p10;
adhithyan15 0:3a4635fed714 26
adhithyan15 0:3a4635fed714 27 int ConfigMode()
adhithyan15 0:3a4635fed714 28 {
adhithyan15 0:3a4635fed714 29 int a;
adhithyan15 0:3a4635fed714 30 Serial DATA(_tx,_rx);
adhithyan15 0:3a4635fed714 31 wait(2);
adhithyan15 0:3a4635fed714 32 DATA.printf("+++");
adhithyan15 0:3a4635fed714 33 while (a != 75) {
adhithyan15 0:3a4635fed714 34 if (DATA.readable()) {
adhithyan15 0:3a4635fed714 35 a = DATA.getc();
adhithyan15 0:3a4635fed714 36 }
adhithyan15 0:3a4635fed714 37 }
adhithyan15 0:3a4635fed714 38 wait(1);
adhithyan15 0:3a4635fed714 39 printf("Configuration Complete!\n\n");
adhithyan15 0:3a4635fed714 40 return 1;
adhithyan15 0:3a4635fed714 41 }
adhithyan15 0:3a4635fed714 42
adhithyan15 0:3a4635fed714 43 int GetSerial(int *serial_no)
adhithyan15 0:3a4635fed714 44 {
adhithyan15 0:3a4635fed714 45 //pc.printf("Start - GetSerial");
adhithyan15 0:3a4635fed714 46 int sh1,sh2,sh3,sl1,sl2,sl3,sl4;
adhithyan15 0:3a4635fed714 47 Serial DATA(_tx,_rx);
adhithyan15 0:3a4635fed714 48 wait_ms(50);
adhithyan15 0:3a4635fed714 49 DATA.printf("ATSL \r");
adhithyan15 0:3a4635fed714 50 DATA.scanf ("%2x%2x%2x%2x",&sl1,&sl2,&sl3,&sl4);
adhithyan15 0:3a4635fed714 51 wait_ms(500);
adhithyan15 0:3a4635fed714 52 DATA.printf("ATSH \r");
adhithyan15 0:3a4635fed714 53 DATA.scanf ("%2x%2x%2x",&sh1,&sh2,&sh3);
adhithyan15 0:3a4635fed714 54
adhithyan15 0:3a4635fed714 55 serial_no[0] = sh1;
adhithyan15 0:3a4635fed714 56 serial_no[1] = sh2;
adhithyan15 0:3a4635fed714 57 serial_no[2] = sh3;
adhithyan15 0:3a4635fed714 58 serial_no[3] = sl1;
adhithyan15 0:3a4635fed714 59 serial_no[4] = sl2;
adhithyan15 0:3a4635fed714 60 serial_no[5] = sl3;
adhithyan15 0:3a4635fed714 61 serial_no[6] = sl4;
adhithyan15 0:3a4635fed714 62 //pc.printf("End - GetSerial");
adhithyan15 0:3a4635fed714 63 return 1;
adhithyan15 0:3a4635fed714 64 }
adhithyan15 0:3a4635fed714 65
adhithyan15 0:3a4635fed714 66 int SetKey(char* key)
adhithyan15 0:3a4635fed714 67 {
adhithyan15 0:3a4635fed714 68 //pc.printf("Start - SetKey");
adhithyan15 0:3a4635fed714 69 Serial DATA(_tx,_rx);
adhithyan15 0:3a4635fed714 70 DATA.printf("ATEE 0 \r");
adhithyan15 0:3a4635fed714 71 char sec_key[8] = {'h','e','l','l','o','m','o','b'};
adhithyan15 0:3a4635fed714 72 DATA.scanf ("%*s");
adhithyan15 0:3a4635fed714 73 wait_ms(1);
adhithyan15 0:3a4635fed714 74 DATA.printf("ATKY %s \r",sec_key);
adhithyan15 0:3a4635fed714 75 DATA.scanf ("%*s");
adhithyan15 0:3a4635fed714 76 //pc.printf("End - SetKey");
adhithyan15 0:3a4635fed714 77 return 1;
adhithyan15 0:3a4635fed714 78 }
adhithyan15 0:3a4635fed714 79
adhithyan15 0:3a4635fed714 80 int SetPanId(int pan_id)
adhithyan15 0:3a4635fed714 81 {
adhithyan15 0:3a4635fed714 82 //pc.printf("Start - PanId");
adhithyan15 0:3a4635fed714 83 Serial DATA(_tx,_rx);
adhithyan15 0:3a4635fed714 84 wait_ms(5);
adhithyan15 0:3a4635fed714 85 DATA.printf("ATID %i\r",pan_id);
adhithyan15 0:3a4635fed714 86 DATA.scanf ("%*s");
adhithyan15 0:3a4635fed714 87 //pc.printf("End - PanID");
adhithyan15 0:3a4635fed714 88 return 1;
adhithyan15 0:3a4635fed714 89 }
adhithyan15 0:3a4635fed714 90
adhithyan15 0:3a4635fed714 91 int WriteSettings()
adhithyan15 0:3a4635fed714 92 {
adhithyan15 0:3a4635fed714 93 //pc.printf("Start - WriteSettings");
adhithyan15 0:3a4635fed714 94 Serial DATA(_tx,_rx);
adhithyan15 0:3a4635fed714 95 wait_ms(5);
adhithyan15 0:3a4635fed714 96 DATA.printf("ATWR \r");
adhithyan15 0:3a4635fed714 97 DATA.scanf ("%*s");
adhithyan15 0:3a4635fed714 98 //pc.printf("End - WriteSettings");
adhithyan15 0:3a4635fed714 99 return 1;
adhithyan15 0:3a4635fed714 100 }
adhithyan15 0:3a4635fed714 101
adhithyan15 0:3a4635fed714 102 int ExitConfigMode()
adhithyan15 0:3a4635fed714 103 {
adhithyan15 0:3a4635fed714 104 //pc.printf("Start - ExitConfig");
adhithyan15 0:3a4635fed714 105 Serial DATA(_tx,_rx);
adhithyan15 0:3a4635fed714 106 wait_ms(5);
adhithyan15 0:3a4635fed714 107 DATA.printf("ATCN \r");
adhithyan15 0:3a4635fed714 108 DATA.scanf ("%*s");
adhithyan15 0:3a4635fed714 109 //pc.printf("End - ExitConfig");
adhithyan15 0:3a4635fed714 110 return 1;
adhithyan15 0:3a4635fed714 111 }
adhithyan15 0:3a4635fed714 112
adhithyan15 0:3a4635fed714 113 void Reset()
adhithyan15 0:3a4635fed714 114 {
adhithyan15 0:3a4635fed714 115 //pc.printf("Start - Reset");
adhithyan15 0:3a4635fed714 116 rst1 = 0;
adhithyan15 0:3a4635fed714 117 wait_ms(10);
adhithyan15 0:3a4635fed714 118 rst1 = 1;
adhithyan15 0:3a4635fed714 119 wait_ms(1);
adhithyan15 0:3a4635fed714 120 //pc.printf("End - Reset");
adhithyan15 0:3a4635fed714 121 }
adhithyan15 0:3a4635fed714 122
adhithyan15 0:3a4635fed714 123 int read_sentence(char* input){
adhithyan15 0:3a4635fed714 124
adhithyan15 0:3a4635fed714 125 char tmp;
adhithyan15 0:3a4635fed714 126 while(!pc.readable());
adhithyan15 0:3a4635fed714 127 tmp = pc.getc();
adhithyan15 0:3a4635fed714 128 pc.putc(tmp);
adhithyan15 0:3a4635fed714 129 int counter = 0;
adhithyan15 0:3a4635fed714 130 while(tmp != '\n'){
adhithyan15 0:3a4635fed714 131 input[counter] = tmp;
adhithyan15 0:3a4635fed714 132 if(pc.readable()){
adhithyan15 0:3a4635fed714 133 tmp = pc.getc();
adhithyan15 0:3a4635fed714 134 pc.putc(tmp);
adhithyan15 0:3a4635fed714 135 counter++;
adhithyan15 0:3a4635fed714 136 }
adhithyan15 0:3a4635fed714 137 }
adhithyan15 0:3a4635fed714 138
adhithyan15 0:3a4635fed714 139 pc.printf("\n");
adhithyan15 0:3a4635fed714 140 return counter-1;
adhithyan15 0:3a4635fed714 141 }
adhithyan15 0:3a4635fed714 142
adhithyan15 0:3a4635fed714 143 int read_xbee_sentence(char* input){
adhithyan15 0:3a4635fed714 144 char tmp;
adhithyan15 0:3a4635fed714 145 while(!xbee1.readable());
adhithyan15 0:3a4635fed714 146 tmp = xbee1.getc();
adhithyan15 0:3a4635fed714 147 int counter = 0;
adhithyan15 0:3a4635fed714 148 while(tmp != '\n'){
adhithyan15 0:3a4635fed714 149 input[counter] = tmp;
adhithyan15 0:3a4635fed714 150 if(xbee1.readable()){
adhithyan15 0:3a4635fed714 151 tmp = xbee1.getc();
adhithyan15 0:3a4635fed714 152 counter++;
adhithyan15 0:3a4635fed714 153 }
adhithyan15 0:3a4635fed714 154 }
adhithyan15 0:3a4635fed714 155 return counter;
adhithyan15 0:3a4635fed714 156 }
adhithyan15 0:3a4635fed714 157
adhithyan15 0:3a4635fed714 158 void print_assessment_available(void const *args){
adhithyan15 0:3a4635fed714 159 pc.printf("Broadcasting assessment available message to all the clickers in the room!\n");
adhithyan15 0:3a4635fed714 160 while(1){
adhithyan15 0:3a4635fed714 161 xbee1.printf("Available\n");
adhithyan15 0:3a4635fed714 162 Thread::wait(100);
adhithyan15 0:3a4635fed714 163 }
adhithyan15 0:3a4635fed714 164 }
adhithyan15 0:3a4635fed714 165
adhithyan15 0:3a4635fed714 166 int main() {
adhithyan15 0:3a4635fed714 167 rst1 = 0; //Set reset pin to 0
adhithyan15 0:3a4635fed714 168 myled = 0;//Set LED3 to 0
adhithyan15 0:3a4635fed714 169 myled2= 0;//Set LED4 to 0
adhithyan15 0:3a4635fed714 170 wait_ms(1);//Wait at least one millisecond
adhithyan15 0:3a4635fed714 171 rst1 = 1;//Set reset pin to 1
adhithyan15 0:3a4635fed714 172 wait_ms(1);//Wait another millisecond
adhithyan15 0:3a4635fed714 173 pc.printf("Teacher Machine\n");
adhithyan15 0:3a4635fed714 174 pc.printf("Start a new assessment? (y/n)\n");
adhithyan15 0:3a4635fed714 175 char sentence_store[255];
adhithyan15 0:3a4635fed714 176 char begin_assessment = 'o';
adhithyan15 0:3a4635fed714 177 char temp;
adhithyan15 0:3a4635fed714 178 int char_count;
adhithyan15 0:3a4635fed714 179 while(begin_assessment == 'o'){
adhithyan15 0:3a4635fed714 180 if(pc.readable()){
adhithyan15 0:3a4635fed714 181 temp = pc.getc();
adhithyan15 0:3a4635fed714 182 }
adhithyan15 0:3a4635fed714 183 if(temp == 'y' || temp == 'n' || temp == 'Y' || temp == 'N'){
adhithyan15 0:3a4635fed714 184 begin_assessment = temp;
adhithyan15 0:3a4635fed714 185 }
adhithyan15 0:3a4635fed714 186 }
adhithyan15 0:3a4635fed714 187 pc.putc(begin_assessment);
adhithyan15 0:3a4635fed714 188 pc.printf("\n");
adhithyan15 0:3a4635fed714 189 if(begin_assessment == 'n' || begin_assessment == 'N'){
adhithyan15 0:3a4635fed714 190 pc.printf("Ok. This program will exit momentarily");
adhithyan15 0:3a4635fed714 191 }else{
adhithyan15 0:3a4635fed714 192 pc.printf("Great! The assessment process begins!\n");
adhithyan15 0:3a4635fed714 193 pc.printf("We are configuring a few things. Wait for a few seconds!\n");
adhithyan15 0:3a4635fed714 194 int device_serial[8];
adhithyan15 0:3a4635fed714 195 char sec_key[8] = {'h','e','l','l','o','m','o','b'};
adhithyan15 0:3a4635fed714 196 int pan_id = 5;
adhithyan15 0:3a4635fed714 197 int assessment_id = rand() % 100 + 1;
adhithyan15 0:3a4635fed714 198 ConfigMode();
adhithyan15 0:3a4635fed714 199 GetSerial(device_serial);
adhithyan15 0:3a4635fed714 200 SetKey(sec_key);
adhithyan15 0:3a4635fed714 201 SetPanId(pan_id);
adhithyan15 0:3a4635fed714 202 WriteSettings();
adhithyan15 0:3a4635fed714 203 //ExitConfigMode();
adhithyan15 0:3a4635fed714 204 Reset();
adhithyan15 0:3a4635fed714 205 pc.printf("Your students will need these parameters to configure their clickers!\n");
adhithyan15 0:3a4635fed714 206 pc.printf("Channel Number: %d\n", pan_id);
adhithyan15 0:3a4635fed714 207 pc.printf("How many questions are going to be in the assessment?\n");
adhithyan15 0:3a4635fed714 208 char_count = read_sentence(sentence_store);
adhithyan15 0:3a4635fed714 209 int number_of_questions;
adhithyan15 0:3a4635fed714 210 number_of_questions = atoi (sentence_store);
adhithyan15 0:3a4635fed714 211 char answer_store[number_of_questions];
adhithyan15 0:3a4635fed714 212 pc.printf("Please enter the correct answer for each of the questions\n");
adhithyan15 0:3a4635fed714 213 for(int j = 0;j < number_of_questions; j++){
adhithyan15 0:3a4635fed714 214 pc.printf("Question %d - Correct Answer: ",j+1);
adhithyan15 0:3a4635fed714 215 read_sentence(sentence_store);
adhithyan15 0:3a4635fed714 216 answer_store[j] = sentence_store[0];
adhithyan15 0:3a4635fed714 217 pc.printf("\n");
adhithyan15 0:3a4635fed714 218 }
adhithyan15 0:3a4635fed714 219 pc.printf("Great! The answer key is now set!\n");
adhithyan15 0:3a4635fed714 220 pc.printf("The system will start accepting connections\n");
adhithyan15 0:3a4635fed714 221 pc.printf("Waiting for student clickers to connect!\n");
adhithyan15 0:3a4635fed714 222 //Thread t(&print_assessment_available);
adhithyan15 0:3a4635fed714 223 int attack_detector = 0;
adhithyan15 0:3a4635fed714 224 while (1) {//Neverending Loop
adhithyan15 0:3a4635fed714 225 // if(pc.readable()){
adhithyan15 0:3a4635fed714 226 if (xbee1.readable()){
adhithyan15 0:3a4635fed714 227 string input;
adhithyan15 0:3a4635fed714 228 char poll_id[255];
adhithyan15 0:3a4635fed714 229 char_count = read_xbee_sentence(poll_id);
adhithyan15 0:3a4635fed714 230 char set_poll_id[char_count];
adhithyan15 0:3a4635fed714 231 for(int i = 0;i < char_count; i++){
adhithyan15 0:3a4635fed714 232 set_poll_id[i] = poll_id[i];
adhithyan15 0:3a4635fed714 233 }
adhithyan15 0:3a4635fed714 234 input = string(set_poll_id);
adhithyan15 0:3a4635fed714 235 pc.printf(input.c_str());
adhithyan15 0:3a4635fed714 236 pc.printf("\n");
adhithyan15 0:3a4635fed714 237 if(input.find("C-") != -1 && attack_detector == 0){
adhithyan15 0:3a4635fed714 238 int cdash = input.find_last_of("-");
adhithyan15 0:3a4635fed714 239 pc.printf("A clicker with #%s is trying to connect\n",input.substr(cdash+1,input.length()-1).c_str());
adhithyan15 0:3a4635fed714 240 pc.printf("NReq-%s\n",input.substr(cdash+1,input.length()-1).c_str());
adhithyan15 0:3a4635fed714 241 xbee1.printf("NReq-%s\n",input.substr(cdash+1,input.length()-1).c_str());
adhithyan15 0:3a4635fed714 242 attack_detector++;
adhithyan15 0:3a4635fed714 243 pc.printf("%d",attack_detector);
adhithyan15 0:3a4635fed714 244 }else if(input.find("NRes-") != -1 && attack_detector == 1){
adhithyan15 0:3a4635fed714 245 pc.printf("The student %s has begun his/her assessment\n",input.substr(15,input.length()-1).c_str());
adhithyan15 0:3a4635fed714 246 pc.printf("Requesting his/her ID number\n");
adhithyan15 0:3a4635fed714 247 int length = input.length();
adhithyan15 0:3a4635fed714 248 int ndash = input.find_last_of("-");
adhithyan15 0:3a4635fed714 249 pc.printf("IDReq-%s\n",input.substr(15,length-1).c_str());
adhithyan15 0:3a4635fed714 250 xbee1.printf("IDReq-%s\n",input.substr(15,length-1).c_str());
adhithyan15 0:3a4635fed714 251 attack_detector++;
adhithyan15 0:3a4635fed714 252 }else if(input.find("IDRes-") != -1 && attack_detector == 2){
adhithyan15 0:3a4635fed714 253 pc.printf(input.c_str());
adhithyan15 0:3a4635fed714 254 pc.printf("The student's ID number is %s\n",input.substr(6,input.length()-1).c_str());
adhithyan15 0:3a4635fed714 255 pc.printf("Sending the clicker the question number count\n");
adhithyan15 0:3a4635fed714 256 pc.printf("QC-%d-%d\n",assessment_id,number_of_questions);
adhithyan15 0:3a4635fed714 257 xbee1.printf("QC-%d-%d\n",assessment_id,number_of_questions);
adhithyan15 0:3a4635fed714 258 attack_detector++;
adhithyan15 0:3a4635fed714 259 }else if(input.find("QA-") != -1 && attack_detector == 3){
adhithyan15 0:3a4635fed714 260 pc.printf("Question count has been acknowledged!\n");
adhithyan15 0:3a4635fed714 261 pc.printf("Now waiting for answers to be sent!\n");
adhithyan15 0:3a4635fed714 262 attack_detector++;
adhithyan15 0:3a4635fed714 263 } else if(input.find("ANS-") != -1 && attack_detector == 4){
adhithyan15 0:3a4635fed714 264 pc.printf("Answers from the clicker has been received!\n");
adhithyan15 0:3a4635fed714 265 int dash = input.find_last_of("-");
adhithyan15 0:3a4635fed714 266 string k = input.substr(dash+1,input.length()-1);
adhithyan15 0:3a4635fed714 267 string answer_key;
adhithyan15 0:3a4635fed714 268 answer_key = string(answer_store);
adhithyan15 0:3a4635fed714 269 int correct = 0;
adhithyan15 0:3a4635fed714 270 for(int i = 0; i < answer_key.length(); i++){
adhithyan15 0:3a4635fed714 271 if(answer_key[i] == k[i]){
adhithyan15 0:3a4635fed714 272 correct++;
adhithyan15 0:3a4635fed714 273 }
adhithyan15 0:3a4635fed714 274 }
adhithyan15 0:3a4635fed714 275 float grade = (float) (correct/number_of_questions)*100.0;
adhithyan15 0:3a4635fed714 276 pc.printf("Output grade - %f\n", grade);
adhithyan15 0:3a4635fed714 277 xbee1.printf("GRADE-%f",grade);
adhithyan15 0:3a4635fed714 278 attack_detector++;
adhithyan15 0:3a4635fed714 279 } else if(input.find("AA") != -1){
adhithyan15 0:3a4635fed714 280 xbee1.printf("Available\n");
adhithyan15 0:3a4635fed714 281 }
adhithyan15 0:3a4635fed714 282 }
adhithyan15 0:3a4635fed714 283 }
adhithyan15 0:3a4635fed714 284 }
adhithyan15 0:3a4635fed714 285 }