Error handling

PJON is designed to inform the user if an error is detected. A function of type void can be defined and registered to be called by the PJON object in case of error; it receives 3 parameters, the first is the error code of type uint8_t, the second is additional data related to the error of type uint16_t and the third is a general purpose custom pointer of type void *.

Error types:

  • PJON_CONNECTION_LOST or 101, data parameter contains lost packet's index in the buffer.
  • PJON_PACKETS_BUFFER_FULL or 102, data parameter contains buffer length.
  • PJON_CONTENT_TOO_LONG or 104, data parameter contains content length.
void error_handler(uint8_t code, uint16_t data, void *custom_pointer) {
  if(code == PJON_CONNECTION_LOST) {
    Serial.print("Connection with device ID ");
    Serial.print(bus.packets[data].content[0], DEC);
    Serial.println(" is lost.");
  }
  if(code == PJON_PACKETS_BUFFER_FULL) {
    Serial.print("Packet buffer is full, has now a length of ");
    Serial.println(data);
    Serial.println("Possible wrong bus configuration!");
    Serial.println("higher PJON_MAX_PACKETS if necessary.");
  }
  if(code == PJON_CONTENT_TOO_LONG) {
    Serial.print("Content is too long, length: ");
    Serial.println(data);
  }
}

Now inform the instance to call error_handler in case of error:

bus.set_error(error_handler);

This document is automatically generated from the github repository. If you have noticed an error or an inconsistency, please report it opening an issue here

The PJON protocol handbook

This is the second publication about PJON, which contains the documentation, the specification, many application examples and troubleshooting techniques. The book is Made in Italy and within its 204 pages the whole PJON network protocol stack and its strategies are accurately described with graphs, color pictures and comments on its implementation. Choosing this handy format, you will always have PJON with you, also when not being connected to Internet.


BUY NOW!