raspberry
This commit is contained in:
1 parent
84dfdf5210
commit
be436085b3
55 files changed
+2195
No files matched your search
@@ -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;
|
||||
}
|
||||
Reference in new issue
Block a user