As the title says... Chat on...

User avatar
By DoubleMintBen
#63838 Hello,

I'm having a problem with my ESP8266's uart.on call. My ESP is flashed with NODEmcu.

On my ESP I have 2 files. "init.lua" and "server.lua". In server I setup uart, and I have my uart.on call. It looks like this:
Code: Select alluart.on("data", 0, function(data)
    message = data
    uart.write(0, data .. "\r\n")
end, 0)

The ESP is connected to an arduino mega 2560's Serial2 connection. I'm trying to make the two act as a MODBUS plc, so I need to just be able to send data from the arduino over the wireless to an HMI. The data sending will be different lengths and what not, and as far as I know I can't give it a stop character.

The problem that I'm having is that when I open my arduino and put in the line
Code: Select allSerial2.println("12345");
and watch my HMI, all I see is "2345". When I saw that I went into my esp's uart call and I put in
Code: Select alluart.write(0, data)
. Upon doing that and watching my serial monitor, I saw "12345". Then I added '.."\r\n"' right after "data" in my write call, and I saw:
1
2456

Is there a reason its sending the first character of that string first, then the last 4 characters quickly after? It doesn't matter how long the string is that I send in. The first character is always sent, following anything after it. If my string is just "1" my HMI shows nothing, and on the serial monitor I see "1" then new line. "123456890" comes out with "1" on the first line, then "234567890" on the second. Is there anything I can change to make it send everything at once without using a stop character, and without knowing what length the message is going to be. I'll include the code to my 2 lua files and to my arduino program below.

init.lua:
Code: Select all--uart config--
BAUD = 115200
DATABITS = 8
PARITY = uart.PARITY_NONE
STOPBITS = uart.STOPBITS_1
 
 
--wifi config--
WIFI_SSID = "SSID"
WIFI_PASSWORD = "PASSWORD"
WIFI_SIGNAL_MODE = wifi.PHYMODE_G
 
--ip config--
IP = "10.10.10.250"
NETMASK = "255.255.255.0"
GATEWAY = "10.10.10.1"
 
--setup uart--
uart.setup(0, BAUD, DATABITS, PARITY, STOPBITS, 0)
 
--connect to wifi--
wifi.setmode(wifi.STATION)
wifi.setphymode(WIFI_SIGNAL_MODE)
wifi.sta.config(WIFI_SSID, WIFI_PASSWORD)
wifi.sta.connect()
 
if ip ~= "" then
    wifi.sta.setip({ip=IP, netmask=NETMASK, gateway=GATEWAY})
end
 
function startup()
    if file.open("init.lua") == nil then
        print("init.lua deleted or renamed")
    else
        print("Running")
        file.close("init.lua")
        -- the actual application is stored in 'application.lua'
        require "server"
    end
end
 
-- Start server after 2.5 sec.
tmr.alarm(0, 2500, tmr.ALARM_SINGLE, function()
    uart.on("data") -- stop listening UART.
    print "starting server..."
    tmr.unregister(0)
    tmr.alarm(1, 3000, tmr.ALARM_SINGLE, startup)
end)
 
-- Listen to UART data and stop the above timer in case of "qw" string was received.
uart.on("data", "w", function(d)
    if d == "qw" then
        tmr.stop(0) -- Stop timer. Server won't start.
        tmr.stop(1)
        uart.on("data")
        print "cancelled."
    elseif data == "287797648" then
        tmr.stop(0) -- Stop timer. Server won't start.
        tmr.stop(1)
        uart.on("data")
        print "cancelled."
    end
end, 0)


server.lua:
Code: Select allBAUD = 115200
DATABITS = 8
PARITY = uart.PARITY_NONE
STOPBITS = uart.STOPBITS_1
uart.setup(0, BAUD, DATABITS, PARITY, STOPBITS, 0)
 
local message = "none"
 
 
sv = net.createServer(net.TCP, 30)
 
local function close_connection(c)
    if data == "close" then
        c:close()
   end
end
 
function receiver(sck, data)
    print(data)
    uart.write(0, data)
    if data == "close" then
        sck:close()
    end
end
 
 
if sv then
    sv:listen(5000, "10.10.10.250", function(conn)
        conn:on("receive", receiver)
        if message ~= ""  and message ~= nil then
            conn:send(message, close_connection)
            message = ""
        end
    end)
end
 
uart.on("data", 0, function(data)
    message = data
    uart.write(0, data .. "\r\n")
end, 0)


arduino.ino:
Code: Select all#include <Arduino.h>
#include <PLDuino.h>
#define ESP_BAUD 115200
#define BAUD3 9600
 
String getResponse()
{
  String response = "";
  for(int i=0; i<10; ++i)
  {
    while(Serial3.available())
      response += char(Serial3.read());
    delay(100);
  }
  return response;
}
 
String getESPResponse()
{
  String response = "";
  for(int i=0; i<10; ++i)
  {
    while(Serial2.available())
      response += char(Serial2.read());
    delay(100);
  }
  return response;
}
 
bool waitUntilStringReceived (String ptn, int timeout)
{
  String response = "";
  long start_time = millis();
  while(millis() - start_time < timeout)
  {
    if (Serial2.available())
    {
      response += (char)Serial2.read();
      if (response.length() >= ptn.length())
      {
        response = response.substring(response.length() - ptn.length());
        if (response == ptn) return true;
      }
    }
  }
  return false;
}
 
void cancelWiFiTestStartup()
{
  if (waitUntilStringReceived("type \"qw\"", 10000))
  {
    Serial.println("sending qw...");
    Serial2.println("qwqwqw;\n");
    Serial.flush();
  }
  Serial2.flush();
}
 
void setup()
{
  using namespace PLDuino;
 
  PLDuino::init();
  enableESP();
  Serial2.begin(ESP_BAUD);
  Serial.begin(ESP_BAUD);
  Serial3.begin(BAUD3);
  cancelWiFiTestStartup();
}
 
void loop()
{
  String response = "";
  String ESPResponse = "";
  response = getResponse();
  ESPResponse = getESPResponse();
 
  if(response != "")
  {
    Serial2.println(response);
    //Serial2.println("12345");
    Serial.println(response);
  }
 
  if(ESPResponse != "")
  {
    Serial.println(ESPResponse);
  }
}


I also put the code onto pastebin if anyone would like to see it on there with syntax highlighting:
init.lua: http://pastebin.com/HYWqd78Z
server.lua: http://pastebin.com/MPvKPZ4Q
arduino.ino: http://pastebin.com/UH712nb3