#include <SPI.h>
#include <LoRa.h>
#include "DHT.h"

#define DHT11PIN 22
#define AOUT_PIN 36

DHT dht(DHT11PIN, DHT11);

//define the pins used by the transceiver module
#define ss 5
#define rst 15
#define dio0 2


void setup() {
  //initialize Serial Monitor
  Serial.begin(115200);
  dht.begin();
  while (!Serial);
  Serial.println("LoRa Sender");

  //setup LoRa transceiver module
  LoRa.setPins(ss, rst, dio0);
  
  //replace the LoRa.begin(---E-) argument with your location's frequency 
  //433E6 for Asia
  //866E6 for Europe
  //915E6 for North America
  while (!LoRa.begin(433E6)) {
    Serial.println(".");
    delay(500);
  }
   // Change sync word (0xF3) to match the receiver
  // The sync word assures you don't get LoRa messages from other LoRa transceivers
  // ranges from 0-0xFF
  LoRa.setSyncWord(0xF3);
  Serial.println("LoRa Initializing OK!");
}

void loop() {
  int value = analogRead(AOUT_PIN); // read the analog value from sensor
  value = map(value, 0, 4095, 0, 999) ;
  value = (1300- value) ;

  Serial.print("Moisture value: ");
  Serial.println(value);

  float humi = dht.readHumidity();
  float temp = dht.readTemperature();
  Serial.print("Temperature: ");
  Serial.print(temp);
  Serial.print("ºC ");
  Serial.print("Humidity: ");
  Serial.println(humi);


  //Send LoRa packet to receiver
  LoRa.beginPacket();
  LoRa.print(value);
  LoRa.print(",");
  LoRa.print(temp);
  LoRa.print(",");
  LoRa.print(humi);
  LoRa.endPacket();

  delay(1000);
}
