You are on page 1of 17

SENDER

// This sketch uses a DHT11 sensor to report temperature, humidity and dew point data to
http://www.thingspeak.com.
//
// Sketch tested with an Arduino Uno, a HanRun Ethernet shield and a DHT11 temperature and humidity
sensor.
//
// See http://playground.arduino.cc/main/DHT11Lib for the origins of the temperature, humidity and dew
point functions.

#include <dht11.h>
#include <SPI.h>
#include <Ethernet.h>
#include <LoRa.h>

// ThingSpeak Settings
char thingSpeakAddress[] = "api.thingspeak.com";
String writeAPIKey = ""; // Add your Thingspeak API key here

EthernetClient client;

// Temperature sensor settings


dht11 DHT11;
#define DHT11PIN 7
int counter = 0;

const int ONE_MINUTE = 60 * 1000;

int status;
int failedConnectionAttempCounter;
//Rounds down (via intermediary integer conversion truncation)
//See : http://lordvon64.blogspot.co.uk/2012/01/simple-arduino-double-to-string.html
String dblToString(double input, int decimalPlaces)
{
if( decimalPlaces != 0)
{
String string = String((int)(input*pow(10,decimalPlaces)));

if(abs(input) < 1)
{
if(input > 0)
{
string = "0" + string;
}
else if(input < 0)
{
string = string.substring(0,1) + "0" + string.substring(1);
}
}

return string.substring(0,string.length()-decimalPlaces) + "." + string.substring(string.length() -


decimalPlaces);
}
else
{
return String((int)input);
}
}
// dewPoint function NOAA
// reference: http://wahiduddin.net/calc/density_algorithms.htm
double dewPoint(double celsius, double humidity)
{
double A0= 373.15/(273.15 + celsius);
double SUM = -7.90298 * (A0-1);
SUM += 5.02808 * log10(A0);
SUM += -1.3816e-7 * (pow(10, (11.344*(1-1/A0)))-1) ;
SUM += 8.1328e-3 * (pow(10,(-3.49149*(A0-1)))-1) ;
SUM += log10(1013.246);
double VP = pow(10, SUM-3) * humidity;
double T = log(VP/0.61078); // temp var
return (241.88 * T) / (17.558-T);
}

void setup()
{
Serial.begin(9600);
Serial.println("DHT11 Temperature Sensor Program");
Serial.print("DHT11 library version: ");
Serial.println(DHT11LIB_VERSION);
Serial.println();

// connectToInternet();

///////////////////////////////////////////////////
//dht.begin();

while (!Serial);
Serial.println("LoRa Sender");

if (!LoRa.begin(433E6)) {
Serial.println("Starting LoRa failed!");
while (1);
}
//////////////////////////////////////////////////////

void connectToInternet()
{
if (client.connected())
{
client.stop();
}

Serial.println("Connecting to the internet via ethernet...");

// the media access control (ethernet hardware) address for the shield
// Leave this as is if your MAC address is not labelled on your ethernet shield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore
for(;;){
;
}
}

Serial.println(Ethernet.localIP());
}

void loop()
{
Serial.println("\n");

int dht11ReadingStatus = DHT11.read(DHT11PIN);

Serial.print("Reading sensor...");
switch (dht11ReadingStatus)
{
case DHTLIB_OK:
Serial.println("Success!");
break;
case DHTLIB_ERROR_CHECKSUM:
Serial.println("Checksum error");
break;
case DHTLIB_ERROR_TIMEOUT:
Serial.println("Timeout error");
break;
default:
Serial.println("Unknown error");
break;
}
double dewPointCelcius = dewPoint(DHT11.temperature, DHT11.humidity);

ReportTemperatureToSerialOut(DHT11.temperature, DHT11.humidity, dewPointCelcius);


/////////////////////////////////////////////////////////////////////
//LoRa
Serial.print("Sending packet: ");
Serial.println(counter);

// send packet
LoRa.beginPacket(); //START
//LoRa.print("hello ");
//LoRa.print(counter);
LoRa.write(DHT11.temperature);
//LoRa.print("Temperature= ");
//LoRa.println(DHT11.temperature);
LoRa.write(DHT11.humidity);
//LoRa.print("Humidity= ");
//LoRa.println(DHT11.humidity);
LoRa.write(dewPointCelcius);
//LoRa.print("DEW POINT= ");
//LoRa.println(dewPointCelcius);
LoRa.endPacket(); //FINISH

counter++;
delay(5000);
//////////////////////////////////////////////////////////////////////

//ReportTemperatureToThingspeak(DHT11.temperature, DHT11.humidity, dewPointCelcius);


}
void ReportTemperatureToSerialOut(int temperature, int humidity, double dewPointCelcius)
{
Serial.print("Temperature (oC): ");
Serial.println((float)temperature, 2);

Serial.print("Humidity (%): ");


Serial.println((float)humidity, 2);

Serial.print("Dew Point (oC): ");


Serial.println(dewPointCelcius);
}

