Tips - Kvel2D/ecs193-wireless-sensor-network GitHub Wiki

Serial

Print to Serial without anything reading the Serial is bad, causes Arduino to work incorrectly, with hangups and other bad stuff. This means that all nodes except gateway must print nothing, which is done by setting PRINT_DEBUG to false.

sprintf %u vs %lu

sprintf with incorrect formats can result in hard to understand bugs. Make sure all formats fit the types of their variables. For example: if you sprintf “%u,%u” a (uint32, uint16) pair, the resulting print will be “uint32, 0”, because uint32 needs “%lu” format.

Constant type

Make sure that the type of number constants matches your intention. By default, Arduino number constants are of int16 type, so if you make a constant without any suffixes that is greater than INT16_MAX=32768, the constant will overflow.
(1000 * 60 * 5) < this will result in a bug
(1000ul * 60ul * 5ul) < correct

Overflow safety

Millis() is uint32 so it’s maximum is 4bil ms which is ~50 days. Some code is overflow safe, other is not. Read this thread for more info or search online.
All other variables that have potential to overflow should be handled so that overflow doesn't caused undefined behavior.

RAM

Use free_ram() function to check for available RAM. Try to stay as far away as possible from running out. Also remember that various functions in the main code and libraries occupy extra RAM when their variables are on the stack.

Debugging nodes

If a node stops working or appears to be down, the fastest way to check it's status is to "spy" with another node. For example, if you have a network like this 1->2->3 and node #2 appears to be down, you can make another node that pretends to be node #1 and sends packets to node #2. You can similarly spy by making a node listen to another node.

Failure to upload arduino sketch

It's possible to get the board to a state where it will fail to upload a sketch. This is primarily caused by a program that was uploaded to the board previously now interfering with the upload process of a new program. You press upload button in Arduino IDE, code compiles then starts uploading ("Uploading") and fails after some time (no "Done Uploading" message). You might also see that the red LED on the board is flashing(meaning the usual upload process is underway) but then the LED abruptly turns off right when the upload process fails on the IDE. There are a number of ways that this can happen:

  1. The code on the board sleeps the board immediately which disconnects the board's Serial, interrupting the upload process. This is why in the current code the board doesn't sleep for real for the first minute.
  2. The code on the board causes a crash or some other significant failure

Arduino boards do have a short period after startup where they do not run the current program and can be flashed(uploaded). This period is short though and compounded with how slow the Arduino IDE is this leads to a situation where you have to time a board restart and upload process just right so that they can happen before the board starts running the current program and the upload process fails. Restarting via the reset button also doesn't work that well, it's better to unplug and replug the usb cable. In general, just know that this is not an unsolvable problem, you just have to avoid writing code that causes this and if you do end up doing it, you'll have to time the upload process together with a restart which might take a bunch of tries and is very annoying.

Arduino IDE

The Arduino IDE is not very good. Sometimes it will fail to upload or fail to connect to a USB port or fail to compile perfectly fine code. The usual fix is to close and reopen the IDE. This is the first thing you should attempt before looking for any further problems with hardware/software.

Long USB cables

Some long USB cables are too long and won't transmit serial data correctly but will transmit power. You should keep this in mind and if you are using a cable like that to power a node you should make sure to leave a way to disconnect the long cable and plug the board to a good cable for reflashing purposes. Also keep this in mind for the cable between the gateway node and Raspberry Pi - you should check that this cable transmits serial data correctly.