Browse Source

Backup Commit

Signed-off-by: Benjamin <benjamin@ewft.org>
Benjamin 5 years ago
parent
commit
f6305cae1b
6 changed files with 413 additions and 23 deletions
  1. 6 2
      Makefile
  2. 323 0
      balaye.c
  3. 4 1
      emit.c
  4. 30 10
      mqtt.c
  5. 24 10
      portail.c
  6. 26 0
      scan.c

+ 6 - 2
Makefile

@@ -1,12 +1,16 @@
 all : emit listen mqtt
 
-mqtt : mqtt.o buffer.o home_easy.o utils.o
+mqtt : mqtt.o buffer.o home_easy.o utils.o portail.o
 	gcc -o $@ $^ -lwiringPi -lpaho-mqtt3cs
 
+scan : scan.o buffer.o home_easy.o utils.o portail.o
+	gcc -o $@ $^ -lwiringPi 
+
+
 listen : listen.o buffer.o home_easy.o utils.o analyze.o
 	gcc -o $@ $^ -lwiringPi
 
-emit : emit.o buffer.o home_easy.o utils.o
+emit : emit.o buffer.o home_easy.o utils.o portail.o
 	gcc -o $@ $^ -lwiringPi
 
 listen.o : listen.c

+ 323 - 0
balaye.c

