  *  GPIO2 - must be high to boot
  *  GPIO3 - Serial RX
  *  ADC
+ *  On an ESP12e also:
+ *  GPIO10 - Can be used if sketch flashed with DIO mode
+ *  GPIO9 - Can be used if you have a DIO ESP such as a "real" NodeMCU 1.0
  *  
  * Author: Ant Elder
  * License: Apache License v2
  */
-
 #include <ESP8266WiFi.h>
 #include <lmic.h>
 #include <hal/hal.h>
 #include <SPI.h>
+#include <EEPROM.h>
 
 ADC_MODE(ADC_VCC); // enables reading the ESP8266 supply voltage
 

 *  GND        GND
 *  VCC        VCC
 *  GPIO15     NSS
 *  GPIO12     MOSI
 *  GPIO13     MIS0
 *  GPIO14     SCK
 *  GPIO5      DIO0
 *  GPIO4      DIO1

    .dio = {5, 4, LMIC_UNUSED_PIN},
};

// Create these with ttnctl or the TTN Dashboard at https://staging.thethingsnetwork.org/
// LoRaWAN end-device address
static const u4_t DEVADDR = 0x762C6511;
// LoRaWAN NwkSKey, network session key
static const PROGMEM u1_t NWKSKEY[16] = { 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11 };
// LoRaWAN AppSKey, application session key
static const u1_t PROGMEM APPSKEY[16] = { 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0xD1, 0x11, 0x11, 0x11, 0x11, 0x11 };
                                                                                                                  
volatile boolean dio0Triggered;

// The LORAWAN session state is saved in EEPROM
// Here just the frame sequence number is saved as its ABP with a Single Channel Gateway,
// See http://forum.thethingsnetwork.org/t/new-backend-how-to-connect/1983/91 
#define INITED_FLAG 0x12344321 // <<<< Change this to have the frame sequence restart from zero  
typedef struct {
  uint32 inited;
  u4_t seqnoUp;
} LORA_STATE;
LORA_STATE loraState;

void setup() {
  Serial.begin(115200); Serial.println();

  EEPROM.begin(sizeof(loraState));
  EEPROM.get(0, loraState);
  if (loraState.inited != INITED_FLAG) {
     loraState.inited = INITED_FLAG;
     loraState.seqnoUp = 0;
  }

  lmicInit();
  // the EV_TXCOMPLETE event takes ages to happen, but DIO0 high indicates TX complete 
  // See https://github.com/matthijskooijman/arduino-lmic/issues/16
  attachInterrupt(digitalPinToInterrupt(lmic_pins.dio[0]), dio0ISR, RISING);

  LMIC.seqnoUp = loraState.seqnoUp;

  String vcc = String(ESP.getVcc()); // Needs ESP SDK 1.5.3, see https://github.com/esp8266/Arduino/issues/1961    
  do_send(vcc);
}


// loop runs until the TX Complete event puts the ESP to sleep
void loop() {
