send.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #include "send.h"
  2. #include "mqtt.h"
  3. #include "esp_log.h"
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include "freertos/FreeRTOS.h"
  7. #include "freertos/task.h"
  8. #include "freertos/queue.h"
  9. #include "config.h"
  10. #ifdef ENABLE_SEND
  11. #define QUEUE_SIZE 3
  12. static QueueHandle_t dataQueue = NULL;
  13. static bool queueReady = false;
  14. void sendTask(void *pvParameters) {
  15. uint8_t aliveCnt = 0;
  16. DisplayData data;
  17. char txt[50];
  18. ESP_LOGI("DISPnSND", "SendTask starting. Core:%d",xPortGetCoreID());
  19. // Create the data input queue, now that all is setup
  20. dataQueue = xQueueCreate( QUEUE_SIZE, sizeof( DisplayData ) );
  21. if(dataQueue == NULL) {
  22. ESP_LOGE("DISPnSND","Error creating the queue");
  23. }
  24. ESP_LOGI("DISPnSND", "Data input queue ready.");
  25. queueReady = true;
  26. while( 1 ) {
  27. if( xQueueReceive(dataQueue,&data, 1000 / portTICK_PERIOD_MS) == pdTRUE ) {
  28. switch( data.type ) {
  29. case type_VPP: sprintf(txt," %d",data.iData);
  30. sprintf(txt,"%d",data.iData);
  31. sendMQTTMessage("homeEnergyMeter/waterHeater/totalEnergy", txt);
  32. aliveCnt++;
  33. break;
  34. case type_TempA:
  35. sprintf(txt,"%.2f",data.dData);
  36. sendMQTTMessage("homeEnergyMeter/internal/temp", txt);
  37. break;
  38. case type_TempB:
  39. sprintf(txt,"%.2f",data.dData);
  40. sendMQTTMessage("homeEnergyMeter/waterHeater/temp", txt);
  41. break;
  42. case type_kWh:
  43. sprintf(txt,"%.5f",data.dData);
  44. sendMQTTMessage("homeEnergyMeter/electricityMeter/kWh", txt);
  45. sprintf(txt,"%d",data.iData);
  46. sendMQTTMessage("homeEnergyMeter/electricityMeter/pulses", txt);
  47. break;
  48. default:
  49. break;
  50. }
  51. }
  52. }
  53. vTaskDelete(NULL);
  54. }
  55. void addDataToQueue(int data_type, double dData_in, int iData_in) {
  56. DisplayData d;
  57. d.type = data_type;
  58. d.dData = dData_in;
  59. d.iData = iData_in;
  60. if( queueReady == true && dataQueue != NULL ) {
  61. if( xQueueSend(dataQueue, &d, 0) != pdPASS ) {
  62. ESP_LOGE("DISPnSND","Queue full");
  63. }
  64. }
  65. else {
  66. ESP_LOGE("DISPnSND","Queue not ready.");
  67. }
  68. }
  69. void initSend() {
  70. xTaskCreate(sendTask, "Send-Task", 1024*10, NULL, 2, NULL);
  71. }
  72. #endif