Coreonetech / Mbed 2 deprecated Nucleo_IoT

Dependencies:   mbed Nucleo_IoT LCDLib HC_SR04_Ultrasonic_Library

Dependents:   Nucleo_IoT

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "TextLCD.h"
00003 #include "ultrasonic.h"
00004 #include "ledControl.h"
00005 
00006 #define led_cmd     1
00007 #define buzzer_cmd  2
00008 #define motor_cmd   3
00009 #define ON          1
00010 #define OFF         0
00011 
00012 void buzzer();
00013 void RGB_LED(void);
00014 void analog_sen();
00015 void digital_sen();
00016 void PIR_sen();
00017 void PSD_sen();
00018 int dht_read(void);
00019 void dth11();
00020 //void dist(int distance);
00021 void ultrasonic_sensor();
00022 void DC_motor(void);
00023 void callback();        // uart receive
00024 void sensordata(void);
00025 
00026 void dist(int distance)
00027 {
00028     //put code here to happen when the distance is changed
00029     printf("Distance %dmm\r\n", distance);
00030 }
00031 
00032 DigitalOut led(LED1);
00033 // buzzer
00034 DigitalOut mybuzzer(PC_4); 
00035 // led
00036 DigitalOut myled [4] = { PC_9, PB_2, PC_2, PC_3};  
00037 // switch
00038 DigitalIn mysw[4] = {PC_10,PC_11, PC_12, PA_13 }; 
00039 // RGB LED
00040 PwmOut mypwmR(PB_5);
00041 PwmOut mypwmG(PB_3);
00042 PwmOut mypwmB(PA_10);
00043 // 2x16 Text LCD 
00044 
00045 TextLCD lcd(PC_6,PC_8,PC_5,PC_0,PB_7,PC_13,PB_12);
00046 
00047 // analog sensor
00048 AnalogIn analog_value(PC_1);
00049 // digital sensor
00050 DigitalIn Sensor_DIN(PA_6);
00051 // PIR sensor 
00052 DigitalIn pir(PD_2);
00053 // PSD sensor
00054 AnalogIn psd(PB_0);   
00055 // DHT11 
00056 #define DHTLIB_OK                0
00057 #define DHTLIB_ERROR_CHECKSUM   -1
00058 #define DHTLIB_ERROR_TIMEOUT    -2
00059 Timer tmr;
00060 DigitalInOut data_pin(PB_10);
00061 //ultra sonic 
00062 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
00063 //DC motor
00064 DigitalOut mo_in1(PB_14);
00065 DigitalOut mo_in2(PB_15);
00066 DigitalOut mo_en(PB_1);
00067 
00068 char buffer[17];
00069 int humidity;
00070 int temperature;
00071 float analog_meas; // analog sensor input data 
00072 char din_dect;      // digital sensor input data
00073 char pir_dect;      // pir sensor detect
00074 float psd_meas;     // psd sensor data
00075 //long dist_cal;      // psd sensor distance
00076 //int distance;
00077 
00078 // serial comm
00079 Serial pi(PA_11, PA_12);
00080 
00081 bool receive_flag = 0;
00082 unsigned int buf_cnt = 0;
00083 char uartBuff[1000];
00084 char uartRcev[1000];
00085 char sensor_data[6];
00086 int main(void)
00087 {
00088     lcd.gotoxy(1,1);
00089     lcd.printf("Core-1000");
00090     lcd.gotoxy(1,2);
00091     lcd.printf("IoT Practice");
00092  
00093     buzzer();
00094     pi.baud(115200);
00095     pi.attach(&callback);
00096     
00097     while (1) 
00098     {
00099         if(receive_flag == 1)   // pi -> nucleo cmd 
00100         {
00101      
00102             printf("%d %d \n\r", uartRcev[0], uartRcev[1]);
00103             // receive command -- actuators
00104             if(uartRcev[0] == led_cmd) // RGB LED ON/OFF
00105             {
00106        
00107                 if(uartRcev[1] == ON) // RGB ON
00108                 {
00109                     mypwmR.period_ms(10);
00110                     mypwmR.pulsewidth_ms(5);
00111                 }
00112                 else 
00113                 {
00114                     led = !led;
00115                     mypwmR.pulsewidth_ms(0);
00116                 }
00117             }
00118             if(uartRcev[0] == buzzer_cmd) // Buzzer ON/OFF
00119             {
00120                 if(uartRcev[1] == ON) // buzzer ON
00121                 {
00122                     mybuzzer = 1; 
00123                 }
00124                 else 
00125                 {
00126                     mybuzzer = 0; 
00127                 }
00128             }
00129             if(uartRcev[0] == motor_cmd) // Motor ON/OFF
00130             {
00131                 if(uartRcev[1] == ON) // motor ON
00132                 {
00133                     mo_in1 = 0;
00134                     mo_in2 = 1;
00135                     mo_en = 1;  
00136                 }
00137                 else 
00138                 {
00139                     mo_in1 = 0;
00140                     mo_in2 = 0;
00141                     mo_en = 0;  
00142                 }
00143             }
00144             receive_flag = 0;
00145         }
00146         sensordata();
00147         wait(1);
00148         
00149         // send sensor data
00150     } 
00151 }
00152 
00153 void sensordata(void)
00154 {
00155     char analog_val[10];
00156     int analog;
00157     analog_sen();   //analog_meas
00158     digital_sen();   //din_dect
00159     PIR_sen();      //pir_dect
00160 //  PSD_sen();      //psd_meas
00161     dht_read();     //humidity, temperature
00162 //  ultrasonic_sensor();   //distance
00163 
00164     analog = analog_meas;   // float -> int
00165     analog_val[0] = (char)(analog >> 8);
00166     analog_val[1] = (char)(analog);
00167     pi.putc(analog_val[0]);
00168     pi.putc(analog_val[1]);
00169     pi.putc(din_dect);
00170     pi.putc(pir_dect);
00171     pi.putc(temperature);
00172     pi.putc(humidity);
00173     
00174 //    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 
00175 
00176 }
00177 
00178 void callback() {
00179     char buf = 0;
00180     myled[0] =1;
00181     buf = pi.getc();
00182    
00183     uartBuff[buf_cnt] = buf;
00184     if(uartBuff[buf_cnt] == '\r' )
00185     {
00186    
00187         //printf("%c", buf);
00188         uartBuff[buf_cnt+1] = 0;
00189         memcpy(uartRcev,uartBuff,sizeof(uartRcev) );
00190         memset(uartBuff,0,sizeof(uartBuff));
00191         buf_cnt = 0;
00192         receive_flag = 1;
00193     }
00194     else
00195     {
00196         buf_cnt++;
00197     }
00198 }
00199 
00200 ////////////////////////////////////////////////////////////////////////////////
00201 // sensors /////////////////////////////////////////////////////////////////////
00202 ////////////////////////////////////////////////////////////////////////////////
00203 void analog_sen()
00204 {
00205         analog_meas = analog_value.read(); // Converts and read the analog input value (value from 0.0 to 1.0)
00206         analog_meas = analog_meas * 3300; // Change the value to be in the 0 to 3300 range
00207         //data char type --> send 
00208 //        printf("measure = %.0f mV\n\r", analog_meas);
00209 }
00210 void digital_sen()
00211 {
00212         if (Sensor_DIN == 0)    din_dect = 1; // detected 
00213         else                    din_dect = 0; // not detected           
00214 }
00215 void PIR_sen()
00216 {
00217     if(pir)
00218     {
00219         pir_dect = 1; 
00220         wait(0.25);
00221     }
00222     else    pir_dect = 0;       
00223 }
00224 void PSD_sen()
00225 {
00226     psd_meas = psd.read(); // Converts and read the analog input value (value from 0.0 to 1.0)
00227     //psd_meas = psd_meas * 3300; // Change the value to be in the 0 to 3300 range
00228     psd_meas = (psd_meas * 3300); 
00229 }
00230 
00231  
00232 
00233 // DHT11 Library
00234 int dht_read(void){
00235     // BUFFER TO RECEIVE
00236     uint8_t bits[5];
00237     uint8_t cnt = 7;
00238     uint8_t idx = 0;
00239     
00240     tmr.stop();
00241     tmr.reset();
00242 
00243     // EMPTY BUFFER
00244     for(int i=0; i< 5; i++) bits[i] = 0;
00245 
00246     // REQUEST SAMPLE
00247     data_pin.output();
00248     data_pin.write(0);
00249     wait_ms(18);
00250     data_pin.write(1);
00251     wait_us(40);
00252     data_pin.input();
00253 
00254     // ACKNOWLEDGE or TIMEOUT
00255     unsigned int loopCnt = 10000;
00256     
00257     while(!data_pin.read())if(!loopCnt--)return DHTLIB_ERROR_TIMEOUT;
00258 
00259     loopCnt = 10000;
00260     
00261     while(data_pin.read())if(!loopCnt--)return DHTLIB_ERROR_TIMEOUT;
00262 
00263     // READ OUTPUT - 40 BITS => 5 BYTES or TIMEOUT
00264     for(int i=0; i<40; i++){      
00265         loopCnt = 10000;      
00266         while(!data_pin.read())if(loopCnt-- == 0)return DHTLIB_ERROR_TIMEOUT;
00267         //unsigned long t = micros();
00268         tmr.start();
00269         loopCnt = 10000;    
00270         while(data_pin.read())if(!loopCnt--)return DHTLIB_ERROR_TIMEOUT;
00271         if(tmr.read_us() > 40) bits[idx] |= (1 << cnt);    
00272         tmr.stop();
00273         tmr.reset();      
00274         if(cnt == 0){   // next byte?     
00275             cnt = 7;    // restart at MSB
00276             idx++;      // next byte!       
00277         }else cnt--;   
00278     }
00279     // WRITE TO RIGHT VARS
00280     // as bits[1] and bits[3] are allways zero they are omitted in formulas.
00281     humidity    = bits[0]; 
00282     temperature = bits[2]; 
00283 
00284     uint8_t sum = bits[0] + bits[2];  
00285     if(bits[4] != sum)return DHTLIB_ERROR_CHECKSUM;  
00286     return DHTLIB_OK;
00287 }
00288 
00289 void dth11()
00290 {
00291         if(!dht_read())
00292         {
00293             printf("Hum %2d%%  Tmp %2dc\n\r", humidity, temperature);
00294             wait(0.5);  
00295         }
00296         else
00297         {
00298             printf("Sensor Error !!!\n\r");
00299         }        
00300 }     
00301 
00302 
00303 void ultrasonic_sensor()
00304 {
00305     mu.startUpdates();//start mesuring the distance
00306     //Do something else here
00307     mu.checkDistance();     //call checkDistance() as much as possible, as this is where the class checks if dist needs to be called.
00308 }
00309 
00310 ////////////////////////////////////////////////////////////////////////////////
00311 // actuators  //////////////////////////////////////////////////////////////////
00312 ////////////////////////////////////////////////////////////////////////////////
00313 void buzzer()
00314 {
00315     mybuzzer = 1; // LED is ON
00316     wait(0.2); // 200 ms
00317     mybuzzer = 0; // LED is OFF
00318     wait(1.0); // 1 sec
00319 }
00320 void RGB_LED(void)
00321 {
00322     mypwmR.period_ms(10);
00323     mypwmR.pulsewidth_ms(5);
00324     wait(1);
00325     mypwmR.pulsewidth_ms(0);
00326     
00327     mypwmG.period_ms(10);
00328     mypwmG.pulsewidth_ms(5);
00329     wait(1);
00330     mypwmG.pulsewidth_ms(0);
00331     
00332     mypwmB.period_ms(10);
00333     mypwmB.pulsewidth_ms(5);
00334     wait(1);
00335     mypwmB.pulsewidth_ms(0);     
00336 }
00337 
00338 void DC_motor(void)
00339 {
00340       mo_in1 = 0;
00341       mo_in2 = 1;
00342       mo_en = 1;  
00343 /*
00344       wait_ms( 2000 );
00345       mo_en = 0; 
00346       wait_ms( 2000 );
00347       mo_in1 = 1;
00348       mo_in2 = 0;
00349       mo_en = 1; 
00350       wait_ms( 2000 );
00351       mo_en = 0; 
00352       wait_ms( 2000 );
00353 */
00354 }