Communication between RS485 module and esp8266

Go To Last Post
26 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello
I want to connect an RS485 module to an esp8266 and read the data coming from the RS485 module and display it on a 2 * 16 LCD. What should my connections look like? And what code should I write for the esp8266 module that can read the data and display it on the lcd? ?

Thank you for your help

Last Edited: Tue. May 10, 2022 - 01:11 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I also found this site but the connections and code are for Arduino Nano and do not work on ESP8266.

https://www.electroniclinic.com/...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

As you posted in the tiny/mega forum of an AVR support site can you just remind me what relevance any of that has to AVRs?

 

Moderator

 

(hint: none of the pins on your ESP look like TX/RX for a UART so may have picked the wrong module to do this with unless you are going to bit-bang?)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks for your help
I noticed that it is not possible to connect the MAX485 to this module due to the 5 volt output, but it is possible with the MAX3485. Is it possible to establish serial communication with other bases except RX and TX bases? Thanks if you have a code for serial communication ESP8266 share with me

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

First you have to tell me what this has got to do with AVR or, indeed, any silicon produced by Microchip ?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I apologize because I saw posts about the esp8266 module in the megaAVR and tinyAVR section, so I left a post so that anyone with information can help. I apologize and delete my post.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

 

Well look. RS485 is basically UART but with some electrical differences in that it's often half-duplex and the wiring uses differential signalling. So it's like using a UART but usually you have to interact with a "line driver" that does the correct signalling on the wire and that usually needs a send/receive switch to be operated. So you might send a byte, wait5 for the UARt to confirm it is fully sent then switch the line driver from send to receive so it's ready to get any response that comes back.

 

But to do that it kinds of needs the micro you use to have a UART peripheral then you just program that and add the RS485 line switching stuff "on top".

 

However this particular EPS8266 you have chosen looks like it has some popular micro peripherals like SPI and I2C but it does not, apparently, have UART. So that is going to make what might already have been a moderately complex job a very complex job as you might have to try and "fake" a UART behaviour in the micro just by setting (and reading) IO lines high/low. That is known as "bit bang". While it's reasonably easy to do UART with bit-bang for sending (you set a clock and then just set the data line high/low at each clock period) the tricky bit is receiving as that can start happening any time so you usually have to use an interrupt and a timer. The system waits to see activity on the line (start bit) and probably does this using an interrupt. Once that occurs you set a clock and then sample whether the line is high/low at each subsequent clock period. But it is NOT easy.

 

Has someone told that you must use this "D1mini" module for this job? Or did you select it yourself? If you did what made you choose this one? Surely there are ESP8266 (and the more modern ESP32) modules that actually have a UART which would make the implementation about 10 times easier ?

 

EDIT: assuming it is this "D1mini": https://www.wemos.cc/en/latest/d... then it looks like it does have a UART. However the UART appears to connected to a CH340 whish is the chip that turns UART into USB and that is how the board is connected to the PC. So the bottom line is that it appears to have one UART but that is "tied up" as the PC link. On that same site there is: https://www.wemos.cc/en/latest/d...

 

 

this looks FAR more suitable for what you want to do because of:

 

 

One of the "Pro" features appears to be that it DOES expose a "user UART" which you could then use for RS485.

 

So how tied are you to using the original "D1mini" ?

 

EDIT2: Googling further I see:

 

https://medium.com/jungletronics...

 

this very interestingly shows:

 

 

That's an EVEN more interesting solution than the D1miniPro because it's basically an Arduino (except that it has an ESP8266 at the core) but being an Arduino is going to make doing things like RS485 entirely trivial as all that stuff is already available for Arduino.

 

