UART data error when using uart.alt(1) - esp8266

I am trying to acquire rs232 data from a device connected to the ESP8266 (data will then be sent our via http/wifi).
I am using max3232 IC to provide the necessary 3.3v TTL to the ESP8266.
I have have connected the max3232 (pin 12) to GPIO pin 13 (rx) on the ESP8266 (I am only receiving data not sending data, so only the rx pin is connected).
The code i am using:
--
--file: test2.lua
--
tst2 = require "tst2"
tst2.start()
--tst2.lua (testing script)
local module = {}
function module.start()
print("in tst2.start")
uart.alt(1) --use alt GPIO pin 13 (Rx)
uart.setup(0, 9600,8, uart.PARITY_NONE, uart.STOPBITS_1,0)
uart.on("data",10,
function(data)
file.open("data.tmp", "w+")
file.writeline("starting")
for i=1,10 do
file.writeline(string.byte(string.sub(data,i,i)) )
end
file.writeline("from uart: ", data)
file.writeline("finished")
file.close()
end, 0)
uart.alt(0) --switch back to standard Rx/Tx pins
end
return module
The rs232 device connected to the ESP8266 is putting out a single alphabetic character every 3 seconds, however the data written to file (data.tmp) is as follows
starting
10
13
10
13
10
13
10
13
10
13
from uart:
finished
file.close()
Problems:
1- The rs232 device is not issuing any newln or cr characters, but these are appearing in the data file.
2- the string "file.close()" is being written to the data file, and looks like it is the actual lua command that follows the final file.writeline command.
3- the alphabetic data is not appearing in the data file.
4- switching back to the standard uart pins via uart.alt(0) does not work (the ESP8266 must be rebooted - this is not a major issue as the standard uart pins are only used during debugging).
I am writing the rs232 data to a file instead of simply printing it out on the screen (I am using ESPlorer v0.2.0) because the uart.alt(1) command redirects the serial port to the alternative ESP8266 gpio pins.
I think I am doing something fundamentally wrong with the uart set up, but i can't tell what it is.

SOLVED:
It appears that you can't connect the ESP8266 to both the serial port for debugging (e.g. the serial port on a pc running ESPlorer) and also have the alternate serial pins (ESP8266 GPIO 13 and 15) connected (to an external serial device) at the same time.
The nodemcu uart.alt() function does not appear to "turn off" the standard serial i/o pins.
Disconnecting the pc from the standard serial i/o pins solved the problem (debugging becomes an issue, but there are work-arounds to resolve this).

