This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
gamecompiler's profile picture

ESP8266 and Computercraft, connect the real world

Started by gamecompiler, 13 May 2015 - 01:44 PM
gamecompiler #1
Posted 13 May 2015 - 03:44 PM
Hi,

I would like to use Computercraft in connection with a ESP8266 to controll Minecraft from the real world and opposide.

On the ESP8266´s side id installed a NodeMCU Webserver with the following code from Scott Beasley
http://letsmakerobot...-server-example


-- Simple NodeMCU web server (done is a not so nodeie fashion :-)
--
-- Written by Scott Beasley 2015
-- Open and free to change and use. Enjoy.
--
-- Your Wifi connection data
local SSID = "dd-wrt"
local SSID_PASSWORD = ""
local function connect (conn, data)
   local query_data
   conn:on ("receive",
	  function (cn, req_data)
		 query_data = get_http_req (req_data)
		 print (query_data["METHOD"] .. " " .. " " .. query_data["User-Agent"])
		 cn:send ("<b>Hello World from ESP8266 and NodeMCU!!</b>")
		 -- Close the connection for the request
		 cn:close ( )
	  end)
end
function wait_for_wifi_conn ( )
   tmr.alarm (1, 1000, 1, function ( )
	  if wifi.sta.getip ( ) == nil then
		 print ("Waiting for Wifi connection")
	  else
		 tmr.stop (1)
		 print ("ESP8266 mode is: " .. wifi.getmode ( ))
		 print ("The module MAC address is: " .. wifi.ap.getmac ( ))
		 print ("Config done, IP is " .. wifi.sta.getip ( ))
	  end
   end)
end
-- Build and return a table of the http request data
function get_http_req (instr)
   local t = {}
   local first = nil
   local key, v, strt_ndx, end_ndx
   for str in string.gmatch (instr, "([^\n]+)") do
	  -- First line in the method and path
	  if (first == nil) then
		 first = 1
		 strt_ndx, end_ndx = string.find (str, "([^ ]+)")
		 v = trim (string.sub (str, end_ndx + 2))
		 key = trim (string.sub (str, strt_ndx, end_ndx))
		 t["METHOD"] = key
		 t["REQUEST"] = v
	  else -- Process and reamaining ":" fields
		 strt_ndx, end_ndx = string.find (str, "([^:]+)")
		 if (end_ndx ~= nil) then
			v = trim (string.sub (str, end_ndx + 2))
			key = trim (string.sub (str, strt_ndx, end_ndx))
			t[key] = v
		 end
	  end
   end
   return t
end
-- String trim left and right
function trim (s)
  return (s:gsub ("^%s*(.-)%s*$", "%1"))
end
-- Configure the ESP as a station (client)
wifi.setmode (wifi.STATION)
wifi.sta.config (SSID, SSID_PASSWORD)
wifi.sta.autoconnect (1)
-- Hang out until we get a wifi connection before the httpd server is started.
wait_for_wifi_conn ( )
-- Create the httpd server
svr = net.createServer (net.TCP, 30)
-- Server listening on port 80, call connect function if a request is received
svr:listen (80, connect)

On the Computercraft computer id used the http get example code.


local sExample = http.get("192.168.178.116") --Get contents of page
write(sExample.readAll()) --Read and print contents of page
sExample.close() --Just in case

But i always got the following response from the Computer

get.lua:2: attempt to index ? (a nil value)

Id expected following output…

<b>Hello World from ESP8266 and NodeMCU!!</b>

I please somebody to help me.
Thanks in advance

Sorry for my English
SquidDev #2
Posted 13 May 2015 - 04:54 PM
Instead of just using the IP address you should pass the whole URL:

http://192.168.178.116

Generally if you are getting this error it is because http.get returns nil - it cannot connect to the website. This is generally because it doesn't exist.
Edited on 13 May 2015 - 02:55 PM