Program stops on battery and Serial1 not working

Hi,

I have a program which sends data once per second. Everything is fine if I plug the Arduino into USB, but if I connect just the battery it will send data just once and after that it stops. I tried to connect a USB - UART on pins 13 and 14, but it does not print anything on the serial. The thing is that if I connect it to USB (just for the power), the serial on 13 and 14 is working.

Any ideas?

Sketch please ?

Also what type and voltage and mAh battery ?

It is a LiPo battery, 3.7V, 2Ah. I tried with a power supply as well.

#include "user_params.h"
#include <SPI.h>
#include <WiFiNINA.h>
#include <Scheduler.h>
#include <Wire.h>
#include <RTCZero.h>
//#include <ArduinoJson.h>
//#include <SparkFunMPU9250-DMP.h>
#include <Fifo.h>

// WIFI
WiFiClient client;
IPAddress server(SERVER_ADDRESS);

// RTC
RTCZero rtc;

// DATA RECORDS
struct DataRecord {
  unsigned long          ts;
  byte          rssi; //: 6;
  uint16_t      bat; //:  10;

  struct AccelData {
    byte  qw; //:   7;
    byte  qx; //:   7;
    byte  qy; //:   7;
    byte  qz; //:   7;
  } accel_data[IMU_FREQ];
};
DataRecord temp_record;
Fifo<DataRecord, DATA_RECORD_LEN> records_fifo;

uint8_t imu_idx = 0;


void setup()
{
  Wire.begin();
  Serial.begin(9600);
  while (!Serial);

  // CONNECT TO WIFI
  connectToWiFi();

  // GET RTC
#if ENABLE_RTC == 1
  rtc.begin();
  unsigned long epoch = 0;
  uint8_t numberOfTries = 0;
  do {
    epoch = WiFi.getTime();
    numberOfTries++;
    delay(500);
  }
  while ((epoch == 0) && (numberOfTries < MAX_EPOCH_TRIES));

  if (epoch == 0) {
    Serial.println("NTP unreachable!!");
    while (1);
  }
  else {
    Serial.print("Epoch received: ");
    Serial.println(epoch);
    rtc.setEpoch(epoch);

    Serial.println();
  }
#endif

  Scheduler.startLoop(sendDataLoop);
  temp_record.ts = rtc.getEpoch();
}

void loop() {
    temp_record.accel_data[imu_idx] = {
      (byte)random(-100, 100), // (byte)(imu.calcQuat(imu.qw) * 100),
      (byte)random(-100, 100), // (byte)(imu.calcQuat(imu.qx) * 100),
      (byte)random(-100, 100), // (byte)(imu.calcQuat(imu.qy) * 100),
      (byte)random(-100, 100), // (byte)(imu.calcQuat(imu.qz) * 100)
    };
    imu_idx++;
  
    if (imu_idx == IMU_FREQ) {
      imu_idx = 0;
      records_fifo.push(temp_record);
      temp_record = DataRecord();
      temp_record.ts = rtc.getEpoch();
    }
  
    delay(5);
}

void sendDataLoop() {
  connectToWiFi();

  if (records_fifo.size() > 0) {
    if (httpPOSTData()) {
      records_fifo.pop();
    }
  }

  delay(500);
}

bool httpPOSTData() {
  Serial.println("Trying to send data");

  if (client.connect(server, SERVER_PORT)) {
    Serial.println("connecting...");


    //    DataRecord record = records_fifo.peek();
    char request_response[1024] = { 0 };
    int request_response_len = 0;
    int request_status = 0;
    char c;

    client.println("POST "SERVER_PATH" HTTP/1.1");
    client.println("Host: 127.0.0.1");
    client.println("User-Agent: ArduinoWiFi/1.1");
    client.println("Connection: close");
    client.println("Content-Type: application/octet-stream");
    client.println("Content-Length: 5");
    client.println();
    client.println("abcde");
    client.println();

    while (!client.available()) {
    }

    // Getting status
    while (client.available()) {
      c = client.read();
      request_response[request_response_len] = c;
      request_response_len++;

      if (request_response_len == 12) {
        request_status = ((request_response[9] - '0') * 10 + (request_response[10] - '0')) * 10 + (request_response[11] - '0');
        Serial.print("Request status: ");
        Serial.println(request_status);

        if (request_status >= 400) {
          Serial.println("SERVER/CLIENT ERROR");
          client.flush();
          client.stop();
          return false;
        }
        else {
          memset(request_response, 0, sizeof(request_response));
          request_response_len = 0;
          break;
        }
      }
    }

    // Getting headers
    while (client.available()) {
      c = client.read();
      request_response[request_response_len] = c;
      request_response_len++;

      if (request_response[request_response_len - 4] == '\r' &&
          request_response[request_response_len - 3] == '\n' &&
          request_response[request_response_len - 2] == '\r' &&
          request_response[request_response_len - 1] == '\n') {
        memset(request_response, 0, sizeof(request_response));
        request_response_len = 0;
        break;
      }
    }

    // Geting payload
    while (client.available()) {
      c = client.read();
      request_response[request_response_len] = c;
      request_response_len++;
    }
    Serial.print("PAYLOAD: ");
    Serial.println(request_response);

    client.flush();
    client.stop();
    return true;
  } else {
    Serial.println("connection failed");
    client.stop();
    return false;
  }
}

void connectToWiFi() {
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print("Attempting to connect to WPA SSID: ");
    Serial.println(SECRET_SSID);

    WiFi.begin(SECRET_SSID, SECRET_PASS);
  }

  Serial.println();
  Serial.print("CONNECTED TO: ");
  Serial.println(SECRET_SSID);
  Serial.print("signal strength (RSSI):");
  Serial.println(WiFi.RSSI());
  Serial.println();
}

If I replace Serial with Serial1, it does not print on the 13 and 14 pins when connected on battery

It looks like the Scheduler library is doing something wrong. If I remove the Scheduler.startLoop line and call sendDataLoop on void loop, it is working.

I cant help you much with that.

Maybe better getting this thread moved to the programming section ?

How should I move it? :slight_smile:

Click "report to moderator" and ask them to move it to the programming section...