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?
Related
wifi.sta.setip({ ip = "192.168.0.111", netmask = "255.255.255.0",gateway = "192.168.0.1"})
with above we can set static IP,
but by mistake if someone enters the netmask/gateway/ip a digit or to wrong (eg:netmask = "255.255.2.0" ) we have no way of detecting. only way is to
by re entering correctly
rebooting
(How to clear static IP configuration and start DHCP)
so it would be nice if we can detect the status somehow like with wifi.sta.status() do.
How to clear static IP configuration and start DHCP
wifi.sta.clearconfig()
Clears the currently saved WiFi station configuration, erasing it from
the flash. May be useful for certain factory-reset scenarios
Not sure if this also deletes the ip but you can probably just set the ip to "0.0.0.0" befor you reconnect.
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.
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?
I just started using NodeMCU with Lua. For a start, I am trying to make a simple wifi controlled relay with NodeMCU as a UDP server. The problem is, after running for several hours, I can't make a connection to the board. I tried pinging the board using the ping command, but got no response. If I restart the board, it works again. Any ideas why? Thanks.
Here is my Lua script:
pin_relay = 1
port = 1310
state = 0
gpio.mode(pin_relay, gpio.OUTPUT)
gpio.write(pin_relay, gpio.HIGH)
wifi.setmode(wifi.STATION)
wifi.sta.config("SSID", "password")
wifi.sta.connect()
wifi.sta.setip({ip="192.168.1.200",netmask="255.255.255.0",gateway="192.168.1.1"})
print("ESP8266 mode is: " .. wifi.getmode())
print("The module MAC address is: " .. wifi.ap.getmac())
print("Config done, IP is "..wifi.sta.getip())
srv=net.createServer(net.UDP)
srv:on("receive", function(srv, pl)
if pl=="switch" then
if state == 0 then
gpio.write(pin_relay,gpio.LOW)
state = 1
elseif state == 1 then
gpio.write(pin_relay,gpio.HIGH)
state = 0
end
end
end)
srv:listen(port)
It turns out that my wireless router is the problem.. when i tried a different router, it runs no problem until now. Been running for 3 days now :)
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
>