Browse Source

Split sensor data and NEXT to two UDP Ports.

Thomas Chef 4 years ago
parent
commit
fdd215912b
7 changed files with 130 additions and 40 deletions
  1. 12 1
      main/Kconfig.projbuild
  2. 10 7
      main/Sensors/nexa.c
  3. 2 0
      main/Sensors/nexa.h
  4. 7 0
      main/nexaTransmit.c
  5. 11 10
      main/receiver.c
  6. 84 20
      main/udp_client.c
  7. 4 2
      main/udp_client.h

+ 12 - 1
main/Kconfig.projbuild

@@ -22,10 +22,21 @@ menu "WIFI_433_Transceiver Application Configuration"
         help
             Units ID. First transceiver is 1, the second one is 2 etc. Translates to 'B', 'C' etc.
 
+    config NEXT_FORWARD_IP_ADDRESS
+        string "Which IP to forward NEXA Transmits to (next transceiver)."
+        default "192.168.1.222"
+
+
     config UDP_PORT_NO
-        int "UDP Port number"
+        int "UDP Port number for NEXA data"
         default 53000
         help
             The UDP Port number to listen on.
+    
+    config UDP_PORT_NO_FOR_SENSOR_DATA
+        int "UDP Port number for sensor data"
+        default 53100
+        help
+            The UDP Port number to send sensor data to.
 
 endmenu

+ 10 - 7
main/Sensors/nexa.c

@@ -11,8 +11,8 @@ typedef struct {
     uint32_t total;
 } debug_data_t;
 
-static debug_data_t debug_data = { 0,99999,0,0 };
-static debug_data_t debug_data_tot = { 0,99999,0,0 };
+//static debug_data_t debug_data = { 0,99999,0,0 };
+//static debug_data_t debug_data_tot = { 0,99999,0,0 };
 
 #define DEBUG_WIDTH_FUNC() { \
                     if( width > debug_data.max ) debug_data.max = width; \
@@ -38,14 +38,16 @@ static uint64_t sensor_data;
 static uint32_t rx_numBits;
 static uint8_t x_2ndbit;
 
