dimanche 24 avril 2016

Programming a XM1000 Sensor Device in C Programming Language Need if statement

I am programming a sensor mote (XM1000) I am using Contiki Operating System to program this devices ( I am using terminal to view the outputs and GEdit to write my 'C' code in. This sensor mote has a temprature, light and humidity sensor on board as well as 3 LED lights.

Below I have two sets of code. The first set of code functions and gives me the sensor readings for the values for temprature, light and humidity.

The second set of code functions. it turns the LED lights on/off and makes them blinking regularly on the sensor node XM1000, it counts how many times the LED has blinked and output the count to the console.

The problem I am having is creating a if statement to meet these following conditions and I am struggling to combine the two codes together. So this is what I want to achieve:

• If the temperature exceeds over 26 Degrees then turn on LED Light 1 for 5 seconds, else if the temperature is equal to and below 26 Degrees then turn off LED Light 1.

• If the Humidity exceeds over 40% then turn on LED Light 2 for 5 seconds, else if the humidity is equal to and below 40% then turn off LED Light 2.

• If the Light intensity exceeds over 510 nanometres then turn on LED Light 3 for 5 seconds, else if the light intensity is equal to and below 510nm then turn off LED Light 3.

First Set of Code: It measures the temprature, light and humidity and outputs the results on a terminal window.

    #include "contiki.h" //Contiki Header File 
    #include "dev/light-sensor.h" //Light Sensor Header File 
    #include "dev/sht11-sensor.h" //Temperature and Humidity Header File 

    #include <stdio.h> /* for printf() */ // standard input/output library needed to write to the standard output

    static struct etimer timer; //Process Requires a Timer 
    int light=0, temp=0, humid=0;

    //To Start

    /*___________________________________________________*/

    PROCESS(sensor_reading_process, "Sensor Reading Process");
    AUTOSTART_PROCESSES(&sensor_reading_process);

    /*___________________________________________________*/

    //PROCESS BEGINS 

    PROCESS_THREAD(sensor_reading_process, ev, data)
   {
     PROCESS_BEGIN();

     SENSORS_ACTIVATE(light_sensor); //Activate the Light Sensor
     SENSORS_ACTIVATE(sht11_sensor);//Activate Temp/Humidity Sensor

     etimer_set(&timer, CLOCK_CONF_SECOND);//Configuring Timer 1SEC

     while(1) {  //Start of While Loop 

     PROCESS_WAIT_EVENT_UNTIL(ev==PROCESS_EVENT_TIMER);//Wait4Time


    // This is how we get the Sensor Values for light, temp, hum
    light = light_sensor.value(LIGHT_SENSOR_PHOTOSYNTHETIC);
    temp = sht11_sensor.value(SHT11_SENSOR_TEMP);
    humid = sht11_sensor.value(SHT11_SENSOR_HUMIDITY);

    printf("Light=%d, Temp=%d, Humid=%d\n", light, temp, humid); 
    //Above Line if Print Plus Values 
    etimer_reset(&timer); //Reset the Timer 

    }

    PROCESS_END(); //End of Process
     }

The second code:

    #include "contiki.h" 
    #include "leds.h"  // LED HEADER FILE 
    #include <stdio.h> /* for printf() */ 
    static struct etimer timer; 
    /*____________________________________________________*/ 
    PROCESS(led_blinking_process, "LED Blinking Process"); 
    PROCESS(LED_process, "LED process"); 
    AUTOSTART_PROCESSES(&LED_process); 
    /*____________________________________________________*/ 


     PROCESS_THREAD(LED_process, ev, data) 
     { 
     static int count = 0; 
     PROCESS_BEGIN(); 
     etimer_set(&timer, CLOCK_CONF_SECOND/2); // 0.5S timer 
     leds_init(); // initialise the LEDs 
     while(1) { 
     PROCESS_WAIT_EVENT_UNTIL(ev==PROCESS_EVENT_TIMER); // wait for timer             event 
    count++; // count the blinking times 
    process_start(&led_blinking_process, NULL); // to blink the BLUE Led 
    printf("Count: %d\n", count); // output the counter number to console 
    etimer_reset(&timer); // reset the timer 
    } 
    PROCESS_END(); 
    } 


    CODE FOR LED LIGHTS 

    /*____________________________________________________*/ 
    PROCESS_THREAD(led_blinking_process, ev, data) 
    { 
    PROCESS_BEGIN(); 
    leds_toggle(LEDS_BLUE); // Blinking the Blue LED 
    PROCESS_END(); 
    }

Please note both codes work when I run them , I am just trying to combine them , use a if statement so i can meet my functions I have stated above.

Thanks in advance for any help or contributions!

Aucun commentaire:

Enregistrer un commentaire