Skip to content

Raspberry Pi interfacing

Giovanni Blu Mitolo edited this page Apr 5, 2017 · 39 revisions

Thanks to the addition of the interfaces, it is finally possible to use PJON on a Raspberry Pi (or more generally on a Linux machine). For now only the ThoughSerial strategy has been ported for Raspberry Pi. A library providing the low level methods is necessary, for now PJON works using wiringPi that should be already installed in the default raspbian distribution.

If in your case it is missing, type sudo apt-get install wiringPi

Connect the Serial GPIO TX and RX of your Raspberry Pi with an Arduino compatible device (crossing the channels) through a level shifter not to damage the 3v3 rpi serial port.

image

To correctly receive data on Raspberry Pi it may be necessary to disable the serial console, accessing as root to boot/cmdline.txt and removing console=ttyAMA0, 115200 or console=serial0, 115200 if present.

Create a file, copy inside the following program and save it as test.cpp.

  // For printf used below
  #include <stdio.h> 
  // PJON library
  #include <inttypes.h>  
  #include <stdlib.h>
  #include <string.h>
  // RPI serial interface
  #include <wiringPi.h> 
  #include <wiringSerial.h>

#ifndef RPI
  #define RPI true
#endif

#define PJON_INCLUDE_TS true // Include only ThroughSerial
#include "PJON/PJON.h"

int main() {
  printf("PJON instantiation... \n");
  PJON<ThroughSerial> bus(45);

  printf("Opening serial... \n");
  int s = serialOpen("/dev/ttyAMA0", 115200);
  if(s < 0) printf("Serial open fail!");
  if(wiringPiSetup() == -1) printf("WiringPi setup fail");
  printf("Setting serial... \n");
  bus.strategy.set_serial(s);

  printf("Opening bus... \n");
  bus.begin();
  printf("Attempting to send a packet... \n");
  bus.send(44, "B", 1);
  printf("Attempting to roll bus... \n");
  bus.update();
  printf("Attempting to receive from bus... \n");
  bus.receive();
  printf("Success! \n");
  return 0;
};

After you have created the file be sure to include in the same directory the PJON library directory.

Now it is necessary to compile test.cpp program using gcc, open the terminal and navigate to the directory where you created test.cpp, then type:

gcc test.cpp -o compiled_program -std=c++11 -lwiringPi

You should see a new file called compiled_program.

Typing sudo ./compiled_program the program is executed and some logging info should appear.

Now program the receiving Arduino compatible device, using the following sketch:

#include <PJON.h>

// <Strategy name> bus(selected device id)
PJON<ThroughSerial> bus(44);

void setup() {
  pinMode(13, OUTPUT);
  digitalWrite(13, LOW); // Initialize LED 13 to be off
  Serial.begin(115200);
  bus.strategy.set_serial(&Serial);
  bus.begin();
  bus.set_receiver(receiver_function);
};

void receiver_function(
  uint8_t *payload, 
  uint16_t length, 
  const PJON_Packet_Info &packet_info
) {
  if(payload[0] == 'B') {
    digitalWrite(13, HIGH);
    delay(5);
    digitalWrite(13, LOW);
  }
}

void loop() {
  bus.receive(1000);
};

The Arduino should blink as soon as you type sudo ./compiled_program and hit enter.