Hello all,
I am a little stuck with my project and i was hoping to get some sort of input as i got way over my head in this and seems i cannot think right.
So the story:
The problem : I want to connect a solar installation controller to INTERNET, the solar controller communicates via RS485 and i have succesfully implemented the protocol to talk with it via USB 2 RS485.
The designed Solution: I will use ESP8266 Chip to querry the device using a BOB 10124 Breakout board, The loop will query the device every 5 minutes or so and transmit the data via POST to Device.IO/ Fusion.IO or something similar. The request is quite simple on a master - slave type like COMMAND - COMMAND RESPONSE. So the device echoes the command and attaches the answer. First step was to get the code running using an ARDUINO BOARD (MEGA 2560) due to multiple Serials that makes debugging easy
The challenge: It seems i cannot get the RS485 Breakout (BOB 10124) to work. It has a RTS pin for flow control and it works randomly, sending and receiving junk data.
My 2 cents on this issue after 2 weeks on this problem. There is a timing issue regarding sending multiple bytes of data to BOB 10124. Even after using Serial.Flush() - to wait for the serial to end transmission and bring RST LOW in more de 6/10 the transmission is grabled.
I have tried multiple delays to the RST pin but with no succes, sniffing the communications shows the bytes intact, checking with an oscilloscope the wave lenghth of a good and an invalid communication shows no remarcable differences in terms of timings.
P.S In the design RX and Tx are inverted as they should be....
I have tried also with 220 Ohm terminal resistors on each B A also used 1k-4.4k PULL UP - PULL DOWN RESISTORS on each B and A. I tried with or without flush, sending either ascii or byte to the Serial.
I used BOB 10124 Just to receive data from the controller (sent the data using a USB TO RS485) and it works like a charm no errors, no nothing, I send data between BOB and USB TO RS485 in both directions and also it works without an issue. So the only thing remaining was the transmision to the solar controller. One way or another the RTS pin triggers instability and i cannot seem to stabilize the line. The backup plan is to switch to a MAX 13487/13488 Chip for this but i was hoping that maybe some of you had this issue and find out a fix.
Thank you
The answer received from the controller are something like this:
05:56:23: ADS123:TEMP1
ADS123:TEMP2 23
05:56:33: ADS123:TEMP2
05:56:43: ADS123:TEMP1
05:56:54: ADS123:TEMP2
ADS123:TEMP2 23
05:57:04: ADS123:TEMP1
ADS123:ERROR
05:57:14: ADS123:TEMP2
ADS123:ERROR
05:57:24: ADS123:TEMP1
05:57:34: ADS123:TEMP2
05:57:44: ADS123:TEMP1
ADS123:TEMP1 22
05:57:54: ADS123:TEMP2
ADS123:ERROR
05:58:04: ADS123:TEMP1
The code used for this test is :
byte Data[200]; // array to store data
byte index; // array index
byte readByte = 0; //variable to store byte
int counter = 0;
int scout = 0;
boolean headerCheck = true;
boolean arrayFilled = false;
#define pin 3
//Use to trigger device querry
const long interval = 1000;
unsigned long previousMillis = 0;
//41 44 53 31 32 33 3A
//A D S 1 2 3 : // Header bytes for valid answers
void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
Serial2.begin(9600);
pinMode(pin, OUTPUT);
}
byte a[]={0x41, 0x44 ,0x53, 0x31, 0x32 ,0x33, 0x3A ,0x54, 0x45, 0x4D, 0x50, 0x31, 0x20, 0x0D, 0x0A};
int k=0;
void loop()
{
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= 10000)
{
previousMillis = currentMillis;
//digitalWrite(pin, HIGH);
//delayMicroseconds(25);
/** Serial2.end();
Serial2.begin(9600);
Serial2.write(0x41);
Serial2.write(0x44);
Serial2.write(0x53);
Serial2.write(0x31);
Serial2.write(0x32);
Serial2.write(0x33);
Serial2.write(0x3A);
Serial2.write(0x54);
Serial2.write(0x45);
Serial2.write(0x4D);
Serial2.write(0x50);
Serial2.write(0x31);
Serial2.write(0x20);
Serial2.write(0x0D);
Serial2.write(0x0A);
Serial2.flush();
//Serial2.flush();
delayMicroseconds(120); **/
if (k==0)
{
//Serial2.println("ADS123:TEMP1 ");
//Serial.println(millis());
Serial.println("ADS123:TEMP1 ");
k++;
}
else
{
//Serial2.println("ADS123:TEMP2 ");
Serial.println("ADS123:TEMP2 ");
k=0;
}
//Serial.println(millis());
Serial2.flush();
// Serial.println(millis());
//delay(5);
digitalWrite(pin, LOW);
}
if(Serial2.available() > 0)
{
byte readByte = Serial2.read();
Serial.write(readByte);
switch(counter)
{
case 0: if (readByte==0x41) counter = 1; else counter = 0; break;
case 1: if (readByte==0x44) counter = 2; else counter = 0; break;
case 2: if (readByte==0x53) counter = 3; else counter = 0; break;
case 3: if (readByte==0x31) counter = 4; else counter = 0; break;
case 4: if (readByte==0x32) counter = 5; else counter = 0; break;
case 5: if (readByte==0x33) counter = 6; else counter = 0; break;
case 6: if (readByte==0x3A) counter = 7; else counter = 0; break;
case 7 ... 200: // 39 bytes
Data[counter-7] = readByte;
if (readByte==13)
{
counter=255;
//Serial1.end();
Serial.print(currentMillis);
Serial.println(" X CR");
break;
}
scout++;
if (counter == 200) counter=255; else counter++;
break;
case 255: // rest state after message has been saved, nothing will happen until counter is set to 0
break;
default:
counter=0;
scout=0;
}
}
if(counter == 255)
{
Serial.print(currentMillis);
Serial.print(" MESSAGE SIZE : " );
Serial.println(scout);
Serial.print("START RX HEX: <");
for(int i=0; i<=scout;i++)
{
Serial.print(Data[i],HEX);
Serial.print(" ");
}
Serial.println("> END RX");
Serial.print("START RX DEC: <");
for(int i=0; i<=scout;i++)
{
Serial.print((char)Data[i]);
}
Serial.println("> END RX");
// use data
counter=0;
scout=0;
}
}