Go Down

Topic: Proper serial communication between arduino and esp8266 (Read 1 time) previous topic - next topic

pufiks

Hi,

First, I will try to explain what I want to achieve. I'm making two controllers for detecting status of floor heating actuators (two floor house). Actuators are powered by 230V. I'm going to use 230V AC to 5V DC converters, so when actuator is open, arduino INPUT will be HIGH.

First controller will request a status of second floor controller, if all actuators are OFF, controller will disable boiler heating function.

I have two of these https://robotdyn.com/uno-wifi-r3-atmega328p-esp8266-32mb-flash-usb-ttl-ch340g-micro-usb.html

My main problem is send and receive serial data between esp and ATmega328P. Very often I'm unable to read received data (e.g I should get <1010>, but getting <?000>, and sometimes data is valid...)

Here is code sample of esp module:

Code: [Select]
#include <ESP8266WiFi.h>

char ssid[] = "*******";
char pass[] = "******";

IPAddress server(*******);
WiFiClient client;

const char startOfNumberDelimiter = '<';
const char endOfNumberDelimiter   = '>';
const char startOfClientDataDelimiter = '[';
const char endOfClientDataDelimiter   = ']';
bool receiveSerialData = false;
bool receiveClientData = false;

void setup() {
  Serial.begin(115200);
  WiFi.begin(ssid, pass);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
  }
}

void loop () {

  //wait for data from arduino (serial.print() char array) 
  while (Serial.available() > 0) {
    char character = Serial.read();

    if (character == startOfNumberDelimiter) {
      receiveSerialData = true;
      client.connect(server, 80);
    }

    if (receiveSerialData) {
      client.print(character);
    }

    if (character == endOfNumberDelimiter) {
      receiveSerialData = false;
      client.flush();
      break;
    }
  }
 
  char serverData = client.read();

  //using different delimitter to avoid sending same data again
  if (serverData == startOfClientDataDelimiter) {
    receiveClientData = true;
  }

  if (receiveClientData) {
    Serial.print(serverData);
  }

  if (serverData == endOfClientDataDelimiter) {
    receiveClientData = false;
  }
}


Can you advice any trustful methods sending data over serial?
I guess it would be much easier to use something like NodeMCU, but it is only 3.3V

pert

My main problem is send and receive serial data between esp and arduino.
By "arduino", do you mean the ATmega328P that's on that UNO+WiFi R3, or do you mean a separate Arduino board?

Please post the sketch that's running on the Arduino.

pufiks

By "arduino", do you mean the ATmega328P that's on that UNO+WiFi R3, or do you mean a separate Arduino board?

Please post the sketch that's running on the Arduino.
Yes, I mean ATmega328P :)

wildbill

If you're getting data corruption, try running at a lower baud rate.

pufiks

I managed to solve data transfer problems by buying 5v-3.3v logic converter and send data over l2C, it seems that everything is working fine so far.

Can anyone explain to me about "receive" event while communicating over l2C?

ESP requests atmega to update digital inputs data before returning it:

Code: [Select]
  Wire.beginTransmission(I2CAddressESPWifi);
  Wire.write(random(100)); // just random data to send
  Wire.endTransmission();


On atmega side I set "onReceive" event on setup()

Code: [Select]
Wire.onReceive(espWifiReceiveEvent);

Code: [Select]
void espWifiReceiveEvent()
{
  Serial.println("Update data request");
  needUpdateActuatorsData = true;

  Serial.print("Received[");
  while (Wire.available()) {
    int c = Wire.read();
    Serial.print(c);
  }
 
  Serial.println("]");
}


above function works fine, in the loop function data is updated every request (if needUpdateActuatorsData is true), but if I write function like this:

Code: [Select]
void espWifiReceiveEvent()
{
  Serial.println("Update data request");
  needUpdateActuatorsData = true;
}


Data is updated only on first request after I reboot atmega device..

Can anybody explain why does it happening?

Go Up