script not running, unless monitor open.

Hi,

I know this has been discussed many times, but I cannot see why this particular script doesn't run unless the Serial Monitor is open.

I'm trying to log temperature sensor data to emoncms.org, using a LinkIt One, over Wifi. This works fine when it's connected to my PC, and the Serial Monitor is open in Arcuino IDE. But as soon as I close the serial monitor, or let it run off a battery, the logging stops.

Here's the full sketch:

#include <LTask.h>
#include <LWiFi.h>
#include <LWiFiClient.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include<LBattery.h>

OneWire  ds(8);  // on pin 8 (a 4.7K resistor is necessary)
DallasTemperature sensors(&ds); // Pass our oneWire reference to Dallas Temperature.

#define WIFI_AP "xxxxxxxxxxxx"
#define WIFI_PASSWORD "xxxxxxxxxxxx"
#define WIFI_AUTH LWIFI_WPA  // choose from LWIFI_OPEN, LWIFI_WPA, or LWIFI_WEP according to your WiFi AP configuration

LWiFiClient client;

// emoncms Settings
char* server = "emoncms.org";
String writeAPIKey = "xxxxxxxxxxxx";
int node = 1; //if 0, not used

int sent = 0;
int myPeriodic = 30;

DeviceAddress Room1Temp = {  0x28, 0xBA, 0xB6, 0x65, 0x05, 0x00, 0x00, 0x21 };
DeviceAddress Room1RoofTemp = { 0x28, 0x52, 0xCA, 0xA0, 0x07, 0x00, 0x00, 0x12 };
DeviceAddress Room2Temp = { 0x28, 0x1D, 0xB3, 0xE6, 0x05, 0x00, 0x00, 0x7B }; // Silver probe
DeviceAddress Room2RoofTemp = { 0x28, 0x92, 0xDC, 0x98, 0x05, 0x00, 0x00, 0x76 }; // Black probe
DeviceAddress Room3Temp = {0x28, 0x50, 0x00, 0xA0, 0x07, 0x00, 0x00, 0xE3 };
DeviceAddress Room3RoofTemp = {0x28, 0x50, 0x00, 0xA0, 0x07, 0x00, 0x00, 0xE3 };
DeviceAddress KitchenTemp = {0x28, 0x50, 0x00, 0xA0, 0x07, 0x00, 0x00, 0xE3 };
DeviceAddress DiningRoomTemp = {0x28, 0x50, 0x00, 0xA0, 0x07, 0x00, 0x00, 0xE3 };
DeviceAddress HallTemp = {0x28, 0x50, 0x00, 0xA0, 0x07, 0x00, 0x00, 0xE3 };
DeviceAddress TVRoomTemp = {0x28, 0x50, 0x00, 0xA0, 0x07, 0x00, 0x00, 0xE3 };
DeviceAddress OutsideTemp = {0x28, 0x50, 0x00, 0xA0, 0x07, 0x00, 0x00, 0xE3 };
DeviceAddress PatioTemp = {0x28, 0x50, 0x00, 0xA0, 0x07, 0x00, 0x00, 0xE3 };


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

  sensors.begin(); // Start up the library
  int res = 12;  // set the resolution to 12 bit. Lower is faster. 9-12 bit
  sensors.setResolution(Room1Temp, res);
  sensors.setResolution(Room1RoofTemp, res);
  sensors.setResolution(Room2Temp, res);
  sensors.setResolution(Room2RoofTemp, res);
  sensors.setResolution(Room3Temp, res);
  sensors.setResolution(Room3RoofTemp, res);
  sensors.setResolution(KitchenTemp, res);
  sensors.setResolution(DiningRoomTemp, res);
  sensors.setResolution(HallTemp, res);
  sensors.setResolution(OutsideTemp, res);
  sensors.setResolution(TVRoomTemp, res);
  sensors.setResolution(PatioTemp, res);


  Serial.println("Connecting to AP"); // keep retrying until connected to AP
  while (0 == LWiFi.connect(WIFI_AP, LWiFiLoginInfo(WIFI_AUTH, WIFI_PASSWORD)))
  {
    delay(1000);
    Serial.println("Connected to AP");
  }

  startWifi();

} // END setup()

