Advertisement
Guest User

Untitled

a guest
Jan 16th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.89 KB | None | 0 0
  1. /**
  2. * On a Raspberry Pi 2 compile with:
  3. *
  4. * g++ -Ofast -mfpu=vfp -mfloat-abi=hard -march=armv7-a -mtune=arm1176jzf-s -I/usr/local/include -L/usr/local/lib -lrf24-bcm PL1167_nRF24.cpp MiLightRadio.cpp openmili$
  5. *
  6. * for receiver mode run with:
  7. * sudo ./openmilight
  8. *
  9. * for sender mode run with:
  10. * sudo ./openmilight "B0 F2 EA 6D B0 02 f0"
  11. */
  12. #define DATABASE_IP "127.0.0.1"
  13. #define DATABASE_NAME "domotica"
  14. #define DATABASE_USERNAME "root"
  15. #define DATABASE_PASSWORD "aweds123"
  16.  
  17. #include <stdio.h>
  18. #include <cstdlib>
  19. #include <iostream>
  20. #include <string.h>
  21.  
  22. using namespace std;
  23.  
  24. #include <RF24/RF24.h>
  25. #include <mysql/mysql.h>
  26.  
  27. #include "PL1167_nRF24.h"
  28. #include "MiLightRadio.h"
  29.  
  30. RF24 radio(RPI_V2_GPIO_P1_22, RPI_V2_GPIO_P1_24, BCM2835_SPI_SPEED_1MHZ);
  31.  
  32. PL1167_nRF24 prf(radio);
  33. MiLightRadio mlr(prf);
  34.  
  35. void setup()
  36. {
  37. mlr.begin();
  38. }
  39.  
  40. static int dupesPrinted = 0;
  41. static bool receiving = false;
  42. static bool escaped = false;
  43. static uint8_t outgoingPacket[7];
  44. static uint8_t outgoingPacketPos = 0;
  45.  
  46. static uint8_t nibble;
  47.  
  48. static char *value_string;
  49.  
  50. MYSQL *mysql1;
  51.  
  52. static enum {
  53. IDLE,
  54. HAVE_NIBBLE,
  55. COMPLETE,
  56. } state;
  57.  
  58. void loop(const char* newPacketBytes)
  59. {
  60. const char* packetBytes = value_string;
  61.  
  62. memset(outgoingPacket, 0, 7);
  63.  
  64. // convert input into hex
  65. int index = 0;
  66. for (int counter = 0; *packetBytes; ++packetBytes) {
  67. int n = 0;
  68. if (*packetBytes >= 'a' && *packetBytes <= 'f') {
  69. n = *packetBytes - 'a' + 10;
  70. }
  71. else if (*packetBytes >= 'A' && *packetBytes <= 'F') {
  72. n = *packetBytes - 'A' + 10;
  73. }
  74. else if (*packetBytes >= '0' && *packetBytes <= '9') {
  75. n = *packetBytes - '0';
  76. }
  77. else if (*packetBytes == ' ') {
  78. index++;
  79. }
  80. else {
  81. cout << "cannot decode" << endl;
  82. exit(1);
  83. }
  84. outgoingPacket[index] = outgoingPacket[index] * 16 + (unsigned long)n;
  85. }
  86.  
  87. printf("sending packet\n");
  88. for (int i = 0; i < 7; i++) {
  89. printf("%02X ", outgoingPacket[i]);
  90. }
  91. printf("\n");
  92.  
  93. for (int index = 0; index < 1; index++) {
  94. }
  95. mlr.write(outgoingPacket, sizeof(outgoingPacket));
  96. delay(10);
  97. mlr.resend();
  98. delay(10);
  99. outgoingPacket[6] += 8;
  100. string line2;
  101. getline(cin, line2);
  102. }
  103.  
  104. void mysql_write_something(void)
  105. {
  106. if (mysql1 != NULL)
  107. {
  108. if (!mysql_query(mysql1, "SELECT id, light FROM test2 WHERE id = 1"))
  109. {
  110. MYSQL_RES *result = mysql_store_result(mysql1);
  111. if (result != NULL)
  112. {
  113. //Get the number of columns
  114. int num_rows = mysql_num_rows(result);
  115. int num_fields = mysql_num_fields(result);
  116.  
  117. MYSQL_ROW row; //An array of strings
  118. while ((row = mysql_fetch_row(result)))
  119. {
  120. if (num_fields >= 2)
  121. {
  122. char *value_int = row[0];
  123. value_string = row[1];
  124.  
  125. printf("Got value %s\n", value_string);
  126. }
  127. }
  128. mysql_free_result(result);
  129. }
  130. else
  131. {
  132. printf("Cannot get data\r\n");
  133. }
  134. }
  135. else
  136. {
  137. printf("Cannot SELECT\r\n");
  138. }
  139. }
  140. }
  141.  
  142. void mysql_connect(void)
  143. {
  144. //initialize MYSQL object for connections
  145. mysql1 = mysql_init(NULL);
  146.  
  147. if (mysql1 == NULL)
  148. {
  149. printf("mysql1 is null");
  150. fprintf(stderr, "ABB : %s\n", mysql_error(mysql1));
  151. return;
  152. }
  153.  
  154. if (mysql_real_connect(mysql1, DATABASE_IP, DATABASE_USERNAME, DATABASE_PASSWORD, DATABASE_NAME, 0, NULL, 0) == NULL)
  155. //Connect to the database
  156. fprintf(stderr, "%s\n", mysql_error(mysql1));
  157. else {
  158. printf("Database connection successful.\r\n");
  159. mysql_write_something();
  160. }
  161. }
  162.  
  163. void mysql_disconnect(void)
  164. {
  165. mysql_close(mysql1);
  166. printf("Disconnected from database.\r\n");
  167. }
  168.  
  169. int
  170. main(int argc, char** arguments)
  171. {
  172. setup();
  173.  
  174. char* packetBytes = 0;
  175. printf("ik doe dingen");
  176. mysql_connect();
  177. mysql_disconnect();
  178. /*if (argc < 2) {
  179. receiving = true;
  180. printf("in listening mode\n");
  181.  
  182. } else {*/
  183. receiving = false;
  184. packetBytes = arguments[1];
  185.  
  186. while (true) {
  187. loop(packetBytes);
  188. }
  189.  
  190. return 0;
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement