How can i use resend when node failed to send?


(Naruebet Phramrit) #1

If my node was send 1 time but gateway unacceptable. I want to resend which the interval to resend I can determined.

What I have to do like this with this code below ???

#include <lmic.h>
#include <hal/hal.h>
#include <SPI.h>
#include <DHT.h>
#include <DHT_U.h>
#include <Wire.h>
#include <LowPower.h>

#define DHTPIN 9     //DHT pin 
#define DHTTYPE DHT11   // DHT22 or DHT11
DHT dht(DHTPIN, DHTTYPE); 

#include <BH1750.h>
BH1750 lightMeter;
#include <Arduino.h>

static const PROGMEM u1_t NWKSKEY[16] = { };

static const u1_t PROGMEM APPSKEY[16] = {  };


static const u4_t DEVADDR = 0x ;


void os_getArtEui (u1_t* buf) { }
void os_getDevEui (u1_t* buf) { }
void os_getDevKey (u1_t* buf) { }

static osjob_t sendjob;

// Schedule TX every this many seconds (might become longer due to duty
// cycle limitations).
const unsigned TX_INTERVAL = 3600;

// Pin mapping
const lmic_pinmap lmic_pins = {
    .nss = 10,
    .rxtx = LMIC_UNUSED_PIN,
    .rst = 5,
    .dio = {2, 3},
};

void onEvent (ev_t ev) {

            Serial.println(F("TXCOMPLETE"));
            // Schedule next transmission
            for (int i=0; i<int(TX_INTERVAL/8); i++) {
              // Use library from https://github.com/rocketscream/Low-Power
               LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);
            }
            delay(1000);
            do_send(&sendjob);
            
}

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 {
        byte payload[8];
        int analogPin = A0;
        int analogRead_value = 0;
        float scale_voltage = 0; 
        float Final_voltage = 0;
        analogRead_value = analogRead(analogPin);
        scale_voltage = map(analogRead_value,0,1023,0,900);
        Final_voltage = scale_voltage/100;
        //uint32_t Battery_percentage = (Final_voltage-2.7)*100.0/6.3*100;
        uint32_t Battery_percentage = (Final_voltage*10000)/9;

        //Serial.println(analogRead_value);
        //Serial.println(Final_voltage);
        //Serial.println(Battery_percentage,HEX);
      
        uint32_t humid = dht.readHumidity(false) * 100;
        uint32_t temp = dht.readTemperature(false) * 100;
        Serial.println(humid);
        Serial.println(temp);

        uint16_t s_value;
        uint16_t lux = lightMeter.readLightLevel();
        s_value=lux;
       
        payload[4]=s_value&0xFF; //lower byte
        payload[5]=s_value>>8;   //higher byte
        payload[6]=highByte(Battery_percentage);
        payload[7]=lowByte(Battery_percentage);
        Serial.println("high low hum temp");
        Serial.println(highByte(humid),HEX);
        Serial.println(lowByte(humid),HEX);
        Serial.println(highByte(temp),HEX);
        Serial.println(lowByte(temp),HEX);
        Serial.println("---------------");

        payload[0] = highByte(humid);
        payload[1] = lowByte(humid);
        payload[2] = highByte(temp);
        payload[3] = lowByte(temp);
        Serial.println(payload[0]);
        Serial.println(payload[1]);
        Serial.println(payload[2]);
        Serial.println(payload[3]);


        LMIC_setTxData2(1, (uint8_t*)payload, sizeof(payload), 0);
        
        
    }
    // Next TX is scheduled after TX_COMPLETE event.
}

void setup() {   
    Serial.begin(115200);
    Serial.println(F("Starting"));
//Start sensor setup
//  pinMode(sensor, INPUT); //declaring sensor as an output
    lightMeter.begin();
    dht.begin();
    Wire.begin();
    //lightMeter.begin();


    // onetime-measure:

    // LMIC init
    os_init();
    // Reset the MAC state. Session and pending data transfers will be discarded.
    LMIC_reset();

    // Set static session parameters. Instead of dynamically establishing a session
    // by joining the network, precomputed session parameters are be provided.
    #ifdef PROGMEM
    // On AVR, these values are stored in flash and only copied to RAM
    // once. Copy them to a temporary buffer here, LMIC_setSession will
    // copy them into a buffer of its own again.
    uint8_t appskey[sizeof(APPSKEY)];
    uint8_t nwkskey[sizeof(NWKSKEY)];
    memcpy_P(appskey, APPSKEY, sizeof(APPSKEY));
    memcpy_P(nwkskey, NWKSKEY, sizeof(NWKSKEY));
    LMIC_setSession (0x1, DEVADDR, nwkskey, appskey);
    #else
    // If not running an AVR with PROGMEM, just use the arrays directly
    LMIC_setSession (0x1, DEVADDR, NWKSKEY, APPSKEY);
    #endif

    LMIC_setupChannel(0, 923200000, DR_RANGE_MAP(DR_SF12, DR_SF7),  BAND_CENTI);      // g-band
    LMIC_setupChannel(1, 923400000, DR_RANGE_MAP(DR_SF12, DR_SF7B), BAND_CENTI);      // g-band
    LMIC_setupChannel(2, 923600000, DR_RANGE_MAP(DR_SF12, DR_SF7),  BAND_CENTI);      // g-band
    LMIC_setupChannel(3, 923800000, DR_RANGE_MAP(DR_SF12, DR_SF7),  BAND_CENTI);      // g-band    
    LMIC_setupChannel(4, 924000000, DR_RANGE_MAP(DR_SF12, DR_SF7),  BAND_CENTI);      // g-band    
    LMIC_setupChannel(5, 924200000, DR_RANGE_MAP(DR_SF12, DR_SF7),  BAND_CENTI);      // g-band 
    LMIC_setupChannel(6, 924400000, DR_RANGE_MAP(DR_SF12, DR_SF7),  BAND_CENTI);      // g-band          
    LMIC_setupChannel(7, 924600000, DR_RANGE_MAP(DR_SF12, DR_SF7),  BAND_CENTI);      // g-band    

    // Disable link check validation
    LMIC_setLinkCheckMode(0);

    // TTN uses SF9 for its RX2 window.
    LMIC.dn2Dr = DR_SF9;

    // Set data rate and transmit power for uplink (note: txpow seems to be ignored by the library)
    LMIC_setDrTxpow(DR_SF7,14);

    // Start job
    do_send(&sendjob);
}

void loop() {
    os_runloop_once();
}