Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "433/RCSwitch.h"
- #include "Controller.h"
- #include <mysql/mysql.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <iostream>
- #include <chrono>
- #include <ctime>
- #include <vector>
- #include <string>
- //Milight
- #include "Milight/PL1167_nRF24.h"
- #include "Milight/MiLightRadio.h"
- #include <RF24/RF24.h>
- using namespace std;
- //433mhz
- RCSwitch mySwitch;
- int receivingMsg = 0;
- int sendingList[2];
- bool sendingChecker = false;
- vector<string> queryList;
- vector<string> queryListMilight;
- auto t1 = chrono::high_resolution_clock::now();
- //Milight
- RF24 radio(RPI_V2_GPIO_P1_22, RPI_V2_GPIO_P1_24, BCM2835_SPI_SPEED_1MHZ);
- PL1167_nRF24 prf(radio);
- MiLightRadio mlr(prf);
- int dupesPrinted = 0;
- bool escaped = false;
- uint8_t outgoingPacket[7];
- uint8_t outgoingPacketPos = 0;
- uint8_t nibble;
- char *hexCode = new char;
- string milightHex = "";
- string milightHexChecker = "";
- bool firstRun = true;
- enum {
- IDLE,
- HAVE_NIBBLE,
- COMPLETE,
- } state;
- MYSQL* mysql1;
- MYSQL_ROW row;
- //#################################### 433 mhz code ####################################
- void Receiver433::station433()
- {
- receivingMsg = reading433();
- if (receivingMsg > 1000000 && receivingMsg < 9999999){
- printf("Received message: %i, %i\n", sendingList[0], sendingList[1]);
- convertingReceivingMSG(to_string(receivingMsg));
- }
- else if(receivingMsg != 0){
- printf("Received but ingored: %i\n", receivingMsg);
- }
- receivingMsg = 0;
- if(sendingList[1] > 0){
- fill_n(sendingList, 2, 0);
- }
- }
- int Receiver433::reading433()
- {
- int value = 0;
- if (mySwitch.available()) {
- value = mySwitch.getReceivedValue();
- mySwitch.resetAvailable();
- int type = (value / 1) % 10;
- if(sendingList[0] == 0 && type != 1)
- sendingList[0] = value;
- else if(sendingList[0] != 0 && sendingList[1] == 0 && sendingList[0] != value)
- sendingList[1] = value;
- if(sendingList[0] != 0 && sendingChecker == false){
- sendingChecker = true;
- return sendingList[0];
- }
- else if(sendingList[1] != 0 && sendingChecker == true){
- sendingChecker = false;
- value = 0;
- return sendingList[1];
- }else return 0;
- } else return 0;
- }
- void Receiver433::convertingReceivingMSG(string msg)
- {
- int device = atoi(msg.substr(0,2).c_str());
- char msgType = msg.back();
- int msg1 = 0;
- int msg2 = 0;
- string info="";
- string query ="";
- switch(device){
- case 10:
- info="Observer";
- msg1 = (atoi(msg.substr(2,2).c_str())) -20;
- msg2 = atoi(msg.substr(4,2).c_str());
- break;
- case 11:
- info = "Hydro";
- break;
- default :
- info="Default";
- break;
- }
- t1 = chrono::high_resolution_clock::now();
- //Huminity and Temp
- if(msgType == '0' && msg1 != 0 && msg2 != 0){
- //Inject Time
- query ="INSERT INTO TimeTrigger SET DeviceCode ='" +to_string(device) + "', Info = '" + info + "'";
- queryList.push_back(query);
- //Inject temp inside
- query = "INSERT INTO TempInside SET CurrentTemp ='"+to_string(msg1)+"', DeviceCode = '" +to_string(device)+"'";
- queryList.push_back(query);
- //Inject Humi inside
- query = "INSERT INTO HumidityInside SET CurrentHumidity ='" +to_string(msg2) + "', DeviceCode = '"+to_string(device)+"'";
- queryList.push_back(query);
- //Inject 433diy
- 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) ";
- queryList.push_back(query);
- }
- else if (msgType == '1' && msg1 != 0 && msg2 != 0){
- printf("Sending msg to GB, Light & Voltage...\n");
- //Inject Time
- query ="INSERT INTO TimeTrigger SET DeviceCode ='" +to_string(device) + "', Info = '" + info + "'";
- queryList.push_back(query);
- //Inject Light inside
- query = "INSERT INTO LightIntensityInside SET CurrentLightIntensityIn ='"+to_string(msg1)+"', DeviceCode = '" +to_string(device)+"'";
- queryList.push_back(query);
- //Inject Voltage inside
- query = "INSERT INTO Voltage SET CurrentVoltage ='" +to_string(msg2) + "', DeviceCode = '"+to_string(device)+"'";
- queryList.push_back(query);
- //Inject 433diy
- 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) ";
- queryList.push_back(query);
- }else printf("Something went terribly wrong!\n");
- }
- //#################################### Milight code ####################################
- void Milight::sentMilightMsg(){
- const char* packetBytes = hexCode;
- memset(outgoingPacket, 0, 7);
- // convert input into hex
- int index = 0;
- for (int counter = 0; *packetBytes; ++packetBytes) {
- int n = 0;
- if (*packetBytes >= 'a' && *packetBytes <= 'f') {
- n = *packetBytes - 'a' + 10;
- }
- else if (*packetBytes >= 'A' && *packetBytes <= 'F') {
- n = *packetBytes - 'A' + 10;
- }
- else if (*packetBytes >= '0' && *packetBytes <= '9') {
- n = *packetBytes - '0';
- }
- else if (*packetBytes == ' ') {
- index++;
- }
- else {
- cout << "cannot decode" << endl;
- }
- outgoingPacket[index] = outgoingPacket[index] * 16 + (unsigned long)n;
- }
- mlr.write(outgoingPacket, sizeof(outgoingPacket));
- delay(10);
- for (int index = 0; index < 10; index++) {
- mlr.resend();
- delay(10);
- }
- outgoingPacket[6] += 8;
- }
- void Milight::recievingMilightMsg()
- {
- usleep(10000);
- MYSQL_RES* result = mysql_store_result(mysql1);
- if (result != NULL)
- {
- //Get the number of columns
- int num_rows = mysql_num_rows(result);
- int num_fields = mysql_num_fields(result);
- MYSQL_ROW row = mysql_fetch_row(result);
- if (num_fields >= 2){
- hexCode = row[1];
- milightHexChecker = string(row[0]);
- //printf("Debugnote: new field => 2\nDebugnote: milight code: ");
- //printf(hexCode);
- if(milightHexChecker != milightHex)
- {
- milightHex = milightHexChecker;
- string temp = row[1];
- if(temp.size() > 1)
- {
- sentMilightMsg();
- printf("#########################\n");
- printf("#Sending milight msg....#\n");
- printf("#########################\n");
- printf("Sending to DB: %s\n",temp.c_str());
- printf("###############\n");
- printf("#Listening....#\n");
- printf("###############\n");
- }
- }
- }
- }else printf("Debugnote: No result milights\n");
- }
- //#################################### Database code ####################################
- void Database::mysql_connect()
- {
- //initialize MYSQL object for connections
- mysql1 = mysql_init(NULL);
- if (mysql1 == NULL)
- {
- fprintf(stderr, "ABB : %s\n", mysql_error(mysql1));
- return;
- }
- if (mysql_real_connect(mysql1, DATABASE_IP, DATABASE_USERNAME, DATABASE_PASSWORD, DATABASE_NAME, 0, NULL, 0) == NULL)
- //Connect to the database
- fprintf(stderr, "%s\n", mysql_error(mysql1));
- else {
- printf("\n#################################\n");
- printf("#Database connection successful.#\n");
- }
- }
- void Database::mysql_disconnect()
- {
- mysql_close(mysql1);
- printf("Disconnected from database.\r\n");
- }
- void Database::sending_sql_msg()
- {
- if (mysql1 != NULL)
- {
- if(queryList.size() > 1)
- {
- printf("##########################\n");
- printf("#Sending 433mhz msg's....#\n");
- printf("##########################\n");
- }
- for(int i = 0; i < queryList.size(); i++){
- mysql_query(mysql1, queryList.at(i).c_str());
- printf("Sending to DB: %s\n", queryList.at(i).c_str());
- }
- //for(int i = 0; i < queryListMilight.size(); i++){
- // mysql_query(mysql1, queryListMilight.at(i).c_str());
- //}
- if(queryList.size() > 1)
- {
- auto t2 = chrono::high_resolution_clock::now();
- auto int_ms = chrono::duration_cast<chrono::milliseconds>(t2 - t1);
- chrono::duration<double, milli> fp_ms = t2 - t1;
- cout << "Sending took " << fp_ms.count() << " ms, " << "or " << int_ms.count() << " whole milliseconds\n";
- printf("###############\n");
- printf("#Listening....#\n");
- printf("###############\n");
- }
- queryList.clear();
- queryListMilight.clear();
- }else printf("No DB connection!\n");
- }
- int main(int argc, char *argv[])
- {
- Receiver433 recr433;
- Milight mili;
- Database cntr;
- if(wiringPiSetup() == -1) {
- printf("wiringPiSetup failed, exiting...");
- return 0;
- }
- mlr.begin();
- mySwitch = RCSwitch();
- mySwitch.setPulseLength(pulseLength);
- mySwitch.enableReceive(PIN); // Receiver on interrupt 0 => that is pin #2
- //making DB connection.
- if(mysql1 == NULL){
- cntr.mysql_connect();
- }
- printf("#Listening Modes.... #\n");
- printf("#################################\n");
- while(true){
- usleep(10000);
- queryListMilight.push_back("SELECT IDMilight, Hex FROM Milight ORDER BY IDMilight DESC LIMIT 1");
- recr433.station433();
- cntr.sending_sql_msg();
- //mili.recievingMilightMsg();
- usleep(10000);
- }
- printf("#############\n");
- printf("#Exiting....#\n");
- printf("#############\n");
- cntr.mysql_disconnect();
- exit(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement