Project.ino 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. // standard library
  2. #include <stdio.h>
  3. // Driverlib includes
  4. #include <inc/hw_types.h>
  5. #include <driverlib/interrupt.h>
  6. #include <inc/hw_ints.h>
  7. #include <inc/hw_apps_rcm.h>
  8. #include <inc/hw_common_reg.h>
  9. #include <driverlib/prcm.h>
  10. #include <driverlib/rom.h>
  11. #include <driverlib/rom_map.h>
  12. #include <inc/hw_memmap.h>
  13. #include <driverlib/timer.h>
  14. #include <driverlib/utils.h>
  15. // energia library
  16. #include <SLFS.h>
  17. #include <WiFi.h>
  18. #include <WiFiClient.h>
  19. #include <Wire.h>
  20. #include <BMA222.h>
  21. // Common interface includes
  22. #include "timer_if.h"
  23. #include "gpio_if.h"
  24. #include "List.h"
  25. #include "Networks.h"
  26. #include "crypto.h"
  27. #include "rng.h"
  28. #if defined(ccs)
  29. extern void (* const g_pfnVectors[])(void);
  30. #endif
  31. #if defined(ewarm)
  32. extern uVectorEntry __vector_table;
  33. #endif
  34. #define MLEN 32
  35. using namespace std;
  36. char choice = 0; // use choice
  37. char sent = 0;
  38. List * known_networks;
  39. char total_networks = 0;
  40. char send_data = 0;
  41. BMA222 my_sensor;
  42. uint16_t ID = 1050; // Device ID
  43. uint16_t port = 9000;
  44. IPAddress server(82,36,88,190);
  45. WiFiClient wclient;
  46. uint8_t aes_key[16] = {
  47. 0x03, 0x02, 0x01, 0x00,
  48. 0x07, 0x06, 0x05, 0x04,
  49. 0x0b, 0x0a, 0x09, 0x08,
  50. 0x0f, 0x0e, 0x0d, 0x0c
  51. };
  52. typedef struct sMessage {
  53. uint32_t acc;
  54. uint8_t light;
  55. uint8_t empty[12];
  56. }Message;
  57. typedef struct {
  58. uint16_t ID;
  59. uint16_t LEN;
  60. } header;
  61. uint8_t AESData[MLEN], *AESResult, AESResult2[MLEN];
  62. uint32_t AESDataLength=MLEN;
  63. uint8_t buffer[32] = {0};
  64. uint16_t bufferPnt = 0;
  65. static volatile unsigned long g_ulSysTickValue;
  66. static volatile unsigned long g_ulBase;
  67. static volatile unsigned long g_ulRefBase;
  68. static volatile unsigned long g_ulRefTimerInts = 0;
  69. static volatile unsigned long g_ulIntClearVector;
  70. unsigned long g_ulTimerInts;
  71. static void BoardInit(void)
  72. {
  73. /* In case of TI-RTOS vector table is initialize by OS itself */
  74. #ifndef USE_TIRTOS
  75. //
  76. // Set vector table base
  77. //
  78. #if defined(ccs)
  79. MAP_IntVTableBaseSet((unsigned long)&g_pfnVectors[0]);
  80. #endif
  81. #if defined(ewarm)
  82. MAP_IntVTableBaseSet((unsigned long)&__vector_table);
  83. #endif
  84. #endif
  85. //
  86. // Enable Processor
  87. //
  88. MAP_IntMasterEnable();
  89. MAP_IntEnable(FAULT_SYSTICK);
  90. PRCMCC3200MCUInit();
  91. }
  92. void TimerBaseIntHandler(void)
  93. {
  94. //
  95. // Clear the timer interrupt.
  96. //
  97. Timer_IF_InterruptClear(g_ulBase);
  98. send_data = 1;
  99. }
  100. void setup() {
  101. //Initialize serial and wait for port to open:
  102. Serial.begin(115200);
  103. SerFlash.begin();
  104. my_sensor.begin();
  105. g_ulBase = TIMERA0_BASE;
  106. Timer_IF_Init(PRCM_TIMERA0, g_ulBase, TIMER_CFG_PERIODIC, TIMER_A, 0);
  107. Timer_IF_IntSetup(g_ulBase, TIMER_A, TimerBaseIntHandler);
  108. WiFi.init();
  109. known_networks = new List();
  110. read_from_file();
  111. Serial.print("Setup RNG ..");
  112. RNGSetup();
  113. Serial.println("OK");
  114. Serial.print("Setup AES ..");
  115. AESSetup(AES_KEY_128BIT, aes_key);
  116. Serial.println("OK");
  117. Serial.print("Setup WiFi ..");
  118. auto_connect_networks();
  119. Serial.println("OK");
  120. Serial.print("Setup DHCP ..");
  121. while (WiFi.localIP() == INADDR_NONE) {delay(200);}
  122. Serial.print("OK ");
  123. Serial.println(WiFi.localIP());
  124. Serial.print("Setup Socket ..");
  125. while(wclient.connect(server, port) == false) {delay(200);}
  126. Serial.println("OK");
  127. pinMode(PIN_04, INPUT);
  128. Timer_IF_Start(g_ulBase, TIMER_A, 1000);
  129. }
  130. void loop() {
  131. // menu
  132. if(sent == 0)
  133. {
  134. Serial.println("****Menu****");
  135. Serial.println("1) Auto connect to Network");
  136. Serial.println("2) Send Data");
  137. Serial.println("3) Add Network");
  138. Serial.println("4) Print Network");
  139. sent = 1;
  140. }
  141. if(Serial.available() > 0)
  142. {
  143. choice = Serial.read();
  144. sent = 0;
  145. switch(choice)
  146. {
  147. case '1':
  148. // scan for existing networks:
  149. Serial.println("Scanning available networks...");
  150. auto_connect_networks();
  151. break;
  152. case '2':
  153. for(uint8_t i = 65; i <= 90; i++)
  154. {
  155. buffer[bufferPnt] = i;
  156. bufferPnt++;
  157. }
  158. // send();
  159. break;
  160. case '3':
  161. add_network();
  162. break;
  163. case '4':
  164. print_networks();
  165. break;
  166. default:
  167. Serial.println("Value has to be between 1 and 3");
  168. break;
  169. }
  170. }
  171. if(send_data == 1)
  172. {
  173. calc_acc();
  174. }
  175. }
  176. void print_networks()
  177. {
  178. ListNode * current = known_networks->get_first();
  179. Networks * current_network;
  180. while(current != nullptr)
  181. {
  182. current_network = (Networks *)current->get_data();
  183. Serial.println(current_network->get_ssid());
  184. Serial.println(current_network->get_passwd());
  185. current = current->get_next();
  186. }
  187. }
  188. void auto_connect_networks()
  189. {
  190. int32_t rssi;
  191. ListNode * current; // pointer used to traverse the list
  192. Networks * current_network = nullptr; // used to see the data
  193. Networks * strongest_network = nullptr; // used to see the data
  194. bool connection = false;
  195. int num_ssid = 0;
  196. while(connection == false)
  197. {
  198. num_ssid = WiFi.scanNetworks();
  199. if (num_ssid == -1)
  200. {
  201. Serial.println("Couldn't get a wifi connection");
  202. }
  203. Serial.print("number of available networks:");
  204. Serial.println(num_ssid);
  205. rssi = -100; // set the defualt rssi to -100
  206. for (int i = 0; i < num_ssid; i++)
  207. {
  208. current = known_networks->get_first();
  209. while(current != nullptr)
  210. {
  211. current_network = (Networks *)current->get_data(); // type cast the current data to valid type
  212. if(strcmp(current_network->get_ssid(), WiFi.SSID(i)) == 0) // compare current ssid in list with ssid scanned
  213. {
  214. if(WiFi.RSSI(i) > rssi) // check if this network strength is greater than current strongest network
  215. {
  216. strongest_network = current_network; // set the id equal to the current number i
  217. }
  218. break;
  219. }
  220. else
  221. {
  222. strongest_network = nullptr;
  223. }
  224. current = current->get_next();
  225. }
  226. }
  227. if(strongest_network != nullptr)
  228. {
  229. WiFi.begin(strongest_network->get_ssid(), strongest_network->get_passwd());
  230. delay(1000); // wait 1 second to connect
  231. if( WiFi.status() == WL_CONNECTED) // if connected exit
  232. {
  233. Serial.println("Connected");
  234. connection = true;
  235. }
  236. }
  237. else
  238. {
  239. Serial.println("Known WiFi not available");
  240. }
  241. }
  242. }
  243. void add_network()
  244. {
  245. char c_ssid[30] = "WannaSeeMyGates";
  246. char c_passwd[30] = "password";
  247. Networks * new_network = new Networks(c_ssid, c_passwd);
  248. known_networks->insert_at_front((void *) new_network);
  249. write_to_file();
  250. }
  251. void read_from_file(void)
  252. {
  253. // open in read mode
  254. int32_t retval = SerFlash.open("/binfile/networks.bin", FS_MODE_OPEN_READ);
  255. // file does not exsit
  256. if (retval != SL_FS_OK)
  257. {
  258. Serial.print("Error reading file /binfile/networks.bin, error code: ");
  259. Serial.println(SerFlash.lastErrorString());
  260. Serial.flush(); // flush pending serial output before entering suspend()
  261. }
  262. else
  263. {
  264. char ssid[30];
  265. char passwd[30];
  266. Networks * new_network;
  267. size_t read_len = SerFlash.size();
  268. Serial.print("File size: ");
  269. Serial.println(read_len);
  270. for(int i = 0; i < (read_len/60); i++)
  271. {
  272. retval = SerFlash.readBytes(ssid, sizeof(ssid));
  273. if(retval < 0) // if less than 0 break
  274. {
  275. break;
  276. }
  277. retval = SerFlash.readBytes(passwd, sizeof(passwd));
  278. if(retval < 0) // if less than 0 break
  279. {
  280. break;
  281. }
  282. Serial.println(ssid);
  283. Serial.println(passwd);
  284. new_network = new Networks(ssid, passwd);
  285. known_networks->insert_at_front((void *) new_network);
  286. total_networks++;
  287. }
  288. }
  289. SerFlash.close();
  290. }
  291. void write_to_file(void)
  292. {
  293. uint32_t retval;
  294. retval = SerFlash.open("/binfile/networks.bin", FS_MODE_OPEN_CREATE((total_networks * 60), _FS_FILE_OPEN_FLAG_COMMIT));
  295. if (retval != SL_FS_OK)
  296. {
  297. Serial.print("Error creating file /binfile/networks.bin, error code: ");
  298. Serial.println(SerFlash.lastErrorString());
  299. Serial.flush();
  300. }
  301. ListNode * current = known_networks->get_first();
  302. Networks * current_network;
  303. while(current != nullptr)
  304. {
  305. current_network = (Networks *)current->get_data();
  306. Serial.println(current_network->get_ssid());
  307. Serial.println(current_network->get_passwd());
  308. SerFlash.write(current_network, sizeof(Networks));
  309. current = current->get_next();
  310. }
  311. SerFlash.close();
  312. }
  313. void send(Message * m)
  314. {
  315. uint32_t resLen = 0;
  316. Serial.print("encrypting..");
  317. uint8_t *res = AESEncrypt((uint8_t*)m, 16, &resLen);
  318. Serial.println(resLen);
  319. header h = {ID, resLen};
  320. wclient.write((uint8_t *) &h, sizeof(h));
  321. wclient.write(res, (uint16_t) resLen);
  322. free(res);
  323. memset(buffer, 0x00, 32);
  324. bufferPnt = 0;
  325. }
  326. void calc_acc(void)
  327. {
  328. Message * m = (Message *)malloc(sizeof(Message));
  329. int minVal = 0;
  330. int maxVal = 65;
  331. double x;
  332. double y;
  333. double z;
  334. int32_t idatax = my_sensor.readXData();
  335. int ixAng = map(idatax, minVal, maxVal, -90, 90);
  336. int32_t idatay = my_sensor.readYData();
  337. int iyAng = map(idatay, minVal, maxVal, -90, 90);
  338. int32_t idataz = my_sensor.readZData();
  339. int izAng = map(idataz, minVal, maxVal, -90, 90);
  340. delay(100);
  341. int32_t fdatay = my_sensor.readYData();
  342. int fyAng = map(fdatay, minVal, maxVal, -90, 90);
  343. int32_t fdataz = my_sensor.readZData();
  344. int fzAng = map(fdataz, minVal, maxVal, -90, 90);
  345. int32_t fdatax = my_sensor.readXData();
  346. int fxAng = map(fdatax, minVal, maxVal, -90, 90);
  347. int xAng = fxAng - ixAng;
  348. int yAng = fyAng - iyAng;
  349. int zAng = fzAng - izAng;
  350. int32_t deltax = (fdatax - idatax)/0.1;
  351. int32_t deltay = (fdatay - idatay)/0.1;
  352. int32_t deltaz = (fdataz - idataz)/0.1;
  353. deltax = deltax * deltax;
  354. deltay = deltay * deltay;
  355. deltaz = deltaz * deltaz;
  356. m->acc = (uint32_t)sqrt(deltax + deltay + deltaz);
  357. x = RAD_TO_DEG*(atan2(-yAng, -zAng) + PI);
  358. y = RAD_TO_DEG*(atan2(-xAng, -zAng) + PI);
  359. z = RAD_TO_DEG*(atan2(-yAng, -xAng) + PI);
  360. m->light = digitalRead(PIN_04);
  361. while(wclient.connect(server, port) == true)
  362. {
  363. Serial.println("Reconnecting");
  364. delay(200);
  365. }
  366. send(m);
  367. free(m);
  368. send_data = 0;
  369. }