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

OCS and wireless

Started by jewelshisen, 17 January 2013 - 08:58 AM
jewelshisen #1
Posted 17 January 2013 - 09:58 AM
Ok I am not a lua programmer so i am coming here trying to get some idea of how to program a wireless sensor turtle so that it reads the sensor data from openCCSensors and then transmits over the redstone network.
Lyqyd #2
Posted 17 January 2013 - 10:09 AM
You'd want to grab the data from the sensor, then serialize the table and send it via rednet. Something like:


local data = sensor.call("left", "getTargetDetails", "0,0,-2")
rednet.send(12, textutils.serialize(data))

Use textutils.unserialize on the other end to get the data back into table form.
jewelshisen #3
Posted 17 January 2013 - 10:25 AM
You'd want to grab the data from the sensor, then serialize the table and send it via rednet. Something like:


local data = sensor.call("left", "getTargetDetails", "0,0,-2")
rednet.send(12, textutils.serialize(data))

Use textutils.unserialize on the other end to get the data back into table form.

Awesome. Will try.
jewelshisen #4
Posted 17 January 2013 - 10:54 AM
You'd want to grab the data from the sensor, then serialize the table and send it via rednet. Something like:


local data = sensor.call("left", "getTargetDetails", "0,0,-2")
rednet.send(12, textutils.serialize(data))

Use textutils.unserialize on the other end to get the data back into table form.

Ok I have my turtle set up. Now to set up the monitor I would just have to have to do something like this:


function update()
  data = textutils.unserialize(rednet.receive())
  return data.Stored
end

That would work right?
Lyqyd #5
Posted 17 January 2013 - 11:05 AM
Not quite. The first return of rednet.receive is the ID of the sender. You'd want something more like:


local id, message = rednet.receive()
local data = textutils.unserialize(message)
if data then --make sure we got a serialized table, not some other rednet traffic.
  print(data.Stored)
end
jewelshisen #6
Posted 17 January 2013 - 11:07 AM
Not quite. The first return of rednet.receive is the ID of the sender. You'd want something more like:


local id, message = rednet.receive()
local data = textutils.unserialize(message)
if data then --make sure we got a serialized table, not some other rednet traffic.
  print(data.Stored)
end

Just figured that out myself! ^w^ Code is working PERFECTLY now! Just had to make it flush the sending computer's id.

Here was my final code:

mon = peripheral.wrap("right")
rednet.open("left")
function update()
  c,data,d = rednet.receive()
  dataRaw = textutils.unserialize(data)
  return dataRaw.Stored
end
graphI = graph.new(mon, update, "EU Tank", nil, 0, 10000000)
while true do
  graphI:draw()
  sleep(0.5)
end