raspberry
This commit is contained in:
1 parent
84dfdf5210
commit
be436085b3
55 files changed
+2195
No files matched your search
Binary file not shown.
@@ -0,0 +1,172 @@
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
#include <unistd.h>
|
||||
#include <sys/socket.h>
|
||||
#include <bluetooth/bluetooth.h>
|
||||
#include <bluetooth/rfcomm.h>
|
||||
#include <bluetooth/hci.h>
|
||||
#include <bluetooth/hci_lib.h>
|
||||
using namespace std;
|
||||
|
||||
|
||||
void log(string log_str, struct tm * timeinfo)
|
||||
{
|
||||
char logname[35];
|
||||
memset(logname,0,sizeof(logname));
|
||||
sprintf(logname,"/home/lucas/.LOG/BTlog%.2i%.2i.txt",timeinfo->tm_mon+1,timeinfo->tm_mday);
|
||||
system("find /home/lucas/.LOG/BTlog* -mtime +10 -type f -delete");
|
||||
ofstream logfile(logname, ios::app);
|
||||
if(logfile.is_open())
|
||||
{
|
||||
logfile << timeinfo->tm_mon<<"."<<timeinfo->tm_mday<<";"<<timeinfo->tm_hour<<":"<<timeinfo->tm_min<<":"<<timeinfo->tm_sec<<": BTserver: "<< log_str <<"\n";
|
||||
logfile.close();
|
||||
}
|
||||
}
|
||||
|
||||
int exec_cmd(int cmd_value,struct tm *timeinfo){
|
||||
string key,cmd=string("irsend SEND_ONCE SAMSUNG ");
|
||||
switch(cmd_value){
|
||||
case 1://ON
|
||||
key=string("KEY_0"); break;
|
||||
case 2://Up
|
||||
key=string("KEY_UP"); break;
|
||||
case 3://OFF
|
||||
key=string("KEY_POWER"); break;
|
||||
case 4://Left
|
||||
key=string("KEY_LEFT"); break;
|
||||
case 5://Enter
|
||||
key=string("KEY_ENTER"); break;
|
||||
case 6://Right
|
||||
key=string("KEY_RIGHT"); break;
|
||||
case 7://Volume UP
|
||||
key=string("KEY_VOLUMEUP"); break;
|
||||
case 8://Down
|
||||
key=string("KEY_DOWN"); break;
|
||||
case 9://Volume Down
|
||||
key=string("KEY_VOLUMEDOWN"); break;
|
||||
case 10://Source
|
||||
key=string("KEY_MEDIA"); break;
|
||||
case 11://Play
|
||||
key=string("KEY_PLAY"); break;
|
||||
case 12://Pause
|
||||
key=string("KEY_PAUSE"); break;
|
||||
case 13://Mute
|
||||
key=string("KEY_MUTE"); break;
|
||||
case 14://Mute
|
||||
key=string("KEY_BACK"); break;
|
||||
case 15://Mute
|
||||
key=string("KEY_EXIT"); break;
|
||||
default: log(to_string(cmd_value),timeinfo); return 1;
|
||||
}
|
||||
cmd.append(key);
|
||||
if(system(cmd.c_str())==0)
|
||||
{
|
||||
log(key,timeinfo);
|
||||
return 0;
|
||||
}
|
||||
else return -1;
|
||||
return -1;
|
||||
}
|
||||
int setBTdevice(int device, struct tm * timeinfo)
|
||||
{
|
||||
int s;
|
||||
struct sockaddr_rc loc_addr = { 0 };
|
||||
struct hci_dev_info di;
|
||||
// allocate socket
|
||||
s = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
|
||||
|
||||
// bind socket to port 1 of the first available
|
||||
// local bluetooth adapter
|
||||
if(hci_devinfo(device, &di) < 0)
|
||||
{
|
||||
log("HCI device info failed",timeinfo);
|
||||
exit(1);
|
||||
}
|
||||
string dev_log=string("Local device: ")+ batostr(&di.bdaddr);
|
||||
log(dev_log,timeinfo);
|
||||
loc_addr.rc_bdaddr = di.bdaddr;
|
||||
loc_addr.rc_family = AF_BLUETOOTH;
|
||||
loc_addr.rc_channel = (uint8_t) 1;
|
||||
if(bind(s, (struct sockaddr *)&loc_addr, sizeof(loc_addr)) < 0)
|
||||
{
|
||||
log("Socket bind Error",timeinfo);
|
||||
exit(1);
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
|
||||
if(argc < 2)
|
||||
{
|
||||
cout << "Usage:\n<1/0> BTdevice"<<endl;
|
||||
return 1;
|
||||
}
|
||||
struct sockaddr_rc rem_addr = { 0 };
|
||||
socklen_t opt = sizeof(rem_addr);
|
||||
struct tm * timeinfo;
|
||||
time_t rawtime;
|
||||
time ( &rawtime );
|
||||
timeinfo= localtime ( &rawtime );
|
||||
log("Start Bluetooth Server",timeinfo);
|
||||
string LF_phone=string("34:14:5F:68:FC:EC");
|
||||
char buf[1024] = { 0 };
|
||||
int sock, client, bytes_read,device=atoi(argv[1]);
|
||||
sock=setBTdevice(device,timeinfo);
|
||||
if(sock==0) log("Socket Failure",timeinfo);
|
||||
|
||||
// put socket into listening mode
|
||||
while(1)
|
||||
{
|
||||
listen(sock, 1);
|
||||
log("Device in Listening Mode",timeinfo);
|
||||
// accept one connection
|
||||
client = accept(sock, (struct sockaddr *)&rem_addr, &opt);
|
||||
if(client < 0)
|
||||
{
|
||||
log("Client accept Error",timeinfo);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
string dev_addr;
|
||||
ba2str( &rem_addr.rc_bdaddr,buf );
|
||||
dev_addr.assign(buf);
|
||||
|
||||
if(dev_addr.compare(LF_phone)!=0)
|
||||
{
|
||||
log("Illegal Connection Attempt",timeinfo);
|
||||
close(client);
|
||||
continue;
|
||||
}
|
||||
else
|
||||
{
|
||||
string log_str=string("Accepted connection from: ")+dev_addr;
|
||||
log(log_str,timeinfo);
|
||||
memset(buf, 0, sizeof(buf));
|
||||
// if(string())
|
||||
// read data from the client
|
||||
bytes_read = read(client, buf, sizeof(buf));
|
||||
while( bytes_read > 0 )
|
||||
{
|
||||
time ( &rawtime );
|
||||
timeinfo= localtime ( &rawtime );
|
||||
bytes_read = read(client, buf, sizeof(buf));
|
||||
if(exec_cmd(atoi(buf),timeinfo)<0)
|
||||
{
|
||||
log_str.assign("Remote Command Failed: " + string(buf));
|
||||
log(log_str,timeinfo);
|
||||
break;
|
||||
}
|
||||
memset(buf, 0, sizeof(buf));
|
||||
}
|
||||
close(client);
|
||||
log_str.assign("Disconnected from: "+dev_addr);
|
||||
log(log_str,timeinfo);
|
||||
}
|
||||
}
|
||||
// close connection
|
||||
close(sock);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
#include <iostream>
|
||||
#include <unistd.h>
|
||||
#include <sys/socket.h>
|
||||
#include <bluetooth/bluetooth.h>
|
||||
#include <bluetooth/rfcomm.h>
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
struct sockaddr_rc loc_addr = { 0 }, rem_addr = { 0 };
|
||||
struct hci_dev_info di;
|
||||
char buf[1024] = { 0 };
|
||||
int s, client, bytes_read;
|
||||
socklen_t opt = sizeof(rem_addr);
|
||||
char address[18] = "XX:XX:XX:XX:XX:XX";
|
||||
// allocate socket
|
||||
if(argc < 2)
|
||||
{
|
||||
cout << "Usage:\n<0/1> BTdevice" << endl;
|
||||
exit(0);
|
||||
}
|
||||
s = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
|
||||
{
|
||||
perror("socket");
|
||||
}
|
||||
if(hci_devinfo(0, &di) < 0)
|
||||
{
|
||||
perror("HCI device info failed");
|
||||
exit(1);
|
||||
}
|
||||
cout << "Local device: " << batostr(&di.bdaddr)) << endl;
|
||||
laddr.rc_family = AF_BLUETOOTH;
|
||||
laddr.rc_bdaddr = di.bdaddr;
|
||||
laddr.rc_channel = 2;
|
||||
|
||||
// bind socket to port 1 of the first available
|
||||
// local bluetooth adapter
|
||||
bind(s, (struct sockaddr *)&loc_addr, sizeof(loc_addr));
|
||||
{
|
||||
perror("bind");
|
||||
exit(1);
|
||||
}
|
||||
// put socket into listening mode
|
||||
listen(s, 1);
|
||||
|
||||
// accept one connection
|
||||
client = accept(s, (struct sockaddr *)&rem_addr, &opt);
|
||||
|
||||
ba2str( &rem_addr.rc_bdaddr, buf );
|
||||
uuuuustderr, "accepted connection from %s\n", buf);
|
||||
memset(buf, 0, sizeof(buf));
|
||||
|
||||
// read data from the client
|
||||
bytes_read = read(client, buf, sizeof(buf));
|
||||
if( bytes_read > 0 ) {
|
||||
printf("received [%s]\n", buf);
|
||||
}
|
||||
|
||||
// close connection
|
||||
close(client);
|
||||
close(s);
|
||||
return 0;
|
||||
}
|
||||
Whitespace-only changes.
Binary file not shown.
@@ -0,0 +1,68 @@
|
||||
#include "/home/lucas/433Utils/rc-switch/RCSwitch.h"
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
RCSwitch mySwitch;
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
// This pin is not the first pin on the RPi GPIO header!
|
||||
// Consult https://projects.drogon.net/raspberry-pi/wiringpi/pins/
|
||||
// for more information.
|
||||
int PIN = 2; //G27
|
||||
|
||||
if(wiringPiSetup() == -1) {
|
||||
printf("wiringPiSetup failed, exiting...");
|
||||
return 0;
|
||||
}
|
||||
|
||||
int pulseLength = 0;
|
||||
if (argv[1] != NULL) pulseLength = atoi(argv[1]);
|
||||
|
||||
mySwitch = RCSwitch();
|
||||
if (pulseLength != 0) mySwitch.setPulseLength(pulseLength);
|
||||
mySwitch.enableReceive(PIN); // Receiver on interrupt 0 => that is pin #2
|
||||
unsigned long value;
|
||||
unsigned long previous=0;
|
||||
int length;
|
||||
int protocol;
|
||||
unsigned int* raw;
|
||||
struct timeval time1,time2;
|
||||
long long elapsedTime;
|
||||
int delay;
|
||||
printf("Stetup Finished, Start Sniffing\n");
|
||||
while(1) {
|
||||
|
||||
if (mySwitch.available()) {
|
||||
|
||||
value = mySwitch.getReceivedValue();
|
||||
length = mySwitch.getReceivedBitlength();
|
||||
protocol = mySwitch.getReceivedProtocol();
|
||||
raw = mySwitch.getReceivedRawdata();
|
||||
delay = mySwitch.getReceivedDelay();
|
||||
if (value == 0)
|
||||
{
|
||||
printf("Unknown encoding\n");
|
||||
}
|
||||
else if (value != previous)
|
||||
{
|
||||
gettimeofday(&time1,NULL);
|
||||
printf("Received %i\nLength: %i\nProtocol: %i\nDelay: %i\nRaw Data:\n", value,length,protocol, delay );
|
||||
for (int i=0; i<= length*2; i++)
|
||||
{
|
||||
printf("%i",raw[i]);
|
||||
printf(",");
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
previous = value;
|
||||
mySwitch.resetAvailable();
|
||||
|
||||
}
|
||||
gettimeofday(&time2,NULL);
|
||||
if ((time2.tv_sec - time1.tv_sec) > 2) previous=0;
|
||||
}
|
||||
exit(0);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/socket.h>
|
||||
#include <bluetooth/bluetooth.h>
|
||||
#include <bluetooth/rfcomm.h>
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
struct sockaddr_rc loc_addr = { 0 }, rem_addr = { 0 };
|
||||
char buf[1024] = { 0 };
|
||||
int s, client, bytes_read;
|
||||
socklen_t opt = sizeof(rem_addr);
|
||||
|
||||
// allocate socket
|
||||
s = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
|
||||
|
||||
// bind socket to port 1 of the first available
|
||||
// local bluetooth adapter
|
||||
loc_addr.rc_family = AF_BLUETOOTH;
|
||||
loc_addr.rc_bdaddr = "00:00:00:00:00:00";
|
||||
loc_addr.rc_channel = (uint8_t) 1;
|
||||
bind(s, (struct sockaddr *)&loc_addr, sizeof(loc_addr));
|
||||
|
||||
// put socket into listening mode
|
||||
listen(s, 1);
|
||||
|
||||
// accept one connection
|
||||
client = accept(s, (struct sockaddr *)&rem_addr, &opt);
|
||||
|
||||
ba2str( &rem_addr.rc_bdaddr, buf );
|
||||
fprintf(stderr, "accepted connection from %s\n", buf);
|
||||
memset(buf, 0, sizeof(buf));
|
||||
|
||||
// read data from the client
|
||||
bytes_read = read(client, buf, sizeof(buf));
|
||||
if( bytes_read > 0 ) {
|
||||
printf("received [%s]\n", buf);
|
||||
}
|
||||
|
||||
int status = 0;
|
||||
|
||||
|
||||
if( status < 0 ) perror("uh oh");
|
||||
|
||||
// close connection
|
||||
close(client);
|
||||
close(s);
|
||||
return 0;
|
||||
}
|
||||
|
||||
Binary file not shown.
@@ -0,0 +1,42 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/socket.h>
|
||||
#include <bluetooth/bluetooth.h>
|
||||
#include <bluetooth/hci.h>
|
||||
#include <bluetooth/hci_lib.h>
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
inquiry_info *ii = NULL;
|
||||
int max_rsp, num_rsp;
|
||||
int dev_id, sock, len, flags;
|
||||
int i;
|
||||
char addr[19] = { 0 };
|
||||
char name[248] = { 0 };
|
||||
dev_id = hci_get_route(NULL);
|
||||
sock = hci_open_dev( dev_id );
|
||||
if (dev_id < 0 || sock < 0) {
|
||||
perror("opening socket");
|
||||
exit(1);
|
||||
}
|
||||
len = 8;
|
||||
max_rsp = 255;
|
||||
flags = IREQ_CACHE_FLUSH;
|
||||
ii = (inquiry_info*)malloc(max_rsp * sizeof(inquiry_info));
|
||||
num_rsp = hci_inquiry(dev_id, len, max_rsp, NULL, &ii, flags);
|
||||
if( num_rsp < 0 ) perror("hci_inquiry");
|
||||
for (i = 0; i < num_rsp; i++) {
|
||||
ba2str(&(ii+i)->bdaddr, addr);
|
||||
memset(name, 0, sizeof(name));
|
||||
if (hci_read_remote_name(sock, &(ii+i)->bdaddr, sizeof(name),
|
||||
name, 0) < 0)
|
||||
strcpy(name, "[unknown]");
|
||||
printf("%s %s\n", addr, name);
|
||||
}
|
||||
|
||||
free( ii );
|
||||
close( sock );
|
||||
return 0;
|
||||
}
|
||||
|
||||
Binary file not shown.
@@ -0,0 +1,76 @@
|
||||
/*
|
||||
* BlueZ example code to build an rfcomm client.
|
||||
* This code just creates a socket and connects
|
||||
* to a remote bluetooth device and sends a string.
|
||||
* Programmed by Bastian Ballmann
|
||||
* http://www.geektown.de
|
||||
* Compile with gcc -lbluetooth <executable> <source>
|
||||
* */
|
||||
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <stdlib.h>
|
||||
#include <bluetooth/bluetooth.h>
|
||||
#include <bluetooth/rfcomm.h>
|
||||
#include <bluetooth/hci.h>
|
||||
#include <unistd.h>
|
||||
#include <bluetooth/hci_lib.h>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int sock, bytes_read, bt_channel=1;
|
||||
struct sockaddr_rc laddr, raddr;
|
||||
struct hci_dev_info di;
|
||||
char bt_addr[]="98:D3:31:FD:14:BC";
|
||||
if(argc < 3)
|
||||
{
|
||||
printf("%s <btaddr> <channel>\n", argv[0]);
|
||||
exit(0);
|
||||
}
|
||||
if(hci_devinfo(0, &di) < 0)
|
||||
{
|
||||
perror("HCI device info failed");
|
||||
exit(1);
|
||||
}
|
||||
printf("Local device %s\n", batostr(&di.bdaddr));
|
||||
|
||||
laddr.rc_family = AF_BLUETOOTH;
|
||||
laddr.rc_bdaddr = di.bdaddr;
|
||||
laddr.rc_channel = 0;
|
||||
raddr.rc_family = AF_BLUETOOTH;
|
||||
str2ba(argv[1],&raddr.rc_bdaddr);
|
||||
raddr.rc_channel = atoi(argv[2]);
|
||||
if( (sock = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM)) < 0)
|
||||
{
|
||||
perror("socket");
|
||||
}
|
||||
|
||||
if(bind(sock, (struct sockaddr *)&laddr, sizeof(laddr)) < 0)
|
||||
{
|
||||
perror("bind");
|
||||
exit(1);
|
||||
}
|
||||
printf("Remote device %s\n", argv[1]);
|
||||
if(connect(sock, (struct sockaddr *)&raddr, sizeof(raddr)) < 0)
|
||||
{
|
||||
perror("connect");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
printf("Connected.\nRecieving data\n");
|
||||
//read data from the client
|
||||
while(1)
|
||||
{
|
||||
char buf[1024]={0};
|
||||
bytes_read = read(sock, buf, sizeof(buf));
|
||||
if( bytes_read > 0 ) {
|
||||
printf("%s\n", buf);
|
||||
}
|
||||
|
||||
}
|
||||
printf("Disconnect.\n");
|
||||
close(sock);
|
||||
return 0;
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,217 @@
|
||||
#include <stdlib.h>
|
||||
#include <iomanip>
|
||||
#include <cerrno>
|
||||
#include <sys/time.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <sys/stat.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <bluetooth/bluetooth.h>
|
||||
#include <bluetooth/rfcomm.h>
|
||||
#include <bluetooth/hci.h>
|
||||
#include <bluetooth/hci_lib.h>
|
||||
|
||||
using namespace std;
|
||||
int BTsocket=0;
|
||||
|
||||
int mail(string mail_str)
|
||||
{
|
||||
struct tm * timeinfo;
|
||||
time_t rawtime;
|
||||
time ( &rawtime );
|
||||
timeinfo= localtime ( &rawtime );
|
||||
string recipient="lucas-f@live.at",cmd=string("printf \"Moist Sensor Alarm:\\n") + asctime(timeinfo) + string("\\n") + mail_str +string("\" | mail -s 'MS Alarm' " + recipient);
|
||||
if(system(cmd.c_str())!=0) cout << "Failed to send Mail" << endl;
|
||||
else cout << "Mail Sent" << endl;
|
||||
return 0;
|
||||
}
|
||||
void log(string log_str)
|
||||
{
|
||||
struct tm * timeinfo;
|
||||
time_t rawtime;
|
||||
time ( &rawtime );
|
||||
timeinfo= localtime ( &rawtime );
|
||||
ostringstream logname;
|
||||
logname << "/home/lucas/.LOG/MSlog" << setfill('0') <<setw(2) << timeinfo->tm_mon+1 << setw(2) << timeinfo->tm_mday << ".txt";
|
||||
system("find /home/lucas/.LOG/MSlog* -mtime +10 -type f -delete");
|
||||
string filename=logname.str();
|
||||
ofstream logfile(filename.c_str(), ios::app);
|
||||
if(logfile.is_open())
|
||||
{
|
||||
logfile << timeinfo->tm_mon<<"."<<timeinfo->tm_mday<<";"<<timeinfo->tm_hour<<":"<<timeinfo->tm_min<<":"<<timeinfo->tm_sec<<": "<<log_str<<endl;
|
||||
logfile.close();
|
||||
}
|
||||
else
|
||||
{
|
||||
mail("Log File Creation Error");
|
||||
}
|
||||
}
|
||||
|
||||
int setBTdevice(int device)
|
||||
{
|
||||
BTsocket=0;
|
||||
int bt_channel=1;
|
||||
struct sockaddr_rc laddr, raddr;
|
||||
struct hci_dev_info di;
|
||||
char bt_addr[]="98:D3:31:FD:14:BC";
|
||||
if(hci_devinfo(device, &di) < 0)
|
||||
{
|
||||
log("HCI device info failed Errno: " + string(strerror(errno)));
|
||||
return errno;
|
||||
}
|
||||
cout << "Local device " << batostr(&di.bdaddr) << endl;
|
||||
|
||||
laddr.rc_family = AF_BLUETOOTH;
|
||||
laddr.rc_bdaddr = di.bdaddr;
|
||||
laddr.rc_channel = 0;
|
||||
raddr.rc_family = AF_BLUETOOTH;
|
||||
str2ba(bt_addr,&raddr.rc_bdaddr);
|
||||
raddr.rc_channel = bt_channel;
|
||||
if( (BTsocket = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM)) <0)
|
||||
{
|
||||
log("Function error: BTsocketet Errno: " + string(strerror(errno)));
|
||||
return errno;
|
||||
}
|
||||
|
||||
struct timeval tv;
|
||||
tv.tv_sec = 30; /* 30 Secs Timeout */
|
||||
tv.tv_usec = 0; // Not init'ing this can cause strange errors
|
||||
setsockopt(BTsocket, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tv,sizeof(struct timeval));
|
||||
int reuse = 1;
|
||||
setsockopt(BTsocket,SOL_SOCKET,SO_REUSEPORT,&reuse,sizeof(int));
|
||||
if(bind(BTsocket, (struct sockaddr *)&laddr, sizeof(laddr)) < 0)
|
||||
{
|
||||
log("Function error: bind Errno: " + string(strerror(errno)));
|
||||
return errno;
|
||||
}
|
||||
if(connect(BTsocket, (struct sockaddr *)&raddr, sizeof(raddr)) <0)
|
||||
{
|
||||
if(errno != EINPROGRESS)
|
||||
{
|
||||
log("Function error: connect. Errno: " + string(strerror(errno)));
|
||||
return errno;
|
||||
}
|
||||
}
|
||||
fd_set readfds;
|
||||
|
||||
FD_ZERO(&readfds);
|
||||
FD_SET(BTsocket, &readfds);
|
||||
//We are going to use select to wait for the BTsocketet to connect
|
||||
struct timeval sel_to; //Time value struct declaration
|
||||
sel_to.tv_sec = 5; //The second portion of the struct
|
||||
sel_to.tv_usec = 0; //The microsecond portion of the struct
|
||||
|
||||
//DEBUG: This is ALWAYS 1
|
||||
if(select(BTsocket + 1, &readfds, NULL, NULL, &sel_to)<=0)
|
||||
{
|
||||
log("Function error: select. Errno: " + string(strerror(errno)));
|
||||
return errno;
|
||||
}
|
||||
cout << "Connected To Arduino" << endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int reconnectBT(int BTsocket){
|
||||
int bt_channel=1;
|
||||
char bt_addr[]="98:D3:31:FD:14:BC";
|
||||
struct sockaddr_rc raddr;
|
||||
raddr.rc_family = AF_BLUETOOTH;
|
||||
str2ba(bt_addr,&raddr.rc_bdaddr);
|
||||
raddr.rc_channel = bt_channel;
|
||||
if(connect(BTsocket, (struct sockaddr *)&raddr, sizeof(raddr)) <0)
|
||||
{
|
||||
log("Function error: connect. Errno: " + string(strerror(errno)) +" Socket: " + to_string(BTsocket) );
|
||||
return 0;
|
||||
}
|
||||
cout << "Connected To Arduino" << endl;
|
||||
return BTsocket;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
if (argc<4)
|
||||
{
|
||||
cout << "Usage: <m/x> Mail/no Mail\n<i> delay\n<0/1> BTdevice"<<endl;
|
||||
return 0;
|
||||
}
|
||||
cout << "Start Moisture Sensor Reciever\n" << "Usage: argv[1] m for Mail ON x for Mail OFF"<<endl;
|
||||
struct timeval time1;
|
||||
int sleeptime=atoi(argv[2]),connection_retries=100,device=atoi(argv[3]);
|
||||
int limit=100;
|
||||
//sleeptime=;
|
||||
bool msg=false;
|
||||
if(strchr(argv[1],'m')!=NULL)
|
||||
{
|
||||
msg=true;
|
||||
cout << "Mail ON\n";
|
||||
}
|
||||
if(device!=0&&device!=1)
|
||||
{
|
||||
cout << "Wrong BT Device" << endl;
|
||||
return 0;
|
||||
}
|
||||
int ret,i;
|
||||
ret=setBTdevice(device);
|
||||
for(i=0;i<connection_retries&&ret!=0;i++){
|
||||
if(BTsocket!=0) close(BTsocket);
|
||||
sleep(sleeptime);
|
||||
ret=setBTdevice(device);
|
||||
if(i==connection_retries-1&&ret!=0)
|
||||
{
|
||||
cout << "Bluetooth Connection cannot be Established" << endl;
|
||||
mail("Bluetooth Connection cannot be established. Errno: " + string(strerror(ret)));
|
||||
log("Bluetooth Connection cannot be established. Errno: " + string(strerror(ret)));
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
int bytes_read,value;
|
||||
cout << "Setup Complete" << endl;
|
||||
gettimeofday(&time1,NULL);
|
||||
while(1)
|
||||
{
|
||||
char buf[1024]={0};
|
||||
bytes_read = read(BTsocket, buf, sizeof(buf));
|
||||
if(bytes_read > 0)
|
||||
{
|
||||
value = atoi(buf);
|
||||
cout << "Received " << value << endl;
|
||||
if(value>=limit&&value<1200)
|
||||
{
|
||||
cout << "Value exceeds Limit!"<< endl;
|
||||
if(msg) mail("Value exceeds Limit: " + to_string(value));
|
||||
|
||||
}
|
||||
log("MSValue: " + to_string(value));
|
||||
sleep(sleeptime);
|
||||
}
|
||||
else
|
||||
{
|
||||
log("Read Error: " + to_string(bytes_read) + " [ Errno: " +to_string(errno) + " ] " + string(strerror(errno)) + "Socket: " + to_string(BTsocket));
|
||||
cout << "Connection Lost" << endl;
|
||||
for(i=0;i<connection_retries;i++)
|
||||
{
|
||||
close(BTsocket);
|
||||
sleep(sleeptime);
|
||||
BTsocket=setBTdevice(device);
|
||||
if(ret==0&&BTsocket!=0)
|
||||
{
|
||||
log("Bluetooth Connection temporarily Lost, reestablished after " + to_string(i) + " retries" );
|
||||
break;
|
||||
}
|
||||
else if(i==connection_retries-1&&ret!=0)
|
||||
{
|
||||
cout << "Bluetooth Connection not possible!" << endl;
|
||||
log("Bluetooth Connection Lost. Errno: " + string(strerror(ret)));
|
||||
mail("Bluetooth Connection Lost. Errno: " + string(strerror(ret)));
|
||||
close(BTsocket);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,41 @@
|
||||
//compile with -lwiringPi
|
||||
//g++ -pthread ../../wiringPi/433Utils/rc-switch/RCSwitch.o sendRC_Schlafzimmer.cpp -o sendRC_Schlafzimmer -lwiringPi
|
||||
#include "/home/lucas/wiringPi/433Utils/rc-switch/RCSwitch.h"
|
||||
#include <stdlib.h>
|
||||
extern "C" {
|
||||
#include <wiringPi.h>
|
||||
}
|
||||
#include <stdio.h>
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
|
||||
// This pin is not the first pin on the RPi GPIO header!
|
||||
// Consult https://projects.drogon.net/raspberry-pi/wiringpi/pins/
|
||||
// for more information.
|
||||
int PIN = 0;//G17
|
||||
|
||||
// Parse the first parameter to this command as an integer
|
||||
int protocol = 2; // A value of 0 will use rc-switch's default value
|
||||
int pulseLength = 0;
|
||||
int length = 24;
|
||||
string arg=string(argv[1]);
|
||||
// Change protocol and pulse length accroding to parameters
|
||||
int code=0;
|
||||
if(arg.compare("OFF")==0) code=891819008;
|
||||
else if(arg.compare("ON")==0) code=958927872;
|
||||
if (wiringPiSetup () == -1) return 1;
|
||||
cout << "Switching " << arg << endl;
|
||||
RCSwitch mySwitch = RCSwitch();
|
||||
if (protocol != 0) mySwitch.setProtocol(protocol);
|
||||
if (pulseLength != 0) mySwitch.setPulseLength(pulseLength);
|
||||
mySwitch.enableTransmit(PIN);
|
||||
|
||||
mySwitch.send(code, length);
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
Binary file not shown.
@@ -0,0 +1,42 @@
|
||||
#include "/home/lucas/433Utils/rc-switch/RCSwitch.h"
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
int main(int argc, char *argv[]) {
|
||||
|
||||
// This pin is not the first pin on the RPi GPIO header!
|
||||
// Consult https://projects.drogon.net/raspberry-pi/wiringpi/pins/
|
||||
// for more information.
|
||||
int PIN = 0;//G17
|
||||
|
||||
// Parse the first parameter to this command as an integer
|
||||
int protocol = 2; // A value of 0 will use rc-switch's default value
|
||||
int pulseLength = 0;
|
||||
int length = 24;
|
||||
// If no command line argument is given, print the help text
|
||||
if (argc == 1) {
|
||||
printf("Usage: %s decimalcode [protocol] [length] [pulselength]\n", argv[0]);
|
||||
printf("decimalcode\t- As decoded by RFSniffer\n");
|
||||
printf("protocol\t- According to rc-switch definitions\n");
|
||||
printf("length\t\t- Whatever the Fuck U want\n");
|
||||
printf("pulselength\t- pulselength in microseconds\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Change protocol and pulse length accroding to parameters
|
||||
int code = atoi(argv[1]);
|
||||
if (argc >= 3) protocol = atoi(argv[2]);
|
||||
if (argc >= 4) length = atoi(argv[3]);
|
||||
if (argc >= 5) pulseLength = atoi(argv[4]);
|
||||
if (wiringPiSetup () == -1) return 1;
|
||||
printf("sending code[%i]\n", code);
|
||||
RCSwitch mySwitch = RCSwitch();
|
||||
if (protocol != 0) mySwitch.setProtocol(protocol);
|
||||
if (pulseLength != 0) mySwitch.setPulseLength(pulseLength);
|
||||
mySwitch.enableTransmit(PIN);
|
||||
|
||||
mySwitch.send(code, length);
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user