Skip to content

mruby-esp32/mruby-socket

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

97 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

mruby-socket

BSD socket interface for mruby on ESP32, compatible with ESP-IDF version 5 and mruby 3.2.

This gem is a modification of the mruby-socket gem from mruby 3.2.0. It depends on the IO class from mruby-io, also modified for the ESP32.

API is compatible with CRuby's socket library.

Installation

Add the line below to your build_config.rb:

  conf.gem :github => 'mruby-esp32/mruby-socket', :branch => '0.5'

If stack overflow occurs, increase the stack size

  • mruby_task: 8192 => 32768

    xTaskCreate() in main/mruby_main.c

  • eventTask: 4096 => 32768

    $ make menuconfig
    Component config ---> ESP32-specific ---> Event loop task stack size
    

Example

puts "Getting ready to start Wi-Fi"

wifi = ESP32::WiFi.new

wifi.on_connected do |ip|
  puts "Wi-Fi Connected: #{ip} (#{Socket.gethostname})"
  soc = TCPSocket.open("www.kame.net", 80)
  msg = "HEAD / HTTP/1.1\r\nHost: www.kame.net\r\nConnection: close\r\n\r\n"
  msg.split("\r\n").each do |e|
    puts ">>> #{e}"
  end
  soc.send(msg, 0)
  puts "--------------------------------------------------------------------------------"
  loop do
      buf = soc.recv(128, 0)
      break if buf.length == 0
      print buf
  end
  puts ""
  puts "--------------------------------------------------------------------------------"
end

wifi.on_disconnected do
  puts "Wi-Fi Disconnected"
end

puts "Connecting to Wi-Fi"
wifi.connect('SSID', 'PASSWORD')

#
# Loop forever otherwise the script ends
#
while true do
  ESP32::System.delay(1000)
end

Requirement

TODO

  • add missing methods
  • fix possible descriptor leakage (see XXX comments)

About

Socket library for mruby-esp32

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C 67.9%
  • Ruby 32.1%