Skip to content

Commit

Permalink
Define macros instead of raw values
Browse files Browse the repository at this point in the history
  • Loading branch information
JandroMejia97 committed Feb 7, 2023
1 parent 3aea46f commit 8aa00b8
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 29 deletions.
28 changes: 28 additions & 0 deletions invernaculo/inc/main.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#ifndef __MAIN_H__
#define __MAIN_H__
#define TIME_INTERVAL_IN_MINUTES 5
#define TIME_INTERVAL_IN_SECONDS TIME_INTERVAL_IN_MINUTES * 60
#define TIME_INTERVAL_IN_MILLIS TIME_INTERVAL_IN_SECONDS * 1000
#define TICKS_TO_CLEAR 600
#define TICKS_TO_SENSE_LIGHT 1
#define TICKS_TO_SENSE_TEMP 2
#define TICKS_TO_SENSE_SOIL 6


#define ONE_HUNDRED_PERCENT 100
#define DHT11_SENSOR_PIN GPIO0
#define LIGHT_SENSOR_PIN CH1
#define SOIL_SENSOR_PIN CH3
/*
* For the measures that we did, the soil sensor was giving us values
* between 410 and 750 for dry and wet soil respectively.
* We are going to use this range to map the values
*/
#define SOIL_ADC_UPPER_LIMIT 613

#define ADC_TOP_VALUE 1023
#define ADC_REFERENCE_VOLTAGE 5.0

#define UART_BAUD_RATE 115200
#define DATA_BUFFER_SIZE 10
#endif
61 changes: 32 additions & 29 deletions invernaculo/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "sapi.h"
#include "soil-sensor.h"
#include "utils.h"
#include "main.h"

void sendToUart(char variableType, int data);

Expand All @@ -15,16 +16,16 @@ int main(void){
boardConfig();

/* Inicializar UART_GPIO a 115200 baudios */
uartConfig(UART_GPIO, 115200);
uartConfig(UART_USB, 115200);
uartConfig(UART_GPIO, UART_BAUD_RATE);
uartConfig(UART_USB, UART_BAUD_RATE);

/* Inicializar AnalogIO */
/* Posibles configuraciones:
* ADC_ENABLE, ADC_DISABLE,
* ADC_ENABLE, ADC_DISABLE,
*/
adcConfig(ADC_ENABLE); /* ADC */
dht11Init(GPIO0); // Inicializo el sensor DHT11
dht11Init(DHT11_SENSOR_PIN); // Inicializo el sensor DHT11

/* Configuración de estado inicial del Led */
bool_t ledState1 = OFF;
Expand All @@ -33,7 +34,7 @@ int main(void){
uint32_t i = 0;

/* Buffer */
static char uartBuff[10];
static char uartBuff[DATA_BUFFER_SIZE];

/* Variable para almacenar el valor leido del ADC CH1 */
uint16_t muestraSOIL = 0;
Expand All @@ -42,14 +43,14 @@ int main(void){
float muestraDHT_HUM = 0;
uint16_t temp = 0;
uint16_t hum = 0;
uint16_t tick = 1;
uint8_t tick = 0;


/* Variables de delays no bloqueantes */
delay_t delay1;

/* Inicializar Retardo no bloqueante con tiempo en ms */
delayConfig(&delay1, 30000); // 5 Minutos
/* Inicializar Retardo no bloqueante con tiempo en ms por 5m */
delayConfig(&delay1, TIME_INTERVAL_IN_MILLIS); // For testing purposes, use 1 second


/* ------------- REPETIR POR SIEMPRE -------------
Expand All @@ -60,38 +61,40 @@ int main(void){
lea a continuación sea de temperatura. Proximamente se envia a la UART su correspondiente dato y se pasa al próximo sensor.
*/
while(1) {
char temp[10];
char temp[DATA_BUFFER_SIZE];

/* delayRead retorna TRUE cuando se cumple el tiempo de retardo */
if (delayRead(&delay1 ) ){
if (delayRead(&delay1 )){
if (tick == TICKS_TO_CLEAR) {
tick = 0;
} else {
tick++;
}

// Seccion para el sensor DHT11
dht11Read(&muestraDHT_HUM, &muestraDHT_TEMP);
// Envio el 0 para avisarle a la uart que el dato proximo corresponde a la temperatura
int temp = round(muestraDHT_TEMP);
sendToUart('0', temp);
// Ahora envio un 1 correspondiente a la humedad del aire
temp = round(muestraDHT_HUM);
sendToUart('1', temp);
if (tick % TICKS_TO_SENSE_TEMP == 0) {
dht11Read(&muestraDHT_HUM, &muestraDHT_TEMP);
// Envio el 0 para avisarle a la uart que el dato proximo corresponde a la temperatura
int temp = round(muestraDHT_TEMP);
sendToUart('0', temp);
// Ahora envio un 1 correspondiente a la humedad del aire
temp = round(muestraDHT_HUM);
sendToUart('1', temp);
}

// Seccion para el sensor de luz
if (tick % 3 == 0){
muestraLDR = 100 - (adcRead(CH1) * 100 / 1023);
// Envio un 3 para avisar que voy a enviar informacion acerca de la cantidad de luz
if (tick % TICKS_TO_SENSE_LIGHT == 0) {
muestraLDR = ONE_HUNDRED_PERCENT - (adcRead(LIGHT_SENSOR_PIN) * ONE_HUNDRED_PERCENT / ADC_TOP_VALUE);
// Envio un 2 para avisar que voy a enviar informacion acerca de la cantidad de luz
sendToUart('2', muestraLDR);
}

// Seccion para el sensor de humedad de la tierra
if (tick % 6 == 0){
muestraSOIL = (1023 - adcRead(CH3)) * 100 / 613;
if (tick % TICKS_TO_SENSE_SOIL == 0){
muestraSOIL = (ADC_TOP_VALUE - adcRead(SOIL_SENSOR_PIN)) * ONE_HUNDRED_PERCENT / SOIL_ADC_UPPER_LIMIT;
// Envio un 3 correspondiente a la humedad de la tierra
sendToUart('3', muestraSOIL);
}

if (tick == 600) {
tick = 0;
} else {
tick++;
}
}
}

Expand All @@ -101,8 +104,8 @@ int main(void){
}

void sendToUart(char variableType, int data) {
char dataString[10];
char uartBuff[10];
char dataString[DATA_BUFFER_SIZE];
char uartBuff[DATA_BUFFER_SIZE];

dataString[0] = variableType;
dataString[1] = '\0';
Expand Down

0 comments on commit 8aa00b8

Please sign in to comment.