Jump to content




Openccsensors won't show graph (via RedNet)

computer networking api

6 replies to this topic

#1 Reneral

  • Members
  • 4 posts

Posted 17 May 2016 - 12:17 PM

Hey guys :)

So i'm trying to send data over my RedNet network and then plot the graph on a remote computer. For some reason, this won't work. I'm not getting any errors and i've tried to use multiple print commands to see if the data is getting through (which it definitely is!) but the monitor is still black.

Here is my code for the computer that's sending the information:

os.loadAPI("ocs/apis/sensor")
os.loadAPI("ocs/apis/graph")
--get sensor data and serialize
rednet.open("top")
local sensor1="left"
local reactorPos="0,0,1"
local details="getTargetDetails"
while true do
local tab=sensor.call(sensor1, details, reactorPos)
rednet.broadcast(textutils.serialize(tab))
term.clear()
print(tab)
sleep(0.5)
end

And here the code for the receiving side (which is plotting the graph):

os.loadAPI("ocs/apis/graph")
os.loadAPI("ocs/apis/sensor")
rednet.open("back")
while true do
local event, id, text=os.pullEvent()
if event=="rednet_message" then
print(text)
local sensorData=textutils.unserialize(text)
print(sensorData)
print(sensorData.Heat)
end
end
local mon=pheripheral.wrap("top")
function update()
return sensorData.Heat
end
local gr=graph.new(pheripheral.wrap("right"), update, "Heat", 1, 1, 7000)
while true do
gr:draw()
sleep(0.5)
end

as you can see, i've already tried multible troubleshooting steps to see where the problem could be, but it didn't help.

I've also written this small application for further testing of the network

rednet.open("back")
while true do
local event, id, text =os.pullEvent()
if event=="rednet_message" then
local sensordata1 = textutils.unserialize(text)
print(sensordata1)
sleep(0.1)
end
end

but nothing helped. :(
I hope some of the Pros can help me here!
thanks in advance!

#2 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,452 posts

Posted 17 May 2016 - 02:28 PM

You've got an infinite loop to receive rednet messages in your receiving program. You also have an infinite loop to update the graph, but you'll never reach that loop.

#3 Reneral

  • Members
  • 4 posts

Posted 17 May 2016 - 04:31 PM

 Lyqyd, on 17 May 2016 - 02:28 PM, said:

You've got an infinite loop to receive rednet messages in your receiving program. You also have an infinite loop to update the graph, but you'll never reach that loop.
alright, thank you! i'll try putting the first loop into the update function of the graph! :)/>

os.loadAPI("ocs/apis/graph")
os.loadAPI("ocs/apis/sensor")
rednet.open("back")
local mon=peripheral.wrap("top")
function update()
  local event, id, text=os.pullEvent()
  if event=="rednet_message" then
	print(text)
	local sensorData=textutils.unserialize(text)
	print(sensorData)
	print(sensorData.Heat)
  end
  return sensorData.Heat
end
local gr=graph.new(peripheral.wrap("top"), update, "Heat", 1, 1, 7000)
while true do
gr:draw()
sleep(0.5)
end

This is the code now. but now i'm getting the following error:
bios:14: [string ".temp"]:25: '<eof>' expected

from what i read this means that there is an "end" too much...

Edited by Reneral, 17 May 2016 - 07:30 PM.


#4 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,452 posts

Posted 17 May 2016 - 07:31 PM

Ideally, what you'd want to do is structure your code such that every half-second, you call the graph's draw function. Your update function would simply return a local variable's value, and every time you received a rednet message with a heat value from the correct computer, it would update that local variable. Something like:

--# load API

local heat
local drawTimer = os.startTimer(0)

--# an update function that returns the value of heat
--# initial graph declaration

while true do
  local event = {os.pullEvent()}
  if event[1] == "timer" and event[2] == drawTimer then
    gr:draw()
    drawTimer = os.startTimer(0.5)
  --# replace 42 with ID of sender computer
  elseif event[1] == "rednet_message" and event[2] == 42 then
    local result = textutils.unserialize(event[3])
    if result and result.Heat then
      heat = result.Heat
    end
  end
end


#5 Reneral

  • Members
  • 4 posts

Posted 17 May 2016 - 07:53 PM

 Lyqyd, on 17 May 2016 - 07:31 PM, said:

Ideally, what you'd want to do is structure your code such that every half-second, you call the graph's draw function. Your update function would simply return a local variable's value, and every time you received a rednet message with a heat value from the correct computer, it would update that local variable. Something like:

--# load API

local heat
local drawTimer = os.startTimer(0)

--# an update function that returns the value of heat
--# initial graph declaration

while true do
  local event = {os.pullEvent()}
  if event[1] == "timer" and event[2] == drawTimer then
	gr:draw()
	drawTimer = os.startTimer(0.5)
  --# replace 42 with ID of sender computer
  elseif event[1] == "rednet_message" and event[2] == 42 then
	local result = textutils.unserialize(event[3])
	if result and result.Heat then
	  heat = result.Heat
	end
  end
end

thank you for your reply. :)

As you might have guessed by now i'm pretty new to programming in lua (and programming in general) so sorry for asking so many questions.

I've written the update function and the graph declaration, but now it says graph:23: attempt to compare nil with number.

the code looks like this at the moment:

local heat
local drawTimer = os.startTimer(0)
--# an update function that returns the value of heat
function update()
return result.Heat
end
--# initial graph declaration
local gr=graph.new(peripheral.wrap("top"), update(), "Heat", 1, 0, 7000)
while true do
  local event = {os.pullEvent()}
  if event[1] == "timer" and event[2] == drawTimer then
    gr:draw()
    drawTimer = os.startTimer(0.5)
  --# replace 42 with ID of sender computer
  elseif event[1] == "rednet_message" and event[2] == 3 then
    local result = textutils.unserialize(event[3])
    if result and result.Heat then
	  heat = result.Heat
    end
  end
end

i realy hope you can help me with that.

#6 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,452 posts

Posted 17 May 2016 - 07:57 PM

The update function should return `heat`, not `result.Heat`. You've also accidentally included parentheses after update in the graph declaration (you have `update()` instead of `update`)

#7 Reneral

  • Members
  • 4 posts

Posted 17 May 2016 - 08:01 PM

Yeah, sorry for that. I still have the same error, i just forgot to update the code in notepad++.

os.loadAPI("ocs/apis/sensor")
os.loadAPI("ocs/apis/graph")
local heat
local drawTimer = os.startTimer(0)
--# an update function that returns the value of heat
function update()
  return heat
end
--# initial graph declaration
local gr=graph.new(peripheral.wrap("top"), update, "Heat", 1, 0, 7000)
while true do
  local event = {os.pullEvent()}
  if event[1] == "timer" and event[2] == drawTimer then
	gr:draw()
	drawTimer = os.startTimer(0.5)
  --# replace 42 with ID of sender computer
  elseif event[1] == "rednet_message" and event[2] == 3 then
	local result = textutils.unserialize(event[3])
	if result and result.Heat then
	  heat = result.Heat
	end
  end
end

is the code now, it still says graph:23: attempt to compare nil with number

[EDIT]: I did it! I just forgot to give the local heat a value to work with in the beginning!

local heat = 0


there we go! thank you so much for your help, Lyqyd!

Edited by Reneral, 17 May 2016 - 08:16 PM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users