I've been battling the last couple of days on/off, Mostly tonight.
Has you may have seen my last few posts in trying to over come this but I keep hitting a brick wall with it.
The code below using an ESP32 works perfectly in sending the data while the digital level is on and does not miss a beat. and trying to set a flag once the serial data stops coming in.
Has the code stands at the moment on powering up the ESP32 without the digital level the been switched on it's sending this below to the serial monitor. (great)
Data.test variable = 20 = // no data flag
receiveUntilTimeout = 0 // no data flag
newData = 0
I turn on the digital level it sending the correct data out and then I get on the serial monitor
Data.test variable = 10 /data is good
receiveUntilTimeout = 1 //data is good
newData = 0
I then turn of the digital level and data stops sending but the flags are been been rest as I added some code to send the data to the serial monitor every second and can see that the flags do not get reset.
Code:
/*********
#### TX CODE ####
Wireless SPI-TRONIC Pro 3600 Digital Protractor
General Information
The Pro 3600 has an ASCII-format RS-232 compatible serial port for remote angle readout.
The T&B Ansley 609-1027 connector on the back of the Pro3600 mates with industry
standard cables. Angles are calculated and transmitted every 8/15 second (533 msec)
Angle Output Format:
The ASCII angle output may be read by a computer, or may directly drive a printer. Measured
angles cover a full 360° range and the readout is between -180.00° and +180.00°.
Format:
<sign> XXX.XX <carriage return><line feed>
examples:
+ 124.50
+ 32.70
+ 9.38
- 4.32
- 179.9
*********/
#include <esp_now.h>
#include <WiFi.h>
#include <TimedAction.h>
#include <HardwareSerial.h>
HardwareSerial MySerial(1);// MySerial.begin(9600, SERIAL_8N1, 16, 17);
const byte numChars = 9; //Number of bits to get
char receivedChars[numChars];//store the value of imcoming data
uint8_t broadcastAddress[] = {0X24, 0X6F, 0X28, 0XB3, 0XF3, 0XE0}; //MAC adress been sent to
boolean newData = false;
unsigned long previousMillis = 0; // will store last time LED was updated
unsigned long timeReceived = 0;
#define ECHO_TO_SERIAL 1 //change to zero = no serial output
boolean receiveUntilTimeout = false;
// constants won't change:
const long interval = 1000; // interval at which to blink (milliseconds)
typedef struct Data_struct {
int test = 10;
char receivedChars[numChars]; // an array to store the received data
} Data_struct;
Data_struct Data;
void printRecord(Print* pr, char sep = ',') {
pr->print(Data.receivedChars[0]); // Print btye 0
pr->print(Data.receivedChars[1]); // Print btye 1
pr->print(Data.receivedChars[2] ); // Print btye 2
pr->print(Data.receivedChars[3]); // Print btye 3
pr->print(Data.receivedChars[4]); // Print btye 4
pr->print(Data.receivedChars[5]); // Print btye 5
pr->print(Data.receivedChars[6] ); // Print btye 6
pr->print(Data.receivedChars[7]); // Print btye 7
pr->print(Data.receivedChars[8]); // Print btye 7
pr->print(sep);
pr->print(Data.test); // Print btye 7
pr->print(sep);
pr->print(receiveUntilTimeout);
pr->print(sep);
pr->print(newData); // Print btye 7
pr->print(sep);
pr->println();
}
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
char macStr[18];
// Serial.print("Packet to: ");
// Copies the sender mac address to a string
snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x",
mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
//Serial.print(macStr);
// Serial.print(" send status:\t");
// Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
}
void setup() {
Serial.begin(9600);
MySerial.begin(9600, SERIAL_8N1, 16, 17);
Serial.println("<Arduino is ready>");
WiFi.mode(WIFI_STA);
if (esp_now_init() != ESP_OK) {
Serial.println("Error initializing ESP-NOW");
return;
}
esp_now_register_send_cb(OnDataSent);
// register peer
esp_now_peer_info_t peerInfo;
peerInfo.channel = 0;
peerInfo.encrypt = false;
memcpy(peerInfo.peer_addr, broadcastAddress, 6);
if (esp_now_add_peer(&peerInfo) != ESP_OK) {
Serial.println("Failed to add peer");
return;
}
}
void loop() {
recvWithEndMarker();
if (newData ) { //new data received from eLevel
newData = false;
Data.test = 10;
#if ECHO_TO_SERIAL // Send debug to serial port if enabled
printRecord(&Serial);
#endif //ECHO_TO_SERIAL
Send_data();
}
//testing code
//This only works on power up gives serail data out
//turn pro3600 on then off nothing else happens
if (!receiveUntilTimeout)// debugging method 1
// if (!receiveUntilTimeout && !newData)// debugging method 2
{
Data.test = 20; // no data
Data.receivedChars[0] = 0;
Data.receivedChars[1] = 0;
Data.receivedChars[0] = 0;
receiveUntilTimeout = false;// change it to reset
#if ECHO_TO_SERIAL
printRecord(&Serial);
#endif //ECHO_TO_SERIAL
// Someone is (or at least was) listening
// Tell them your life story
}
// just for debuging
/*
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
//send data outside the while loop
#if ECHO_TO_SERIAL
printRecord(&Serial);
#endif //ECHO_TO_SERIAL
}
*/
}
void recvWithEndMarker() {
static byte ndx = 0;
char endMarker = '\n';
char rc;
if (Serial.available() > 0 && newData == false) { // Tried this
// while (Serial.available() > 0 && newData == false) { //tried this
receiveUntilTimeout = true; //tried here
rc = Serial.read();
if (rc != endMarker) {
Data.receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
Data.receivedChars[ndx] = '\0'; // terminate the string
ndx = 0;
newData = true;
// receiveUntilTimeout = true; //tried here
}
}
}
void Send_data() {
esp_err_t result1 = esp_now_send(broadcastAddress, (uint8_t *) &Data, sizeof(Data));
}
Is there a way I can achieve this so that the flags get set to 20,1,0 when there is no serial data been received ?