0

i am trying to send gps data via sim800l gsm module. Working fine without the power down code but when i add the power down code it stops working (not sending the gps data. code execution stops in sendGPS() function). Tried different functions but i could not get it work. Thanks.

#include <TinyGPS++.h>
#include <SoftwareSerial.h>
#include <avr/power.h>
#include <avr/sleep.h>
#include <avr/wdt.h>

volatile bool flag_wdt=HIGH;                           // flag of watchdog
ISR(WDT_vect) {                                        // Watchdog Interrupt Service est exécité lors d'un timeout du WDT
  if(flag_wdt == LOW){flag_wdt = HIGH;}                // Reset watchdog flag
}

void setup_my_watchdog(const byte interval) {
  MCUSR = 0;                              // reset various flags
  WDTCSR |= 0b00011000;                   // see docs, set WDCE, WDE
  WDTCSR =  0b01000000 | interval;        // set WDIE, and appropriate delay
  wdt_reset();
}

TinyGPSPlus gps;
int RXPin = 8;
int TXPin = 9;
int GPSBaud = 9600;
SoftwareSerial gpsSerial(RXPin, TXPin);
SoftwareSerial gprsSerial(2, 3); // RX, TX

void enterSleep(int hour_to_sleep) {
  int i=0;                                // Create a counter
  //time_to_sleep = 360*hour_to_sleep;   // how many 10sec i need to sleep ? 6*60*hour(s)

  // ---------------------------- START Save power -------------------
  power_adc_disable();                  // ADC OFF
  power_spi_disable();                  // SPI OFF
  //power_twi_disable();                  // ? OFF
  power_usart0_disable();               // USART et USB COM OFF
  power_timer0_disable();               // Timer 0 OFF (attention timer0 utilisé par millis ou delay)
  //power_timer1_disable();               // Timer 1 OFF
  //power_timer2_disable();               // Timer 2 OFF
  set_sleep_mode(SLEEP_MODE_PWR_DOWN);    // Configures the Atmega168 for the specified sleep mode
 
  for(i=0 ; i< hour_to_sleep ; i++){       
    setup_my_watchdog(0b100001);          // Set-up first watchdog 8sec
    sleep_enable();                       // Enables the sleep mode to be entered;
    sleep_mode();                         // Entre dans le mode veille choisi
    // -----------------------------------// program restart here after watchdog 8sec timeout
    sleep_disable();                      // First : disable sleep mode
    setup_my_watchdog(0b000111);          // Set-up new watchdog 2sec
    sleep_enable();                       // Enables the sleep mode to be entered;
    sleep_mode();                         // Entre dans le mode veille choisi
    // -----------------------------------// program restart here after watchdog 2sec timeout
  }

  power_adc_enable();                  // ADC ON
  power_timer0_enable();               // Timer 0 ON
  power_usart0_enable();               // USART et USB COM OFF
}

void setup() {
  gpsSerial.begin(GPSBaud);
  gprsSerial.begin(9600);
  delay(10000);
}

void loop() {
    
    sendGPS();
    enterSleep(30);

}

void sendGPS() {
  gpsSerial.listen();
  while (gpsSerial.available() > 0) {
    if (gps.encode(gpsSerial.read()))
      if (gps.location.isValid())
      {
        gprsSerial.listen();
        String geo;
        geo.concat("field1=" + String(gps.location.lat(), 6));
        geo.concat("&field2=" + String(gps.location.lng(), 6));
        gprsSerial.println("AT");
        delay(1000);
        gprsSerial.println("AT+CGATT?");
        delay(1000);

        gprsSerial.println("AT+CIPSHUT");
        delay(1000);

        gprsSerial.println("AT+CIPSTATUS");
        delay(2000);

        gprsSerial.println("AT+CIPMUX=0");
        delay(2000);
        gprsSerial.println("AT+CSTT=\"internet\"");//start task and setting the APN,
        delay(1000);
        gprsSerial.println("AT+CIICR");//bring up wireless connection
        delay(3000);
        gprsSerial.println("AT+CIFSR");//get local IP adress
        delay(2000);
        gprsSerial.println("AT+CIPSPRT=0");
        delay(3000);
        gprsSerial.println("AT+CIPSTART=\"TCP\",\"api.thingspeak.com\",\"80\"");//start up the connection
        delay(6000);
        gprsSerial.println("AT+CIPSEND");//begin send data to remote server
        delay(4000);
        String str = "GET https://api.thingspeak.com/update?api_key=XXXXXXXXX" + String("&") + geo.c_str();
        String str1 = str + String("&");
        String str2 = str1 + geo.c_str();
        gprsSerial.println(str2);
        delay(4000);
        gprsSerial.println((char)26);//sending
        delay(5000);//waitting for reply, important! the time is base on the condition of internet
        gprsSerial.println();
        gprsSerial.println("AT+CIPSHUT");//close the connection
        delay(2000);
        delay(60000);
      }
  }
}

  • Define "stops working"...? – Majenko Jul 11 at 11:56
  • i mean not sending the gps data. code execution stops in sendGPS() function. – GkNx Jul 11 at 11:59
  • 1
    So... you're waking up, seeing there's no data to read (which there won't be - it's been asleep...), then going back to sleep again? Maybe you should actually wait until some valid data arrives before going back to sleep? – Majenko Jul 11 at 12:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.