Daniel Rossi
Published © GPL3+

Custom UART Protocol on ESP32

Use OSCUP in your project to speedup the development of a custom UART communication! It is an entirely customizable protocol

IntermediateProtip3 minutes4,188
Custom UART Protocol on ESP32

Things used in this project

Hardware components

ESP32S
Espressif ESP32S
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

ESP32-ESP32 communication

Connect
RX to TX
TX to RX
GND to GND

Code

Write

C/C++
/* This is a demo of OSCUP protocol, in this example you have to flash this
    firmware on the ESP32 and then run the python code.
    This software is provided AS IS, no Warranties!
    Copyright © 2021 Projecto - Dott. Daniel Rossi, Dott. Riccardo Salami
    License GPL-V3
    @version 1.2.0
    
    @brief this example sketch uses oscup for writing data on UART. It initializes Oscup with a
    fixed ID and Baudrate and initializes the protocol inside void setup function.
    In the void loop it retrieves the value of the APB clock as an uint64_t, then it calls a function
    which converts this value into an array of character and after that it writes this data on UART. 
    Finishing, it calls a free on the buffer.
*/

#include <stdlib.h>
#include "Oscup.h"

uint8_t id = 0x5D;

//constructor takes device's ID and baudrate
Oscup oscup = Oscup(id);

void setup() {
  //it is mandatory to call begin() for starting UART
  oscup.begin(115200);
}

void loop() {
  //this function returns the APB clock frequency (set inside ESP32 libraries)
  uint64_t tim = oscup.get_APB_clk();
  
  //convert uint64_t to byte array
  char *arr2 = uint64_toBytes(tim);
  
  //write the packet, parameters: command(uint8_t), length of the payload(uint8_t), payload(char *)
  uint8_t error = oscup.write((uint8_t)TxCommands::SHARE, sizeof(uint64_t), arr2);
 
  //remember always to free dangling pointers!
  free(arr2);

  
  delay(2000);
}

char *uint64_toBytes(uint64_t number) {
  /*@brief converts a uint64_t into a char * pointer
   * 
   * @param number uint64_t you want to convert
   * 
   * @return the pointer containing the new array of chars
  */
  size_t dim = sizeof(uint64_t);
  char *buff = (char *)calloc(dim, sizeof(char));
  for (int i = 0 ; i < dim; i++) {
    buff[i] = (number >> (8 * i)) & 0xFF;
  }
  return buff;
}

Read

C/C++
/* This is a demo of OSCUP protocol, in this example you have to flash this
    firmware on the ESP32 and then run the python code.
    This software is provided AS IS, no Warranties!
    Copyright © 2021 Projecto - Dott. Daniel Rossi, Dott. Riccardo Salami
    License GPL-V3
    Version: 1.2.0
*/

#include <stdlib.h>
#include "Oscup.h"

uint8_t id = 0x5D;
packet_t packet;
uint8_t errore = 255;

//constructor takes device's ID and baudrate
Oscup oscup = Oscup(id);

void setup() {
  //it is mandatory to call begin() for starting UART
  oscup.begin(115200);
}

void loop() {
  errore = oscup.read(&packet);

  delay(3000);

}

PyRead

Python
'''
Oscup: Open Source Custom Uart Protocol
This Software was release under: GPL-3.0 License
Copyright � 2021 Daniel Rossi & Riccardo Salami
Version: ALPHA 1.2.0
'''

from pyOscup import Oscup
from pyOscup import ErrorCodes
from struct import unpack
id = 0x1C
baudrate = 115200
port = "COM3"
oscup = Oscup(id, baudrate, port)

while True:
    error, packet = oscup.read()

    if error:
        if error != ErrorCodes.NO_DATA:
            print("Error: {}".format(error))
    else:
        id, command, length, payload, crc = packet.getParams()
        print("id: {} - command: {} - length: {}".format(hex(id), command, length))
        print("payload: {}".format(payload))
        print("long long value: {}".format(unpack('Q', bytearray(payload[:8]))))
        print("crc: {}".format(crc))
        print("\n\n")

OSCUP GitHub

Credits

Daniel Rossi

Daniel Rossi

7 projects • 25 followers
PhD Candidate in ICT @ AImageLab - University of Modena and Reggio Emilia Instagram: @officialprojecto

Comments