@@ -0,0 +1,323 @@
+/*******************************************************************************
+ * Copyright (c) 2012, 2013 IBM Corp.
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * and Eclipse Distribution License v1.0 which accompany this distribution.
+ *
+ * The Eclipse Public License is available at
+ *   http://www.eclipse.org/legal/epl-v10.html
+ * and the Eclipse Distribution License is available at
+ *   http://www.eclipse.org/org/documents/edl-v10.php.
+ *
+ * Contributors:
+ *    Ian Craggs - initial contribution
+ *    Ian Craggs - change delimiter option from char to string
+ *    Guilherme Maciel Ferreira - add keep alive option
+ *******************************************************************************/
+
+/*
+
+ stdout subscriber
+
+ compulsory parameters:
+
+  --topic topic to subscribe to
+
+ defaulted parameters:
+
+	--host localhost
+	--port 1883
+	--qos 2
+	--delimiter \n
+	--clientid stdout-subscriber
+	--showtopics off
+	--keepalive 10
+
+	--userid none
+	--password none
+
+*/
+#include "MQTTClient.h"
+#include "MQTTClientPersistence.h"
+
+#include <stdio.h>
+#include <signal.h>
+#include <string.h>
+#include <stdlib.h>
+#include "home_easy.h"
+
+
+
+#if defined(WIN32)
+#define sleep Sleep
+#else
+#include <sys/time.h>
+#endif
+
+
+
+unsigned int id = 0x01379F0E;
+
+
+
+volatile int toStop = 0;
+
+typedef struct Item {
+	    char * topic;
+	    unsigned long int id;
+	    char section;
+	    char channel;
+	    char type;
+} Item;
+
+
+struct opts_struct
+{
+	char* clientid;
+	int nodelimiter;
+	char* delimiter;
+	int qos;
+	char* username;
+	char* password;
+	char* host;
+	char* port;
+	int showtopics;
+	int keepalive;
+} opts =
+{
+	"stdout-subscriber", 0, "\n", 2, NULL, NULL, "localhost", "1883", 0, 10
+};
+
+
+void usage(void)
+{
+	printf("MQTT stdout subscriber\n");
+	printf("Usage: stdoutsub topicname <options>, where options are:\n");
+	printf("  --host <hostname> (default is %s)\n", opts.host);
+	printf("  --port <port> (default is %s)\n", opts.port);
+	printf("  --qos <qos> (default is %d)\n", opts.qos);
+	printf("  --delimiter <delim> (default is \\n)\n");
+	printf("  --clientid <clientid> (default is %s)\n", opts.clientid);
+	printf("  --username none\n");
+	printf("  --password none\n");
+	printf("  --showtopics <on or off> (default is on if the topic has a wildcard, else off)\n");
+	printf("  --keepalive <seconds> (default is %d seconds)\n", opts.keepalive);
+	exit(EXIT_FAILURE);
+}
+
+
+void myconnect(MQTTClient* client, MQTTClient_connectOptions* opts)
+{
+	int rc = 0;
+	if ((rc = MQTTClient_connect(*client, opts)) != 0)
+	{
+		printf("Failed to connect, return code %d\n", rc);
+		exit(EXIT_FAILURE);
+	}
+}
+
+
+void cfinish(int sig)
+{
+	signal(SIGINT, NULL);
+	toStop = 1;
+}
+
+void getopts(int argc, char** argv);
+
+int main(int argc, char** argv)
+{
+	MQTTClient client;
+	MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
+	
+	
+	Item lampH = { "chacon/maison/salon/lampehaute", 0x01379F0E, 'A', 1, 0};
+ 	Item videoP = { "chacon/maison/salon/videoproj", 0x00DE29E6, 'A', 2, 0};
+	Item tardis = { "chacon/maison/salon/tardis", 0x01379F0E, 'A', 3, 0};
+	Item lampC = { "chacon/maison/chambre/lampe", 0x00DE29E6, 'A', 1, 0};
+	Item lampP = { "chacon/maison/salon/lampe", 0x1379F0E, 'A', 2, 0};
+	Item portail = { "chacon/maison/portail", 0, 'B', 0, 1};
+	Item garage = { "chacon/maison/garage", 0, 'A', 0, 1};
+
+
+
+	
+	//Item lampH = { "chacon/maison/salon/lampehaute", 0x01379F0E, 'A', 1};
+
+
+	Item * listItem[10];
+	listItem[0] = &lampH;
+	listItem[1] = &videoP;
+	listItem[2] = &tardis;
+	listItem[3] = &lampC;
+	listItem[4] = &lampP;
+	listItem[5] = &portail;
+	listItem[6] = &garage;
+
+
+	char* topic = "chacon/#";
+	int rc = 0;
+	char url[100];
+
+
+	opts.showtopics = 1;
+	if (opts.showtopics)
+		printf("topic is %s\n", topic);
+
+	getopts(argc, argv);
+	sprintf(url, "%s:%s", opts.host, opts.port);
+
+	rc = MQTTClient_create(&client, url, opts.clientid, MQTTCLIENT_PERSISTENCE_NONE, NULL);
+
+	signal(SIGINT, cfinish);
+	signal(SIGTERM, cfinish);
+
+	conn_opts.keepAliveInterval = opts.keepalive;
+	conn_opts.reliable = 0;
+	conn_opts.cleansession = 1;
+//	conn_opts.username = opts.username;
+//	conn_opts.password = opts.password;
+
+	myconnect(&client, &conn_opts);
+
+	rc = MQTTClient_subscribe(client, topic, opts.qos);
+
+	while (!toStop)
+	{
+		char* topicName = NULL;
+		int topicLen;
+		MQTTClient_message* message = NULL;
+		rc = MQTTClient_receive(client, &topicName, &topicLen, &message, 1000);
+		if (message)
+		{
+			if (opts.showtopics)
+				printf("%s\t", topicName);
+			
+			for(int i=0;i < 7; i++){
+				if(strcmp(listItem[i]->topic, topicName) == 0){
+					
+					initIO();
+					if(listItem[i]->type == 0){
+						((char*)message->payload)[message->payloadlen] = 0;
+						char onoff = strcmp(message->payload,"1") ? 0 : 1;
+						printf("payload  %s %d\n", message->payload, message->payloadlen);
+						printf("%s %x %c %d %d \n",listItem[i]->topic,listItem[i]->id,listItem[i]->section,listItem[i]->channel, onoff);
+						sendHomeEasyCommand(listItem[i]->id,listItem[i]->section,listItem[i]->channel, onoff,10);
+					}else{
+						printf("sendPortail\n");
+						sendPortailCommand(0,((char*)message->payload)[0],100);
+						printf("Command Sent\n");
+					}
+				}
+			}
+			if (opts.nodelimiter)
+				printf("%.*s", message->payloadlen, (char*)message->payload);
+			else
+				printf("%.*s%s", message->payloadlen, (char*)message->payload, opts.delimiter);
+			fflush(stdout);
+			MQTTClient_freeMessage(&message);
+			MQTTClient_free(topicName);
+		}
+		if (rc != 0)
+			myconnect(&client, &conn_opts);
+	}
+
+	printf("Stopping\n");
+
+	MQTTClient_disconnect(client, 0);
+
+	MQTTClient_destroy(&client);
+
+	return EXIT_SUCCESS;
+}
+
+void getopts(int argc, char** argv)
+{
+	int count = 2;
+
+	while (count < argc)
+	{
+		if (strcmp(argv[count], "--qos") == 0)
+		{
+			if (++count < argc)
+			{
+				if (strcmp(argv[count], "0") == 0)
+					opts.qos = 0;
+				else if (strcmp(argv[count], "1") == 0)
+					opts.qos = 1;
+				else if (strcmp(argv[count], "2") == 0)
+					opts.qos = 2;
+				else
+					usage();
+			}
+			else
+				usage();
+		}
+		else if (strcmp(argv[count], "--host") == 0)
+		{
+			if (++count < argc)
+				opts.host = argv[count];
+			else
+				usage();
+		}
+		else if (strcmp(argv[count], "--port") == 0)
+		{
+			if (++count < argc)
+				opts.port = argv[count];
+			else
+				usage();
+		}
+		else if (strcmp(argv[count], "--clientid") == 0)
+		{
+			if (++count < argc)
+				opts.clientid = argv[count];
+			else
+				usage();
+		}
+		else if (strcmp(argv[count], "--username") == 0)
+		{
+			if (++count < argc)
+				opts.username = argv[count];
+			else
+				usage();
+		}
+		else if (strcmp(argv[count], "--password") == 0)
+		{
+			if (++count < argc)
+				opts.password = argv[count];
+			else
+				usage();
+		}
+		else if (strcmp(argv[count], "--delimiter") == 0)
+		{
+			if (++count < argc)
+				opts.delimiter = argv[count];
+			else
+				opts.nodelimiter = 1;
+		}
+		else if (strcmp(argv[count], "--showtopics") == 0)
+		{
+			if (++count < argc)
+			{
+				if (strcmp(argv[count], "on") == 0)
+					opts.showtopics = 1;
+				else if (strcmp(argv[count], "off") == 0)
+					opts.showtopics = 0;
+				else
+					usage();
+			}
+			else
+				usage();
+		}
+		else if (strcmp(argv[count], "--keepalive") == 0)
+		{
+			if (++count < argc)
+				opts.keepalive = atoi(argv[count]);
+			else
+				usage();
+		}
+		count++;
+	}
+
+}