void loop()
{
  float Room1TempC;
  float Room1RoofTempC;
  float Room2TempC;
  float Room2RoofTempC;
  float Room3TempC;
  float Room3RoofTempC;
  float KitchenTempC;
  float DiningRoomTempC;
  float HallTempC;
  float OutsideTempC;
  float TVRoomTempC;
  float PatioTempC;

  sensors.requestTemperatures();
  Room1TempC = sensors.getTempC(Room1Temp);
  Room1RoofTempC = sensors.getTempC(Room1RoofTemp);
  Room2TempC = sensors.getTempC(Room2Temp);
  Room2RoofTempC = sensors.getTempC(Room2RoofTemp);
  Room3TempC = sensors.getTempC(Room3Temp);
  Room3RoofTempC = sensors.getTempC(Room3RoofTemp);
  KitchenTempC = sensors.getTempC(KitchenTemp);
  DiningRoomTempC = sensors.getTempC(DiningRoomTemp);
  HallTempC = sensors.getTempC(HallTemp);
  OutsideTempC = sensors.getTempC(OutsideTemp);
  TVRoomTempC = sensors.getTempC(TVRoomTemp);
  PatioTempC = sensors.getTempC(PatioTemp);

  if (client.connect(server, 80)) {
    Serial.print("Connected to ");
    Serial.println(server);

    String postStr;
    // Room1Temp
    if ((Room1TempC == 0.00) || (Room1TempC == 85.00) || (Room1TempC == -127.00)) {
      Serial.println("Room1Temp: " + String(Room1TempC));
    } else {
      postStr += "Room1:";
      postStr += String(Room1TempC);
      Serial.println("Room1Temp: " + String(Room1TempC));
    }
    //Room1RoofTemp
    if ((Room1RoofTempC == 0.00) || (Room1RoofTempC == 85.00) || (Room1RoofTempC == -127.00)) {
      Serial.println("Room1TempRoof: " + String(Room1RoofTempC));
    } else {
      postStr += "&Room1Roof:";
      postStr += String(Room1RoofTempC);
      Serial.println("Room1RoofTemp: " + String(Room1RoofTempC));
    }
    //Room2Temp
    if ((Room2TempC == 0.00) || (Room2TempC == 85.00) || (Room2TempC == -127.00)) {
      Serial.println("Room2Temp: " + String(Room2TempC));
    } else {
      postStr += "&Room2:";
      postStr += String(Room2TempC);
      Serial.println("Room2Temp: " + String(Room2TempC));
    }
    //Room2RoofTemp
    if ((Room2RoofTempC == 0.00) || (Room2RoofTempC == 85.00) || (Room2RoofTempC == -127.00)) {
      Serial.println("Room2RoofTemp: " + String(Room2RoofTempC));
    } else {
      postStr += "&Room2Roof:";
      postStr += String(Room2RoofTempC);
      Serial.println("Room2RoofTemp: " + String(Room2RoofTempC));
    }
    //Room3Temp
    if ((Room3TempC == 0.00) || (Room3TempC == 85.00) || (Room3TempC == -127.00)) {
      Serial.println("Room3Temp: " + String(Room3TempC));
    } else {
      postStr += "&Room3:";
      postStr += String(Room3TempC);
      Serial.println("Room3Temp: " + String(Room3TempC));
    }
    if (postStr != NULL) {
      client.print("GET /input/post?apikey=");
      client.print(writeAPIKey);
      if (node > 0) {
        client.print("&node=");
        client.print(node);
      }
      client.print("&json={");
      //    client.print("Room1:");
      //    client.print(Room1TempC);
      client.print(postStr);
      client.println("} HTTP/1.1");
      client.println("Host:emoncms.org");
      client.println("User-Agent: Arduino-ethernet");
      client.println("Connection: close");
      client.println();
      client.read();

//Debugging URL to be posted to emoncms
      Serial.print("GET /input/post.json?apikey=");
      Serial.print(writeAPIKey);
      if (node > 0) {
        Serial.print("&node=");
        Serial.print(node);
      }
      Serial.print("&json={");
      Serial.print(postStr);
      Serial.println();
    }

    delay(1000);
  }//end if
  sent++;
client.stop();

  int count = myPeriodic;
  while (count--)
    delay(1000);

} // END loop()


void startWifi()
{
  client.stop();
  Serial.println("Connecting to emoncms.org ...");
  Serial.println();
  delay(1000);
}

but I cannot see why this particular script doesn't run unless the Serial Monitor is open.

I can't, either, because you use a lot of non-standard libraries that you did not post links to.

I guess we need to know more about the battery, to start. And how much current the Linkit draws and how the battery is connected and what else is consuming current. You give little to work with, so far.

groundfungus:
I guess we need to know more about the battery, to start. And how much current the Linkit draws and how the battery is connected and what else is consuming current. You give little to work with, so far.

Forget about the battery for a moment.