Last Edited: Mon. May 9, 2022 - 12:02 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
(hint: none of the pins on your ESP look like TX/RX for a UART so may have picked the wrong module to do this

Pins 21/22 are marked TX/RX so there may be hope for this, but as it's not AVR related, perhaps its time to lock this thread!

 

FF = PI > S.E.T

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Oh well spotted - with my crap eyesight I was simply going by the "bubbles" down either side blush

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thank you very much for your answer
Well, actually someone told me to do this with the D1mini. It seems to be hard and unprincipled work. Of course, I just want to read the data from the module and sending the data does not need to be active. In fact, I want to connect an npk soil integrated sensor to an ESP8266 by converting rs485 to ttl.

This is done in this link. But connected to an Arduino. Do you have a solution to this problem?
https://www.electroniclinic.com/...
 

Thanks for your help

ki0bk I'm sorry for this if this thread is open for another day so I can get my answer.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Why are you using a wireless chip, if you are connecting it using RS-485?

 

Perhaps wrong assumption, or your description is missing something:

 

connect an RS485 module to an esp8266 and read the data coming from the RS485 module and display it on a 2 * 16 LCD

 

Is wireless just there for the ride/future expansion?

 

Why doe you need RS-485?  Use RS-232 if possible....do you have a long distance to go or banking many together?

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

Last Edited: Mon. May 9, 2022 - 08:11 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Because the output of the sensor I use is RS-485. And to connect to the ESP8266 module, the RS485 needs to be converted to TTL. Finally, the read information is to be sent with the wireless module.
This is my sensor
https://www.amazon.com/Comprehen...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Ok, the sensor has RS-485 half duplex (meaning you can send either from micro to sensor or sensor to micro, but not both at the same time).  Half duplex is plenty fine since even a few readings a second is way overkill.   Not like humidity is going to change every 5 seconds.

Where is the wireless final destination--a phone or PC app?

 

the RS485 needs to be converted to TTL.

Well need to be converted to CMOS levels would be a better statement

....the ESP probably probably rather see CMOS than TTL levels. They often have enough overlap to work out, at the expense of noise margin suffering.  

The MAX485 chip outputs >=3.5V  and <= 0.7V.  3.5 min is indeed short of a typical CMOS output, so you live on the good graces of the ESP chip.   Other convertors might have a wider range output.

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ki0bk wrote:
but as it's not AVR related, perhaps its time to lock this thread!

 

I moved it to general electronics as CLiff is having too much fun.....wink

 

And at 14 comments/posts, does not seem fair.

 

Jim

 

 

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The final destination is both Android and PC software, but there is currently a problem with serial communication between the RS485 converter and the ESP8266 module.

Last Edited: Tue. May 10, 2022 - 03:40 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Can you share a sample code for bitbang theory?

thank you

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0


 

Can you share a sample code for bitbang theory?

forget bitbang...they didn't notice the serial pins RX (from module TX) & TX (to the module RX)

You must provide a signal to the 485 module for direction of transmission ...set for outbound just before sending.  Set to inbound slightly after outbound is finished.

 

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Can you explain a bit more? How do I solve the voltage problem? The ESP8266 digital pins only receive a voltage of 3.3. While the RS485 produces 5 volts

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Must you use the RS-485 sensor?

 

You would typically do this if the sensor is very distant from the controller. 

 

If this is not the case, there might be a far simpler sensor to use.

regards
Greg

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

How do I solve the voltage problem? The ESP8266 digital pins only receive a voltage of 3.3. While the RS485 produces 5 volts

Use a different part than the MAX485 ...do an EBAY search for RS-485 3.3V

 

The ESP might tolerate 5V logic signals on its RX pin, but you'd have to look that up.  Easier to use a part that matches exactly.

one example:

https://www.ebay.com/itm/3728627...

 

https://www.ebay.com/itm/2547695...

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0


Here is a listing of 3v 485 chips from Digikey, you will need to pick one that is through hole or smd as needed, lots to choose from:

 

FF = PI > S.E.T

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I finally got the output by connecting an Arduo Uno and RS485, but the output is still not correct. The sensor seems to detect the parameters, but it is not accurate and is very variable. This is my code and how to connect from this site:

 

#include <SoftwareSerial.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128    // OLED display width, in pixels
#define SCREEN_HEIGHT 64    // OLED display height, in pixels
#define OLED_RESET -1       // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

#define RE 8
#define DE 7

//const byte code[]= {0x01, 0x03, 0x00, 0x1e, 0x00, 0x03, 0x65, 0xCD};
const byte nitro[] = {0x01,0x03, 0x00, 0x1e, 0x00, 0x01, 0xe4, 0x0c};
const byte phos[] = {0x01,0x03, 0x00, 0x1f, 0x00, 0x01, 0xb5, 0xcc};
const byte pota[] = {0x01,0x03, 0x00, 0x20, 0x00, 0x01, 0x85, 0xc0};

byte values[11];
SoftwareSerial mod(2,3);

void setup() {
  Serial.begin(9600);
  mod.begin(9600);
  pinMode(RE, OUTPUT);
  pinMode(DE, OUTPUT);

  display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //initialize with the I2C addr 0x3C (128x64)
  delay(500);
  display.clearDisplay();
  display.setCursor(25, 15);
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.println(" NPK Sensor");
  display.setCursor(25, 35);
  display.setTextSize(1);
  display.print("Initializing");
  display.display();
  delay(3000);
}

void loop() {
  byte val1,val2,val3;
  val1 = nitrogen();
  delay(250);
  val2 = phosphorous();
  delay(250);
  val3 = potassium();
  delay(250);

  Serial.print("Nitrogen: ");
  Serial.print(val1);
  Serial.println(" mg/kg");
  Serial.print("Phosphorous: ");
  Serial.print(val2);
  Serial.println(" mg/kg");
  Serial.print("Potassium: ");
  Serial.print(val3);
  Serial.println(" mg/kg");
  delay(2000);

  display.clearDisplay();

  display.setTextSize(2);
  display.setCursor(0, 5);
  display.print("N: ");
  display.print(val1);
  display.setTextSize(1);
  display.print(" mg/kg");

  display.setTextSize(2);
  display.setCursor(0, 25);
  display.print("P: ");
  display.print(val2);
  display.setTextSize(1);
  display.print(" mg/kg");

  display.setTextSize(2);
  display.setCursor(0, 45);
  display.print("K: ");
  display.print(val3);
  display.setTextSize(1);
  display.print(" mg/kg");

  display.display();
}

byte nitrogen(){
  digitalWrite(DE,HIGH);
  digitalWrite(RE,HIGH);
  delay(10);
  if(mod.write(nitro,sizeof(nitro))==8){
    digitalWrite(DE,LOW);
    digitalWrite(RE,LOW);
    for(byte i=0;i<7;i++){
    //Serial.print(mod.read(),HEX);
    values[i] = mod.read();
    Serial.print(values[i],HEX);
    }
    Serial.println();
  }
  return values[4];
}

byte phosphorous(){
  digitalWrite(DE,HIGH);
  digitalWrite(RE,HIGH);
  delay(10);
  if(mod.write(phos,sizeof(phos))==8){
    digitalWrite(DE,LOW);
    digitalWrite(RE,LOW);
    for(byte i=0;i<7;i++){
    //Serial.print(mod.read(),HEX);
    values[i] = mod.read();
    Serial.print(values[i],HEX);
    }
    Serial.println();
  }
  return values[4];
}

byte potassium(){
  digitalWrite(DE,HIGH);
  digitalWrite(RE,HIGH);
  delay(10);
  if(mod.write(pota,sizeof(pota))==8){
    digitalWrite(DE,LOW);
    digitalWrite(RE,LOW);
    for(byte i=0;i<7;i++){
    //Serial.print(mod.read(),HEX);
    values[i] = mod.read();
    Serial.print(values[i],HEX);
    }
    Serial.println();
  }
  return values[4];
}

https://www.cybertice.com/articl...

And this is my output:

Last Edited: Sat. May 14, 2022 - 05:20 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Do you need some delay after making DE & RE low?  

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

How long should I delay?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

How long should I delay?

What did you use?  What does the datasheet show for the switchover times?  few us? 100us would be way plenty.

 

Do you know what values you should be getting from your soil (do any of these numbers seem to be valid for the soil you are plugged into)?

 

Did you double check you are controlling direction in the proper direction?

 

 

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

Last Edited: Sat. May 14, 2022 - 03:51 PM