Código sensor para material particulado con PMS 5003

Dependencies:   DmTftLibrary

Committer:
mateojs16
Date:
Sat Nov 13 14:53:48 2021 +0000
Revision:
4:c8e5928f440c
Parent:
3:4c7554fd88a5
Sensor de material particulado con Pms 5003

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sebastianrestrepo 0:d5dc1e5564a5 1 #include "PMS5003.h"
sebastianrestrepo 0:d5dc1e5564a5 2
sebastianrestrepo 1:5bc605d2b8f8 3 //#include "DmTftHX8353C.h"
sebastianrestrepo 1:5bc605d2b8f8 4 //#include "DmTftS6D0164.h"
sebastianrestrepo 1:5bc605d2b8f8 5 //#include "DmTftIli9325.h"
sebastianrestrepo 1:5bc605d2b8f8 6 #include "DmTftIli9341.h"
sebastianrestrepo 1:5bc605d2b8f8 7 #include "DmTftSsd2119.h"
sebastianrestrepo 1:5bc605d2b8f8 8 #include "DmTftRa8875.h"
sebastianrestrepo 1:5bc605d2b8f8 9 #include "DmTouch.h"
sebastianrestrepo 1:5bc605d2b8f8 10 #include "DmTpFt6x06.h"
sebastianrestrepo 1:5bc605d2b8f8 11
sebastianrestrepo 1:5bc605d2b8f8 12 //#define log(...) printf(__VA_ARGS__)
sebastianrestrepo 1:5bc605d2b8f8 13 #define log(...)
sebastianrestrepo 1:5bc605d2b8f8 14
sebastianrestrepo 1:5bc605d2b8f8 15 /* Displays without adapter */
sebastianrestrepo 1:5bc605d2b8f8 16 #define DM_PIN_SPI_MOSI D11
sebastianrestrepo 1:5bc605d2b8f8 17 #define DM_PIN_SPI_MISO D12
sebastianrestrepo 1:5bc605d2b8f8 18 #define DM_PIN_SPI_SCK D13
sebastianrestrepo 1:5bc605d2b8f8 19 #define DM_PIN_CS_TOUCH D4
sebastianrestrepo 1:5bc605d2b8f8 20 #define DM_PIN_CS_TFT D10
sebastianrestrepo 1:5bc605d2b8f8 21 #define DM_PIN_CS_SDCARD D8
sebastianrestrepo 1:5bc605d2b8f8 22 #define DM_PIN_CS_FLASH D6
sebastianrestrepo 1:5bc605d2b8f8 23 /******************************************************************************
sebastianrestrepo 1:5bc605d2b8f8 24 * Local variables
sebastianrestrepo 1:5bc605d2b8f8 25 *****************************************************************************/
sebastianrestrepo 1:5bc605d2b8f8 26
sebastianrestrepo 1:5bc605d2b8f8 27 /********* TFT DISPLAY INIT *********/
sebastianrestrepo 1:5bc605d2b8f8 28 DmTftIli9341 tft(D10, D9, D11, D12, D13); /* DmTftIli9341(PinName cs, PinName dc, PinName mosi, PinName miso, PinName clk) DM_TFT28_105 and DM_TFT28_116*/
sebastianrestrepo 1:5bc605d2b8f8 29 DmTouch touch(DmTouch::DM_TFT28_105, D11, D12, D13);
sebastianrestrepo 1:5bc605d2b8f8 30
sebastianrestrepo 1:5bc605d2b8f8 31 DigitalInOut csTouch(DM_PIN_CS_TOUCH, PIN_OUTPUT, PullUp, 1);
sebastianrestrepo 1:5bc605d2b8f8 32 DigitalInOut csDisplay(DM_PIN_CS_TFT, PIN_OUTPUT, PullUp, 1);
sebastianrestrepo 1:5bc605d2b8f8 33 DigitalInOut csSDCard(DM_PIN_CS_SDCARD, PIN_OUTPUT, PullUp, 1);
sebastianrestrepo 1:5bc605d2b8f8 34 #ifdef DM_PIN_CS_FLASH
sebastianrestrepo 1:5bc605d2b8f8 35 DigitalInOut csFlash(DM_PIN_CS_FLASH, PIN_OUTPUT, PullUp, 1);
sebastianrestrepo 1:5bc605d2b8f8 36 #endif
sebastianrestrepo 1:5bc605d2b8f8 37 /********* SENSOR INIT *********/
sebastianrestrepo 0:d5dc1e5564a5 38 PMS5003 pm25(D1, D0, D3); // UART TX, UART RX, POWER
sebastianrestrepo 0:d5dc1e5564a5 39
sebastianrestrepo 1:5bc605d2b8f8 40 /******************************************************************************
sebastianrestrepo 1:5bc605d2b8f8 41 * Global variables
sebastianrestrepo 1:5bc605d2b8f8 42 *****************************************************************************/
sebastianrestrepo 2:e80f510b3cb2 43 uint16_t pm = 0;
sebastianrestrepo 1:5bc605d2b8f8 44
sebastianrestrepo 1:5bc605d2b8f8 45 /******************************************************************************
sebastianrestrepo 1:5bc605d2b8f8 46 * Local functions
sebastianrestrepo 1:5bc605d2b8f8 47 *****************************************************************************/
sebastianrestrepo 0:d5dc1e5564a5 48 static EventQueue queue; // The callback from the driver is triggered from IRQ, use EventQueue to debounce to normal context
sebastianrestrepo 0:d5dc1e5564a5 49
sebastianrestrepo 0:d5dc1e5564a5 50 void pm25_data_callback(pms5003_data_t data) {
sebastianrestrepo 0:d5dc1e5564a5 51 printf("---------------------------------------\n");
sebastianrestrepo 0:d5dc1e5564a5 52 printf("Concentration Units (standard)\n");
sebastianrestrepo 0:d5dc1e5564a5 53 printf("PM 1.0: %u", data.pm10_standard);
mateojs16 3:4c7554fd88a5 54 tft.drawString(70, 70, "PM 1.0:");
mateojs16 4:c8e5928f440c 55 tft.drawNumber(140, 70, data.pm10_standard, 5, false);
sebastianrestrepo 0:d5dc1e5564a5 56 printf("\t\tPM 2.5: %u", data.pm25_standard);
mateojs16 3:4c7554fd88a5 57 tft.drawString(70, 110, "PM 2.5:");
mateojs16 4:c8e5928f440c 58 tft.drawNumber(140, 110, data.pm25_standard, 5, false);
sebastianrestrepo 0:d5dc1e5564a5 59 printf("\t\tPM 10: %u\n", data.pm100_standard);
mateojs16 3:4c7554fd88a5 60 tft.drawString(70, 150, "PM 10.0:");
mateojs16 4:c8e5928f440c 61 tft.drawNumber(140, 150, data.pm100_standard, 5, false);
mateojs16 4:c8e5928f440c 62
mateojs16 4:c8e5928f440c 63 if ((data.pm25_standard>=0 && data.pm25_standard<=12) || (data.pm100_standard>=0 && data.pm100_standard<=54) ){
mateojs16 4:c8e5928f440c 64 tft.fillCircle(120, 250, 40, 0x7E0);
mateojs16 4:c8e5928f440c 65 }
mateojs16 4:c8e5928f440c 66 else if ((data.pm25_standard>=13 && data.pm25_standard<=37) || (data.pm100_standard>=55 && data.pm100_standard<=154) ){
mateojs16 4:c8e5928f440c 67 tft.fillCircle(120, 250, 40, 0xFFE0);
mateojs16 4:c8e5928f440c 68 }
mateojs16 4:c8e5928f440c 69 else if ((data.pm25_standard>=38 && data.pm25_standard<=55) || (data.pm100_standard>=155 && data.pm100_standard<=254) ){
mateojs16 4:c8e5928f440c 70 tft.fillCircle(120, 250, 40, 0xFBE0);
mateojs16 4:c8e5928f440c 71 }
mateojs16 4:c8e5928f440c 72 else if ((data.pm25_standard>=56 && data.pm25_standard<=150) || (data.pm100_standard>=255 && data.pm100_standard<=354) ){
mateojs16 4:c8e5928f440c 73 tft.fillCircle(120, 250, 40, 0xF800);
mateojs16 4:c8e5928f440c 74 }
mateojs16 4:c8e5928f440c 75 else if ((data.pm25_standard>=151 && data.pm25_standard<=250) || (data.pm100_standard>=355 && data.pm100_standard<=424) ){
mateojs16 4:c8e5928f440c 76 tft.fillCircle(120, 250, 40, 0xF81F);
mateojs16 4:c8e5928f440c 77 }
mateojs16 4:c8e5928f440c 78 else{
mateojs16 4:c8e5928f440c 79 tft.fillCircle(120, 250, 40, 0x0000);
mateojs16 4:c8e5928f440c 80 }
mateojs16 4:c8e5928f440c 81
mateojs16 4:c8e5928f440c 82
sebastianrestrepo 0:d5dc1e5564a5 83 printf("---------------------------------------\n");
sebastianrestrepo 0:d5dc1e5564a5 84 printf("Concentration Units (environmental)\n");
sebastianrestrepo 0:d5dc1e5564a5 85 printf("PM 1.0: %u", data.pm10_env);
sebastianrestrepo 0:d5dc1e5564a5 86 printf("\t\tPM 2.5: %u", data.pm25_env);
sebastianrestrepo 0:d5dc1e5564a5 87 printf("\t\tPM 10: %u\n", data.pm100_env);
sebastianrestrepo 0:d5dc1e5564a5 88 printf("---------------------------------------\n");
sebastianrestrepo 0:d5dc1e5564a5 89 printf("Particles > 0.3um / 0.1L air: %u\n", data.particles_03um);
sebastianrestrepo 0:d5dc1e5564a5 90 printf("Particles > 0.5um / 0.1L air: %u\n", data.particles_05um);
sebastianrestrepo 0:d5dc1e5564a5 91 printf("Particles > 1.0um / 0.1L air: %u\n", data.particles_10um);
sebastianrestrepo 0:d5dc1e5564a5 92 printf("Particles > 2.5um / 0.1L air: %u\n", data.particles_25um);
sebastianrestrepo 0:d5dc1e5564a5 93 printf("Particles > 5.0um / 0.1L air: %u\n", data.particles_50um);
sebastianrestrepo 0:d5dc1e5564a5 94 printf("Particles > 10.0 um / 0.1L air: %u\n", data.particles_100um);
sebastianrestrepo 0:d5dc1e5564a5 95 printf("---------------------------------------\n");
sebastianrestrepo 0:d5dc1e5564a5 96 }
sebastianrestrepo 0:d5dc1e5564a5 97
sebastianrestrepo 1:5bc605d2b8f8 98 /******************************************************************************
sebastianrestrepo 1:5bc605d2b8f8 99 * Main
sebastianrestrepo 1:5bc605d2b8f8 100 *****************************************************************************/
sebastianrestrepo 1:5bc605d2b8f8 101
sebastianrestrepo 1:5bc605d2b8f8 102
sebastianrestrepo 1:5bc605d2b8f8 103
sebastianrestrepo 1:5bc605d2b8f8 104
sebastianrestrepo 0:d5dc1e5564a5 105 int main() {
sebastianrestrepo 1:5bc605d2b8f8 106 log("init tft \r\n");
sebastianrestrepo 2:e80f510b3cb2 107 tft.init();
sebastianrestrepo 2:e80f510b3cb2 108
mateojs16 3:4c7554fd88a5 109
sebastianrestrepo 2:e80f510b3cb2 110 uint16_t w = tft.width();
sebastianrestrepo 2:e80f510b3cb2 111 uint16_t h = tft.height();
mateojs16 3:4c7554fd88a5 112 tft.fillRectangle(0, 0, 1000,1000, 0xFFFF);
mateojs16 4:c8e5928f440c 113 tft.setTextColor(0xFFFF,0x0000);
sebastianrestrepo 2:e80f510b3cb2 114 tft.drawString(35, 30, "Cantidad de particulas : ");
mateojs16 3:4c7554fd88a5 115
sebastianrestrepo 0:d5dc1e5564a5 116
sebastianrestrepo 0:d5dc1e5564a5 117 // This callback runs in an interrupt context, thus we debounce to the event queue here
sebastianrestrepo 0:d5dc1e5564a5 118 pm25.enable(queue.event(&pm25_data_callback));
mateojs16 4:c8e5928f440c 119 queue.dispatch_forever();
sebastianrestrepo 0:d5dc1e5564a5 120
sebastianrestrepo 0:d5dc1e5564a5 121 }