It does the same while connected to my laptop, WITHOUT the battery. i.e. it gets power from the USB port. I can see the script doesn't run cause the temperature readings fall behind. As soon as I open the Serial Monitor in the Arduino IDE (version 1.6.5) it starts logging again.

But it's a 1050maH Lithium-ion battery.

I'm not sure how to measure the current draw, but it's plugged into the USB port on my laptop. There's a single DS18B20 sensor, on a breadboard, with a 4K7 resistor over the + and Data pins of the DS18B20. Similar to thishttps://create.arduino.cc/projecthub/everth-villamil-ruiz/temperature-sensor-ds18b20-3decfc

Create a function to blink the onboard LED (if there is one, or an external one if not). Call that function at various places in the code, to see whether setup() starts, ends, etc.

I think I found the problem. There's a switch on the LinkIt One, which I need to switch over between USB and battery, depending on what I use to power the device. It seems like it's faulty on the USB side, would need to replace it.

Ok, so the switch wasn't the problem as I thought. I hooked up a relay (it's easier to listen to clicks, than watch blinks :wink: ) and can hear the relay clicking away every 100ms while the Serial Monitor is open, but stop as soon as I close the Serial Monitor.
This is with, and without the battery attached.

Any pointers will be great!

I modified the blink script so that it can activate the relay on port (using a Grove relay kit with adapter cable for this) and it blinks, I mean clicks away fine

/*
  Blink
  Turns on an LED on for one second, then off for one second, repeatedly.

  Most Arduinos have an on-board LED you can control. On the Uno and
  Leonardo, it is attached to digital pin 13. If you're unsure what
  pin the on-board LED is connected to on your Arduino model, check
  the documentation at http://www.arduino.cc

  This example code is in the public domain.

  modified 8 May 2014
  by Scott Fitzgerald
 */


// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin 13 as an output.
  pinMode(0, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(0, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);              // wait for a second
  digitalWrite(0, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);              // wait for a second
}

Which Arduino?...

septillion:
Which Arduino?...

As per the OP, it's a Linkit One

Ah, sorry. LinkIt One isn't one of the normal Arduino's so missed it when looking through the OP. With an exotic board like that I'll leave you to it :slight_smile:

Hi,

But it's a 1050maH Lithium-ion battery.

Can you post a picture of your battery and can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Can you post a picture of your project with the battery plugged in please?

Do you have a DMM to measure circuit parameters?

Thanks.. Tom... :slight_smile:

TomGeorge:
Hi,Can you post a picture of your battery and can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Can you post a picture of your project with the battery plugged in please?

Do you have a DMM to measure circuit parameters?

Thanks.. Tom... :slight_smile:

Tom I don't think it's the battery at all.

The "Blink" example code runs fine, on it's own, with just the battery plugged in.

Another script I wrote some time ago to login sensor data to thinkspeak.com works fine, also with just the battery connected to the LinkIt one. I modified that exact same script (i.e. using the same libraries) to work the emoncms.org, and lot more DS18B20 sensors, which are on a breadboard at this stage.

Here's the script which runs without the Serial Monitor being open, and disconnected from my PC:

#include <LTask.h>
#include <LWiFi.h>
#include <LWiFiClient.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include<LBattery.h>

#define myPeriodic 30 //in sec | Thingspeak updates in sec

OneWire  ds(8);  // on pin 8 (a 4.7K resistor is necessary)
DallasTemperature sensors(&ds); // Pass our oneWire reference to Dallas Temperature.

#define WIFI_AP "xxxxx"
#define WIFI_PASSWORD "xxxxxx"
#define WIFI_AUTH LWIFI_WPA  // choose from LWIFI_OPEN, LWIFI_WPA, or LWIFI_WEP according to your WiFi AP configuration

LWiFiClient client;

DeviceAddress Room1Temp = {  0x28, 0xBA, 0xB6, 0x65, 0x05, 0x00, 0x00, 0x21 };
DeviceAddress GarageRoofTemp = { 0x28, 0x52, 0xCA, 0xA0, 0x07, 0x00, 0x00, 0x12 };
DeviceAddress PVTemp = { 0x28, 0x1D, 0xB3, 0xE6, 0x05, 0x00, 0x00, 0x7B }; // Silver probe
DeviceAddress UnderPVTemp = { 0x28, 0x92, 0xDC, 0x98, 0x05, 0x00, 0x00, 0x76 }; // Black probe
DeviceAddress OutsideTemp = {0x28, 0xDA, 0x9E, 0xA1, 0x07, 0x00, 0x00, 0x5C };

 
// ThingSpeak Settings
char* server = "api.thingspeak.com";
String writeAPIKey = "xxxxxx";


