PJON, my Son!

October 5th, 2015 4 min read

Series

This post is part of the Automatic Curtains series. Check out the other posts here:

    Tags

    Ads via Carbon

    Since my son Enzo was born, I haven’t really had the time to work on my projects. The upside of this is the fact the the world keeps evolving while my project’s are on hold. One of the best improvements in the last few weeks, is the release of the new PJON library.

    A few weeks ago, I already wrote about this promising library. But back then, it really suffered some bugs and usability issues. Luckily Giovanni Blu Mitolo did a hell of a job in improving the library. So, time to give it an other try!

    The best improvement of the library is the possibility to send arbitrary values. Something that wasn’t really easy to do in the first version, but is easy as pie in the latest release.

    To give it try, I connected OLED screens to two Arduino’s (in this case a Nano and a Pro Mini), and added a rotary encoder to one of the Arduino’s. This way I can change a value on Arduino 1, and display the value on Arduino 2.

    In this image, the two Arduino’s are connected with one wire. Event though the white cable contains 4 wires, only one of them is connecting the Arduino’s.
    In this image, the two Arduino’s are connected with one wire. Event though the white cable contains 4 wires, only one of them is connecting the Arduino’s.

    The best part of this is that the data is transmitted over one wire. Not even the ground between the two Arduino’s are connected. (Although this is recommended.)

    Sending and receiving the data is clearly demonstrated in the library example files, but to make it even simpler, this is all you really need:

    Sender

    #include <PJON.h>
    
    // network(Arduino pin used, selected device id)
    PJON network(12, 1);
    
    byte value = 0
    
    void setup() {
        // you could register a network error handler in the
        // setup, but to keep things simple, I leave it out
        // in this example.
    }
    
    void loop() {
        // Run the network update to process the package queue
        network.update();
    };
    
    void sendValue() {
        // create the package.
        char package[1] = {value};
    
        // send(receiver, package, package length)
        network.send(2, package, 1);
    }
    

    Of course, you would need to call the sendValue() function to really send it. In my case this is done after updating the value using the rotary encoder.

    Receiver

    #include <PJON.h>
    
    // network(Arduino pin used, selected device id)
    PJON network(12, 2);
    
    byte value = 0
    
    void setup() {
        // register the function to handle new packets.
        network.set_receiver(receiver_function);
    };
    
    static void receiver_function(uint8_t length, uint8_t *payload) {
        // if a new package is received, take get
        //  the value from from the payload
        value = payload[0];
    }
    
    void loop() {
        // listen 1000 microseconds for new packets.
        network.receive(1000);
    };
    

    As you can see, I didn’t really include any code for the OLED’s or the rotary encoder in the examples above. If you are interested in the implementation of these two elements, leave a comment down below.

    image

    The pitfall

    As you might have noticed, the receive function might take up 1000 microseconds. Since the stepper motors in my automatic curtain system use a step delay much lower than 1000 microseconds, there is a chance I can’t listen for new packets during the movement of my curtains. In this case I can only listen for new packets when the curtain system is idle. Although this isn’t the desired result, it isn’t a real show stopper.

    With the test setup running, it’s time to give it a try on the curtain prototype. Keep an eye on my blog for my next update.

    A big thank you!

    I can only end this post with a big big big thank you to Giovanni Blu Mitolo for creating this awesome library. Not only did he create a beautiful solution for Arduino to Arduino communication, he also incorporated a lot of my personal wished in the final version of the PJON-library. This really shows the beauty of the open source community.

    If you’re looking for a wireless communication solution, make sure to check out Giovanni’s new project: PJON_ASK, a 433Mhz radio transceiver multimaster communication protocol for Arduino.

    Loading comments …
    ©2021 - MichaelTeeuw.nl