void ReportTemperatureToThingspeak(int temperature, int humidity, double dewPoint)


{
// Use short field names i.e. 1 instead of field1
String fields = "1=" + String(temperature, DEC);
fields += "&2=" + String(humidity, DEC);
fields += "&3=" + dblToString(dewPoint, 2);
Serial.println(fields);

if (client.connect(thingSpeakAddress, 80))
{
Serial.println("Connected to thingspeak.com");

// Create HTTP POST Data


client.print("POST /update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: "+writeAPIKey+"\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(fields.length());
client.print("\n\n");
client.print(fields);

Serial.print(fields);
Serial.print("\n");
Serial.println("Fields sent sent to www.thingspeak.com");
delay(ONE_MINUTE);
}
else
{
Serial.println("Connection to thingSpeak Failed");
Serial.println();
failedConnectionAttempCounter++;

// Re-start the ethernet connection after three failed connection attempts


if (failedConnectionAttempCounter > 3 )
{
Serial.println("Re-starting the ethernet connection...");
connectToInternet();
failedConnectionAttempCounter = 0;
}
}
}
RECEIVER
// This sketch uses a DHT11 sensor to report temperature, humidity and dew point data to
http://www.thingspeak.com.
//
// Sketch tested with an Arduino Uno, a HanRun Ethernet shield and a DHT11 temperature and humidity
sensor.
//
// See http://playground.arduino.cc/main/DHT11Lib for the origins of the temperature, humidity and dew
point functions.

#include <dht11.h>
#include <SPI.h>
#include <Ethernet.h>
#include <LoRa.h>

// ThingSpeak Settings
char thingSpeakAddress[] = "api.thingspeak.com";
String writeAPIKey = "4R759FFFBIDPVQJQ"; // Add your Thingspeak API key here

EthernetClient client;

int SS1 =10;


int SS2 =3;
int resetPin=9;
int irqPin=1;

// Temperature sensor settings


dht11 DHT11;
#define DHT11PIN 7
const int ONE_MINUTE = 10 * 1000;

int status;
int failedConnectionAttempCounter;

//Rounds down (via intermediary integer conversion truncation)


//See : http://lordvon64.blogspot.co.uk/2012/01/simple-arduino-double-to-string.html
String dblToString(double input, int decimalPlaces)
{
if( decimalPlaces != 0)
{
String string = String((int)(input*pow(10,decimalPlaces)));

if(abs(input) < 1)
{
if(input > 0)
{
string = "0" + string;
}
else if(input < 0)
{
string = string.substring(0,1) + "0" + string.substring(1);
}
}

return string.substring(0,string.length()-decimalPlaces) + "." + string.substring(string.length() -


decimalPlaces);
}
else
{
return String((int)input);
}
}

// dewPoint function NOAA


// reference: http://wahiduddin.net/calc/density_algorithms.htm
double dewPoint(double celsius, double humidity)
{
double A0= 373.15/(273.15 + celsius);
double SUM = -7.90298 * (A0-1);
SUM += 5.02808 * log10(A0);
SUM += -1.3816e-7 * (pow(10, (11.344*(1-1/A0)))-1) ;
SUM += 8.1328e-3 * (pow(10,(-3.49149*(A0-1)))-1) ;
SUM += log10(1013.246);
double VP = pow(10, SUM-3) * humidity;
double T = log(VP/0.61078); // temp var
return (241.88 * T) / (17.558-T);
}

void setup()
{
Serial.begin(9600);
Serial.println("Thingspeak connect Sensor Program");
//Serial.print("DHT11 library version: ");
//Serial.println(DHT11LIB_VERSION);
//Serial.println();
pinMode(SS1,OUTPUT);
pinMode(SS2,INPUT);
LoRa.setPins(SS2,resetPin,irqPin);
digitalWrite(SS1,LOW);
connectToInternet();
digitalWrite(SS1,HIGH);
//////////////////////////////////////////////////////////////////
digitalWrite(SS2,LOW);
while (!Serial);

Serial.println("LoRa Receiver");

if (!LoRa.begin(433E6)) {
Serial.println("Starting LoRa failed!");
while (1);
}
digitalWrite(SS2,HIGH);
////////////////////////////////////////////////////////////////////

void connectToInternet()
{
//SET ETHERNRET ON
if (client.connected())
{
client.stop();
}

Serial.println("Connecting to the internet via ethernet...");

// the media access control (ethernet hardware) address for the shield
// Leave this as is if your MAC address is not labelled on your ethernet shield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore
for(;;){
;
}
}

Serial.println(Ethernet.localIP());
}
//SET ETHERNET OFF
void loop()
{
Serial.println("\n");
////////////////////////////////////////////////////////
// try to parse packet
digitalWrite(SS2,LOW);
int packetSize = LoRa.parsePacket();
if (packetSize) {
// received a packet
Serial.print("Received packet '");

// read packet
while (LoRa.available()) {
//Serial.print((char)LoRa.read());
int temperatureR = LoRa.read();
int humidityR = LoRa.read();
double dewPointR = LoRa.read();
digitalWrite(SS2,HIGH);

digitalWrite(SS1,LOW);
ReportTemperatureToSerialOut(temperatureR, humidityR, dewPointR);
ReportTemperatureToThingspeak(temperatureR, humidityR, dewPointR);
digitalWrite(SS1,HIGH);
}

/*
// print RSSI of packet
Serial.print("' with RSSI ");
Serial.println(LoRa.packetRssi());*/

}
//////////////////////////////////////////////////////

/*
int dht11ReadingStatus = DHT11.read(DHT11PIN);

Serial.print("Reading sensor...");
switch (dht11ReadingStatus)
{
case DHTLIB_OK:
Serial.println("Success!");
break;
case DHTLIB_ERROR_CHECKSUM:
Serial.println("Checksum error");
break;
case DHTLIB_ERROR_TIMEOUT:
Serial.println("Timeout error");
break;
default:
Serial.println("Unknown error");
break;
}
*/
//double dewPointCelcius = dewPoint(DHT11.temperature, DHT11.humidity);

//SET ETHERNET HIGH


// ReportTemperatureToThingspeak(DHT11.temperature, DHT11.humidity, dewPointCelcius);

void ReportTemperatureToSerialOut(int temperature, int humidity, double dewPointCelcius)


{
Serial.print("Temperature (oC): ");
Serial.println((float)temperature, 2);
Serial.print("Humidity (%): ");
Serial.println((float)humidity, 2);

Serial.print("Dew Point (oC): ");


Serial.println(dewPointCelcius);
}

void ReportTemperatureToThingspeak(int temperature, int humidity, double dewPoint)


{
// Use short field names i.e. 1 instead of field1
String fields = "1=" + String(temperature, DEC);
fields += "&2=" + String(humidity, DEC);
fields += "&3=" + dblToString(dewPoint, 2);
Serial.println(fields);

if (client.connect(thingSpeakAddress, 80))
{
Serial.println("Connected to thingspeak.com");

// Create HTTP POST Data


client.print("POST /update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: "+writeAPIKey+"\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(fields.length());
client.print("\n\n");
client.print(fields);
Serial.print(fields);
Serial.print("\n");
Serial.println("Fields sent sent to www.thingspeak.com");
//delay(ONE_MINUTE);
delay(10000);
}
else
{
Serial.println("Connection to thingSpeak Failed");
Serial.println();
failedConnectionAttempCounter++;

// Re-start the ethernet connection after three failed connection attempts


if (failedConnectionAttempCounter > 3 )
{
Serial.println("Re-starting the ethernet connection...");
connectToInternet();
failedConnectionAttempCounter = 0;
}
}
}

You might also like