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

Arduino and ComputerCraft

Started by mattsmithDW, 16 October 2015 - 02:32 PM
mattsmithDW #1
Posted 16 October 2015 - 04:32 PM
I am working on a project to allow my arduino to activate redstone in singleplayer (rather than through a console on a server.)
I just don't know how i am going to go about doing so.
I want the arduino to send information from buttons and potentiometers through the serial port, into the lua console, and activate some redstone. But I have no idea how to allow lua to read the serial port. Any help would be greatly appreciated. I have never used lua before. But i do have previous coding knowledge with java, C, C++, and gmk if that helps.
TYKUHN2 #2
Posted 16 October 2015 - 08:27 PM
Computercraft is sandboxed heavily and has no access to your computer. If you want it to directly access your computer then bring the old Java out and make an addon for Computercraft. I suggest a peripheral you can add. Otherwise you can do HTTP requests (to localhost I suppose) and respond using java/C/C++/LUA/WhateverGMKIs/any other language and use that response to trigger the redstone.

Eitherway your not avoiding programming outside of Minecraft.
mattsmithDW #3
Posted 16 October 2015 - 08:58 PM
thats fine, i'm not trying to avoid programming outside of minecraft. I was just hoping that the lua console in minecraft had access to real files on my computer.
Bomb Bloke #4
Posted 16 October 2015 - 11:42 PM
With vanilla ComputerCraft, your best option would be to write an external program that alters the content of your virtual computer's "drive".

If you browse into your Minecraft save directory, you'll find a folder called "computer". In there is one subfolder for every CC computer in the world that has had files stored on it, labelled according to their ID numbers (just mashing eg "mkdir asdf" into their consoles is enough to get these made - typing "id" will get you their number).

By writing a separate program which dumps port-related data into a file at that location, you can then run a Lua script which periodically reads that file's content and acts on it.

while true do  -- Start a loop that repeats indefinitely.
	local input = fs.open("filename.txt", "r")  -- Attempt to get a file handle.
	
	if input then  -- If we were successfully able to open the file (ie it's not locked by your other process),
		local data = input.readAll()
		input.close()
		
		-- Do stuff with "data" here.
	end
	
	sleep(10)  -- Or however often you want to check the file.
end

Assuming you're familiar with arrays, you can dump a declaration along these lines directly into a file, just on its own:

{"a string", 123, true, false, etc}

… and then load it directly into a Lua table using textutils.unserialise():

local data = textutils.unserialise(input.readAll())
Konlab #5
Posted 17 October 2015 - 07:05 PM
Or run a PHP server and use the HTTP api