+ 4 - 1
emit.c

@@ -46,7 +46,7 @@ int main(int argc, char** argv)
     unsigned int verbose = 0;
     unsigned repeat = 5;
 
-
+#if 0
     /* reading options */
     //opterr=0; /* Pas de message d'erreur automatique */
     while ((option = getopt(argc, argv, optstring)) != -1) {
@@ -104,6 +104,9 @@ int main(int argc, char** argv)
     // Send the data
     initIO();
     sendHomeEasyCommand(id, section, number, onOff, repeat);
+#endif
 
+        initIO();
+        sendPortailCommand(0,0,200);	
     return 0;
 }

+ 30 - 10
mqtt.c

@@ -65,9 +65,10 @@ volatile int toStop = 0;
 
 typedef struct Item {
 	    char * topic;
-	    unsigned int id;
+	    unsigned long int id;
 	    char section;
 	    char channel;
+	    char type;
 } Item;
 
 
@@ -131,18 +132,29 @@ int main(int argc, char** argv)
 	MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
 	
 	
-	Item lampH = { "chacon/maison/salon/lampehaute", 0x01379F0E, 'A', 1};
- 	
-	Item videoP = { "chacon/maison/salon/videoproj", 0x01379F0E, 'A', 2};
-	Item tardis = { "chacon/maison/salon/tardis", 0x01379F0E, 'A', 3};
-	Item lampC = { "chacon/maison/chambre/lampe", 0x00DE29E6, 'A', 1};
+	Item lampH = { "chacon/maison/salon/lampehaute", 0x01379F0E, 'A', 1, 0};
+ 	Item videoP = { "chacon/maison/salon/videoproj", 0x00DE29E6, 'A', 2, 0};
+	Item tardis = { "chacon/maison/salon/tardis", 0x01379F0E, 'A', 3, 0};
+	Item lampC = { "chacon/maison/chambre/lampe", 0x00DE29E6, 'A', 1, 0};
+	Item lampP = { "chacon/maison/salon/lampe", 0x1379F0E, 'A', 2, 0};
+	Item portail = { "chacon/maison/portail", 0, 'B', 0, 1};
+	Item garage = { "chacon/maison/garage", 0, 'A', 0, 1};
+
+
+
+	
 	//Item lampH = { "chacon/maison/salon/lampehaute", 0x01379F0E, 'A', 1};
 
+
 	Item * listItem[10];
 	listItem[0] = &lampH;
 	listItem[1] = &videoP;
 	listItem[2] = &tardis;
 	listItem[3] = &lampC;
+	listItem[4] = &lampP;
+	listItem[5] = &portail;
+	listItem[6] = &garage;
+
 
 	char* topic = "chacon/#";
 	int rc = 0;
@@ -176,19 +188,27 @@ int main(int argc, char** argv)
 		char* topicName = NULL;
 		int topicLen;
 		MQTTClient_message* message = NULL;
