Page 1 of 1

Roomba data

Posted: Thursday 13 June 2019 0:43
by roblom
Hi,

I have a JSON response that looks like
Spoiler: show
{
"response": {
"r0": {
"name": "Bumps Wheeldrops",
"value": "0"
},
"r1": {
"name": "Wall",
"value": "0"
},
"r2": {
"name": "Cliff Left",
"value": "0"
},
"r3": {
"name": "Cliff Front Left",
"value": "0"
},
"r4": {
"name": "Cliff Front Right",
"value": "0"
},
"r5": {
"name": "Cliff Right",
"value": "0"
},
"r6": {
"name": "Virtual Wall",
"value": "0"
},
"r7": {
"name": "Motor Overcurrents",
"value": "0"
},
"r8": {
"name": "Dirt Detector - Left",
"value": "0"
},
"r9": {
"name": "Dirt Detector - Right",
"value": "0"
},
"r10": {
"name": "Remote Opcode",
"value": "0"
},
"r11": {
"name": "Buttons",
"value": "0"
},
"r12": {
"name": "Distance",
"value": "0"
},
"r13": {
"name": "Angle",
"value": "0"
},
"r14": {
"name": "Charging State",
"value": "0"
},
"r15": {
"name": "Voltage",
"value": "16531"
},
"r16": {
"name": "Current",
"value": "-151"
},
"r17": {
"name": "Temperature",
"value": "23"
},
"r18": {
"name": "Charge",
"value": "2643"
},
"r19": {
"name": "Capacity",
"value": "2697"
}
}
}
I try to get some of the data in a virtual device. I had a normal lua script running but that doesn't work any more so I try to rewrite it into a Dzvents script. Hope someone can help me with this because I was searching for a simple example but could not find one that suits my need.

What I have is:

Code: Select all

local ROOMBAIP = "192.168.178.185"			-- Roomba IP Address
local ROOMBA_STATUS_IDX="1609"				-- Roomba status IDX
local ROOMBA_BATTERY_STATUS_IDX="1611"		-- Roomba battery charge status IDX

return {
	active = true,
	
	on = {
		timer = { 'every 1 minutes' },
		httpResponses = { 'Roomba' }
	},
	
	logging =	{
		level	=	domoticz.LOG_INFO,
		marker	=	'Roomba'
	},
	
	execute = function(domoticz, item)
		if (item.isTimer) then
			domoticz.openURL({
			url = 'http://192.168.178.186/roomba.json',
			method = 'GET',
			callback = 'Roomba'
		})
		elseif (item.isHTTPResponse) then
			if (item.ok) then -- success
				if (item.isJSON) then
					domoticz.log(response.r0.value)
					domoticz.log('Roomba values checked', domoticz.LOG_INFO)
				end
			else
				domoticz.log('Roomba values not JSON', domoticz.LOG_INFO)
			end
			domoticz.log('Roomba values not ok', domoticz.LOG_INFO)
		end
	end
}
But I can't figure out how i can get the values of each data fields.

Re: Roomba data

Posted: Thursday 13 June 2019 1:22
by waaren
roblom wrote: Thursday 13 June 2019 0:43 I have a JSON response but I can't figure out how i can get the values of each data fields.
You were pretty close already.
Try this

Code: Select all

local ROOMBAIP = "192.168.178.185"			-- Roomba IP Address
local ROOMBA_STATUS_IDX="1609"				-- Roomba status IDX
local ROOMBA_BATTERY_STATUS_IDX="1611"		-- Roomba battery charge status IDX

return {
	active = true,
	
	on = {
		timer = { 'every 1 minutes' },
		httpResponses = { 'Roomba' }
	},
	
	logging =	{
		level	=	domoticz.LOG_INFO,
		marker	=	'Roomba'
	},
	
	execute = function(domoticz, item)
		if (item.isTimer) then
			domoticz.openURL({
			url = 'http://192.168.178.186/roomba.json',
			method = 'GET',
			callback = 'Roomba'
		})
		elseif (item.isHTTPResponse) then
			if (item.ok) then -- success
				if (item.isJSON) then
					rt = item.json
					domoticz.log(rt.response.r0.value)
					domoticz.log('Roomba values checked', domoticz.LOG_INFO)
				else
					domoticz.log('Roomba values not JSON', domoticz.LOG_ERROR)	
				end	
			else
				domoticz.log('Roomba values not ok', domoticz.LOG_ERROR)
			end
		end
	end
}

Re: Roomba data

Posted: Saturday 15 June 2019 18:19
by roblom
Then I get

Code: Select all

_dzVents_ Error_ (2.4.23) Roomba_ Roomba values not JSON
When i move the

Code: Select all

rt = item.json
domoticz.log(rt.response.r0.value)
One "if" higher, I get

Code: Select all

Error: dzVents: Error: (2.4.23) Roomba: An error occurred when calling event handler Roomba
Error: dzVents: Error: (2.4.23) Roomba: /home/pi/domoticz/scripts/dzVents/scripts/Roomba.lua:35: attempt to index global 'rt' (a nil value)
When I manually put the json url in the browser I do get response.

Re: Roomba data

Posted: Saturday 15 June 2019 19:26
by waaren
roblom wrote: Saturday 15 June 2019 18:19 Then I get

Code: Select all

_dzVents_ Error_ (2.4.23) Roomba_ Roomba values not JSON
This is because Roomba is not "well behaving". It should return 'application/json' in the 'Content-Type' field of the headers in the response if it returns in a json format. If it does, dzVents does already converts the json to a Lua table in the background (if you are interested; it is done in <domoticz dir>/dzvents/runtime/HTTPResponse.lua

So now you have do this conversion yourself. Try it with this.

Code: Select all

local ROOMBAIP = "192.168.178.185"			-- Roomba IP Address
local ROOMBA_STATUS_IDX="1609"				-- Roomba status IDX
local ROOMBA_BATTERY_STATUS_IDX="1611"		-- Roomba battery charge status IDX

return {
	active = true,
	
	on = {
		timer = { 'every 1 minutes' },
		httpResponses = { 'Roomba' }
	},
	
	logging =	{
		level	=	domoticz.LOG_INFO,
		marker	=	'Roomba'
	},
	
	execute = function(domoticz, item)
		if item.isTimer then
			domoticz.openURL({
			url = 'http://192.168.178.186/roomba.json',
			method = 'GET',
			callback = 'Roomba'
		})
		elseif item.isHTTPResponse then
			if item.ok then -- success
				if item.isJSON then
					rt = item.json
				else
					domoticz.log('Roomba values not JSON', domoticz.LOG_INFO)
					rt = domoticz.utils.fromJSON(item.data)
				end	
					domoticz.log(rt.response.r0.value)
					domoticz.log('Roomba values checked', domoticz.LOG_INFO)
				end	
			else
				domoticz.log('Roomba values not ok', domoticz.LOG_ERROR)
			end
		end
	end
}