+uint32_t startPulseWidth = 0;
+
 void NEXA_resetDecoder () {
 	sensor_data = 0;
 	rx_numBits = 0;
 	x_2ndbit = false;
 	rx_state = UNKNOWN;
 
-    debug_data.max = 0;
-    debug_data.min = 99999;
+    //debug_data.max = 0;
+    //debug_data.min = 99999;
 }
 
 static void addBit (uint8_t value) {
@@ -83,6 +85,7 @@ static int rx_decode(uint32_t width) {
         case UNKNOWN: // Start of frame
             if ( 2240 <= width && width <= 3540 ) { // Hallway-button: 2760
                 //DEBUG_WIDTH_FUNC();
+                startPulseWidth = width;
 
                 rx_state = T0;
             }
@@ -156,9 +159,9 @@ int64_t nextPulseNEXA(uint32_t width)
     }
     if (rx_state == DONE) {
         now = millis();
-        if( debug_data.max > debug_data_tot.max ) debug_data_tot.max = debug_data.max;
-        if( debug_data.min < debug_data_tot.min ) debug_data_tot.min = debug_data.min;
-        printf("NEXA Debug min,max:%u %u\n",debug_data_tot.min,debug_data_tot.max);
+        //if( debug_data.max > debug_data_tot.max ) debug_data_tot.max = debug_data.max;
+        //if( debug_data.min < debug_data_tot.min ) debug_data_tot.min = debug_data.min;
+        //printf("NEXA Debug min,max:%u %u\n",debug_data_tot.min,debug_data_tot.max);
 
         if( sensor_data != previous_data || (now > (old_time+1000)) ) {
             previous_data = sensor_data;

+ 2 - 0
main/Sensors/nexa.h

@@ -8,4 +8,6 @@ void NEXA_resetDecoder ();
 
 int64_t nextPulseNEXA(uint32_t width);
 
+extern uint32_t startPulseWidth;
+
 #endif

+ 7 - 0
main/nexaTransmit.c

@@ -18,6 +18,7 @@
 #include "transceiver.h"
 #include "rxTimer.h"
 #include "led.h"
+#include "udp_client.h"
 
 QueueHandle_t nexaTxQueue = NULL;
 
@@ -125,6 +126,12 @@ static void nexaTxTask(void *pvParameter)
     {
         while( xQueueReceive( nexaTxQueue, &data, portMAX_DELAY ) == pdTRUE ) {
             sendNexaCodeNo(data);
+
+            // Forward the NEXA code to the next transceiver
+            char strCode[20];
+            sprintf(strCode,"<NT%08lX>",(unsigned long int)data);
+            forwardNEXAMessage(strCode);
+
             vTaskDelay(200 / portTICK_PERIOD_MS);   // Wait a while between transmits
         }
     }

+ 11 - 10
main/receiver.c

@@ -61,9 +61,9 @@ void receiverTask(void *pvParameter)
 
                 ESP_LOGI("RX", "ClasO:  <TR%08llX> %u               %4.1f",data,id, (float)value/10);
 
-                sprintf(dataStr,"<TR%08llX>\n",data);
+                sprintf(dataStr,"<TR%08llX>",data);
 #ifdef WIFI_ENABLED
-                sendUDPMessage(dataStr);
+                sendUDPMessage(dataStr, true);
 #endif
             }
 
@@ -76,9 +76,9 @@ void receiverTask(void *pvParameter)
 
                 ESP_LOGI("RX", "Proove: <Tr%016llX> ID:%d  %4.1f",data,id,(float)value/10);
 
-                sprintf(dataStr,"<Tr%016llX>\n",data);
+                sprintf(dataStr,"<Tr%016llX>",data);
 #ifdef WIFI_ENABLED
-                sendUDPMessage(dataStr);
+                sendUDPMessage(dataStr, true);
 #endif
             }
 
@@ -88,9 +88,10 @@ void receiverTask(void *pvParameter)
 
                 ESP_LOGI("RX", "NEXA: <NR%08llX>",data);
 
-                sprintf(dataStr,"<NR%08llX>\n",data);
+                sprintf(dataStr,"<NR%08llX>%u",data,startPulseWidth);
+
 #ifdef WIFI_ENABLED
-                sendUDPMessage(dataStr);
+                sendUDPMessage(dataStr, false);
 #endif
             }
 
@@ -127,8 +128,8 @@ void receiverTask(void *pvParameter)
                 ESP_LOGI("RX", "OREGON: <RR%s> %6.4f cm2", ch_data, total );
 
 #ifdef WIFI_ENABLED
-                sprintf(dataStr,"<RR%s>\n",ch_data);
-                sendUDPMessage(dataStr);
+                sprintf(dataStr,"<RR%s>",ch_data);
+                sendUDPMessage(dataStr, true);
 #endif
 
                 Oregon_ResetDecoder();
@@ -145,9 +146,9 @@ void receiverTask(void *pvParameter)
 
                 ESP_LOGI("RX", "ESIC:   <TE%08llX> %u               %4.1f",data,id, (float)value/10);
 
-                sprintf(dataStr,"<TE%08llX>\n",data);
+                sprintf(dataStr,"<TE%08llX>",data);
 #ifdef WIFI_ENABLED
-                sendUDPMessage(dataStr);
+                sendUDPMessage(dataStr, true);
 #endif
             }
         }

+ 84 - 20
main/udp_client.c

@@ -28,32 +28,49 @@
 
 #ifdef WIFI_ENABLED
 
-#define HOST_IP_ADDR CONFIG_BACK_END_SERVER_IP_ADDRESS
-#define PORT CONFIG_UDP_PORT_NO
-
 static QueueHandle_t udpTxQueue = NULL; // Queue for transmitting UDP-Data to back-end server
 
