Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // dooralarm.c
- // works with motion daemon
- // sends email with attached snapshot from usb webcam
- // when door is opened
- //
- // connect reed switch to gpio 7
- // raspberry pi pin 7
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <stdlib.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <syslog.h>
- #include <time.h>
- #include <errno.h>
- #include <netdb.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #include <resolv.h>
- #include <string.h>
- #include <bcm2835.h>
- #include <stdio.h>
- #define PIN RPI_GPIO_P1_07
- uint8_t LASTSTATE = 1;
- char *getdatetime();
- char *getoutput1_base64();
- long int getoutput1size();
- int main(int argc, char **argv)
- {
- logit ("Calling daemonize()");
- daemonize();
- int i;
- const char *email[2];
- email[0] = "noc@<DOMAIN>";
- if (!bcm2835_init())
- return 1;
- // Set RPI pin P1-15 to be an input
- bcm2835_gpio_fsel(PIN, BCM2835_GPIO_FSEL_INPT);
- // with a pullup
- bcm2835_gpio_set_pud(PIN, BCM2835_GPIO_PUD_UP);
- while (1)
- {
- // Read some data
- uint8_t value = bcm2835_gpio_lev(PIN);
- if ( value == 1 && LASTSTATE == 0 ){
- char *timestamp;
- timestamp = getdatetime();
- long int output1size = getoutput1size();
- int count = 0;
- logit ("Door 01 OPENED.");
- //sleep(15);
- //system("/usr/bin/fswebcam -d /dev/video0 -i 0 -r 960x720 --timestamp '%Y-%m-%d %H:%M:%S (%Z)' --scale 640x480 --rotate 180 - | /usr/bin/base64 > /tmp/output1.jpg");
- //while ( output1size < 367001 && count <= 20 ){
- while ( output1size < 3625001 && count <= 20 ){
- sleep ( 3 );
- output1size = getoutput1size();
- count++;
- }
- for ( i=0; i<1; i++){
- sendemail( email[i], timestamp );
- }
- }
- if ( value == 0 && LASTSTATE == 1 ){
- logit ("Door 01 CLOSED.");
- }
- LASTSTATE = value;
- delay(500);
- }
- bcm2835_close();
- return 0;
- }
- int sendemail(char *email, char *timestamp){
- int a;
- int socket_desc;
- char sendto[128];
- char atsymbol[] = "@";
- char *mxs[10];
- char mailserver[128];
- char *domain;
- char *output1 = getoutput1_base64();
- struct sockaddr_in server;
- // copy email address
- strcpy(sendto,email);
- // parse domain portion of email
- domain = strstr(sendto, atsymbol);
- memmove(&domain[0], &domain[1], strlen(domain) - 0);
- // get mx record
- a=resolvmx("newbeecolo.com", mxs,10);
- struct hostent* mxserver = gethostbyname (mxs[a-1]);
- strcpy(mailserver,inet_ntoa( *( struct in_addr*)( mxserver -> h_addr_list[0])));
- //Create socket
- socket_desc = socket(AF_INET , SOCK_STREAM , 0);
- if (socket_desc == -1)
- {
- printf("Could not create socket");
- }
- server.sin_addr.s_addr = inet_addr(mailserver);
- server.sin_family = AF_INET;
- server.sin_port = htons( 25 );
- //Connect to remote server
- if (connect(socket_desc , (struct sockaddr *)&server , sizeof(server)) < 0)
- {
- puts("connect error");
- return 1;
- }
- char *message , server_reply[2000];
- char *message2, *message3, *message4, *message5, *message6, *message7;
- char *mime1, *mime2, *mime3, *mime4, *mime5, *mime6, *mime7;
- char *emailbody;
- char emailbody2[256];
- message = "helo lab01.newbeecolo.com\r\n";
- send(socket_desc , message , strlen(message) , 0);
- //if( recv(socket_desc, server_reply , 2000 , 0) < 0) { //puts("recv failed"); }
- //puts("Reply received\n");
- //puts(server_reply);
- message2 = "mail from:<alarmpi@HOSTNAME>\r\n";
- send(socket_desc , message2 , strlen(message2) , 0);
- message3 = "rcpt to:";
- send(socket_desc , message3 , strlen(message3) , 0);
- send(socket_desc , email , strlen(email) , 0);
- send(socket_desc , "\r\n" , strlen("\r\n") , 0);
- message4 = "data\r\n";
- send(socket_desc , message4 , strlen(message4) , 0);
- message5 = "To:";
- send(socket_desc , message5 , strlen(message5) , 0);
- send(socket_desc , email , strlen(email) , 0);
- send(socket_desc , "\r\n" , strlen("\r\n") , 0);
- message6 = "Subject: Door 01 opened.\r\n";
- send(socket_desc , message6 , strlen(message6) , 0);
- mime1 = "Content-Type: multipart/mixed; boundary=\"3.1415926535897932384626\"\r\n";
- send(socket_desc , mime1 , strlen(mime1) , 0);
- mime2 = "MIME_Version: 1.0\r\n";
- send(socket_desc , mime2 , strlen(mime2) , 0);
- send(socket_desc , "\r\n" , strlen("\r\n") , 0);
- mime3 = "--3.1415926535897932384626\r\n";
- send(socket_desc , mime3 , strlen(mime3) , 0);
- mime4 = "Content-Type: text/html; charset=ISO-8859-1\r\n";
- send(socket_desc , mime4 , strlen(mime4) , 0);
- send(socket_desc , "\r\n" , strlen("\r\n") , 0);
- send(socket_desc , "\r\n" , strlen("\r\n") , 0);
- mime3 = "--3.1415926535897932384626\r\n";
- send(socket_desc , mime3 , strlen(mime3) , 0);
- //mime5 = "Content-Type: application/octet-stream; name=\"door01.mp4\"\r\n";
- //mime5 = "Content-Type: video/x-msvideo; name=\"door01.avi\"\r\n";
- //mime5 = "Content-Type: video/H264; name=\"door01.avi\"\r\n";
- mime5 = "Content-Type: video/mpeg; name=\"door01.mp4\"\r\n";
- send(socket_desc , mime5 , strlen(mime5) , 0);
- mime6 = "Content-Transfer-Encoding: base64\r\n";
- send(socket_desc , mime6 , strlen(mime6) , 0);
- mime7 = "Content-Disposition: attachment; filename=\"door01.mp4\"\r\n";
- send(socket_desc , mime7 , strlen(mime7) , 0);
- send(socket_desc , "\r\n" , strlen("\r\n") , 0);
- send(socket_desc , output1, strlen(output1), 0);
- send(socket_desc , "\r\n" , strlen("\r\n") , 0);
- mime3 = "--3.1415926535897932384626--\r\n";
- send(socket_desc , mime3 , strlen(mime3) , 0);
- message7 = ".\r\n";
- send(socket_desc , message7 , strlen(message7) , 0);
- return 1;
- }
- int resolvmx(const char *name, char **mxs, int limit) {
- unsigned char response[NS_PACKETSZ]; /* big enough, right? */
- ns_msg handle;
- ns_rr rr;
- int mx_index, ns_index, len;
- char dispbuf[4096];
- if ((len = res_search(name, C_IN, T_MX, response, sizeof(response))) < 0) {
- /* WARN: res_search failed */
- return -1;
- }
- if (ns_initparse(response, len, &handle) < 0) {
- /* WARN: ns_initparse failed */
- return 0;
- }
- len = ns_msg_count(handle, ns_s_an);
- if (len < 0)
- return 0;
- for (mx_index = 0, ns_index = 0;
- mx_index < limit && ns_index < len;
- ns_index++) {
- if (ns_parserr(&handle, ns_s_an, ns_index, &rr)) {
- /* WARN: ns_parserr failed */
- continue;
- }
- ns_sprintrr (&handle, &rr, NULL, NULL, dispbuf, sizeof (dispbuf));
- if (ns_rr_class(rr) == ns_c_in && ns_rr_type(rr) == ns_t_mx) {
- char mxname[MAXDNAME];
- dn_expand(ns_msg_base(handle), ns_msg_base(handle) + ns_msg_size(handle), ns_rr_rdata(rr) + NS_INT16SZ, mxname, sizeof(mxname));
- mxs[mx_index++] = strdup(mxname);
- }
- }
- return mx_index;
- }
- int daemonize() {
- pid_t pid, sid;
- /* Fork off the parent process */
- pid = fork();
- if (pid < 0) {
- exit(EXIT_FAILURE);
- }
- /* If we got a good PID, then we can exit the parent process. */
- if (pid > 0) {
- exit(EXIT_SUCCESS);
- }
- /* Change the file mode mask */
- umask(0);
- /* Open any logs here */
- /* Create a new SID for the child process */
- sid = setsid();
- if (sid < 0) {
- /* Log the failure */
- exit(EXIT_FAILURE);
- }
- /* Change the current working directory */
- if ((chdir("/")) < 0) {
- /* Log the failure */
- exit(EXIT_FAILURE);
- }
- /* Close out the standard file descriptors */
- close(STDIN_FILENO);
- close(STDOUT_FILENO);
- close(STDERR_FILENO);
- writepid( pid );
- return 1;
- }
- int logit (char *msg) {
- char logfile[] = "/tmp/dooralarm.log";
- char *timestamp;
- char logmsg[128];
- timestamp = getdatetime();
- strcpy(logmsg, timestamp);
- strcat(logmsg, " ");
- strcat(logmsg, msg);
- strcat(logmsg, "\n");
- FILE *fp;
- fp = fopen(logfile, "a");
- fprintf(fp, logmsg );
- fclose(fp);
- }
- char *getdatetime() {
- char buffer[128];
- time_t curtime;
- struct tm *loctime;
- /* Get the current time. */
- curtime = time (NULL);
- /* Convert it to local time representation. */
- loctime = localtime (&curtime);
- strftime (buffer, 128, "%Y-%m-%d %H:%M:%S", loctime);
- return buffer;
- }
- int writepid(){
- char pidfile[] = "/var/run/dooralarm.pid";
- char str[128];
- sprintf(str,"%d\n",getpid());
- FILE *fp;
- fp = fopen(pidfile, "w");
- fprintf(fp, str );
- fclose(fp);
- }
- char *getoutput1_base64(){
- //system("/usr/bin/cat /srv/http/cam1/output1.avi | /usr/bin/base64 > /tmp/output1_base64.avi");
- //FILE *f = fopen("/tmp/output1.jpg", "rb");
- //FILE *f = fopen("/srv/http/cam1/output1.avi", "rb");
- //FILE *f = fopen("/tmp/output1_base64.avi", "rb");
- //system("/usr/bin/mencoder /srv/http/cam1/output1.avi -idx -ovc lavc -lavcopts vcodec=mjpeg:mbd=1:vbitrate=1800 -o - | /usr/bin/base64 > /tmp/door01.mp4");
- //system("/usr/bin/mencoder /srv/http/cam1/output1.avi -idx -ovc x264 -x264encopts pass=1:turbo:bitrate=900:bframes=1 -vf scale=-10:-1,harddup -o - | /usr/bin/base64 > /tmp/door01.avi");
- system("/usr/bin/cat /srv/http/cam1/output1.avi | /usr/bin/base64 > /tmp/door01.avi");
- FILE *f = fopen("/tmp/door01.avi", "rb");
- fseek(f, 0, SEEK_END);
- long fsize = ftell(f);
- fseek(f, 0, SEEK_SET);
- char *string = malloc(fsize + 1);
- fread(string, fsize, 1, f);
- fclose(f);
- string[fsize] = 0;
- return string;
- }
- long getoutput1size(){
- FILE *f = fopen("/srv/http/cam1/output1.avi", "rb");
- fseek(f, 0, SEEK_END);
- long fsize = ftell(f);
- //fseek(f, 0, SEEK_SET);
- //char *string = malloc(fsize + 1);
- //fread(string, fsize, 1, f);
- fclose(f);
- //string[fsize] = 0;
- //return string;
- return fsize;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement