echo '' ;

Category Archives: ESP32 NodeMCU

ESP32 NodeMCU Module – Net

The ESP32 NodeMCU Module is a versatile development board known for its robust capabilities in IoT projects. With built-in Wi-Fi and Bluetooth connectivity, it offers seamless integration into networked environments. Its compact size and powerful features make it a preferred choice for various IoT applications, from home automation to industrial monitoring. here will discuss ESP32 NodeMCU Net Module.

Read more: ESP32 NodeMCU Module – Net

Functions

FunctionDescription
net.createConnection()Creates a client.
net.createServer()Creates a server.
net.createUDPSocket()Creates a UDP socket.
net.multicastJoin()Joins a multicast group.
net.multicastLeave()Leaves a multicast group.
net.server:close()Closes the server.
net.server:listen()Listens on a port from an IP address.
net.server:getaddr()Returns the server’s local address and port.
net.socket:close()Closes a socket.
net.socket:connect()Connects to a remote server.
net.socket:dns()Provides DNS resolution for a hostname.
net.socket:getpeer()Retrieves the port and IP of the remote peer.
net.socket:getaddr()Retrieves the local port and IP of the socket.
net.socket:hold()Throttles data reception by placing a request to block the TCP receive function.
net.socket:on()Registers callback functions for specific events.
net.socket:send()Sends data to the remote peer.
net.socket:ttl()Changes or retrieves the Time-To-Live value on the socket.
net.socket:unhold()Unblocks TCP receiving data by revoking a preceding hold().
net.udpsocket:close()Closes a UDP socket.
net.udpsocket:listen()Listens on a port from an IP address.
net.udpsocket:on()Registers callback functions for specific events.
net.udpsocket:send()Sends data to a specific remote peer.
net.udpsocket:dns()Provides DNS resolution for a hostname.
net.udpsocket:getaddr()Retrieves the local port and IP of the socket.
net.udpsocket:ttl()Changes or retrieves the Time-To-Live value on the socket.
net.dns.getdnsserver()Gets the IP address of the DNS server used to resolve hostnames.
net.dns.resolve()Resolves a hostname to an IP address.
net.dns.setdnsserver()Sets the IP of the DNS server used to resolve hostnames.

Ex : Create a server Connection

The function net.createServer([type[, timeout]]) is used to create a server with an optional timeout parameter. When you specify a timeout value, it determines the duration for which the server will wait for activity from a client before considering it inactive and closing the connection.

For example, if you create a TCP server with a timeout of 30 seconds using

-- 30s time out for a inactive client
net.createServer(net.TCP, 30) -- 30s timeout

it means that if a client connects to this server but does not send any data within 30 seconds, the server will automatically close the connection to that client.

This timeout functionality is useful for managing server resources efficiently. It allows servers to automatically clean up inactive connections, preventing them from occupying resources indefinitely. In scenarios where maintaining active connections is crucial, such as in real-time communication applications, setting a timeout ensures that resources are available for active clients and that inactive ones do not unnecessarily consume server resources.

Ex : Receive function

function receiver(sck, data)
  print(data)
  sck:close()
end

Ex : Web server function

-- server listens on 80, if data received, print data to console and send "hello world" back to caller
if sv then
  sv:listen(80, function(conn)
    conn:on("receive", receiver)
    conn:send("Arunworld")
  end)
end

TCP Server Connection(Client)

here the code for ESP32 NodeMCU Net Module example

  • Connect the WiFi Station
  • Once got IP address then run Server_Run()  Function
  • Create a connection
  • Connect the server
  • Send the Data “ArunEwolrd IOT Project” to Server
print("wifi init")
wifi.start()
wifi.mode(wifi.STATION)
 
--connect to Access Point (DO NOT save config to flash)
station_cfg={}
station_cfg.ssid="ArunEwolrd"
station_cfg.pwd="Arun"
wifi.sta.config(station_cfg)
 
wifi.sta.connect()


function Server_Run()
print("Server Running")
    srv = net.createConnection(net.TCP, 0)
    srv:on("receive", function(sck, c) print(c) end)
    -- Wait for connection before sending.
    srv:on("connection", function(sck, c)
    sck:send("ArunEworld IOT Projects")
    end)
    srv:connect(80,"192.168.4.1")
end


--register callback
wifi.sta.on("connected", function(ev, info)
    print("Connected to Router")
    print("NodeMCU IP Connected ssid", info.ssid, "bssid", info.bssid, "Channel", info.channel, "Auth", info.auth)
end)

--register callback
wifi.sta.on("disconnected", function(ev, info)
    print("disconnected to Router")
    print("NodeMCU Disconnected ssid", info.ssid, "bssid", info.bssid, "reason", info.reason)
end)

--register callback
wifi.sta.on("authmode_changed", function(ev, info)
print("authmode_changed in Router")
print("NodeMCU authmode_changed old_mode", info.old_mode, "new_mode", info.new_mode)
end)

--register callback
wifi.sta.on("got_ip", function(ev, info)
print("GOt IP")
  print("NodeMCU IP config:", info.ip, "netmask", info.netmask, "gw", info.gw)
  Server_Run()
end)

ESP32 NodeMCU Module – I2C Interface

The ESP32 NodeMCU module supports the I2C (Inter-Integrated Circuit) communication protocol. With its built-in hardware support for I2C, the ESP32 NodeMCU module can easily interface with various I2C devices such as sensors, displays, and other peripherals. This allows for efficient data exchange between the ESP32 and I2C devices, enabling a wide range of applications in IoT, robotics, automation, and more.

Read more: ESP32 NodeMCU Module – I2C Interface

Pr-Request to Lean


ESP32 NodeMCU Module I2C Functions

FunctionDescription
i2c.address()Send (SW) or queue (HWx) I²C address and read/write mode for the next transfer.
i2c.read()Read (SW) or queue (HWx) data for a variable number of bytes.
i2c.setup()Initialize the I²C interface for master mode.
i2c.start()Send (SW) or queue (HWx) an I²C start condition.
i2c.stop()Send (SW) or queue (HWx) an I²C stop condition.
i2c.transfer()Starts a transfer for the specified hardware module.
i2c.write()Write (SW) or queue (HWx) data to the I²C bus.
i2c.slave.on()Registers or unregisters an event callback handler.
i2c.slave.setup()Initialize the I²C interface for slave mode.
i2c.slave.send()Writes send data for the master into the transmit buffer.

Code of ESP32 I2C Scanner

  • In this code used to can the i2c Devices
  • Used Software i2c and GPIO-23 as SDA and GPIO-22 as SCL of I2C Device
  • Connected DS3231 Device and check i2c address of DS3231
-- setup
id  =   i2c.SW                      --  Software
pinSDA  =   23                      --  1~12, IO index
pinSCL  =   22                      --  1~12, IO index
speed   =   i2c.SLOW                    --  only i2c.SLOW supported

--Initialize
i2c.setup(id, pinSDA, pinSCL, speed)                --  Initialize the I²C module.

print("Scanning Started....")
for count = 0,127 do
  i2c.start(id)                         --  Send an I²C start condition.
  Status = i2c.address(id, count, i2c.TRANSMITTER)      --  Setup I²C address and read/write mode for the next transfer.
  i2c.stop(id)                          --  Send an I²C stop condition.
    if Status == true then
        print("Addrss - "..count.." Detected device address is  0x" .. string.format("%02x", count) .. " (" .. count ..")") 
    elseif Status == false then
        print("Addrss - "..count.." nil") 
    end
end
print("Scanning End")

Results

