-

Dependencies:   DHT DS18B20 Decagon10HS DecagonEC-5 FastIO LightSensorALS-PT19 WaterMark200ss WeatherMeters mbed-src floathex1

Fork of testgeneral by Javier Sing

main.cpp

Committer:
agrosmart
Date:
2016-12-14
Revision:
1:7f1e6470cc93
Parent:
0:232c507002f4
Child:
2:a13c531579dd

File content as of revision 1:7f1e6470cc93:

#include "mbed.h"
#include "LightSensor.h"
#include "Decagon10HS.h"
#include "DecagonEC-5.h"
#include "DHT.h"
#include "FastIO.h"
#include "DS18B20.h"
#include "WaterMark200ss.h"
#include "WeatherMeters.h"
#include "Anemometer.h"


//Declaración de Puertos para cada sensor
AnalogIn moisture10HS(A0);  //Decagon 10HS en el A0
AnalogIn light(A1);         //ALS-PT19LIGHT en el A1
AnalogIn moistureEC5(A2);   //Decagon EC-5 en el A2
AnalogIn lightref(A3);      //Referencia 3.3V en el A3
AnalogIn moisture200ss(A4); //WaterMark200ss en el A4

/*El sensor DS18B20 se
define el pin en la
librería DHT*/              //Ds18B20 en el D2
DHT dht22(PB_3,DHT22);      //DHT22 en el D3
CWeatherMeters WeatherMeters(D4, D5, A5); //Weather Station Anemómetro D4, Veleta D5, Pluviómetro A5

//Declaración variables para sensores:
float lightLvl      = 0;    // Sensor LUZ               //Rango: 0.0 a 3.3 //2
float VWC10HS       = 0;    // Sensor 10HS              //Rango: 0.0 a 3.4  //2
float VWCEC5        = 0;    // Sensor EC-5              //Rango: 0.0 a 4 //2
float tempdht22     = 0;    // Sensor DHT22             //Rango: -40.0 a 80 //4
float humdht22      = 0;    // Sensor DHT22             //Rango: 0 a 100% //2
float tempDS18B20   = 0;    // Sensor DS18B20           //Rango: -55.0 a 125.0 //3 
float centibar      = 0;    // Sensor WaterMark 200ss   //Rango: 0 a 240 //2
float rainfall      = 0;    // Weather Station          //Rango: 0 a 100  //3 
float windSpeed     = 0;    // Weather Station          //Rango: 0 a 2000 //3  
float maxWindSpeed  = 0;    // Weather Station          //Rango: 0 a 2000 //3
float windDir  = 0;         // Weather Station          //Rango: 0 a 337.5 //3
float value;

void ReadSensors();

int main()
{
    pc.baud(115200);
    DQ.output();
    DQ = 0;
    DQ.input();
    pc.printf("All Sensors Test \r\n");
    pc.printf("Memory allocated for %d DS18B20.\r\n",MaxROMs);
    pc.printf("Scanning for devices...\r\n");
    ow_reset();
    FindDevices();
    pc.printf("Scanning completed.\r\n");


    while(1) {
        //Leer Sensores
        wait(1);
        ReadSensors(); //No hace lectura de la Temperatura con la ds18b20??
        pc.printf("Direction: %s\n",windDir);
        pc.printf("Depth: %f\n",rainfall);
        pc.printf("Current speed: %f\n", windSpeed);
        pc.printf("Maximum speed: %f\n", maxWindSpeed);
        pc.printf("200ss Centibar: %2.2f\n", centibar);
        pc.printf("DHT22 Humidity: %.1f \n\r",humdht22);
        pc.printf("DHT22 Temp: %.1f C",tempdht22);
        pc.printf("EC-5 VWC: %2.2f VMC\r\n", VWCEC5);
        pc.printf("10HS VWC: %2.2f\r\n", VWC10HS);
        pc.printf("Light: %3.2f \r\n", lightLvl);
        ConvT();                        // Todos los DS18B20 inician la conversión a temp
        wait_ms(750);                   // Tiempo minimo de conversion a 12-bit
        for(int i=1; i<=numROMs; i++) { // Bucle para todos los DS18B20
            tempDS18B20 = Get_Temp(i);
            pc.printf("DS18B20 Temp: %08.4f Device: %02X%02X%02X%02X%02X%02X %03d\r\n",tempDS18B20,FoundROM[i][6],FoundROM[i][5],FoundROM[i][4],FoundROM[i][3],FoundROM[i][2],FoundROM[i][1],i);
        }
        pc.printf("\r\n");
        wait(5.0f);
    }
}

/*
Hay que organizar con funciones, una para petición de datos, otra para cambiarlos a HEX todo

Hay que definir los datos en HEX, para que ocupen lo menos posibles, la temperatura x2 y a hex, y cosas así, para ocupar los menores bytes posibles uniendo
todos y generando un buffer de envío

Las definiciones de la placa tienen que estar como externas, para poder exportar el ejemplo e integrarlo con otros (algo así como el marlin) hay ejemplos
de como se hace
*/
void ReadSensors()
{
    lightLvl = readLightSensor(light,lightref);     //Sensor de Luz
    VWC10HS = Moist10HS(moisture10HS);              //Sensor 10HS
    VWCEC5 = MoistEC5(moistureEC5);                 //Sensor EC-5
    tempdht22 = dht22.ReadTemperature(CELCIUS);     //Sensor DHT22
    humdht22 = dht22.ReadHumidity();                //Sensor DHT22
    centibar = Moist200ss(moisture200ss);           //Sensor WaterMark 200ss
    CWeatherMeters::SMeasurements Measurements;     //Weather Station
    WeatherMeters.GetMeasurements(&Measurements);   //Weather Station
    rainfall = Measurements.Rainfall;               //Weather Station
    windSpeed = Measurements.WindSpeed;             //Weather Station
    maxWindSpeed = Measurements.MaximumWindSpeed;   //Weather Station
    windDir = Measurements.WindDirectionAngle;      //Weather Station
}
char *float2Hex(float value)
{
    int signo; // para las temperaturas
    int ent1;
    int dec1;
    char value2[5];
    
    if(value<0) { 
        signo = 1; // 1 es negativo, 0 es positivo
        value = value * -1;
    } else {
        signo = 0;
    }
    ent1 = value;
    dec1 = (value - ent1)*16;
    sprintf(value2, "%.1X.%.2X.%.1X\n\r", signo, ent1,dec1); //solo queda un decimal. para que queden dos decimales hay que multiplicar decimal por 10
    return value2;
}

/* CALL  char *temphex = temp2Hex(dht22);

FUNCTION

char * createStr() {

    char char1= 'm';
    char char2= 'y';

    char *str = (char *) malloc(sizeof(char) * 3);
    str[0] = char1;
    str[1] = char2;
    str[2] = '\0';

    return str;

}
*/