Skip to content

Tutorial: Wireless MTC, transmitting MTC data

Kenny Mensah edited this page Jan 15, 2020 · 1 revision

Tutorial: Wireless MTC, transmitting MTC data

Wireless Minecraft Train Control is more complicated than standard MTC, but it allows for wireless data communication, self-driving trains, and fewer cables. So come learn how to use it, right here...

Requirements

ComputerCraft. You can get the latest 1.7.10 version here.

A little bit more time, this is a little bit more complicated than normal MTC, however, most things are similar.

More experience with ComputerCraft. This isn't very advanced but you may want to learn more about ComputerCraft before indulging. Don't worry, it shouldn't take long.

A way to parse JSON in ComputerCraft: For transmitting from the train to the computer, it uses JSON. There isn't any native support for it, however, with APIs it is possible. Here is a good parser: JSON API v2.0.1 for ComputerCraft

Try to connect to the server

The first part of Wireless MTC is making the train say to the server, "Hi! I am in the W-MTC area, so give me the speed limit data or somebody gets hurt."

I mean..not really that, but without the hurt part. Haha, right? Anyway, to do this, we need an MTC Status Transmitter, and a W-MTC wireless radio.

First, we set up the status transmitter by placing a computer next to it, setting the MTC Type to 2 for W-MTC, the status to 1 to turn it on, and the server UUID to know who to talk to. But first, we need to get the UUID of the radio! Place a radio somewhere you want the central control center, then type in lua.

Now when it is placed, it does not have a UUID, so we need to make one by running a command. It is, peripheral.find("wirelessMTCRadio").activate(). peripheral.find finds a peripheral by a name, for example, disk_drive. After that in the parentheses and quotation mark (wirelessMTCRadio) is the name of what you want, then we end it off with a quotation mark and an end parentheses at the end. Finally, at the end is .activate(), which activates it, generating a UUID. If it returns true, that means everything went well. Now, do peripheral.find("wirelessMTCRadio").getSelfUUID() to get your very own, radio UUID.

Don't worry if it's too long, it's printed into your game's log so you can copy-paste it and not have to type the whole thing in.

Now we have the UUID, so let's set up the status transmitter by:

local mtcTransmitter = peripheral.find("info_transmitter_mtc") -- Finds a status transmitter attached to the computer
local mtcStatus = 1 -- The status we want to transmit to the train
local mtcType = 2 -- The MTC type we want to send to the train
local uuid = "e898ea81-0ad7-4926-9a5a-feddf083bd9d" -- The unique ID that is the server
mtcTransmitter.setMTCType(1) -- Sets the MTC Status
mtcTransmitter.setMTCType(2) -- Sets the MTC Type
mtcTransmitter.setServerUUID(uuid) -- Sets the UUID for the train when it passes over
mtcTransmitter.activate() -- Actives the status transmitter

Now once this is done, once a train goes over the sensor, the train sends a message to the server, telling it that it wants to start a connection.

Getting messages from trains

Now that's done, we can write a little program to get the messages from the trains. So, create a new file with edit <filename>, (or startup if you want to do it when it starts up).

First, we need to find the radio that's connected, so we use peripheral.find("wirelessMTCRadio") to find a radio that's connected to the computer, either by peripheral cable or on one of the sides.

local radio = peripheral.find("wirelessMTCRadio")

Now that we found it, we need to activate it, so we use radio.activate()

local radio = peripheral.find("wirelessMTCRadio")
radio.activate()

Next, we want to do a while true do loop. This makes it where forever, code will repeat in.

local radio = peripheral.find("wirelessMTCRadio")
radio.activate()

while true do

end

Now, when a radio gets a message, it uses ComputerCraft events. You can get these events by using, os.pullEvent(). It returns what it got in this order. First, the side where it came from (or the peripheral name), then the train ID of the train that it came from, it's own UUID, the raw message, then the train level. The last one is the system, however it is 1 because it isn't really used.

local radio = peripheral.find("wirelessMTCRadio")
radio.activate()

while true do
local side, trainID, myID, message, system = os.pullEvent("radio_message") 
end

Now let's make it print out what it got.

local radio = peripheral.find("wirelessMTCRadio")
radio.activate()

while true do
local side, trainID, myID, message, system = os.pullEvent("radio_message") 
print("The side is "..side)
print("The trainID is ".. trainID)
print("The radio's ID is "..myID)
print("The raw message is:"..message) 
end

Making the computer understand what has been sent

Now that we know what it sent, now we can parse (or understand) what it is and what to do next.

When a train tries to connect, it sends this (in JSON)

{"funct":"attemptconnection","trainType":"1"}

The first part is the funct or function. What it's saying here is, "Hey, I want to connect, and here is all of the details you need." We want it to send back some juicy data to start W-MTC.

But first, let's put it from JSON to a ComputerCraft table with the API. First, pastebin it in, then load it by putting os.loadAPI("json") at the top.

os.loadAPI("json")
local radio = peripheral.find("wirelessMTCRadio")
radio.activate()

while true do
local side, trainID, myID, message, system = os.pullEvent("radio_message") 

local theMessage = json.decode(message)
end

With this JSON API, it turns it from unusable (well raw in ComputerCraft anyway) into data that can be used. Now, let's make it so if it is trying to connect, send it some goodies like the speed limit, the next speed limit, if there is going to be a speed change soon (speedChange), if there's a stop point, (endSoon), and if there is a station stop soon, (stationStopSoon).

Here is an example in ComputerCraft table format.

{funct = "startlevel2", speedLimit = 60, nextSpeedLimit = 10, mtcStatus = 1, endSoon = false, speedChange = false, stationStopSoon = false}

Let's check if it is attempting a connection and send a startlevel2 if it is. We are going to use this as static information. We encode it using json.encode(<the table>), and send it with radio.sendMessage(uuidto, message).

os.loadAPI("json")
local radio = peripheral.find("wirelessMTCRadio")
radio.activate()

while true do
local side, trainID, myID, message, system = os.pullEvent("radio_message") 

local theMessage = json.decode(message)

if theMessage.funct == "attemptConnection" then
radio.sendMessage(trainID, json.encode({funct = "startlevel2", speedLimit = 60, nextSpeedLimit = 10, mtcStatus = 1, endSoon = false, speedChange = false, stationStopSoon = false})
end

end

Now, the train should have the speed info and the player will know all of the MTC things in the HUD, just like regular MTC.

Additionally, every about 1 second, a train will send an update to the server.

For extra things, go to the original article:

Anything else?

From this tutorial, you learned how to send MTC information without using speed transmitters.

If you still need help, contact PeachMaster#9135 (or ((PeachMaster)chair.ridingEntity)) on the Discord. Happy coding!