Scanning Started....
Addrss - 0 nil
Addrss - 1 nil
Addrss - 2 nil
Addrss - 3 nil
Addrss - 4 nil
Addrss - 5 nil
Addrss - 6 nil
Addrss - 7 nil
Addrss - 8 nil
Addrss - 9 nil
Addrss - 10 nil
Addrss - 11 nil
Addrss - 12 nil
Addrss - 13 nil
Addrss - 14 nil
Addrss - 15 nil
Addrss - 16 nil
Addrss - 17 nil
Addrss - 18 nil
Addrss - 19 nil
Addrss - 20 nil
Addrss - 21 nil
Addrss - 22 nil
Addrss - 23 nil
Addrss - 24 nil
Addrss - 25 nil
Addrss - 26 nil
Addrss - 27 nil
Addrss - 28 nil
Addrss - 29 nil
Addrss - 30 nil
Addrss - 31 nil
Addrss - 32 nil
Addrss - 33 nil
Addrss - 34 nil
Addrss - 35 nil
Addrss - 36 nil
Addrss - 37 nil
Addrss - 38 nil
Addrss - 39 nil
Addrss - 40 nil
Addrss - 41 nil
Addrss - 42 nil
Addrss - 43 nil
Addrss - 44 nil
Addrss - 45 nil
Addrss - 46 nil
Addrss - 47 nil
Addrss - 48 nil
Addrss - 49 nil
Addrss - 50 nil
Addrss - 51 nil
Addrss - 52 nil
Addrss - 53 nil
Addrss - 54 nil
Addrss - 55 nil
Addrss - 56 nil
Addrss - 57 nil
Addrss - 58 nil
Addrss - 59 nil
Addrss - 60 nil
Addrss - 61 nil
Addrss - 62 nil
Addrss - 63 nil
Addrss - 64 nil
Addrss - 65 nil
Addrss - 66 nil
Addrss - 67 nil
Addrss - 68 nil
Addrss - 69 nil
Addrss - 70 nil
Addrss - 71 nil
Addrss - 72 nil
Addrss - 73 nil
Addrss - 74 nil
Addrss - 75 nil
Addrss - 76 nil
Addrss - 77 nil
Addrss - 78 nil
Addrss - 79 nil
Addrss - 80 nil
Addrss - 81 nil
Addrss - 82 nil
Addrss - 83 nil
Addrss - 84 nil
Addrss - 85 nil
Addrss - 86 nil
Addrss - 87 Detected device address is  0x57 (87)
Addrss - 88 nil
Addrss - 89 nil
Addrss - 90 nil
Addrss - 91 nil
Addrss - 92 nil
Addrss - 93 nil
Addrss - 94 nil
Addrss - 95 nil
Addrss - 96 nil
Addrss - 97 nil
Addrss - 98 nil
Addrss - 99 nil
Addrss - 100 nil
Addrss - 101 nil
Addrss - 102 nil
Addrss - 103 nil
Addrss - 104 Detected device address is  0x68 (104)
Addrss - 105 nil
Addrss - 106 nil
Addrss - 107 nil
Addrss - 108 nil
Addrss - 109 nil
Addrss - 110 nil
Addrss - 111 nil
Addrss - 112 nil
Addrss - 113 nil
Addrss - 114 nil
Addrss - 115 nil
Addrss - 116 nil
Addrss - 117 nil
Addrss - 118 nil
Addrss - 119 nil
Addrss - 120 nil
Addrss - 121 nil
Addrss - 122 nil
Addrss - 123 nil
Addrss - 124 nil
Addrss - 125 nil
Addrss - 126 nil
Addrss - 127 nil
Scanning End
>

ESP32 NodeMCU I2C Interface: Code Explanation

This table format provides a structured breakdown of each section of the code along with its explanation.

SectionExplanation
Setup Section
idDefines the I2C interface as software-based.
pinSDASpecifies the GPIO pin used for the data line (SDA) of the I2C interface.
pinSCLSpecifies the GPIO pin used for the clock line (SCL) of the I2C interface.
speedSets the speed of the I2C communication to slow. Only i2c.SLOW speed is supported.
Initialization
i2c.setup()Initializes the I2C module with the specified parameters: interface ID, SDA pin, SCL pin, and speed.
Scanning for Devices
print(“Scanning Started….”)Prints a message indicating the start of device scanning.
Looping through device addressesIterates through device addresses from 0 to 127.
i2c.start()Sends an I2C start condition.
i2c.address()Sets up the I2C address and read/write mode for the next transfer.
i2c.stop()Sends an I2C stop condition.
Check StatusChecks the status of the address detection: If Status is true, prints the detected device address in hexadecimal and decimal format. If Status is false, prints “nil” for the address.
print(“Scanning End”)Prints a message indicating the end of device scanning.

Suggest


NEXT

NodeMCU Get Start
NodeMCU Build Firmware
NodeMCU Flash Firmware
NodeMCU IDE
ESP8266 NodeMCU Modules
NodeMCU Module–Firmware Info
NodeMCU Module – GPIO
NodeMCU Module – Node
NodeMCU Module – WiFi
NodeMCU Module – Timer
NodeMCU Module – I2C
NodeMCU Module – File
NodeMCU Module – NET
NodeMCU Module – HTTP
NodeMCU Module – MQTT
ESP8266 NodeMCU Interface
NodeMCU Interface LED
NodeMCU Interface Button
NodeMCU Interface 7 Seg
NodeMCU Interface LCD
NodeMCU Interface ADC
NodeMCU Interface DHT11
NodeMCU Interface MCP23008
NodeMCU Interface MCP23017
NodeMCU Interface ADXL345
NodeMCU Interface DS18B20
ESP8266 NodeMCU Tutorials
NodeMCU Tutorials Google Time
NodeMCU Tutorials WebServer
ESP8266 NodeMCU Projects
Imperial March Ringtone Play
WiFi Router Status Indicator
ESP8266 NodeMCU Project – Home Automation
Others
NodeMCU All Post
Sitemap