(updated) one workaround is to use a simple telnet server to interact with the lua interpreter. you can either connect the ESP8266 to your wifi router or, even better, set it up as an access point (AP) so that all you have to do is to connect your computer to it and then simply telnet in (to the gateway's IP). so, in addition to the telnet code, you'll need set up the AP in your init.lua. full code for the telnet server and the AP setup is below. A nice benefit is that I can program and monitor the ESP8266 from my phone using an off-the-shelf telnet app!
jj = [[
sock = 22 -- just a placeholder, so it stays global. may not be needed.
-- use sock:send("hello") to insert your own custom output to the client.
telnet_srv = net.createServer(net.TCP, 180)
telnet_srv:listen(2323, function(socket)
local fifo = {}
local fifo_drained = true
local function sender(c)
if #fifo > 0 then
c:send(table.remove(fifo, 1))
else
fifo_drained = true
end
end
local function s_output(str)
table.insert(fifo, str)
if socket ~= nil and fifo_drained then
fifo_drained = false
sender(socket)
end
end
sock = socket -- make the socket globally available.
node.output(s_output, 0) -- re-direct output to function s_ouput.
socket:on("receive", function(c, l)
node.input(l) -- works like pcall(loadstring(l)) but support multiple separate line
end)
socket:on("disconnection", function(c)
node.output(nil) -- un-regist the redirect output function, output goes to serial
end)
socket:on("sent", sender)
print("Welcome to NodeMCU world.")
end)
]]
file.open("telnet.lua", "w")
file.write(jj)
file.close()
jj = [[
wifi.setmode(wifi.STATIONAP);
wifi.ap.config({ssid="ESPtest",pwd=""});
print("Server IP Address:",wifi.ap.getip())
dofile("telnet.lua")
]]
file.open("init.lua","w")
file.write(jj)
file.close()
node.restart()
output:
Server IP Address: 192.168.4.1 255.255.255.0 192.168.4.1
>

Related

Wifi with ESP 8266 or 32

I need your help because I don't understand what's going on. I have 2 esp8266 , one in AP mode and the second in station mode.
When I connect the ESP in station mode to my box, it's ok.
On the other hand when I use this code to connect to the other ESP in AP mode with a fixed IP, the ESP remains in the while loop
import network
from time import sleep
def config_wifi():
ip = '192.168.144.3'
subnet = '255.255.255.0'
gateway = '192.168.144.1'
dns = '8.8.8.8'
ssid = 'MicroPython-AP'
password = '123456789'
ap=network.WLAN(network.AP_IF)
ap.active(False)
ap.disconnect()
print(ap.status()) # return None
sleep(2)
sta=network.WLAN(network.STA_IF)
sta.active(True)
sta.disconnect() # for ESP8266
sta.connect(ssid,password)
print(sta.status())# return 1001 : CONNEXION
sleep(2)
while (sta.isconnected()==False):
print(sta.isconnected())
sta.ifconfig((ip,subnet,gateway,dns))
ip=sta.ifconfig()[0]
print('Connected',ip)
return ip
def run():
config_wifi()
run()
Following my tests, I used an ESP32 Lolin with this program and I have the same result, ie False in the while loop.
The first status gives me None and the second 1001. Searching on different forums, 1001 means Connexion.
Status indicates that there is connection, on the other hand the while loop gives False
I do not understand Do you have an idea?

read and write on serial port with a lua script on raspberry

i'm currently working on a project aiming at controlling a RS232 device by a Raspberry pi4. I'm forced by other softwares to use a lua script and i never coded in lua, even if i have already made the code in Python... I've searched on google far and wide for an answer but have not found anything helping me.
I want my raspberry to open the COM port, and then read and send messages through that port.
I've tried the lua user wiki and This post on stackoverflow (read and write on windows) but both can't be applied to raspberry.
If anyone could help that'd be awesome !
here's my code in python if that can help you.
import serial
def convertisseur(chemin):
Tableau = []
f = open(chemin)
for row in f:
Tableau.append(row)
f.close
return (Tableau)
def statut(ser):
ser.write(bytearray([0X53,0X07,0X01,0X01,0X41,0X60,0X00,0X73,0X45]));
lecture=ser.readline()
print(lecture);
return()
def main():
ser = serial.Serial('COM3', 115200, timeout=1)
ser.close()
ser.open()
Code=convertisseur('C:/Users/Ello/Desktop/CodePosition.txt')
statut(ser)
ser.write(bytearray([0X53,0X09,0X01,0X02,0X40,0X60,0X00,0X06,0X00,0X2C,0X45]));
ser.readline()
ser.write(bytearray([0X53,0X09,0X01,0X02,0X40,0X60,0X00,0X0F,0X00,0X25,0X45,0X53,0X08,0X01,0X02,0X60,0X60,0X00,0X01,0XF5,0X45]));
ser.readline()
statut(ser)
print('Engine move in cm ? - for trigo, STOP to stop')
Speed = input()
while Speed != "STOP":
speed = int(Speed)
if speed > 38:
speed = 38
if speed < -37:
speed = -37
speed = round(abs((speed-38)))
ser.write(bytearray(int(i, 16) for i in Code[speed-1].split(",")))
ser.write(bytearray([0X53,0X09,0X01,0X02,0X40,0X60,0X00,0X0F,0X00,0X25,0X45,0X53,0X09,0X01,0X02,0X40,0X60,0X00,0X7F,0X00,0X55,0X45]));
ser.readline()
statut(ser)
Speed = input()
print("Fin du programme, fermeture du port, passage du moteur sur OFF")
ser.write(bytearray(int(i, 16) for i in Code[60].split(",")))
statut(ser)
ser.close()
return 0
I want my raspberry to open the COM port, and then read and send messages through that port.
Hi, try vsergeev/lua-periphery: A Lua library for peripheral I/O (GPIO, LED, PWM, SPI, I2C, MMIO, Serial) in Linux
, especially Serial section.
Not sure the dev path for you.
In my case the periphery COM device connect to TTL-USB converter, then USB of Pi, so path is '/dev/ttyUSBx'.
As I know, GPIO has UART serial support too.

Trying to control fans on a raspberry pi 3 GPIO over Bluetooth Low Energey

I have 2 dual-h bridge controllers(used to supple power to fans) that are connected to my raspberry pi 3 b via GPIO and a battery for power I have created a script in python 3 that uses Bluetooth classic to ask the connected phone in a terminal how long the fan should be on for and how long they should be off for in a loop I have got this to work GREAT, but this works only on android as it is Bluetooth classic i now want to make this work for my iPhone as well i found out i would need to use BLE so my question is how can i convert my original script in Bluetooth classic(using RF COMM Sockets) to use Bluetooth Low Energy.
Here is my original script
# Importing the Bluetooth Socket library
import bluetooth
# Importing the GPIO library to use the GPIO pins of Raspberry pi
import RPi.GPIO as GPIO
import time
fan_pin = 16# Initializing pin 16 for fan
fan2_pin = 18
GPIO.setmode(GPIO.BOARD) # Using BCM numbering
GPIO.setup(fan_pin, GPIO.OUT) # Declaring the pin 16 as output pin
GPIO.setup(fan2_pin, GPIO.OUT) # Declaring the pin 16 as output pin
host = ""
port = 1 # Raspberry Pi uses port 1 for Bluetooth Communication
# Creaitng Socket Bluetooth RFCOMM communication
server = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
print('Bluetooth Socket Created')
try:
server.bind((host, port))
print("Bluetooth Binding Completed")
except:
print("Bluetooth Binding Failed")
server.listen(1) # One connection at a time
# Server accepts the clients request and assigns a mac address.
client, address = server.accept()
print("Connected To", address)
print("Client:", client)
while 1:
# Receivng the data.
client.send("How long do you want the fans to be on for?/n")
On = int(client.recv(1024)) # 1024 is the buffer size.
time.sleep(5)
client.send("Ok, Now how long do you want the fans to be off for?/n")
Off = int(client.recv(1024))
print(On)
print(Off)
while True:
GPIO.output(fan_pin, GPIO.HIGH)
GPIO.output(fan2_pin, GPIO.HIGH)
time.sleep(On)
GPIO.output(fan_pin, GPIO.LOW)
GPIO.output(fan2_pin, GPIO.LOW)
time.sleep(Off)
# Making all the output pins LOW
GPIO.cleanup()
# Closing the client and server connection
client.close()
server.close()
I would imagine this would be as simple as changing anything that was Bluetooth classic code to BLE code. Max like 20 lines to change?

Telnet server example

Today I installed NodeMCU on one ESP8266 module.
I made a simple telnet server (logging in to the AP is already done and connected)
srv=net.createServer(net.TCP,7200)
srv:listen(23,function(conn)
conn:on("receive", function(conn,telnetdata)
print(telnetdata)
conn:send("Got it\r\n")
end)
conn:on("sent",function(conn)
print("[Sent]");
--conn:close();
--collectgarbage();
end)
end)
I can telnet into the ESP8266 using Putty and I can send static data back to Putty (conn:send("Got it\r\n")).
What I want to do is to send data to Putty from the esp serial port (UART). I've googled for an example without success. BTW I'm using ESPlorer to program the ESP and to send data back to putty.
Instead of using conn:send("Got it\r\n") I want to dynamically type the response. I've tried uart:on callback but I haven't fully understood how it works.
NodeMCU has an "official" Telnet example in the repository. However, that one is also WiFi/socket-based.
If you're connected to the device through ESPlorer (i.e. serial/UART) you can't open another serial connection. See https://stackoverflow.com/a/36779799/131929 for details.
Marcel thank you for your answer.
The link posted is not exactly what I need but it offered good info.
I finally came up with this code that works as a simple bridge between putty and a micro controller (arduino mega 2560 in this case)
socket = net.createServer(net.TCP,7200)
socket:listen(23,function(c)
c:on("receive",function(c,l)
uart.write(0, l)
end)
c:on("disconnection",function(c)
-- not yet implemented
end)
uart.on("data", 0,
function(data)
c:send(data)
end, 0)
end)

Print to remote Comm Port in VB6

I have a software that prints out to a thermal printer connected to the comm port, the code below works well.
Open "COM3:" For Output Access Write As #1
Print #1, Space(8) & "FRAKAS LTD"
Print #1, Space(7) & "P.O. Box 7678 SHIMONI"
...
...
Close #1
Now I want to print on the same comm port but from a different computer on the network, is it possible?
You can redirect the serial port across the network by running software at each end. There is some info on Wikipedia here about some solutions - I haven't tried any.
We have used ethernet to serial converters from Moxa - these redirect a local COM port to a remote one without any PC at the remote end. One example is here
if the printer is availabe then you can use the following to prepare text to be printed :
printer.print "text"
to start the actual printing you use the following :
printer.enddoc

Resources