-static void setupConnection(int *socket_fd, struct sockaddr_in *backEndAddress) {
+static QueueHandle_t udpForwardQueue = NULL; // Queue for forwarding NEXA-UDP-Data to next transceiver
+
+static int socket_fd;
+static struct sockaddr_in backEndAddress;               // Address to back-end server
+static struct sockaddr_in backEndAddressSensorData;     // Address to back-end server (sensor)
+static struct sockaddr_in forwardAddress;               // Address to next transceiver for NEXA-code-forwarding
+
+static void setupConnection() {
 
     struct sockaddr_in recvFromAddress;
 
-    closesocket(*socket_fd);
+    closesocket(socket_fd);
+
+    // This Address is used for sending to the back-end server
+    backEndAddress.sin_addr.s_addr = inet_addr(CONFIG_BACK_END_SERVER_IP_ADDRESS);
+    backEndAddress.sin_family = AF_INET;
+    backEndAddress.sin_port = htons(CONFIG_UDP_PORT_NO);
 
-    backEndAddress->sin_addr.s_addr = inet_addr(HOST_IP_ADDR);
-    backEndAddress->sin_family = AF_INET;
-    backEndAddress->sin_port = htons(PORT);
+    // This Address is used for sending to the back-end server for sensor-data (different port)
+    backEndAddressSensorData.sin_addr.s_addr = inet_addr(CONFIG_BACK_END_SERVER_IP_ADDRESS);
+    backEndAddressSensorData.sin_family = AF_INET;
+    backEndAddressSensorData.sin_port = htons(CONFIG_UDP_PORT_NO_FOR_SENSOR_DATA);
+
+    // This Address is used for forwaring NEXA codes to next transceiver
+    forwardAddress.sin_addr.s_addr = inet_addr(CONFIG_NEXT_FORWARD_IP_ADDRESS);
+    forwardAddress.sin_family = AF_INET;
+    forwardAddress.sin_port = htons(CONFIG_UDP_PORT_NO);
 
     // Wait for WiFi to be connected first
     while( commIsUpAndRunning == 0 ) vTaskDelay(500 / portTICK_PERIOD_MS);
 
-    *socket_fd = socket(AF_INET, SOCK_DGRAM, 0);
-    if (*socket_fd < 0) ESP_LOGE("UDP","Socket error");
+    socket_fd = socket(AF_INET, SOCK_DGRAM, 0);
+    if (socket_fd < 0) ESP_LOGE("UDP","Socket error");
 
     // Bind our server socket to a port.
-    recvFromAddress.sin_family = AF_INET;
+    // We listen on this port from ANY other host. It could be the 
+    // back-end server, or it could be another Transceiver that forwards
     recvFromAddress.sin_addr.s_addr = htonl(INADDR_ANY);
-    recvFromAddress.sin_port = htons(PORT);
-    if( (bind(*socket_fd, (const struct sockaddr *)&recvFromAddress, sizeof(struct sockaddr_in))) == -1 )
+    recvFromAddress.sin_family = AF_INET;
+    recvFromAddress.sin_port = htons(CONFIG_UDP_PORT_NO);
+    if( (bind(socket_fd, (const struct sockaddr *)&recvFromAddress, sizeof(struct sockaddr_in))) == -1 )
         ESP_LOGE("UDP","Bind error");
     else
         ESP_LOGI("UDP","UDP Rx Bind.");
@@ -62,12 +79,10 @@ static void setupConnection(int *socket_fd, struct sockaddr_in *backEndAddress)
 
 void udp_client_task(void *pvParameter)
 {
-    struct sockaddr_in backEndAddress;
     int recv_len;
     char dataBuff[UDP_QUEUE_OBJ_LENGTH];
-    int socket_fd;
 
-    setupConnection(&socket_fd, &backEndAddress);
+    setupConnection();
 
     while(1)
     {
@@ -87,8 +102,27 @@ void udp_client_task(void *pvParameter)
         // Transmit
         while( xQueueReceive( udpTxQueue, &dataBuff, 0 ) == pdTRUE ) {
 
-            //ESP_LOGI("UDP","Send data:%s",dataBuff);
-            sendto(socket_fd, dataBuff, strlen(dataBuff), 0, (struct sockaddr *)&backEndAddress, sizeof(backEndAddress));
+            // Last byte contains type of data
+            const uint8_t dataType = dataBuff[UDP_QUEUE_OBJ_LENGTH-1];
+
+            ESP_LOGI("UDP","Send data:%s  Type:%u",dataBuff,dataType);
+
+            if( dataType == 0 ) {
+                sendto(socket_fd, dataBuff, strlen(dataBuff), 0, (struct sockaddr *)&backEndAddress, sizeof(backEndAddress));
+            }
+            else if( dataType == 1 ) {
+                sendto(socket_fd, dataBuff, strlen(dataBuff), 0, (struct sockaddr *)&backEndAddressSensorData, sizeof(backEndAddressSensorData));
+            }
+            else {
+                ESP_LOGE("UDP","Wrong dataType:%u",dataType);
+            }
+        }
+
+        // Forward NEXT Codes to next transceiver
+        while( xQueueReceive( udpForwardQueue, &dataBuff, 0 ) == pdTRUE ) {
+
+            ESP_LOGI("UDP","Forward NEXA-data:%s",dataBuff);
+            sendto(socket_fd, dataBuff, strlen(dataBuff), 0, (struct sockaddr *)&forwardAddress, sizeof(forwardAddress));
         }
 
         vTaskDelay(50 / portTICK_RATE_MS);
@@ -99,11 +133,39 @@ void udp_client_task(void *pvParameter)
 
 // Public interface for sending a UDP Message to the back-end server.
 // Argument #1 must be UDP_QUEUE_OBJ_LENGTH chars in length.
-void sendUDPMessage(char *p) {
+// In the last data byte transfered, the type of data is specified
+void sendUDPMessage(char *p, bool sensorData) {
+
+    char data[UDP_QUEUE_OBJ_LENGTH];
+    
 
-  if( udpTxQueue != NULL ) xQueueSend( udpTxQueue, p, 0 );
+    data[0] = '<';
+    data[1] = CONFIG_UNIT_ID + 'A'; // Source
+    data[2] = 'A';                  // Destination
+
+    if( p[0] == '<') {
+
+        strncpy(data+3, p+1, UDP_QUEUE_OBJ_LENGTH-4);
+
+        if( strchr(data,'>') != NULL ) {
+
+            if( sensorData == true )
+                data[UDP_QUEUE_OBJ_LENGTH-1] = 1;   // Sensor data
+            else
+                data[UDP_QUEUE_OBJ_LENGTH-1] = 0;   // NEXA
+
+            if( udpTxQueue != NULL ) xQueueSend( udpTxQueue, data, 0 );
+        }
+    }
+}
+
+// Public interface for forwaring a NEXA code to the next transceiver.
+void forwardNEXAMessage(char *p) {
+
+  if( udpForwardQueue != NULL ) xQueueSend( udpForwardQueue, p, 0 );
 }
 
+
 void udpClientInit()
 {
     ESP_ERROR_CHECK(esp_netif_init());
@@ -111,6 +173,8 @@ void udpClientInit()
 
     udpTxQueue = xQueueCreate( 5, UDP_QUEUE_OBJ_LENGTH );
 
+    udpForwardQueue = xQueueCreate( 5, UDP_QUEUE_OBJ_LENGTH );
+
     xTaskCreate(udp_client_task, "udp_client", 4096, NULL, 5, NULL);
 }
 

+ 4 - 2
main/udp_client.h

@@ -1,10 +1,12 @@
 #ifndef __UDP_CLIENT__
 #define __UDP_CLIENT__
 
-#define UDP_QUEUE_OBJ_LENGTH 30
+#define UDP_QUEUE_OBJ_LENGTH 40
 
 void udpClientInit();
 
-void sendUDPMessage(char *p);
+void sendUDPMessage(char *p, bool sensorData);
+
+void forwardNEXAMessage(char *p);
 
 #endif