Tag Archives: EasyTransfer for ESP8266

EasyTransfer for ESP8266

I’m going to describe here the transmitting side of the EasyTransfer system as used in Arduino – the reason I did this was because I wanted to ship a bunch of binary data from the ESP8266 to the Arduino where I have a nice little wall mounted LCD display – no point in re-inventing the wheel.

EasyTransfer is a simple enough format – it starts with 0x6 and 0x85 followed by the length (all in bytes) of what you want to send, the data itself and a one-byte checksum which is the length XOR’d by every byte in the data (not including the checksum itself of the header 0x6 and 0x85.

So you point a structure to it – and off it goes and the structure is recreated at the other end. Or it is?

struct transmit_lcdX {
  //put variable definitions here for the data you want to receive
  //THIS MUST BE EXACTLY THE SAME ON THE DISPLAY BOARD
  uint8_t cmd;
  int16_t passed;
  uint8_t humidity;
  uint32_t thetime;
  int8_t internal;
  int8_t external;
  uint8_t pre;
  uint8_t heat;
  uint8_t hold;
  int8_t offset;
  uint8_t set;
  uint8_t fallback;
  uint8_t falltemp;
  uint8_t thermenable;
  uint8_t dusk_hour;
  uint8_t dusk_min;
  uint8_t dawn_hour;
  uint8_t dawn_min;
} ;

struct transmit_lcdX transmit_lcd;

 

//Sends out struct in binary, with header, length info and checksum
void ICACHE_FLASH_ATTR EasyTransfer_sendData( uint8_t *bfr, uint8_t size){
    uint8_t buf[40];
    uint8_t CS;
    CS = size;
    buf[0]=0x06;
    buf[1]=0x85;
    buf[2]=CS;
    for(int i = 0; i<size; i++){
        CS^=*(bfr+i);
        buf[i+3]=*(bfr+i);
    }
    buf[size+3]=CS;
    uart0_tx_buffer(buf,size+4);
}

There you go – what could be simpler – define the struct – make an instance of it called transmit_lcd – and call a function passing the address of the structure and it’s length.

Well the first bit I got wrong was to send it out in pieces. I did not realise that Espressif’s function uart0_tx_buffer(buf,len) doesn’t operate immediately – clearly it works in the background under interrupts – I guess the word “later” in the instructions should have made me twig but it didn’t.  So first things first, that structure buf[] has to be static – so that it stays there even after the function has done and cleaned up.

That was fine – and so I called my function – nothing… the LCD would not accept the data… I must’ve spent hours on this – I eventually grabbed a terminal with hex output and…. I’d set the time – and sure enough my checksum was working – but the time was in the wrong place.  2 8-bit integers and a 16-bit come to 4 bytes so what I should have seen was 06 86 0xff 00 00 00 00 aa bb cc dd   where ff is the checksum and aa bb cc dd is the 32 bit time.

But no – the time was much further on! How can this be! WELL it turns out that without a special optimisation, DESPITE the fact that I called some of those variables in the struct 8 bits – they were ACTUALLY being stored as 16 bits -   WHAT!!!

So here we have the version that WORKS – of course you can replace the struct data with any old variables – this just happens to be mine….

struct transmit_lcdX {
  //put variable definitions here for the data you want to receive
  //THIS MUST BE EXACTLY THE SAME ON THE DISPLAY BOARD
  uint8_t cmd;
  int16_t passed;
  uint8_t humidity;
  uint32_t thetime;
  int8_t internal;
  int8_t external;
  uint8_t pre;
  uint8_t heat;
  uint8_t hold;
  int8_t offset;
  uint8_t set;
  uint8_t fallback;
  uint8_t falltemp;
  uint8_t thermenable;
  uint8_t dusk_hour;
  uint8_t dusk_min;
  uint8_t dawn_hour;
  uint8_t dawn_min;
__attribute__((packed)) ;

struct transmit_lcdX transmit_lcd;

 

//Sends out struct in binary, with header, length info and checksum
void ICACHE_FLASH_ATTR EasyTransfer_sendData( uint8_t *bfr, uint8_t size){
    static uint8_t buf[40];
    uint8_t CS;
    CS = size;
    buf[0]=0x06;
    buf[1]=0x85;
    buf[2]=CS;
    for(int i = 0; i<size; i++){
        CS^=*(bfr+i);
        buf[i+3]=*(bfr+i);
    }
    buf[size+3]=CS;
    uart0_tx_buffer(buf,size+4);
}

Note the two additions in BOLD above – if either is missing – this does NOT work. 

So now, if you have some legacy stuff on an Arduino and you need to pass a struct back to it using EasyTransfer – you can. 

Facebooktwittergoogle_pluspinterestlinkedin