Good day everyone,
I am facing a problem when trying to use nodemcu (Nodemcu ESP8266 Lolin V3) to communicate with an RS485 device. I sent an enquiry frame to the device and got the response in bytes. However, the response is not meaningful as it cannot be converted to readable ASCII characters.
I used the built in Serial for this purpose. Hence i debug the program by sending the program flow state and data received to a server as shown in the Web UI below :-
The response that i expect is the ASCII bytes:-
AFC54D48355CC030B14ED8D4B459
which is completely different than what is shown in the Web UI above.
Here is the connection and the code that i have tried so far:-
The code :-
#include<ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
int enable_pin = 5; //DE and RE pin is jumped together to Nodemcu D1 pin.
String link = "http://someserver.ngrok.io"; //the server
void setup() {
pinMode(enable_pin,OUTPUT);
sendActivity("baud_rate_is_set");
Serial.begin(9600);
delay(2000);
digitalWrite(enable_pin,HIGH); //mcu - master : meter - slave
}
void loop() {
int timeout = 5000;
int delay_unit = 100;
byte request[] = {0xb7,0xb7,0xaf,0x3f,0x30,0xb7,0xb4,0xb4,0x36,0x33,0x35,0xb1,0x21,0x8d,0x0a};
sendActivity("send_enquiry_frame");
//write
Serial.write(request,sizeof(request) / sizeof(request[0]));
digitalWrite(enable_pin,LOW);
//delay & timeout
sendActivity("start_waiting_5s");
int current_delay = 0;
while(current_delay <= timeout)
{
current_delay = current_delay + delay_unit;
delay(delay_unit);
}
sendActivity("done_waiting_5s");
//get the response part
if(Serial.available())
{
sendActivity("serial_data_available");
byte serial_receive_buff[200];
Serial.readBytes(serial_receive_buff,200);
char buff[200];
array_to_string(serial_receive_buff,200,buff);
sendResponse(buff);
}
else{
sendActivity("serial_data_not_available");
}
delay(3000);
}
void sendActivity(String act)
{
HTTPClient http;
http.begin(link+"/api/activity/"+act);
int code = http.GET();
}
void sendResponse(String data)
{
HTTPClient http;
http.begin(link+"/api/data/"+data);
int code = http.GET();
}
void array_to_string(byte array[], unsigned int len, char buffer[])
{
for (unsigned int i = 0; i < len; i++)
{
byte nib1 = (array[i] >> 4) & 0x0F;
byte nib2 = (array[i] >> 0) & 0x0F;
buffer[i*2+0] = nib1 < 0xA ? '0' + nib1 : 'A' + nib1 - 0xA;
buffer[i*2+1] = nib2 < 0xA ? '0' + nib2 : 'A' + nib2 - 0xA;
}
buffer[len*2] = '\0';
}
Can anyone point out what i messed up ? I am stuck on this problem for few days. Thanks a lot.