📜 ⬆️ ⬇️

Manage the real world things from the virtual world of Minecraft (translation)

Recently, I started playing Minecraft again. Vanilla Minecraft is somewhat boring, as for me, and I always look for modpacks (add-on packs). Looking for new modpaks, I stumbled upon FTB Horizons: Daybreaker . From the list of mods contained in it, my attention was attracted by 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 that add features such as graphics and networking. And these computers can be programmed in Lua , right in the game. And there is such a kind of maps, as an Internet card, which you can imagine, can connect to the Internet of the real world. Not bad.

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

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

Getting 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 I could bring everything I wanted into the game, and built OpenComputers a computer. There are a lot of instructions on this, but it’s still a little time to figure out how such computers work. 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 byte of the header (header byte), three additional bytes defining the color in the RGB format, and a byte determining the brightness of the white (RGBW bulb), and one byte switching between the RGB mode and the white mode, and the closing 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 are supported such as levers, pistons, comparators, etc. People create really complex designs using Redstone, such as a real processor . OpenComputers computers work from redstone, so we can read the binary value of the state of the Maynkraftovskogo lever: "on" or "off" and switch the WHITE byte to turn on or off the light bulb.

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

image

We put it all together


So, we have a computer, a way to control a computer and incoming signals. We also have a path and protocol for communicating with a light bulb. The last element of the puzzle is a program that starts on a computer and revives the entire system. First of all, I started a hard drive in Minecraft. When writing information to a hard disk, the 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 on the computer in the game.

As an editor, I used Sublime Text , 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 

The Lua program, when executed, responds to two events (events): “key_up” and “redstone_changed”. The first occurs when a key is pressed while the program is running, and the last is when a redstone signal is received.
Now we have the main event loop, and it was possible to implement simple logic.

image

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

References:

Source: https://habr.com/ru/post/271527/


All Articles