I'm customising the code in emonTH and trying to better understand a few parts of it. Line 111 from the current code caught my eye:
if (Serial) debug = 1; else debug=0;
I'd never seen this used before, so looked into it and couldn't see how it worked, so I wrote a sketch to try it out. Indeed, it doesn't seem to work - it always returns true it seems.
Am I missing something?
Re: if (Serial) debug = 1; else debug=0; line in emonTH code - does it work?
How are you testing this?
I think I know how it should work, and what you're missing is no serial monitor. According to the language: "boolean : returns true if the specified serial port is available.". So it should prevent writing debug information to the serial port if the serial port is not available. But if you're monitoring on the serial port....
Re: if (Serial) debug = 1; else debug=0; line in emonTH code - does it work?
I'm monitoring a pin that I am toggling.
Re: if (Serial) debug = 1; else debug=0; line in emonTH code - does it work?
I think the short answer is that it doesn't seem to work.
Each time it sends data, it outputs ~50 characters to Serial, which in turn means that 50*8/9600 = 0.04s is spent sending the data.
Re: if (Serial) debug = 1; else debug=0; line in emonTH code - does it work?
It doesn't work on my emonTH units. I discovered this because I wrap my LED pulses in if(debug) statements... then noticed it would still flash away happily when I plugged the batteries in!
IOW, if(Serial) is always true, whether or not anything is plugged into the serial pins.
Re: if (Serial) debug = 1; else debug=0; line in emonTH code - does it work?
Yes, if you look in HardwareSerial.h:
I think that all that it will do is either exist or not-exist i.e. if the hardware didn't support Serial it might not compile.
I think this might have a significant impact on battery usage.
Re: if (Serial) debug = 1; else debug=0; line in emonTH code - does it work?
As you found, if(Serial) is just a test if a serial port is available on that hardware (and this will never change on a given hardware). How should it work instead? It would be necessary to test communication with an external host to know that 'something' is connected to the serial port externally.
I have always removed all this 'serial' stuff from low power code. Why not simply use a define like _UseSerial or directly #define debug?
PS: this might be different on a Leonardo, where an existing USB connection can be clearly detected.
Re: if (Serial) debug = 1; else debug=0; line in emonTH code - does it work?
It's an odd way to detect the hardware though, as it will produced a compile time error if the hardware isn't present i.e. the debug = 1/0 will never actually be used.
How should it work? For most of my projects I wait a few seconds at startup and if I detect any serial input, I switch debug output on.
It's not my code, so I was curious about this construct I had never seen before.
Re: if (Serial) debug = 1; else debug=0; line in emonTH code - does it work?
Good point regarding the serial printing.
I have updated the code on GitHub to have debug turned off by default. The .hex file has also been updated. All the emonTH sold through the shop will have debug turned off by default to improve battery life.
https://github.com/openenergymonitor/emonTH/tree/master/emonTH_DHT22_DS1...
Re: if (Serial) debug = 1; else debug=0; line in emonTH code - does it work?
Glyn - on a lot of my sketches (well, code, I'm only really getting into Arduino vs AVR now), I check to see if serial is available for the first 10s of operation, and if so, turn debug output on, otherwise continue.
I can put an example on github later.
Re: if (Serial) debug = 1; else debug=0; line in emonTH code - does it work?
I believe it's not possible to detect if a USB to UART cable is connected, as Jbecker mentioned above the check for Serial checks to see if the serial hardware is available (which is always is on the ATmega328). Things are different on the 32u4 used on the Leonardo.
Re: if (Serial) debug = 1; else debug=0; line in emonTH code - does it work?
I think he means
From the emonTH perspective, listen on the USB port for any input, and if there is something, assume someone is on the other side listening.
From user perspective, when switching on, send anything on UART to have debug activated.
But I may be wrong.
Re: if (Serial) debug = 1; else debug=0; line in emonTH code - does it work?
But if you check for a serial input from an external node, then the UART is already enabled (drawing a little bit of additional current too). Then you can also enable and disable debug output by a command from the external node and it is not necessary to have it enabled all the time.
Re: if (Serial) debug = 1; else debug=0; line in emonTH code - does it work?
Would some code like this do the trick of checking at startup to see if anything is received on serial and if so turning on debug. Obviously a serial string would have to be sent during the two seconds it waits with with UART switching on. If no serial is received the UART can then be turned off to save battery
Re: if (Serial) debug = 1; else debug=0; line in emonTH code - does it work?
Yes, sorry, I wasn't clear. If you call Serial.available after waiting a short period, then use that to set debug. A lot of routers and other embedded devices do similar.