IoT test for the CORE-1000

Dependencies:   mbed Nucleo_IoT LCDLib HC_SR04_Ultrasonic_Library

Dependents:   Nucleo_IoT

Committer:
odb
Date:
Thu Jan 10 08:24:44 2019 +0000
Revision:
0:11e5a993a331
IoT test for the CORE-1000

Who changed what in which revision?

UserRevisionLine numberNew contents of line
odb 0:11e5a993a331 1 #include "mbed.h"
odb 0:11e5a993a331 2 #include "TextLCD.h"
odb 0:11e5a993a331 3 #include "ultrasonic.h"
odb 0:11e5a993a331 4 #include "ledControl.h"
odb 0:11e5a993a331 5
odb 0:11e5a993a331 6 #define led_cmd 1
odb 0:11e5a993a331 7 #define buzzer_cmd 2
odb 0:11e5a993a331 8 #define motor_cmd 3
odb 0:11e5a993a331 9 #define ON 1
odb 0:11e5a993a331 10 #define OFF 0
odb 0:11e5a993a331 11
odb 0:11e5a993a331 12 void buzzer();
odb 0:11e5a993a331 13 void RGB_LED(void);
odb 0:11e5a993a331 14 void analog_sen();
odb 0:11e5a993a331 15 void digital_sen();
odb 0:11e5a993a331 16 void PIR_sen();
odb 0:11e5a993a331 17 void PSD_sen();
odb 0:11e5a993a331 18 int dht_read(void);
odb 0:11e5a993a331 19 void dth11();
odb 0:11e5a993a331 20 //void dist(int distance);
odb 0:11e5a993a331 21 void ultrasonic_sensor();
odb 0:11e5a993a331 22 void DC_motor(void);
odb 0:11e5a993a331 23 void callback(); // uart receive
odb 0:11e5a993a331 24 void sensordata(void);
odb 0:11e5a993a331 25
odb 0:11e5a993a331 26 void dist(int distance)
odb 0:11e5a993a331 27 {
odb 0:11e5a993a331 28 //put code here to happen when the distance is changed
odb 0:11e5a993a331 29 printf("Distance %dmm\r\n", distance);
odb 0:11e5a993a331 30 }
odb 0:11e5a993a331 31
odb 0:11e5a993a331 32 DigitalOut led(LED1);
odb 0:11e5a993a331 33 // buzzer
odb 0:11e5a993a331 34 DigitalOut mybuzzer(PC_4);
odb 0:11e5a993a331 35 // led
odb 0:11e5a993a331 36 DigitalOut myled [4] = { PC_9, PB_2, PC_2, PC_3};
odb 0:11e5a993a331 37 // switch
odb 0:11e5a993a331 38 DigitalIn mysw[4] = {PC_10,PC_11, PC_12, PA_13 };
odb 0:11e5a993a331 39 // RGB LED
odb 0:11e5a993a331 40 PwmOut mypwmR(PB_5);
odb 0:11e5a993a331 41 PwmOut mypwmG(PB_3);
odb 0:11e5a993a331 42 PwmOut mypwmB(PA_10);
odb 0:11e5a993a331 43 // 2x16 Text LCD
odb 0:11e5a993a331 44
odb 0:11e5a993a331 45 TextLCD lcd(PC_6,PC_8,PC_5,PC_0,PB_7,PC_13,PB_12);
odb 0:11e5a993a331 46
odb 0:11e5a993a331 47 // analog sensor
odb 0:11e5a993a331 48 AnalogIn analog_value(PC_1);
odb 0:11e5a993a331 49 // digital sensor
odb 0:11e5a993a331 50 DigitalIn Sensor_DIN(PA_6);
odb 0:11e5a993a331 51 // PIR sensor
odb 0:11e5a993a331 52 DigitalIn pir(PD_2);
odb 0:11e5a993a331 53 // PSD sensor
odb 0:11e5a993a331 54 AnalogIn psd(PB_0);
odb 0:11e5a993a331 55 // DHT11
odb 0:11e5a993a331 56 #define DHTLIB_OK 0
odb 0:11e5a993a331 57 #define DHTLIB_ERROR_CHECKSUM -1
odb 0:11e5a993a331 58 #define DHTLIB_ERROR_TIMEOUT -2
odb 0:11e5a993a331 59 Timer tmr;
odb 0:11e5a993a331 60 DigitalInOut data_pin(PB_10);
odb 0:11e5a993a331 61 //ultra sonic
odb 0:11e5a993a331 62 ultrasonic mu(PC_7, PA_9, .1, 1, &dist); //Set the trigger pin to D8 and the echo pin to D9 have updates every .1 seconds and a timeout after 1 second, and call dist when the distance changes
odb 0:11e5a993a331 63 //DC motor
odb 0:11e5a993a331 64 DigitalOut mo_in1(PB_14);
odb 0:11e5a993a331 65 DigitalOut mo_in2(PB_15);
odb 0:11e5a993a331 66 DigitalOut mo_en(PB_1);
odb 0:11e5a993a331 67
odb 0:11e5a993a331 68 char buffer[17];
odb 0:11e5a993a331 69 int humidity;
odb 0:11e5a993a331 70 int temperature;
odb 0:11e5a993a331 71 float analog_meas; // analog sensor input data
odb 0:11e5a993a331 72 char din_dect; // digital sensor input data
odb 0:11e5a993a331 73 char pir_dect; // pir sensor detect
odb 0:11e5a993a331 74 float psd_meas; // psd sensor data
odb 0:11e5a993a331 75 //long dist_cal; // psd sensor distance
odb 0:11e5a993a331 76 //int distance;
odb 0:11e5a993a331 77
odb 0:11e5a993a331 78 // serial comm
odb 0:11e5a993a331 79 Serial pi(PA_11, PA_12);
odb 0:11e5a993a331 80
odb 0:11e5a993a331 81 bool receive_flag = 0;
odb 0:11e5a993a331 82 unsigned int buf_cnt = 0;
odb 0:11e5a993a331 83 char uartBuff[1000];
odb 0:11e5a993a331 84 char uartRcev[1000];
odb 0:11e5a993a331 85 char sensor_data[6];
odb 0:11e5a993a331 86 int main(void)
odb 0:11e5a993a331 87 {
odb 0:11e5a993a331 88 lcd.gotoxy(1,1);
odb 0:11e5a993a331 89 lcd.printf("Core-1000");
odb 0:11e5a993a331 90 lcd.gotoxy(1,2);
odb 0:11e5a993a331 91 lcd.printf("IoT Practice");
odb 0:11e5a993a331 92
odb 0:11e5a993a331 93 buzzer();
odb 0:11e5a993a331 94 pi.baud(115200);
odb 0:11e5a993a331 95 pi.attach(&callback);
odb 0:11e5a993a331 96
odb 0:11e5a993a331 97 while (1)
odb 0:11e5a993a331 98 {
odb 0:11e5a993a331 99 if(receive_flag == 1) // pi -> nucleo cmd
odb 0:11e5a993a331 100 {
odb 0:11e5a993a331 101
odb 0:11e5a993a331 102 printf("%d %d \n\r", uartRcev[0], uartRcev[1]);
odb 0:11e5a993a331 103 // receive command -- actuators
odb 0:11e5a993a331 104 if(uartRcev[0] == led_cmd) // RGB LED ON/OFF
odb 0:11e5a993a331 105 {
odb 0:11e5a993a331 106
odb 0:11e5a993a331 107 if(uartRcev[1] == ON) // RGB ON
odb 0:11e5a993a331 108 {
odb 0:11e5a993a331 109 mypwmR.period_ms(10);
odb 0:11e5a993a331 110 mypwmR.pulsewidth_ms(5);
odb 0:11e5a993a331 111 }
odb 0:11e5a993a331 112 else
odb 0:11e5a993a331 113 {
odb 0:11e5a993a331 114 led = !led;
odb 0:11e5a993a331 115 mypwmR.pulsewidth_ms(0);
odb 0:11e5a993a331 116 }
odb 0:11e5a993a331 117 }
odb 0:11e5a993a331 118 if(uartRcev[0] == buzzer_cmd) // Buzzer ON/OFF
odb 0:11e5a993a331 119 {
odb 0:11e5a993a331 120 if(uartRcev[1] == ON) // buzzer ON
odb 0:11e5a993a331 121 {
odb 0:11e5a993a331 122 mybuzzer = 1;
odb 0:11e5a993a331 123 }
odb 0:11e5a993a331 124 else
odb 0:11e5a993a331 125 {
odb 0:11e5a993a331 126 mybuzzer = 0;
odb 0:11e5a993a331 127 }
odb 0:11e5a993a331 128 }
odb 0:11e5a993a331 129 if(uartRcev[0] == motor_cmd) // Motor ON/OFF
odb 0:11e5a993a331 130 {
odb 0:11e5a993a331 131 if(uartRcev[1] == ON) // motor ON
odb 0:11e5a993a331 132 {
odb 0:11e5a993a331 133 mo_in1 = 0;
odb 0:11e5a993a331 134 mo_in2 = 1;
odb 0:11e5a993a331 135 mo_en = 1;
odb 0:11e5a993a331 136 }
odb 0:11e5a993a331 137 else
odb 0:11e5a993a331 138 {
odb 0:11e5a993a331 139 mo_in1 = 0;
odb 0:11e5a993a331 140 mo_in2 = 0;
odb 0:11e5a993a331 141 mo_en = 0;
odb 0:11e5a993a331 142 }
odb 0:11e5a993a331 143 }
odb 0:11e5a993a331 144 receive_flag = 0;
odb 0:11e5a993a331 145 }
odb 0:11e5a993a331 146 sensordata();
odb 0:11e5a993a331 147 wait(1);
odb 0:11e5a993a331 148
odb 0:11e5a993a331 149 // send sensor data
odb 0:11e5a993a331 150 }
odb 0:11e5a993a331 151 }
odb 0:11e5a993a331 152
odb 0:11e5a993a331 153 void sensordata(void)
odb 0:11e5a993a331 154 {
odb 0:11e5a993a331 155 char analog_val[10];
odb 0:11e5a993a331 156 int analog;
odb 0:11e5a993a331 157 analog_sen(); //analog_meas
odb 0:11e5a993a331 158 digital_sen(); //din_dect
odb 0:11e5a993a331 159 PIR_sen(); //pir_dect
odb 0:11e5a993a331 160 // PSD_sen(); //psd_meas
odb 0:11e5a993a331 161 dht_read(); //humidity, temperature
odb 0:11e5a993a331 162 // ultrasonic_sensor(); //distance
odb 0:11e5a993a331 163
odb 0:11e5a993a331 164 analog = analog_meas; // float -> int
odb 0:11e5a993a331 165 analog_val[0] = (char)(analog >> 8);
odb 0:11e5a993a331 166 analog_val[1] = (char)(analog);
odb 0:11e5a993a331 167 pi.putc(analog_val[0]);
odb 0:11e5a993a331 168 pi.putc(analog_val[1]);
odb 0:11e5a993a331 169 pi.putc(din_dect);
odb 0:11e5a993a331 170 pi.putc(pir_dect);
odb 0:11e5a993a331 171 pi.putc(temperature);
odb 0:11e5a993a331 172 pi.putc(humidity);
odb 0:11e5a993a331 173
odb 0:11e5a993a331 174 // printf("%d %d %d %d %d %d\r\n", analog_val[0], analog_val[1], din_dect, pir_dect, temperature, humidity); // analog, digital, pir, temp, humi // use windows debug
odb 0:11e5a993a331 175
odb 0:11e5a993a331 176 }
odb 0:11e5a993a331 177
odb 0:11e5a993a331 178 void callback() {
odb 0:11e5a993a331 179 char buf = 0;
odb 0:11e5a993a331 180 myled[0] =1;
odb 0:11e5a993a331 181 buf = pi.getc();
odb 0:11e5a993a331 182
odb 0:11e5a993a331 183 uartBuff[buf_cnt] = buf;
odb 0:11e5a993a331 184 if(uartBuff[buf_cnt] == '\r' )
odb 0:11e5a993a331 185 {
odb 0:11e5a993a331 186
odb 0:11e5a993a331 187 //printf("%c", buf);
odb 0:11e5a993a331 188 uartBuff[buf_cnt+1] = 0;
odb 0:11e5a993a331 189 memcpy(uartRcev,uartBuff,sizeof(uartRcev) );
odb 0:11e5a993a331 190 memset(uartBuff,0,sizeof(uartBuff));
odb 0:11e5a993a331 191 buf_cnt = 0;
odb 0:11e5a993a331 192 receive_flag = 1;
odb 0:11e5a993a331 193 }
odb 0:11e5a993a331 194 else
odb 0:11e5a993a331 195 {
odb 0:11e5a993a331 196 buf_cnt++;
odb 0:11e5a993a331 197 }
odb 0:11e5a993a331 198 }
odb 0:11e5a993a331 199
odb 0:11e5a993a331 200 ////////////////////////////////////////////////////////////////////////////////
odb 0:11e5a993a331 201 // sensors /////////////////////////////////////////////////////////////////////
odb 0:11e5a993a331 202 ////////////////////////////////////////////////////////////////////////////////
odb 0:11e5a993a331 203 void analog_sen()
odb 0:11e5a993a331 204 {
odb 0:11e5a993a331 205 analog_meas = analog_value.read(); // Converts and read the analog input value (value from 0.0 to 1.0)
odb 0:11e5a993a331 206 analog_meas = analog_meas * 3300; // Change the value to be in the 0 to 3300 range
odb 0:11e5a993a331 207 //data char type --> send
odb 0:11e5a993a331 208 // printf("measure = %.0f mV\n\r", analog_meas);
odb 0:11e5a993a331 209 }
odb 0:11e5a993a331 210 void digital_sen()
odb 0:11e5a993a331 211 {
odb 0:11e5a993a331 212 if (Sensor_DIN == 0) din_dect = 1; // detected
odb 0:11e5a993a331 213 else din_dect = 0; // not detected
odb 0:11e5a993a331 214 }
odb 0:11e5a993a331 215 void PIR_sen()
odb 0:11e5a993a331 216 {
odb 0:11e5a993a331 217 if(pir)
odb 0:11e5a993a331 218 {
odb 0:11e5a993a331 219 pir_dect = 1;
odb 0:11e5a993a331 220 wait(0.25);
odb 0:11e5a993a331 221 }
odb 0:11e5a993a331 222 else pir_dect = 0;
odb 0:11e5a993a331 223 }
odb 0:11e5a993a331 224 void PSD_sen()
odb 0:11e5a993a331 225 {
odb 0:11e5a993a331 226 psd_meas = psd.read(); // Converts and read the analog input value (value from 0.0 to 1.0)
odb 0:11e5a993a331 227 //psd_meas = psd_meas * 3300; // Change the value to be in the 0 to 3300 range
odb 0:11e5a993a331 228 psd_meas = (psd_meas * 3300);
odb 0:11e5a993a331 229 }
odb 0:11e5a993a331 230
odb 0:11e5a993a331 231
odb 0:11e5a993a331 232
odb 0:11e5a993a331 233 // DHT11 Library
odb 0:11e5a993a331 234 int dht_read(void){
odb 0:11e5a993a331 235 // BUFFER TO RECEIVE
odb 0:11e5a993a331 236 uint8_t bits[5];
odb 0:11e5a993a331 237 uint8_t cnt = 7;
odb 0:11e5a993a331 238 uint8_t idx = 0;
odb 0:11e5a993a331 239
odb 0:11e5a993a331 240 tmr.stop();
odb 0:11e5a993a331 241 tmr.reset();
odb 0:11e5a993a331 242
odb 0:11e5a993a331 243 // EMPTY BUFFER
odb 0:11e5a993a331 244 for(int i=0; i< 5; i++) bits[i] = 0;
odb 0:11e5a993a331 245
odb 0:11e5a993a331 246 // REQUEST SAMPLE
odb 0:11e5a993a331 247 data_pin.output();
odb 0:11e5a993a331 248 data_pin.write(0);
odb 0:11e5a993a331 249 wait_ms(18);
odb 0:11e5a993a331 250 data_pin.write(1);
odb 0:11e5a993a331 251 wait_us(40);
odb 0:11e5a993a331 252 data_pin.input();
odb 0:11e5a993a331 253
odb 0:11e5a993a331 254 // ACKNOWLEDGE or TIMEOUT
odb 0:11e5a993a331 255 unsigned int loopCnt = 10000;
odb 0:11e5a993a331 256
odb 0:11e5a993a331 257 while(!data_pin.read())if(!loopCnt--)return DHTLIB_ERROR_TIMEOUT;
odb 0:11e5a993a331 258
odb 0:11e5a993a331 259 loopCnt = 10000;
odb 0:11e5a993a331 260
odb 0:11e5a993a331 261 while(data_pin.read())if(!loopCnt--)return DHTLIB_ERROR_TIMEOUT;
odb 0:11e5a993a331 262
odb 0:11e5a993a331 263 // READ OUTPUT - 40 BITS => 5 BYTES or TIMEOUT
odb 0:11e5a993a331 264 for(int i=0; i<40; i++){
odb 0:11e5a993a331 265 loopCnt = 10000;
odb 0:11e5a993a331 266 while(!data_pin.read())if(loopCnt-- == 0)return DHTLIB_ERROR_TIMEOUT;
odb 0:11e5a993a331 267 //unsigned long t = micros();
odb 0:11e5a993a331 268 tmr.start();
odb 0:11e5a993a331 269 loopCnt = 10000;
odb 0:11e5a993a331 270 while(data_pin.read())if(!loopCnt--)return DHTLIB_ERROR_TIMEOUT;
odb 0:11e5a993a331 271 if(tmr.read_us() > 40) bits[idx] |= (1 << cnt);
odb 0:11e5a993a331 272 tmr.stop();
odb 0:11e5a993a331 273 tmr.reset();
odb 0:11e5a993a331 274 if(cnt == 0){ // next byte?
odb 0:11e5a993a331 275 cnt = 7; // restart at MSB
odb 0:11e5a993a331 276 idx++; // next byte!
odb 0:11e5a993a331 277 }else cnt--;
odb 0:11e5a993a331 278 }
odb 0:11e5a993a331 279 // WRITE TO RIGHT VARS
odb 0:11e5a993a331 280 // as bits[1] and bits[3] are allways zero they are omitted in formulas.
odb 0:11e5a993a331 281 humidity = bits[0];
odb 0:11e5a993a331 282 temperature = bits[2];
odb 0:11e5a993a331 283
odb 0:11e5a993a331 284 uint8_t sum = bits[0] + bits[2];
odb 0:11e5a993a331 285 if(bits[4] != sum)return DHTLIB_ERROR_CHECKSUM;
odb 0:11e5a993a331 286 return DHTLIB_OK;
odb 0:11e5a993a331 287 }
odb 0:11e5a993a331 288
odb 0:11e5a993a331 289 void dth11()
odb 0:11e5a993a331 290 {
odb 0:11e5a993a331 291 if(!dht_read())
odb 0:11e5a993a331 292 {
odb 0:11e5a993a331 293 printf("Hum %2d%% Tmp %2dc\n\r", humidity, temperature);
odb 0:11e5a993a331 294 wait(0.5);
odb 0:11e5a993a331 295 }
odb 0:11e5a993a331 296 else
odb 0:11e5a993a331 297 {
odb 0:11e5a993a331 298 printf("Sensor Error !!!\n\r");
odb 0:11e5a993a331 299 }
odb 0:11e5a993a331 300 }
odb 0:11e5a993a331 301
odb 0:11e5a993a331 302
odb 0:11e5a993a331 303 void ultrasonic_sensor()
odb 0:11e5a993a331 304 {
odb 0:11e5a993a331 305 mu.startUpdates();//start mesuring the distance
odb 0:11e5a993a331 306 //Do something else here
odb 0:11e5a993a331 307 mu.checkDistance(); //call checkDistance() as much as possible, as this is where the class checks if dist needs to be called.
odb 0:11e5a993a331 308 }
odb 0:11e5a993a331 309
odb 0:11e5a993a331 310 ////////////////////////////////////////////////////////////////////////////////
odb 0:11e5a993a331 311 // actuators //////////////////////////////////////////////////////////////////
odb 0:11e5a993a331 312 ////////////////////////////////////////////////////////////////////////////////
odb 0:11e5a993a331 313 void buzzer()
odb 0:11e5a993a331 314 {
odb 0:11e5a993a331 315 mybuzzer = 1; // LED is ON
odb 0:11e5a993a331 316 wait(0.2); // 200 ms
odb 0:11e5a993a331 317 mybuzzer = 0; // LED is OFF
odb 0:11e5a993a331 318 wait(1.0); // 1 sec
odb 0:11e5a993a331 319 }
odb 0:11e5a993a331 320 void RGB_LED(void)
odb 0:11e5a993a331 321 {
odb 0:11e5a993a331 322 mypwmR.period_ms(10);
odb 0:11e5a993a331 323 mypwmR.pulsewidth_ms(5);
odb 0:11e5a993a331 324 wait(1);
odb 0:11e5a993a331 325 mypwmR.pulsewidth_ms(0);
odb 0:11e5a993a331 326
odb 0:11e5a993a331 327 mypwmG.period_ms(10);
odb 0:11e5a993a331 328 mypwmG.pulsewidth_ms(5);
odb 0:11e5a993a331 329 wait(1);
odb 0:11e5a993a331 330 mypwmG.pulsewidth_ms(0);
odb 0:11e5a993a331 331
odb 0:11e5a993a331 332 mypwmB.period_ms(10);
odb 0:11e5a993a331 333 mypwmB.pulsewidth_ms(5);
odb 0:11e5a993a331 334 wait(1);
odb 0:11e5a993a331 335 mypwmB.pulsewidth_ms(0);
odb 0:11e5a993a331 336 }
odb 0:11e5a993a331 337
odb 0:11e5a993a331 338 void DC_motor(void)
odb 0:11e5a993a331 339 {
odb 0:11e5a993a331 340 mo_in1 = 0;
odb 0:11e5a993a331 341 mo_in2 = 1;
odb 0:11e5a993a331 342 mo_en = 1;
odb 0:11e5a993a331 343 /*
odb 0:11e5a993a331 344 wait_ms( 2000 );
odb 0:11e5a993a331 345 mo_en = 0;
odb 0:11e5a993a331 346 wait_ms( 2000 );
odb 0:11e5a993a331 347 mo_in1 = 1;
odb 0:11e5a993a331 348 mo_in2 = 0;
odb 0:11e5a993a331 349 mo_en = 1;
odb 0:11e5a993a331 350 wait_ms( 2000 );
odb 0:11e5a993a331 351 mo_en = 0;
odb 0:11e5a993a331 352 wait_ms( 2000 );
odb 0:11e5a993a331 353 */
odb 0:11e5a993a331 354 }