-
 		rc = MQTTClient_receive(client, &topicName, &topicLen, &message, 1000);
 		if (message)
 		{
 			if (opts.showtopics)
 				printf("%s\t", topicName);
 			
-			for(int i=0;i < 4; i++){
+			for(int i=0;i < 7; i++){
 				if(strcmp(listItem[i]->topic, topicName) == 0){
 					
 					initIO();
-					char onoff = strcmp(message->payload,"1") ? 0 : 1;
-					sendHomeEasyCommand(listItem[i]->id,listItem[i]->section,listItem[i]->channel, onoff,10);
+					if(listItem[i]->type == 0){
+						((char*)message->payload)[message->payloadlen] = 0;
+						char onoff = strcmp(message->payload,"1") ? 0 : 1;
+						printf("payload  %s %d\n", message->payload, message->payloadlen);
+						printf("%s %x %c %d %d \n",listItem[i]->topic,listItem[i]->id,listItem[i]->section,listItem[i]->channel, onoff);
+						sendHomeEasyCommand(listItem[i]->id,listItem[i]->section,listItem[i]->channel, onoff,10);
+					}else{
+						printf("sendPortail\n");
+						sendPortailCommand(0,((char*)message->payload)[0],100);
+						printf("Command Sent\n");
+					}
 				}
 			}
 			if (opts.nodelimiter)

+ 24 - 10
portail.c

@@ -22,9 +22,9 @@
 #include "utils.h"
 
 unsigned int portail_timings[3][2] = {
-    {1140, 375},  //  bit 0
-    {400, 1140}, //  bit 1
-    {790, 790},  //  start of data
+    {1140, 380},  //  bit 0
+    {380, 1140}, //  bit 1
+    {380, 790},  //  start of data
 };
 
 extern unsigned char homeEasyPinOut;
@@ -44,10 +44,22 @@ unsigned int size = 0;
  */
 void createPortailCommand(unsigned long int id, char section)
 {
+	int btn = 0;
+	if(section == 'B'){
+		btn = 0x7B;
+	}else if(section == 'A'){
+		btn = 0x7D;
+	}else if(section == 'C'){
+		btn = 0x79;
+	}else if(section == 'D'){
+		btn = 0x7F;
+	}
 
-	frame[0]=0xB6;
+
+	printf(" section :%c  btn : %x  id: %x \n",section,btn,id);
+	frame[0] = id;
 	frame[1]=0xF7;
-	frame[2]=0x7B;
+	frame[2]=btn;
 	frame[3]=0xA1;
 	frame[4]=0x00;
 	size = 36;
@@ -71,6 +83,7 @@ void sendPortailFrame(unsigned int repeat)
     	sendPortailBit(START_OF_DATA);
         sendPortailBytes();
         sendPortailBit(BIT0);
+	delayMicroseconds(53000);
     }
     digitalWrite(homeEasyPinOut, LOW);
     // Exit real time mode
@@ -107,6 +120,7 @@ void sendPortailBit(unsigned char bit)
     delayMicroseconds(portail_timings[bit][0]);
     digitalWrite(homeEasyPinOut, HIGH);
     delayMicroseconds(portail_timings[bit][1]);
+    digitalWrite(homeEasyPinOut, LOW);
 }
 
 /**
@@ -117,9 +131,9 @@ void sendPortailBit(unsigned char bit)
 void sendPortailByte(unsigned char byte, unsigned int bits)
 {
     int i;
-    for(i=(0x01 << (bits-1));i>0; i>>=1) {
-    	sendPortailBit((byte & i) == i);
-    }
+    for(i=0; i < bits; i++) {
+	sendPortailBit((byte & (0x01 << i)) == (0x01 << i));
+   }
 }
 
 /**
@@ -130,10 +144,10 @@ void sendPortailByte(unsigned char byte, unsigned int bits)
 void sendPortailBytes(void)
 {
     int i;
-
+   
     for(i = 0; i < size; i+=8 ) {
     	register unsigned int bits = (size - i >= 8) ? 8: (size - i);
-        sendPortailByte(frame[i],bits);
+        sendPortailByte(frame[i/8],bits);
     }
 }
 

+ 26 - 0
scan.c

@@ -0,0 +1,26 @@
+
+
+
+#include <stdio.h>
+#include <stdlib.h>
+
+
+
+
+
+
+
+void main(void){
+int i =0;
+	initIO();
+	for(i = 0; i < 0x1; i++){
+		sendPortailCommand(0xB6,'A',60);
+		printf("%d \n",i);
+	}
+
+
+}
+
+
+
+