/**
 * NRF24l01 - Codigo Servidor, ejemplo 3.
 * Envia una estructura de datos.
 */

#include <SPI.h>
#include <Mirf.h>
#include <nRF24L01.h>
#include <MirfHardwareSpiDriver.h>

#include <LCD.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
LiquidCrystal_I2C lcd( 0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE );

struct DATOS{
    float         temp;
    unsigned long time;
}
datos;


void setup()
{
    lcd.begin(16,2);
    lcd.backlight();

    Mirf.spi    = &MirfHardwareSpi;
    Mirf.cePin  = 8;
    Mirf.csnPin = 10;

    Mirf.init();
    Mirf.setRADDR((byte *)"server");
    Mirf.payload = sizeof(datos);
    Mirf.channel = 10;
    Mirf.config();
}


void loop()
{
    if( millis() % 1000 == 0 )
    {
        datos.time = millis()/1000;
        datos.temp = datos.time * 0.288;

        lcd.setCursor(0,0);
        lcd.print(datos.temp);
        lcd.print(" c");
        lcd.setCursor(10,0);
        lcd.print(datos.time);
        lcd.print(" s");

        if( !Mirf.isSending() )
        {
            Mirf.setTADDR((byte *)"client");
            Mirf.send((byte *)&datos);
        }
    }

}