Managing Real-World Things from the Minecraft Virtual World (Translation)

Original author: giannoug
  • Transfer
I recently started playing Minecraft again. Vanilla Minecraft is a little boring, as for me, and I'm always looking for modpacks (add-on packages). Looking for new modpacks, I came across FTB Horizons: Daybreaker . From the list of mods it contains, my attention was drawn to the OpenComputers mod .

As the name suggests, the OpenComputers module adds computers to Minecraft. Real computers! However, they are also modular. You can add peripherals: from monitors to keyboards and expansion cards, which add features such as graphics and network. And these computers can be programmed in Luaright in the game. And there is such a kind of card as an Internet card, which you can imagine, can connect to the Internet in the real world. Not bad.

image

So what can we do in about one hour of free time?

I had a couple of bulbs with WiFi, would it really be nice to manage them from Minecraft? My wifi bulbs are unknown devices found on eBay that were controlled by a clumsy mobile app. My friend Thomas did the reverse engineering of the protocol and I was able to control their color and status via TCP on port 5577. So, I have everything I need, what's next?

Start tinkering


First of all, I launched Minecraft. Next, I created a new world in Minecraft and entered the game. I switched to creative mode, which means that I could add whatever I want to the game, and built an OpenComputers computer. There are many instructions on this subject, but still to figure out how such computers work - it took some time. After some trial and error, I had a working computer and I could create a TCP connection with the real world!

The protocol used by the light bulb is quite simple, one header byte (header byte), three additional bytes that determine the color in RGB format, and a byte that determines the brightness of white (RGBW bulb), and one byte that switches between RGB mode and white mode, and a trailing byte (footer byte ) Simple enough!

0x56 RED GREEN BLUE WHITE MODE 0xAA


In vanilla Minecraft there is a redstone ("red dust"), the equivalent of electricity in our world. By default, various blocks such as levers, pistons, comparators, etc. are supported. People create really complex designs using redstone, such as a real processor . OpenComputers computers work from redstone, which means we can read the binary value of the state of the Minecraft lever: “on” or “off” and switch the WHITE byte to turn the light on or off.

This is the computer that I built in the game, it has 6 monitors that make up one large monitor, a hard drive and a Redstone lever connected to the right with a red dust line:

image

Putting it all together


So, we have a computer, a way to control the computer and incoming signals. We also have a path and protocol for communication with a light bulb. The last element of the puzzle is a program that runs on a computer and revitalizes the entire system. First of all, I spawned a hard drive in Minecraft. When writing information to the hard drive, OpenComputers computer creates a directory with unique identifiers in the Minecraft file directory. This way I can add, edit and delete files from my computer to the computer in the game.

I used Sublime Text as an editor , in my opinion writing code on a computer in a game is not very convenient. And after some research and errors, I wrote the following script:

local event = require('event')  
local net = require('internet')
local myEventHandlers = {}  
local running = true
local con = net.open('192.168.1.110', 5577)
function myEventHandlers.key_up(address, char, code, playerName)
  if (char == 'q') then
    running = false
    print('Goodbye ' .. playerName .. '!')
  end
end
function myEventHandlers.redstone_changed(_, address, side)
  local brightness = 0xff;
  if side > 0 then
    brightness = 0xff
  else
    brightness = 0x00
  end
  print('Sending ' .. brightness .. ' to lamp...')
  con:write(string.char(0x56))
  con:write(string.char(0x00))
  con:write(string.char(0x00))
  con:write(string.char(0x00))
  con:write(string.char(brightness))
  con:write(string.char(0x0f))
  con:write(string.char(0xaa))
  con:flush()
end
function handleEvent(eventID, ...)
  local event = myEventHandlers[eventID]
  if (event) then
    event(...)
  end
end
if con then  
  print('Connected to the bulb!')
end
while running do  
  handleEvent(event.pull())
end  

A Lua program, when executed, responds to two events (events): key_up and redstone_changed. The first takes place when a key is pressed while the program is running, and the last when a redstone signal is received.
Now we have the main event loop, and simple logic could be implemented.

image

First of all, if the 'q' key is pressed, the program ends. When a redstone signal is received, the program converts the lever position to '0x00' or '0xFF', which means turning on and off the wifi lamp. Packets are transmitted through the channel and the lamp responds accordingly. Finally, the TCP connection opens when the program starts. Simple and works!

References:

Also popular now: