/***************************************************
  ESP32 talking to a pretty dumb MAX485 chip (github:iconnor/max485-chip)

  Written by Ian Connor.
  MIT license, all text above must be included in any redistribution
 ****************************************************/

#include <SPI.h>       // this is needed for display
#include <Wire.h>      // this is needed for FT6206
#include <HardwareSerial.h>
constexpr int BOARD_485_RX = 32;  // yellow wire to RO (Receiver Output)
constexpr int BOARD_485_TX = 33;  // brown wire to DI (Driver Input)
constexpr int MAX485_DE = 23;  // blue wire to RE (Receiver Enable) 
constexpr int MAX485_RE = 23;  // and can be tied to DE (Driver Enable)
constexpr int MODBUS_BAUD_RATE = 9600;

const String firmware_version = "v0.0.1";
#define Serial485 Serial1


void setup(void) {
  Serial.begin(115200);
  Serial.println(F("Wake up"));

  Serial485.begin(MODBUS_BAUD_RATE, SERIAL_8N1, BOARD_485_RX, BOARD_485_TX);
    auto modbus_read_request = {
      0x01, 0x03, 0x00, 0x01, 0x00, 0x02, 0x05, 0xCB
    };

    for (int b : modbus_read_request) {
      Serial485.write(b);
    }
  Serial.println(F("Sent 485 data"));
  }

void loop() {
  while (Serial485.available()) {
    byte b = Serial485.read();

    Serial.printf("Incoming MAX485 data: %02X\n", b);
  }
}
esp:VIN
esp:GND.2
esp:D13
esp:D12
esp:D14
esp:D27
esp:D26
esp:D25
esp:D33
esp:D32
esp:D35
esp:D34
esp:VN
esp:VP
esp:EN
esp:3V3
esp:GND.1
esp:D15
esp:D2
esp:D4
esp:RX2
esp:TX2
esp:D5
esp:D18
esp:D19
esp:D21
esp:RX0
esp:TX0
esp:D22
esp:D23
MAX485Breakout
chip2:RO
chip2:RE
chip2:DE
chip2:DI
chip2:VCC
chip2:B
chip2:A
chip2:GND