Advertisement
Guest User

Untitled

a guest
Nov 18th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.49 KB | None | 0 0
  1. #include "433/RCSwitch.h"
  2. #include "Controller.h"
  3. #include <mysql/mysql.h>
  4. #include <unistd.h>
  5. #include <stdlib.h>
  6. #include <stdio.h>
  7. #include <iostream>
  8. #include <chrono>
  9. #include <ctime>
  10. #include <vector>
  11. #include <string>
  12. //Milight
  13. #include "Milight/PL1167_nRF24.h"
  14. #include "Milight/MiLightRadio.h"
  15. #include <RF24/RF24.h>
  16.  
  17. using namespace std;
  18.  
  19. //433mhz
  20. RCSwitch mySwitch;
  21.  
  22. int receivingMsg = 0;
  23. int sendingList[2];
  24. bool sendingChecker = false;
  25. vector<string> queryList;
  26. vector<string> queryListMilight;
  27. auto t1 = chrono::high_resolution_clock::now();
  28.  
  29. //Milight
  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. int dupesPrinted = 0;
  36. bool escaped = false;
  37. uint8_t outgoingPacket[7];
  38. uint8_t outgoingPacketPos = 0;
  39.  
  40. uint8_t nibble;
  41.  
  42. char *hexCode = new char;
  43. string milightHex = "";
  44. string milightHexChecker = "";
  45. bool firstRun = true;
  46.  
  47. enum {
  48. IDLE,
  49. HAVE_NIBBLE,
  50. COMPLETE,
  51. } state;
  52.  
  53. MYSQL* mysql1;
  54. MYSQL_ROW row;
  55. //#################################### 433 mhz code ####################################
  56. void Receiver433::station433()
  57. {
  58. receivingMsg = reading433();
  59.  
  60. if (receivingMsg > 1000000 && receivingMsg < 9999999){
  61. printf("Received message: %i, %i\n", sendingList[0], sendingList[1]);
  62. convertingReceivingMSG(to_string(receivingMsg));
  63. }
  64. else if(receivingMsg != 0){
  65. printf("Received but ingored: %i\n", receivingMsg);
  66. }
  67. receivingMsg = 0;
  68. if(sendingList[1] > 0){
  69. fill_n(sendingList, 2, 0);
  70. }
  71. }
  72.  
  73. int Receiver433::reading433()
  74. {
  75. int value = 0;
  76. if (mySwitch.available()) {
  77. value = mySwitch.getReceivedValue();
  78. mySwitch.resetAvailable();
  79. int type = (value / 1) % 10;
  80. if(sendingList[0] == 0 && type != 1)
  81. sendingList[0] = value;
  82. else if(sendingList[0] != 0 && sendingList[1] == 0 && sendingList[0] != value)
  83. sendingList[1] = value;
  84. if(sendingList[0] != 0 && sendingChecker == false){
  85. sendingChecker = true;
  86. return sendingList[0];
  87. }
  88. else if(sendingList[1] != 0 && sendingChecker == true){
  89. sendingChecker = false;
  90. value = 0;
  91. return sendingList[1];
  92. }else return 0;
  93. } else return 0;
  94. }
  95.  
  96. void Receiver433::convertingReceivingMSG(string msg)
  97. {
  98. int device = atoi(msg.substr(0,2).c_str());
  99. char msgType = msg.back();
  100.  
  101. int msg1 = 0;
  102. int msg2 = 0;
  103.  
  104. string info="";
  105. string query ="";
  106.  
  107. switch(device){
  108. case 10:
  109. info="Observer";
  110. msg1 = (atoi(msg.substr(2,2).c_str())) -20;
  111. msg2 = atoi(msg.substr(4,2).c_str());
  112. break;
  113. case 11:
  114. info = "Hydro";
  115. break;
  116. default :
  117. info="Default";
  118. break;
  119.  
  120. }
  121.  
  122. t1 = chrono::high_resolution_clock::now();
  123.  
  124. //Huminity and Temp
  125. if(msgType == '0' && msg1 != 0 && msg2 != 0){
  126. //Inject Time
  127. query ="INSERT INTO TimeTrigger SET DeviceCode ='" +to_string(device) + "', Info = '" + info + "'";
  128. queryList.push_back(query);
  129.  
  130. //Inject temp inside
  131. query = "INSERT INTO TempInside SET CurrentTemp ='"+to_string(msg1)+"', DeviceCode = '" +to_string(device)+"'";
  132. queryList.push_back(query);
  133.  
  134. //Inject Humi inside
  135. query = "INSERT INTO HumidityInside SET CurrentHumidity ='" +to_string(msg2) + "', DeviceCode = '"+to_string(device)+"'";
  136. queryList.push_back(query);
  137.  
  138. //Inject 433diy
  139. query ="INSERT INTO 433diy SET Code ='" + to_string(device) + "', IDHumidity =(SELECT IDHumidity FROM HumidityInside WHERE DeviceCode = '"+to_string(device)+"' ORDER BY IDHumidity DESC LIMIT 1), IDTempIn =(SELECT IDTempIn FROM TempInside WHERE DeviceCode = '"+to_string(device)+"' ORDER BY IDTempIn DESC LIMIT 1), IDTime = (SELECT IDTime FROM TimeTrigger WHERE DeviceCode = '"+to_string(device)+"' ORDER BY IDTime DESC LIMIT 1) ";
  140. queryList.push_back(query);
  141. }
  142. else if (msgType == '1' && msg1 != 0 && msg2 != 0){
  143. printf("Sending msg to GB, Light & Voltage...\n");
  144.  
  145. //Inject Time
  146. query ="INSERT INTO TimeTrigger SET DeviceCode ='" +to_string(device) + "', Info = '" + info + "'";
  147. queryList.push_back(query);
  148.  
  149. //Inject Light inside
  150. query = "INSERT INTO LightIntensityInside SET CurrentLightIntensityIn ='"+to_string(msg1)+"', DeviceCode = '" +to_string(device)+"'";
  151. queryList.push_back(query);
  152.  
  153. //Inject Voltage inside
  154. query = "INSERT INTO Voltage SET CurrentVoltage ='" +to_string(msg2) + "', DeviceCode = '"+to_string(device)+"'";
  155. queryList.push_back(query);
  156.  
  157. //Inject 433diy
  158. query ="INSERT INTO 433diy SET Code ='" + to_string(device) + "', IDLightIntensityIn =(SELECT IDLightIntensityIn FROM LightIntensityInside WHERE DeviceCode = '"+to_string(device)+"' ORDER BY IDLightIntensityIn DESC LIMIT 1), IDVoltage =(SELECT IDVoltage FROM Voltage WHERE DeviceCode = '"+to_string(device)+"' ORDER BY IDVoltage DESC LIMIT 1), IDTime = (SELECT IDTime FROM TimeTrigger WHERE DeviceCode = '"+to_string(device)+"' ORDER BY IDTime DESC LIMIT 1) ";
  159. queryList.push_back(query);
  160. }else printf("Something went terribly wrong!\n");
  161. }
  162.  
  163. //#################################### Milight code ####################################
  164. void Milight::sentMilightMsg(){
  165. const char* packetBytes = hexCode;
  166. memset(outgoingPacket, 0, 7);
  167.  
  168. // convert input into hex
  169. int index = 0;
  170. for (int counter = 0; *packetBytes; ++packetBytes) {
  171. int n = 0;
  172. if (*packetBytes >= 'a' && *packetBytes <= 'f') {
  173. n = *packetBytes - 'a' + 10;
  174. }
  175. else if (*packetBytes >= 'A' && *packetBytes <= 'F') {
  176. n = *packetBytes - 'A' + 10;
  177. }
  178. else if (*packetBytes >= '0' && *packetBytes <= '9') {
  179. n = *packetBytes - '0';
  180. }
  181. else if (*packetBytes == ' ') {
  182. index++;
  183. }
  184. else {
  185. cout << "cannot decode" << endl;
  186. }
  187. outgoingPacket[index] = outgoingPacket[index] * 16 + (unsigned long)n;
  188. }
  189. mlr.write(outgoingPacket, sizeof(outgoingPacket));
  190. delay(10);
  191. for (int index = 0; index < 10; index++) {
  192. mlr.resend();
  193. delay(10);
  194. }
  195. outgoingPacket[6] += 8;
  196. }
  197.  
  198. void Milight::recievingMilightMsg()
  199. {
  200. usleep(10000);
  201. MYSQL_RES* result = mysql_store_result(mysql1);
  202. if (result != NULL)
  203. {
  204. //Get the number of columns
  205. int num_rows = mysql_num_rows(result);
  206. int num_fields = mysql_num_fields(result);
  207.  
  208. MYSQL_ROW row = mysql_fetch_row(result);
  209. if (num_fields >= 2){
  210. hexCode = row[1];
  211. milightHexChecker = string(row[0]);
  212. //printf("Debugnote: new field => 2\nDebugnote: milight code: ");
  213. //printf(hexCode);
  214. if(milightHexChecker != milightHex)
  215. {
  216. milightHex = milightHexChecker;
  217. string temp = row[1];
  218. if(temp.size() > 1)
  219. {
  220. sentMilightMsg();
  221. printf("#########################\n");
  222. printf("#Sending milight msg....#\n");
  223. printf("#########################\n");
  224. printf("Sending to DB: %s\n",temp.c_str());
  225. printf("###############\n");
  226. printf("#Listening....#\n");
  227. printf("###############\n");
  228. }
  229. }
  230. }
  231. }else printf("Debugnote: No result milights\n");
  232. }
  233.  
  234. //#################################### Database code ####################################
  235. void Database::mysql_connect()
  236. {
  237. //initialize MYSQL object for connections
  238. mysql1 = mysql_init(NULL);
  239.  
  240. if (mysql1 == NULL)
  241. {
  242. fprintf(stderr, "ABB : %s\n", mysql_error(mysql1));
  243. return;
  244. }
  245.  
  246. if (mysql_real_connect(mysql1, DATABASE_IP, DATABASE_USERNAME, DATABASE_PASSWORD, DATABASE_NAME, 0, NULL, 0) == NULL)
  247. //Connect to the database
  248. fprintf(stderr, "%s\n", mysql_error(mysql1));
  249. else {
  250. printf("\n#################################\n");
  251. printf("#Database connection successful.#\n");
  252. }
  253. }
  254.  
  255. void Database::mysql_disconnect()
  256. {
  257. mysql_close(mysql1);
  258. printf("Disconnected from database.\r\n");
  259. }
  260.  
  261. void Database::sending_sql_msg()
  262. {
  263. if (mysql1 != NULL)
  264. {
  265. if(queryList.size() > 1)
  266. {
  267. printf("##########################\n");
  268. printf("#Sending 433mhz msg's....#\n");
  269. printf("##########################\n");
  270. }
  271. for(int i = 0; i < queryList.size(); i++){
  272. mysql_query(mysql1, queryList.at(i).c_str());
  273. printf("Sending to DB: %s\n", queryList.at(i).c_str());
  274. }
  275.  
  276. //for(int i = 0; i < queryListMilight.size(); i++){
  277. // mysql_query(mysql1, queryListMilight.at(i).c_str());
  278. //}
  279.  
  280. if(queryList.size() > 1)
  281. {
  282. auto t2 = chrono::high_resolution_clock::now();
  283. auto int_ms = chrono::duration_cast<chrono::milliseconds>(t2 - t1);
  284. chrono::duration<double, milli> fp_ms = t2 - t1;
  285. cout << "Sending took " << fp_ms.count() << " ms, " << "or " << int_ms.count() << " whole milliseconds\n";
  286.  
  287. printf("###############\n");
  288. printf("#Listening....#\n");
  289. printf("###############\n");
  290. }
  291. queryList.clear();
  292. queryListMilight.clear();
  293. }else printf("No DB connection!\n");
  294. }
  295.  
  296. int main(int argc, char *argv[])
  297. {
  298. Receiver433 recr433;
  299. Milight mili;
  300. Database cntr;
  301.  
  302. if(wiringPiSetup() == -1) {
  303. printf("wiringPiSetup failed, exiting...");
  304. return 0;
  305. }
  306.  
  307. mlr.begin();
  308.  
  309. mySwitch = RCSwitch();
  310. mySwitch.setPulseLength(pulseLength);
  311. mySwitch.enableReceive(PIN); // Receiver on interrupt 0 => that is pin #2
  312.  
  313. //making DB connection.
  314. if(mysql1 == NULL){
  315. cntr.mysql_connect();
  316. }
  317. printf("#Listening Modes.... #\n");
  318. printf("#################################\n");
  319. while(true){
  320. usleep(10000);
  321. queryListMilight.push_back("SELECT IDMilight, Hex FROM Milight ORDER BY IDMilight DESC LIMIT 1");
  322. recr433.station433();
  323. cntr.sending_sql_msg();
  324. //mili.recievingMilightMsg();
  325. usleep(10000);
  326. }
  327.  
  328. printf("#############\n");
  329. printf("#Exiting....#\n");
  330. printf("#############\n");
  331. cntr.mysql_disconnect();
  332. exit(0);
  333. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement