Here is the link to lmic : arduino-lmic-v1.5/lmic.h at master · tftelkamp/arduino-lmic-v1.5 · GitHub
and the tinyGPS should be this : https://github.com/mikalhart/TinyGPS/blob/master/TinyGPS.h
Here is the hardware library I used for Atmega 1284 : GitHub - MCUdude/MightyCore: Arduino hardware package for ATmega1284, ATmega644, ATmega324, ATmega324PB, ATmega164, ATmega32, ATmega16 and ATmega8535
And the Atmega 2560 core is the original one that comes with Arduino IDE.
(It WORKED with Atmega 2560, not 1284)
And the full sketch :
#include <Wire.h>
#include <RTClibExtended.h>
#include <LowPower.h>
#include <lmic.h>
#include <hal/hal.h>
#include <SPI.h>
#include <SoftwareSerial.h>
#include <TinyGPS.h>
int rtcpin = 5;
int gpspin = 4;
#define wakePin 3 //use interrupt 0 (pin 2) and run function wakeUp when pin 2 gets LOW
#define ledPin 13
//#define alarmingPin 7
volatile bool txcompletion = true; // helps send just once
static const PROGMEM u1_t NWKSKEY[16] = { 0xDB, 0x64, 0xF6, 0xCF, 0x4D, 0x5A, 0xAB, 0x96, 0xFC, 0xF5, 0xFF, 0x45, 0xF8, 0x35, 0xCF, 0xBC };
static const u1_t PROGMEM APPSKEY[16] = { 0x23, 0xB4, 0x10, 0x60, 0x8E, 0x41, 0xBE, 0x84, 0x41, 0x9B, 0x36, 0xE4, 0x69, 0xA5, 0x54, 0xD0 };
static const u4_t DEVADDR = 0x26011EEB;
void os_getArtEui (u1_t* buf) { }
void os_getDevEui (u1_t* buf) { }
void os_getDevKey (u1_t* buf) { }
RTC_DS3231 RTC; //we are using the DS3231 RTC
TinyGPS gps;
//SoftwareSerial ss(8, 7); //Arduino RX, TX
SoftwareSerial ss(13, 12);
String datastring="";
String datastring1="";
char gps_lon[20]={""};
static char gps_lat[20]={""}; //
const unsigned TX_INTERVAL = 0; //20 idi
static osjob_t initjob,sendjob,blinkjob;
const lmic_pinmap lmic_pins = {
.nss = 11, //changed from 10 not to coincide int0
.rxtx = LMIC_UNUSED_PIN,
.rst = 9,
.dio = {2, 22, 23}, // changed from 6 ve 7 to open space for miso mosi
};
void do_send(osjob_t* j){
if (LMIC.opmode & OP_TXRXPEND) {
Serial.println("OP_TXRXPEND, not sending");
} else {
// Prepare upstream data transmission at the next possible time.
LMIC_setTxData2(1, gps_lat, sizeof(gps_lat)-1, 0); //gps_lat, changed with dataoutgoing
Serial.print("data=");
Serial.println(gps_lat);
Serial.println("Packet queued");
Serial.println(LMIC.freq);
delay(15000);
}
}
void onEvent (ev_t ev) {
Serial.print(os_getTime());
Serial.print(": ");
Serial.println(ev);
switch(ev) {
case EV_SCAN_TIMEOUT:
Serial.println("EV_SCAN_TIMEOUT");
break;
case EV_BEACON_FOUND:
Serial.println("EV_BEACON_FOUND");
break;
case EV_BEACON_MISSED:
Serial.println("EV_BEACON_MISSED");
break;
case EV_BEACON_TRACKED:
Serial.println("EV_BEACON_TRACKED");
break;
case EV_JOINING:
Serial.println("EV_JOINING");
break;
case EV_JOINED:
Serial.println("EV_JOINED");
break;
case EV_RFU1:
Serial.println("EV_RFU1");
break;
case EV_JOIN_FAILED:
Serial.println("EV_JOIN_FAILED");
break;
case EV_REJOIN_FAILED:
Serial.println("EV_REJOIN_FAILED");
break;
case EV_TXCOMPLETE:
Serial.println("EV_TXCOMPLETE (includes waiting for RX windows)");
txcompletion = false;
if(LMIC.dataLen) {
// data received in rx slot after tx
Serial.print("Data Received: ");
Serial.write(LMIC.frame+LMIC.dataBeg, LMIC.dataLen);
Serial.println(); //the rest of the code is a later addition
Serial.print(F("Received "));
Serial.print(LMIC.dataLen);
Serial.print(F(" bytes for downlink: 0x"));
for (int i = 0; i < LMIC.dataLen; i++) {
if (LMIC.frame[LMIC.dataBeg + i] < 0x10) {
Serial.print(F("0"));
}
Serial.print(LMIC.frame[LMIC.dataBeg + i], HEX);
}
Serial.println();
}
// Schedule next transmission
os_setTimedCallback(&sendjob, os_getTime()+sec2osticks(TX_INTERVAL), do_send);
break;
case EV_LOST_TSYNC:
Serial.println("EV_LOST_TSYNC");
break;
case EV_RESET:
Serial.println("EV_RESET");
break;
case EV_RXCOMPLETE:
// data received in ping slot
Serial.println("EV_RXCOMPLETE");
break;
case EV_LINK_DEAD:
Serial.println("EV_LINK_DEAD");
break;
case EV_LINK_ALIVE:
Serial.println("EV_LINK_ALIVE");
break;
default:
Serial.println("Unknown event");
break;
}
}
void telltime()
{
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
DateTime now = RTC.now();
}
//-------------------------------------------------
void wakeUp() // here the interrupt is handled after wakeup
{
Serial.println("wakeup worked");
}
void getGPS () {
bool newData = false;
bool checkdata = true;
unsigned long chars;
unsigned short sentences, failed;
for (unsigned long gpscheck = millis(); millis() - gpscheck < 600000;)
{
if (checkdata)
{
delay(1000);
Serial.println("gps searching");
// For one second we parse GPS data and report some key values
for (unsigned long start = millis(); millis() - start < 1000;)
{
while (ss.available())
{
char c = ss.read(); //changed with serial
// Serial.write(c); // uncomment this line if you want to see the GPS data flowing
if (gps.encode(c)) // Did a new valid sentence come in?
{newData = true;
delay(100);
Serial.println("new data true");// for debugging
}
}
}
if (newData)
{
checkdata = false;
float flat, flon;
unsigned long age;
gps.f_get_position(&flat, &flon, &age);
flat == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flat, 6;
flon == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flon, 6;
// Once the GPS fixed,send the data to the server.
datastring +=dtostrf(flat, 0, 6, gps_lat);
datastring1 +=dtostrf(flon, 0, 6, gps_lon);
Serial.println(strcat(strcat(gps_lon,","),gps_lat));
strcpy(gps_lat,gps_lon);
Serial.println(gps_lat); //Print gps_lon and gps_lat;
telltime ();
senddata ();
}
}
else
break;
}
}
void alarmset() {
RTC.alarmInterrupt(1, true);
attachInterrupt(digitalPinToInterrupt(10), wakeUp, LOW); //use interrupt 0 (pin 2) and run function wakeUp when pin 2 gets LOW
digitalWrite(gpspin, HIGH);
delay(1000);
//digitalWrite(rtcpin, LOW);
byte savedPCICR = PCICR;
PCICR = 0; // Disable all pin change interrupts
delay(1000);
void hal_sleep (void);
LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF); //arduino enters sleep mode here
detachInterrupt(1); //execution resumes from here after wake-up
//digitalWrite(rtcpin, HIGH);
digitalWrite(gpspin, LOW);
telltime();
RTC.armAlarm(1, false);
RTC.clearAlarm(1);
RTC.alarmInterrupt(1, false);
PCICR = savedPCICR; // Restore any pin change interrupts that were disabled
getGPS ();
delay(1000);
}
void senddata(){
while (txcompletion)
{
os_runloop_once();
delay(1000);
}
txcompletion = true;
}
void setup() {
Serial.begin(115200);
ss.begin(9600);
//Initialize communication with the clock
Wire.begin();
pinMode(gpspin, OUTPUT);
RTC.begin();
RTC.adjust(DateTime(__DATE__, __TIME__)); //set RTC date and time to COMPILE time
//clear any pending alarms
RTC.armAlarm(1, false);
RTC.clearAlarm(1);
RTC.alarmInterrupt(1, false);
RTC.armAlarm(2, false);
RTC.clearAlarm(2);
RTC.alarmInterrupt(2, false);
RTC.writeSqwPinMode(DS3231_OFF);
telltime ();
while(!Serial);
Serial.println("Starting");
}
//------------------------------------------------------------
void loop() {
RTC.setAlarm(ALM1_MATCH_HOURS, 31, 18, 0); //set your wake-up time here
telltime ();
alarmset ();
RTC.setAlarm(ALM1_MATCH_HOURS, 53, 19, 0); //set your wake-up time here
telltime ();
alarmset ();
}