summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--client/Makefile23
-rw-r--r--client/main.c59
-rw-r--r--server/Makefile23
-rw-r--r--server/main.c103
4 files changed, 208 insertions, 0 deletions
diff --git a/client/Makefile b/client/Makefile
new file mode 100644
index 0000000..f2b6ca3
--- /dev/null
+++ b/client/Makefile
@@ -0,0 +1,23 @@
+TARGET = $(shell basename $(CURDIR))
+
+SRCS = $(wildcard *.c)
+OBJS = $(SRCS:.c=.o)
+
+CC = gcc
+
+all: build
+
+build: $(TARGET)
+
+clean:
+ rm -f $(OBJS)
+ rm -f $(TARGET)
+
+run: $(TARGET)
+ $(realpath $(TARGET))
+
+$(TARGET): $(OBJS)
+ $(CC) -o $(TARGET) $(OBJS)
+
+.c.o:
+ $(CC) -c -o $*.o $^
diff --git a/client/main.c b/client/main.c
new file mode 100644
index 0000000..8ea731b
--- /dev/null
+++ b/client/main.c
@@ -0,0 +1,59 @@
+#include <arpa/inet.h>
+#include <fcntl.h>
+#include <netinet/in.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/socket.h>
+#include <unistd.h>
+
+#define PORT 2319
+
+int main(int argc, char **argv) {
+ int ret;
+ char c, last;
+ ssize_t in;
+
+ int fd;
+ int sfd;
+
+ char buf[256];
+
+ struct sockaddr_in addr;
+ addr.sin_family = AF_INET;
+ addr.sin_port = htons(PORT);
+ addr.sin_addr.s_addr = htonl(INADDR_BROADCAST);
+
+ sfd = socket(AF_INET, SOCK_DGRAM, 0);
+ if(sfd < 0) {
+ perror("Failed to open socket");
+ exit(EXIT_FAILURE);
+ }
+
+ ret = bind(sfd, (struct sockaddr*) &addr, sizeof(addr));
+ if(ret < 0) {
+ perror("Failed to bind socket");
+ exit(EXIT_FAILURE);
+ }
+
+ for(;;) {
+ in = read(sfd, buf, sizeof(buf) - 1);
+ if(in < 0) {
+ perror("Failed to read from serial port");
+ exit(EXIT_FAILURE);
+ }
+ buf[in] = 0;
+
+ ret = sscanf(buf, "%c->%c\n", &last, &c);
+ if(ret < 2)
+ continue;
+
+ if(last == 'L' && c == 'H')
+ system("/home/jake/.bin/scripts/reed open");
+
+ if(last == 'H' && c == 'L')
+ system("/home/jake/.bin/scripts/reed close");
+ }
+
+ return 0;
+}
diff --git a/server/Makefile b/server/Makefile
new file mode 100644
index 0000000..f2b6ca3
--- /dev/null
+++ b/server/Makefile
@@ -0,0 +1,23 @@
+TARGET = $(shell basename $(CURDIR))
+
+SRCS = $(wildcard *.c)
+OBJS = $(SRCS:.c=.o)
+
+CC = gcc
+
+all: build
+
+build: $(TARGET)
+
+clean:
+ rm -f $(OBJS)
+ rm -f $(TARGET)
+
+run: $(TARGET)
+ $(realpath $(TARGET))
+
+$(TARGET): $(OBJS)
+ $(CC) -o $(TARGET) $(OBJS)
+
+.c.o:
+ $(CC) -c -o $*.o $^
diff --git a/server/main.c b/server/main.c
new file mode 100644
index 0000000..e37c33e
--- /dev/null
+++ b/server/main.c
@@ -0,0 +1,103 @@
+#include <arpa/inet.h>
+#include <fcntl.h>
+#include <netinet/in.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/socket.h>
+#include <unistd.h>
+
+#define PORT 2319
+
+static int ifd;
+
+static void log_data(char *name, char *value) {
+ char buf[512];
+ struct sockaddr_in addr;
+ struct timeval tv;
+
+ addr.sin_family = AF_INET;
+ addr.sin_port = htons(8089);
+ inet_aton("10.51.0.15", &addr.sin_addr);
+
+ gettimeofday(&tv, NULL);
+
+ snprintf(buf, sizeof(buf), "%s %s %ld\n", name, value, (tv.tv_sec * 1000000000) + (tv.tv_usec * 1000));
+ printf(buf);
+
+ sendto(ifd, buf, strlen(buf), 0, (struct sockaddr*) &addr, sizeof(addr));
+}
+
+int main(int argc, char **argv) {
+ int ret;
+ char c = 'L', last = 'L';
+ ssize_t in;
+
+ int fd;
+ int sfd;
+
+ int be = 1;
+ char buf[256];
+
+ struct sockaddr_in addr;
+
+ if(argc < 2) {
+ printf("Usage: %s <serial port>\n", argv[0]);
+ exit(EXIT_FAILURE);
+ }
+
+ fd = open(argv[1], O_RDONLY);
+ if(fd < 0) {
+ perror("Failed to open serial port");
+ exit(EXIT_FAILURE);
+ }
+
+ sfd = socket(AF_INET, SOCK_DGRAM, 0);
+ if(sfd < 0) {
+ perror("Failed to open socket");
+ exit(EXIT_FAILURE);
+ }
+
+ setsockopt(sfd, SOL_SOCKET, SO_BROADCAST, &be, sizeof(be));
+ if(ret < 0) {
+ perror("Failed to enable broadcasts on socket");
+ exit(EXIT_FAILURE);
+ }
+
+ ifd = socket(AF_INET, SOCK_DGRAM, 0);
+ if(sfd < 0) {
+ perror("Failed to open InfluxDB socket");
+ exit(EXIT_FAILURE);
+ }
+
+ addr.sin_family = AF_INET;
+ addr.sin_port = htons(PORT);
+ addr.sin_addr.s_addr = htonl(INADDR_BROADCAST);
+
+ for(;;) {
+ in = read(fd, &c, 1);
+ if(in < 0) {
+ perror("Failed to read from serial port");
+ exit(EXIT_FAILURE);
+ }
+
+ if(c != 'H' && c != 'L')
+ continue;
+
+ snprintf(buf, sizeof(buf), "%c->%c", last, c);
+ puts(buf);
+ sendto(sfd, buf, strlen(buf), 0, (struct sockaddr*) &addr, sizeof(addr));
+
+ if(c != last) {
+ if(last == 'L' && c == 'H')
+ log_data("bedroom_door", "open=true");
+
+ if(last == 'H' && c == 'L')
+ log_data("bedroom_door", "open=false");
+ }
+
+ last = c;
+ }
+
+ return 0;
+}