Location Aware Sensing System (LASS)

Dependencies:   DHT Grove_LCD_RGB_Backlight mbed

Committer:
royedfa5229
Date:
Mon Aug 01 09:03:19 2016 +0000
Revision:
0:82e30fd2f376
Child:
1:2fc8a8915250
Location Aware Sensing System (LASS)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
royedfa5229 0:82e30fd2f376 1 /*
royedfa5229 0:82e30fd2f376 2 Version: 1.00
royedfa5229 0:82e30fd2f376 3 Purpose: This example use mbed NXP LPC1769
royedfa5229 0:82e30fd2f376 4 Using sensors:
royedfa5229 0:82e30fd2f376 5 -- PMS3003: forPM1.0, PM2.5, PM10 sensing
royedfa5229 0:82e30fd2f376 6 -- DHT22: for temperature and humidity
royedfa5229 0:82e30fd2f376 7 Using transfer module:
royedfa5229 0:82e30fd2f376 8 -- LoRa module: GIoT GL6509 (Gemtek LoRa)
royedfa5229 0:82e30fd2f376 9 Circuit diagram remind:
royedfa5229 0:82e30fd2f376 10 --DHT22: Data pin parallel to Vcc(use 5.1KΩ)
royedfa5229 0:82e30fd2f376 11 --Grove_LCD_RGB_Backlight: Sda pin and Scl pin parallel to Vcc(use 5.1KΩ)
royedfa5229 0:82e30fd2f376 12
royedfa5229 0:82e30fd2f376 13 */
royedfa5229 0:82e30fd2f376 14
royedfa5229 0:82e30fd2f376 15 #include "mbed.h"
royedfa5229 0:82e30fd2f376 16 #include "DHT.h"
royedfa5229 0:82e30fd2f376 17 #include "Grove_LCD_RGB_Backlight.h"
royedfa5229 0:82e30fd2f376 18
royedfa5229 0:82e30fd2f376 19 Serial pc(USBTX, USBRX); // tx, rx
royedfa5229 0:82e30fd2f376 20 Serial device(p28, p27); //tx, rx
royedfa5229 0:82e30fd2f376 21 Serial lora(p13, p14); //tx, rx
royedfa5229 0:82e30fd2f376 22 DHT dht22(p21, SEN51035P);
royedfa5229 0:82e30fd2f376 23 Grove_LCD_RGB_Backlight rgbLCD(p9, p10);
royedfa5229 0:82e30fd2f376 24
royedfa5229 0:82e30fd2f376 25 char buffer[24];
royedfa5229 0:82e30fd2f376 26 int app_id = 15;
royedfa5229 0:82e30fd2f376 27 char lora_trans[11];
royedfa5229 0:82e30fd2f376 28 char gps_lat[] = "25.0227";
royedfa5229 0:82e30fd2f376 29 char gps_lon[] = "121.3653";
royedfa5229 0:82e30fd2f376 30 int fix_num = 15;
royedfa5229 0:82e30fd2f376 31 int count = 1;
royedfa5229 0:82e30fd2f376 32 int err ;
royedfa5229 0:82e30fd2f376 33 unsigned short pm1;
royedfa5229 0:82e30fd2f376 34 unsigned short pm25;
royedfa5229 0:82e30fd2f376 35 unsigned short pm10;
royedfa5229 0:82e30fd2f376 36
royedfa5229 0:82e30fd2f376 37 int main() {
royedfa5229 0:82e30fd2f376 38 wait(2);
royedfa5229 0:82e30fd2f376 39 device.baud(9600);
royedfa5229 0:82e30fd2f376 40 lora.baud(9600);
royedfa5229 0:82e30fd2f376 41 wait(1);
royedfa5229 0:82e30fd2f376 42 buffer[0] = 0x42;
royedfa5229 0:82e30fd2f376 43 buffer[1] = 0x4d;
royedfa5229 0:82e30fd2f376 44 while (1) {
royedfa5229 0:82e30fd2f376 45 while (device.getc() != 0x42) {
royedfa5229 0:82e30fd2f376 46 }
royedfa5229 0:82e30fd2f376 47 if (device.getc() == 0x4d) {
royedfa5229 0:82e30fd2f376 48 for (int i = 2; i <24; i++) {
royedfa5229 0:82e30fd2f376 49 buffer[i] = device.getc();
royedfa5229 0:82e30fd2f376 50 }
royedfa5229 0:82e30fd2f376 51 unsigned short calcsum = buffer[2] + buffer[3]+ buffer[4] + buffer[5] + buffer[6] + buffer[7] + buffer[8] + buffer[9] + buffer[10] + buffer[11] + buffer[12] + buffer[13] + buffer[14] + buffer[15] + buffer[16] + buffer[17] + buffer[18] + buffer[19] + buffer[20] + buffer[21];
royedfa5229 0:82e30fd2f376 52 unsigned short calcsum1 = (calcsum + 0x42 +0x4d);
royedfa5229 0:82e30fd2f376 53 unsigned short exptsum = (buffer[22]<<8) + buffer[23];
royedfa5229 0:82e30fd2f376 54 if( exptsum == calcsum1){
royedfa5229 0:82e30fd2f376 55 pm1 = buffer[10] + buffer[11];
royedfa5229 0:82e30fd2f376 56 pm25 = buffer[12] + buffer[13];
royedfa5229 0:82e30fd2f376 57 pm10 = buffer[14] + buffer[15];
royedfa5229 0:82e30fd2f376 58 }
royedfa5229 0:82e30fd2f376 59 }
royedfa5229 0:82e30fd2f376 60 wait(1);
royedfa5229 0:82e30fd2f376 61 do{
royedfa5229 0:82e30fd2f376 62 err = dht22.readData();
royedfa5229 0:82e30fd2f376 63 }while(err != 0);
royedfa5229 0:82e30fd2f376 64 float temp = dht22.ReadTemperature(CELCIUS);
royedfa5229 0:82e30fd2f376 65 float hum = dht22.ReadHumidity();
royedfa5229 0:82e30fd2f376 66 wait(0.5);
royedfa5229 0:82e30fd2f376 67 pc.printf("T: %.1f C \n", temp);
royedfa5229 0:82e30fd2f376 68 pc.printf("H: %.1f %% \n", hum);
royedfa5229 0:82e30fd2f376 69 pc.printf("pm1= %X ug/m3\n", pm1);
royedfa5229 0:82e30fd2f376 70 pc.printf("pm25= %X ug/m3\n", pm25);
royedfa5229 0:82e30fd2f376 71 pc.printf("pm10= %X ug/m3\n", pm10);
royedfa5229 0:82e30fd2f376 72 pc.printf("%i \n" ,count);
royedfa5229 0:82e30fd2f376 73 char lcd_h_buff[16];
royedfa5229 0:82e30fd2f376 74 char lcd_buff[12];
royedfa5229 0:82e30fd2f376 75 sprintf(lcd_h_buff, "T: %.1f H: %.1f" ,temp ,hum);
royedfa5229 0:82e30fd2f376 76 sprintf(lcd_buff, "PM25:%i PM10:%i" ,pm25 ,pm10);
royedfa5229 0:82e30fd2f376 77 rgbLCD.setRGB(0xff, 0xff, 0xff);
royedfa5229 0:82e30fd2f376 78 rgbLCD.clear();
royedfa5229 0:82e30fd2f376 79 rgbLCD.locate(0,0);
royedfa5229 0:82e30fd2f376 80 rgbLCD.print(lcd_h_buff);
royedfa5229 0:82e30fd2f376 81 rgbLCD.locate(0,1);
royedfa5229 0:82e30fd2f376 82 rgbLCD.print(lcd_buff);
royedfa5229 0:82e30fd2f376 83 wait(5);
royedfa5229 0:82e30fd2f376 84
royedfa5229 0:82e30fd2f376 85 if(count==13&&temp!=0){
royedfa5229 0:82e30fd2f376 86 int temperatureLora = (int)((temp + 20) * 10);
royedfa5229 0:82e30fd2f376 87 int humiditylora = (int)(hum * 10);
royedfa5229 0:82e30fd2f376 88 unsigned char pm25lora = pm25;
royedfa5229 0:82e30fd2f376 89 unsigned char pm100Offset = pm10 - pm25;
royedfa5229 0:82e30fd2f376 90 float gps_lat_f = (float) atof(gps_lat);
royedfa5229 0:82e30fd2f376 91 float gps_lon_f = (float) atof(gps_lon);
royedfa5229 0:82e30fd2f376 92 gps_lat_f += 90;
royedfa5229 0:82e30fd2f376 93 gps_lon_f += 180;
royedfa5229 0:82e30fd2f376 94 int gps_lat_i = (int)(gps_lat_f * 10000);
royedfa5229 0:82e30fd2f376 95 int gps_lon_i = (int)(gps_lon_f * 10000);
royedfa5229 0:82e30fd2f376 96 unsigned char lat_D = (int) gps_lat_f;
royedfa5229 0:82e30fd2f376 97 float temp_lat_M = (gps_lat_f - lat_D) * 100;
royedfa5229 0:82e30fd2f376 98 unsigned char lat_M = (int) temp_lat_M;
royedfa5229 0:82e30fd2f376 99 unsigned char lat_S = (int) gps_lat_i % 100;
royedfa5229 0:82e30fd2f376 100 short lon_D = (int) gps_lon_f;
royedfa5229 0:82e30fd2f376 101 float temp_lon_M = (gps_lon_f - lon_D) * 100;
royedfa5229 0:82e30fd2f376 102 unsigned char lon_M = (int) temp_lon_M;
royedfa5229 0:82e30fd2f376 103 unsigned char lon_S = (int) gps_lon_i % 100;
royedfa5229 0:82e30fd2f376 104 unsigned char gps_fix = fix_num;
royedfa5229 0:82e30fd2f376 105 char buff[150];
royedfa5229 0:82e30fd2f376 106
royedfa5229 0:82e30fd2f376 107 lora_trans[0] = (app_id << 4) | (temperatureLora >> 6);
royedfa5229 0:82e30fd2f376 108 lora_trans[1] = (temperatureLora << 2) | (humiditylora >> 8);
royedfa5229 0:82e30fd2f376 109 lora_trans[2] = humiditylora;
royedfa5229 0:82e30fd2f376 110 // END FOR THE APP_ID, TEMPERATURE AND HUMIDITY
royedfa5229 0:82e30fd2f376 111 lora_trans[3] = pm25lora >> 3;
royedfa5229 0:82e30fd2f376 112 lora_trans[4] = (pm25lora << 5) | (pm100Offset >> 3);
royedfa5229 0:82e30fd2f376 113 // END FOR PM2.5
royedfa5229 0:82e30fd2f376 114 lora_trans[5] = (pm100Offset << 5) | (lat_D >> 3);
royedfa5229 0:82e30fd2f376 115 lora_trans[6] = (lat_D << 5) | (lat_M >> 1);
royedfa5229 0:82e30fd2f376 116 lora_trans[7] = (lat_M << 7) | (lat_S << 1) | (lon_D >> 8);
royedfa5229 0:82e30fd2f376 117 lora_trans[8] = (unsigned char) lon_D;
royedfa5229 0:82e30fd2f376 118 lora_trans[9] = (lon_M << 2) | (lon_S >> 4);
royedfa5229 0:82e30fd2f376 119 lora_trans[10] = (lon_S << 4) | gps_fix;
royedfa5229 0:82e30fd2f376 120 // END FOR PM10 AND GPS
royedfa5229 0:82e30fd2f376 121 sprintf(buff, "AT+DTX=22,%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X\r\n", lora_trans[0], lora_trans[1], lora_trans[2], lora_trans[3], lora_trans[4], lora_trans[5], lora_trans[6], lora_trans[7], lora_trans[8], lora_trans[9], lora_trans[10]);
royedfa5229 0:82e30fd2f376 122 lora.printf(buff);
royedfa5229 0:82e30fd2f376 123 pc.printf(buff);
royedfa5229 0:82e30fd2f376 124 count = 0;
royedfa5229 0:82e30fd2f376 125 rgbLCD.clear();
royedfa5229 0:82e30fd2f376 126 rgbLCD.locate(0,0);
royedfa5229 0:82e30fd2f376 127 rgbLCD.print("LoRa Send");
royedfa5229 0:82e30fd2f376 128 }
royedfa5229 0:82e30fd2f376 129 else if (count == 14){
royedfa5229 0:82e30fd2f376 130 count = 0;
royedfa5229 0:82e30fd2f376 131 }
royedfa5229 0:82e30fd2f376 132 count++;
royedfa5229 0:82e30fd2f376 133 }
royedfa5229 0:82e30fd2f376 134 }