Skip to content

Instantly share code, notes, and snippets.

@moisespsena
Created May 5, 2023 15:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moisespsena/9988c86b1bc3bf2d7c12662ce6126ebb to your computer and use it in GitHub Desktop.
Save moisespsena/9988c86b1bc3bf2d7c12662ce6126ebb to your computer and use it in GitHub Desktop.
//*****************************************************************************************************************
// Library(ies)
//*****************************************************************************************************************
// To ESP32
#include <Arduino.h>
// To String
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <sstream>
#include <cstdint>
//#include <SoftwareSerial.h>
#include <HardwareSerial.h>
// To ST-X3
//#include <iomanip>
//#include <iostream>
#include <sstream>
//*****************************************************************************************************************
// Constant(s)
//*****************************************************************************************************************
// Hardware mapping
// Serial number
#define ID 100001 // Serial Number of device - 5 digits
// Macro(s) generic(s)
// To Time
#define timeToWait 1
#define delayloop 1 // Delay entre envios via wifi // soma dos delays = 12000
#define delaysetup 1
// To Serial
#define SERIAL_DEBUG 115200
#define SERIAL_HW 9600
// To debug
#define DEBUG
// max length of a the data or command
#define MAX_DATA_LEN (8)
#define TERMINATOR_CHAR ('\n')
//*****************************************************************************************************************
// Prototype of the function(s)
//*****************************************************************************************************************
// To Setup
void setupSerial();
bool setupHW();
//*****************************************************************************************************************
// Object(s)
//*****************************************************************************************************************
// To HW
#define HW_RX 16
#define HW_TX 17
#define HW_RTS 18
#define HW_CTS 5
#define HW_CTS_OPEN 1
#define HW_CTS_CLOSE 0
#define HW_RTS_OPEN 1
#define HW_RTS_CLOSE 0
unsigned long second = 1000;
HardwareSerial HWSerial(2);
//*****************************************************************************************************************
// Initial settings
//*****************************************************************************************************************
void setCTS(int v) {
Serial.print("set CTS to ");
if (v == HW_CTS_OPEN) {
Serial.println("OPEN");
digitalWrite(HW_RTS, HW_RTS_OPEN);
} else {
Serial.println("CLOSE");
digitalWrite(HW_RTS, HW_RTS_CLOSE);
}
}
bool checkCTSIs(int v) {
Serial.print("check CTS is ");
if (v == HW_CTS_OPEN) Serial.print("OPEN");
else Serial.print("CLOSE");
int val = digitalRead(HW_CTS);
if (val == v) Serial.println(": YES");
else Serial.println(": NO");
return val == v;
}
void waitCTSIs(int v) {
while (!checkCTSIs(v)) {
delay(second);
}
}
void setWaitCTSIs(int v) {
setCTS(v);
delay(second);
while (!checkCTSIs(v)) {
delay(second);
}
}
unsigned int action = 0;
void printAction() {
Serial.print("action: ");
Serial.println(action);
}
void setup() {
setupSerial();
if (!setupHW()) return;
} // end setup
//*****************************************************************************************************************
// Loop infinite
//*****************************************************************************************************************
void loop() {
onHwSender();
delay(second);
//processSerialData();
//delay(delayloop);
}
//*****************************************************************************************************************
// Function to setup serial
//*****************************************************************************************************************
void setupSerial() {
Serial.begin(SERIAL_DEBUG);
delay(500 * timeToWait);
} // end setupSerial
//*****************************************************************************************************************
// Function to HW
//*****************************************************************************************************************
bool setupHW() {
pinMode(HW_RX, INPUT);
pinMode(HW_TX, OUTPUT);
pinMode(HW_RTS, OUTPUT);
pinMode(HW_CTS, INPUT);
HWSerial.begin(SERIAL_HW, SERIAL_8N1, HW_RX, HW_TX);
if (!HWSerial) {
Serial.println("Invalid EspSoftwareSerial pin configuration, check config");
return false;
}
Serial.println("Serial Txd is on pin: " + String(HW_TX));
Serial.println("Serial Rxd is on pin: " + String(HW_RX));
delay(second);
if (!checkCTSIs(HW_CTS_CLOSE)) {
setWaitCTSIs(HW_CTS_CLOSE);
}
Serial.println("HW setup done.");
return true;
}
unsigned long tempoAnterior = 0;
void onHwSender() {
unsigned long tempoAtual = millis();
unsigned long diff = tempoAtual - tempoAnterior;
if (action < 5)
printAction();
switch (action) {
case 0:
if (diff >= second) {
tempoAnterior = tempoAtual;
if (!checkCTSIs(HW_CTS_CLOSE)) {
return;
}
action++;
setCTS(HW_CTS_OPEN);
}
return;
case 1:
if (!checkCTSIs(HW_CTS_OPEN)) {
return;
}
action++;
return;
case 2:
Serial.println("send now.");
HWSerial.print("AA050150D5");
Serial.println("send done.");
action++;
return;
case 3:
setCTS(HW_CTS_CLOSE);
action++;
return;
case 4:
if (!checkCTSIs(HW_CTS_CLOSE)) {
return;
}
action++;
return;
case 5:
int count = HWSerial.available();
if (count > 0) {
tempoAnterior = tempoAtual;
Serial.print("response detected: ");
Serial.println(count);
while (count > 0) {
int data = HWSerial.read();
Serial.print("received: ");
Serial.println((int)data);
count = HWSerial.available();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment