here you are. I uncomment some serial print for save some space on memory. sorry for midunderstanding
#include <Adafruit_ILI9341.h>
/*******************************************************************************
* Copyright (c) 2019 Thomas Telkamp and Matthijs Kooijman
*
* Permission is hereby granted, free of charge, to anyone
* obtaining a copy of this document and accompanying files,
* to do whatever they want with them without any restriction,
* including, but not limited to, copying, modification and redistribution.
* NO WARRANTY OF ANY KIND IS PROVIDED.
*
* This example sends a valid LoRaWAN packet with payload "Hello,
* world!", using frequency and encryption settings matching those of
* the The Things Network.
*
* This uses OTAA (Over-the-air activation), where where a DevEUI and
* application key is configured, which are used in an over-the-air
* activation procedure where a DevAddr and session keys are
* assigned/generated for use with all further communication.
*
* Note: LoRaWAN per sub-band duty-cycle limitation is enforced (1% in
* g1, 0.1% in g2), but not the TTN fair usage policy (which is probably
* violated by this sketch when left running for longer)!
* To use this sketch, first register your application and device with
* the things network, to set or generate an AppEUI, DevEUI and AppKey.
* Multiple devices can use the same AppEUI, but each device has its own
* DevEUI and AppKey.
*
* Do not forget to define the radio type correctly in config.h.
*
*******************************************************************************/
#include <stdint.h>
#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#include <Adafruit_GFX.h>
//extern const uint16_t SoilMoisture48[];
// For the Adafruit shield, these are the default.
#define TFT_DC 9
#define TFT_CS 10
#define TFT_RST 8
#define TFT_MISO 12
#define TFT_MOSI 11
#define TFT_CLK 13
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_MOSI, TFT_CLK, TFT_RST, TFT_MISO);
#include <dht.h>
#include <lmic.h>
#include <hal/hal.h>
#include <SPI.h>
#include <SimpleTimer.h>
SimpleTimer timer;
dht DHT;
#define DHT11_PIN A0
#define Moisture1 A1
#define lightPin A2
#define Moisture2 A3
#define Moisture3 A4
static float moistureValue = -1;
const int ctl_pin=4; //define the output pin of realy
//static int lightsen_pin=3; //define the input pin of light sensor I DONOT NEED IT
//static int led_pin=8;
// This EUI must be in little-endian format, so least-significant-byte
// first. When copying an EUI from ttnctl output, this means to reverse
// the bytes. For TTN issued EUIs the last bytes should be 0xD5, 0xB3,
// 0x70.
static const u1_t PROGMEM APPEUI[8]={ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
void os_getArtEui (u1_t* buf) { memcpy_P(buf, APPEUI, 8);}
// This should also be in little endian format, see above.
static const u1_t PROGMEM DEVEUI[8]={0xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx};
void os_getDevEui (u1_t* buf) { memcpy_P(buf, DEVEUI, 8);}
// This key should be in big endian format (or, since it is not really a
// number but a block of memory, endianness does not really apply). In
// practice, a key taken from ttnctl can be copied as-is.
// The key shown here is the semtech default key.
static const u1_t PROGMEM APPKEY[16] ={xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx};
void os_getDevKey (u1_t* buf) { memcpy_P(buf, APPKEY, 16);}
static float temperature,humidity,tem,hum;
//ch1: for Temp 67 , ch2 for Humidity 68, ch3: light sensor Analog input0x02 , ch4 digital output 0x01 Relay, ch5 for soil moisture 1 0x02 Analog Input .
// ch 6: for soil moisture 2 0x02 analog input ch7: for soil moisture3 analog input 0x02.
static uint8_t LPP_data[26] = {0x01,0x67,0x00,0x00,0x02,0x68,0x00,0x03,0x02,0x00,0x00,0x04,0x01,0x00,0x05,0x02,0x00,0x00,0x06,0x02,0x00,0x00,0x07,0x02,0x00,0x00}; //0xO1,0x02,0x03,0x04 is Data Channel,0x67,0x68,0x01,0x00 is Data Type
static uint8_t opencml[4]={0x04,0x00,0x64,0xFF},closecml[4]={0x04,0x00,0x00,0xFF}; //the payload of the cayenne or ttn downlink
static unsigned int count = 1;
static osjob_t sendjob;
// Schedule TX every this many seconds (might become longer due to duty
// cycle limitations).
const unsigned TX_INTERVAL = 31;
// Pin mapping
const lmic_pinmap lmic_pins = {
.nss = 10,
.rxtx = LMIC_UNUSED_PIN,
.rst = 9,
.dio = {2, 6, 7},
};
void onEvent (ev_t ev) {
Serial.print(os_getTime());
Serial.print(": ");
switch(ev) {
case EV_SCAN_TIMEOUT:
Serial.println(F("EV_SCAN_TIMEOUT"));
break;
case EV_BEACON_FOUND:
Serial.println(F("EV_BEACON_FOUND"));
break;
case EV_BEACON_MISSED:
Serial.println(F("EV_BEACON_MISSED"));
break;
case EV_BEACON_TRACKED:
Serial.println(F("EV_BEACON_TRACKED"));
break;
case EV_JOINING:
Serial.println(F("EV_JOINING"));
break;
case EV_JOINED:
Serial.println(F("EV_JOINED"));
// Disable link check validation (automatically enabled
// during join, but not supported by TTN at this time).
LMIC_setLinkCheckMode(0);
break;
case EV_RFU1:
Serial.println(F("EV_RFU1"));
break;
case EV_JOIN_FAILED:
Serial.println(F("EV_JOIN_FAILED"));
break;
case EV_REJOIN_FAILED:
Serial.println(F("EV_REJOIN_FAILED"));
break;
break;
case EV_TXCOMPLETE:
Serial.println(F("EV_TXCOMPLETE (includes waiting for RX windows)"));
if (LMIC.txrxFlags & TXRX_ACK)
Serial.println(F("Received ack"));
if(LMIC.dataLen>0)
{
int i,j=0;
uint8_t received[4]={0x00,0x00,0x00,0x00};
Serial.println("Received :");
for(i=9;i<(9+LMIC.dataLen);i++) //the received buf
{
Serial.print(LMIC.frame[i],HEX);
received[j]=LMIC.frame[i];
j++;
Serial.print(" ");
}
Serial.println();
//comparing incoming msg "downlink" with defined value of open relay "open cml"
//in case of this statement TRUE the follwing commands will excute, otherwise jump to next statement.
if ((received[0]==opencml[0])&&(received[1]==opencml[1])&&(received[2]==opencml[2])&&(received[3]==opencml[3])) {
Serial.println("Set pin to HIGH.");
digitalWrite(ctl_pin, HIGH);
}
if ((received[0]==closecml[0])&&(received[1]==closecml[1])&&(received[2]==closecml[2])&&(received[3]==closecml[3])) {
Serial.println("Set pin to LOW.");
digitalWrite(ctl_pin, LOW);
}
}
// Schedule next transmission
os_setTimedCallback(&sendjob, os_getTime()+sec2osticks(TX_INTERVAL), do_send);
break;
case EV_LOST_TSYNC:
Serial.println(F("EV_LOST_TSYNC"));
break;
case EV_RESET:
Serial.println(F("EV_RESET"));
break;
case EV_RXCOMPLETE:
// data received in ping slot
Serial.println(F("EV_RXCOMPLETE"));
break;
case EV_LINK_DEAD:
Serial.println(F("EV_LINK_DEAD"));
break;
case EV_LINK_ALIVE:
Serial.println(F("EV_LINK_ALIVE"));
break;
default:
Serial.println(F("Unknown event"));
break;
}
}
void dhtTem()
{
int16_t tem_LPP;
temperature = DHT.read11(DHT11_PIN); //Read Tmperature data
tem = DHT.temperature*1.0;
humidity = DHT.read11(DHT11_PIN); //Read humidity data
hum = DHT.humidity* 1.0;
int16_t moistureValue = analogRead(Moisture1); // soil moisture value
int16_t moist_result=map(moistureValue,611,284,0,100);
int16_t moistureValue2 = analogRead(Moisture2); // soil moisture2 value
int16_t moist_result2=map(moistureValue2,610,285,0,100);
int16_t moistureValue3 = analogRead(Moisture3); // soil moisture3 value
int16_t moist_result3=map(moistureValue3,605,284,0,100);
int16_t brightness= analogRead(lightPin);
int16_t mapped_brightness = map(brightness,925,14,0,100);
/*
Serial.print("######### Smart Plant from THM ####### ");
Serial.print("COUNT=");
Serial.print(count);
Serial.println(" ###########");
Serial.println(F("The temperature and humidity:"));
Serial.print("[");
Serial.print(tem);
Serial.print("℃");
Serial.print(",");
Serial.print(hum);
Serial.print("%");
Serial.print("]");
Serial.println("");
Serial.print("moisture is: ");
Serial.println(moist_result);
Serial.println( moist_result2);Serial.println( moist_result3);
if (moist_result<65){
//trun on the PUMP or closed the circuit.
digitalWrite(4,LOW);
LPP_data[13]=0x01;
Serial.println(" I Am Dry pls Water!!\n Pump is ON ");
delay(6500);
digitalWrite(4,HIGH);
}
else {
digitalWrite(4,HIGH);
LPP_data[13]=0x00;
}
Serial.print("Britghtness is : ");
Serial.print(mapped_brightness);
if ( mapped_brightness < 10) {
Serial.println(F(" - Dark! "));
Serial.println(F(" I need Light !!"));
} else if ( mapped_brightness < 20) {
Serial.println(" - DIM: ");
} else if ( mapped_brightness < 50) {
Serial.println(" - Light: ");
} else if ( mapped_brightness < 80) {
Serial.println(" -Bright : ");
} else {
Serial.println(" - Very light: ");
}
*/
count++;
tem_LPP=tem * 10;
LPP_data[2] = tem_LPP>>8;
LPP_data[3] = tem_LPP;
LPP_data[6] = hum * 2;
//sensor moisture0
int16_t soil_final= moist_result*100;
LPP_data[16]= soil_final>>8;
LPP_data[17]=soil_final;
int16_t lightresult= mapped_brightness*100;
LPP_data[9]= lightresult>>8 ;
LPP_data[10]= lightresult;
//sensor moisture 2
int16_t soil_final2= moist_result2*100;
LPP_data[20]= soil_final2>>8;
LPP_data[21]=soil_final2;
//sensor mositure 3
int16_t soil_final3= moist_result3*100;
LPP_data[24]= soil_final3>>8;
LPP_data[25]=soil_final3;
tft.fillScreen(ILI9341_BLACK);
unsigned long start = micros();
tft.setCursor(0, 0);
tft.setTextColor(ILI9341_WHITE); tft.setTextSize(3);
tft.println("Smart Plant");
tft.setTextColor(ILI9341_YELLOW); tft.setTextSize(3);
//tft.println("Soil Moisture Result\n");
tft.setTextColor(ILI9341_GREEN); tft.setTextSize(3);
tft.print("Sensor 1:");
tft.setTextColor(ILI9341_WHITE);
tft.print(moistureValue);tft.setTextSize(2);
tft.setTextColor(ILI9341_WHITE);
tft.print(" Hz,");tft.setTextSize(2);
tft.print(moist_result);tft.println("%");
tft.setTextColor(ILI9341_GREEN);
tft.setTextSize(3);
tft.print("\n");
tft.print("Sensor 2:");
tft.setTextSize(3);
tft.setTextColor(ILI9341_WHITE);
tft.print(moistureValue2);
tft.setTextSize(2);
tft.print(" HZ,");
tft.setTextSize(3);
tft.print(moist_result2);
tft.println("%");
tft.print("\n");
tft.setTextColor(ILI9341_GREEN);
tft.setTextSize(3);
tft.print("Sensor 3:");
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.print(moistureValue3);
tft.print(" Hz,");
tft.print(moist_result3);
tft.println("%");
delay(5000);
//****************************************************************************************************************
}
void action_relays(){
relay_ON();
timer.setTimeout(1000,relay_ON);
timer.setTimeout(5000,relay_OFF);
}
void relay_ON(){
digitalWrite(4,LOW);
}
void relay_OFF(){
digitalWrite(4,HIGH);
}
void pinread()
{
int val_relay,val_lightsen;
val_relay=digitalRead(ctl_pin);
//val_lightsen=digitalRead(lightsen_pin);
//Serial.println(" BRightness: 0 is dark and 1 is Sun: ");
//Serial.println( digitalRead( lightsen_pin ));
/*if(val_lightsen==1)
{
LPP_data[9]=0x01;
digitalWrite(led_pin,HIGH);
}
else
{
LPP_data[9]=0x00;
digitalWrite(led_pin,LOW);
}*/
if(val_relay==1)
{
LPP_data[13]=0x01;
}
else
{
LPP_data[13]=0x00;
}
}
void do_send(osjob_t* j){
// Check if there is not a current TX/RX job running
if (LMIC.opmode & OP_TXRXPEND) {
Serial.println(F("OP_TXRXPEND, not sending"));
} else {
dhtTem();
pinread();
// int -> byte array
// byte payload[2];
//payload[0] = lowByte(sensorValue);
//payload[1] = highByte(sensorValue);
// Prepare upstream data transmission at the next possible time.
LMIC_setTxData2(1,LPP_data, sizeof(LPP_data), 0);
Serial.println(F("Packet queued"));
}
// Next TX is scheduled after TX_COMPLETE event.
}
void setup(void) {
Serial.begin(9600);
while(!Serial);
tft.begin();
Serial.println("Connect to TTN and Send data to mydevice cayenne(Use DHT11 Sensor):");
pinMode(ctl_pin,OUTPUT);
//pinMode(lightsen_pin,INPUT);
//pinMode(led_pin,OUTPUT);
// attachInterrupt(1,fire,LOW); //no connect Flame sensor should commented this code
#ifdef VCC_ENABLE
// For Pinoccio Scout boards
pinMode(VCC_ENABLE, OUTPUT);
digitalWrite(VCC_ENABLE, HIGH);
delay(1000);
#endif
// LMIC init
os_init();
// Reset the MAC state. Session and pending data transfers will be discarded.
LMIC_reset();
// Start job (sending automatically starts OTAA too)
do_send(&sendjob);
}
//void fire()
/*{
LPP_data[12]=0x00;
dhtTem();
LMIC_setTxData2(1,LPP_data, sizeof(LPP_data), 0);
Serial.println("Have fire,the temperature is send");
}*/
void loop() {
os_runloop_once();
}