int sent = 0;


void setup()
{
  
  pinMode(0, OUTPUT);
  LWiFi.begin();
  Serial.begin(115200);

  sensors.begin(); // Start up the library
  // set the resolution to 12 bit. Lower is faster. 9-12 bit
  sensors.setResolution(Room1Temp, 12);
  sensors.setResolution(PVTemp, 12);
  sensors.setResolution(UnderPVTemp, 12);
  sensors.setResolution(GarageRoofTemp, 12);
  sensors.setResolution(OutsideTemp, 12);
  
  // keep retrying until connected to AP
  Serial.println("Connecting to AP");
  while (0 == LWiFi.connect(WIFI_AP, LWiFiLoginInfo(WIFI_AUTH, WIFI_PASSWORD)))
  {
    delay(1000);
  }

  startWifi();

} // END setup()

void loop()
{
digitalWrite(0, LOW);
  float Room1TempC;
  float GarageRoofTempC;
  float PVTempC;
  float UnderPVTempC;
  float OutsideTempC;

  //char buffer[10];
  sensors.requestTemperatures();
  Room1TempC = sensors.getTempC(Room1Temp);
  GarageRoofTempC = sensors.getTempC(GarageRoofTemp);
  PVTempC = sensors.getTempC(PVTemp);
  UnderPVTempC = sensors.getTempC(UnderPVTemp);
  OutsideTempC = sensors.getTempC(OutsideTemp);


  if (client.connect(server, 80)) { // use ip 184.106.153.149 or api.thingspeak.com
    //   Serial.println("WiFi Client connected ");
    String postStr = writeAPIKey;

    if ((Room1TempC == 0.00) || (Room1TempC == 85.00) || (Room1TempC == -127.00)) {
      Serial.print("Room1Temp: ");
      Serial.println(Room1TempC);
    } else {
      postStr += "&field1=";
      postStr += String(Room1TempC);
          Serial.println("Room1Temp Temperature: " + String(Room1TempC));
    }

    if ((GarageRoofTempC == 0.00) || (GarageRoofTempC == 85.00) || (GarageRoofTempC == -127.00)) {
      Serial.print("GarageRoofTempC: ");
      Serial.println(GarageRoofTempC);
    } else {
      postStr += "&field2=";
      postStr += String(GarageRoofTempC);
      //      Serial.println("GarageRoofTempC Temperature: " + String(GarageRoofTempC));
    }


    if ((PVTempC == 0.00) || (PVTempC == 85.00) || (PVTempC == -127.00)) {
      Serial.print("PVTempC: ");
      Serial.println(PVTempC);
    } else {
      postStr += "&field3=";
      postStr += String(PVTempC);
      //      Serial.println("PVTempC Temperature: " + String(PVTempC));
    }

    if ((UnderPVTempC == 0.00) || (UnderPVTempC == 85.00) || (UnderPVTempC == -127.00)) {
      Serial.print("PVTempC: ");
      Serial.println(UnderPVTempC);
    } else {
      postStr += "&field4=";
      postStr += String(UnderPVTempC);
      //      Serial.println("UnderPVTempC Temperature: " + String(UnderPVTempC));
    }

    if ((OutsideTempC == 0.00) || (OutsideTempC == 85.00) || (OutsideTempC == -127.00)) {
      Serial.print("OutsideTempC: ");
      Serial.println(OutsideTempC);
    } else {
      postStr += "&field5=";
      postStr += String(OutsideTempC);
      //      Serial.println("OutsideTempC Temperature: " + String(OutsideTempC));
    }

    
    postStr += "&field8=";
    postStr += LBattery.level();

               postStr += "\r\n\r\n";

    client.print("POST /update HTTP/1.1\n");
    client.print("Host: api.thingspeak.com\n");
    client.print("Connection: close\n");
    client.print("X-THINGSPEAKAPIKEY: " + writeAPIKey + "\n");
    client.print("Content-Type: application/x-www-form-urlencoded\n");
    client.print("Content-Length: ");
    client.print(postStr.length());
    client.print("\n\n");
    client.print(postStr);
    Serial.println();
    Serial.println(postStr);
    Serial.println();
    //Serial.println(millis()/60);
    delay(1000);
    digitalWrite(0, HIGH);

  }//end if
  sent++;
  client.stop();


  int count = myPeriodic;
    while (count--)
      delay(1000);

} // END loop()


void startWifi()
{
  client.stop();
  Serial.println("Connecting Thingspeak to network...");
  Serial.println();
  delay(1000);
}