Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * On a Raspberry Pi 2 compile with:
- *
- * 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$
- *
- * for receiver mode run with:
- * sudo ./openmilight
- *
- * for sender mode run with:
- * sudo ./openmilight "B0 F2 EA 6D B0 02 f0"
- */
- #define DATABASE_IP "127.0.0.1"
- #define DATABASE_NAME "domotica"
- #define DATABASE_USERNAME "root"
- #define DATABASE_PASSWORD "aweds123"
- #include <stdio.h>
- #include <cstdlib>
- #include <iostream>
- #include <string.h>
- using namespace std;
- #include <RF24/RF24.h>
- #include <mysql/mysql.h>
- #include "PL1167_nRF24.h"
- #include "MiLightRadio.h"
- RF24 radio(RPI_V2_GPIO_P1_22, RPI_V2_GPIO_P1_24, BCM2835_SPI_SPEED_1MHZ);
- PL1167_nRF24 prf(radio);
- MiLightRadio mlr(prf);
- void setup()
- {
- mlr.begin();
- }
- static int dupesPrinted = 0;
- static bool receiving = false;
- static bool escaped = false;
- static uint8_t outgoingPacket[7];
- static uint8_t outgoingPacketPos = 0;
- static uint8_t nibble;
- static char *value_string;
- MYSQL *mysql1;
- static enum {
- IDLE,
- HAVE_NIBBLE,
- COMPLETE,
- } state;
- void loop(const char* newPacketBytes)
- {
- const char* packetBytes = value_string;
- 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;
- exit(1);
- }
- outgoingPacket[index] = outgoingPacket[index] * 16 + (unsigned long)n;
- }
- printf("sending packet\n");
- for (int i = 0; i < 7; i++) {
- printf("%02X ", outgoingPacket[i]);
- }
- printf("\n");
- for (int index = 0; index < 1; index++) {
- }
- mlr.write(outgoingPacket, sizeof(outgoingPacket));
- delay(10);
- mlr.resend();
- delay(10);
- outgoingPacket[6] += 8;
- string line2;
- getline(cin, line2);
- }
- void mysql_write_something(void)
- {
- if (mysql1 != NULL)
- {
- if (!mysql_query(mysql1, "SELECT id, light FROM test2 WHERE id = 1"))
- {
- 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; //An array of strings
- while ((row = mysql_fetch_row(result)))
- {
- if (num_fields >= 2)
- {
- char *value_int = row[0];
- value_string = row[1];
- printf("Got value %s\n", value_string);
- }
- }
- mysql_free_result(result);
- }
- else
- {
- printf("Cannot get data\r\n");
- }
- }
- else
- {
- printf("Cannot SELECT\r\n");
- }
- }
- }
- void mysql_connect(void)
- {
- //initialize MYSQL object for connections
- mysql1 = mysql_init(NULL);
- if (mysql1 == NULL)
- {
- printf("mysql1 is 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("Database connection successful.\r\n");
- mysql_write_something();
- }
- }
- void mysql_disconnect(void)
- {
- mysql_close(mysql1);
- printf("Disconnected from database.\r\n");
- }
- int
- main(int argc, char** arguments)
- {
- setup();
- char* packetBytes = 0;
- printf("ik doe dingen");
- mysql_connect();
- mysql_disconnect();
- /*if (argc < 2) {
- receiving = true;
- printf("in listening mode\n");
- } else {*/
- receiving = false;
- packetBytes = arguments[1];
- while (true) {
- loop(packetBytes);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement