Skip to content

Raspberry Pi interfacing

Giovanni Blu Mitolo edited this page Mar 10, 2017 · 39 revisions

Thanks to the addition of the interfaces, it is finally possible to use PJON on a linux machine directly. For now only the ThoughSerial strategy has been ported for Raspberry Pi.

Create a file, copy inside this basic program and save it as test.cpp.

#include <inttypes.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#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(44);

  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 digit:

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

You should see a new file called compiled_program.

Digiting: sudo ./compiled_program you should see positive logging info appearing.

Now lets program the Arduino compatible receiving device, using this simple 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(30);
    digitalWrite(13, LOW);
  }
}

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

Connect the Serial GPIO of your Raspberry Pi with an Arduino Duemilanove/Mega through a level shifter, and enjoy to see the arduino blinking as requested as soon as you digit:

sudo ./compiled_program

and hit enter.