receiver.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5. #include <sys/time.h>
  6. #include "freertos/FreeRTOS.h"
  7. #include "freertos/task.h"
  8. #include "freertos/queue.h"
  9. #include "driver/gpio.h"
  10. #include "esp_sleep.h"
  11. #include "esp_log.h"
  12. #include "driver/uart.h"
  13. #include "driver/rtc_io.h"
  14. #include "esp_intr_alloc.h"
  15. #include "esp_attr.h"
  16. #include "driver/timer.h"
  17. #include <stddef.h>
  18. #include "esp_intr_alloc.h"
  19. #include "receiver.h"
  20. #include "rxTimer.h"
  21. #include "toshiba_ir.h"
  22. #include "wifi.h"
  23. #include "mqtt.h"
  24. #ifdef IR_RECEIVER
  25. uint32_t dataArr[1000];
  26. uint32_t dataArrIdx = 0;
  27. QueueHandle_t toshibaTxQueue = NULL;
  28. void receiverTask(void *pvParameter)
  29. {
  30. ESP_LOGI("RX", "Receiver task starting.");
  31. uint32_t width;
  32. uint8_t data[kToshibaNumberOfBytes];
  33. while (1) {
  34. while( xQueueReceive( toshibaTxQueue, data, 100 ) == pdTRUE ) {
  35. ESP_LOGI("TOSHIBA","Received a TX from MQTT");
  36. sendToshibaIRCode(data);
  37. }
  38. while( xQueueReceive( rxQueue, &width, 100 ) == pdTRUE ) {
  39. //ESP_LOGI("D", "%u", width);
  40. // Receive the Toshiba IR
  41. union ToshibaProtocolU* data = (union ToshibaProtocolU*)nextPulseToshiba_ir(width);
  42. if( data != NULL ) {
  43. char mqtt_s[50];
  44. sprintf(mqtt_s,"%02X%02X%02X%02X%02X%02X%02X%02X%02X",data->raw[0],data->raw[1],data->raw[2],data->raw[3],data->raw[4],data->raw[5],data->raw[6],data->raw[7],data->raw[8]);
  45. ESP_LOGI("TOSHIBA","MQTT: %s", mqtt_s);
  46. #ifdef MQTT_ENABLED
  47. sendMQTTMessage("hall/ac/ir_cmd_rx", mqtt_s);
  48. #endif
  49. ESP_LOGI("TOSHIBA", "Data: %02X %02X %02X %02X %02X %02X %02X %02X %02X",data->raw[0],data->raw[1],data->raw[2],data->raw[3],data->raw[4],data->raw[5],data->raw[6],data->raw[7],data->raw[8]);
  50. const uint8_t fan = (data->data.Fan > 0) ? data->data.Fan-1 : data->data.Fan;
  51. const uint8_t power = (data->data.Mode == 3) ? 1 : 0;
  52. ESP_LOGI("TOSHIBA","Mode:%u Temp:%u Fan:%u",power,data->data.Temp+17u,fan);
  53. #ifdef WIFI_ENABLED
  54. //sendUDPMessage(dataStr, true);
  55. #endif
  56. }
  57. }
  58. }
  59. vTaskDelete(NULL);
  60. }
  61. void initReceiver() {
  62. gpio_pad_select_gpio(GPIO_IR_RX_DATA);
  63. gpio_set_direction(GPIO_IR_RX_DATA, GPIO_MODE_INPUT);
  64. gpio_pad_select_gpio(GPIO_IR_TX_DATA);
  65. gpio_set_direction(GPIO_IR_TX_DATA, GPIO_MODE_OUTPUT);
  66. gpio_set_level(GPIO_IR_TX_DATA, 0);
  67. Toshiba_ir_ResetDecoder();
  68. toshibaTxQueue = xQueueCreate( 5, kToshibaNumberOfBytes );
  69. ESP_LOGI("RX", "Receiver has been initialized.");
  70. xTaskCreatePinnedToCore(&receiverTask, "receiverTask", 8192, NULL, tskIDLE_PRIORITY + 5, NULL, 0);
  71. }
  72. #endif