Author Topic: Adding a Foscam FI9900P to Vera Plus  (Read 20589 times)

Offline Phenix74

  • Newbie
  • *
  • Posts: 3
  • Karma: +0/-0
Re: Adding a Foscam FI9900P to Vera Plus
« Reply #15 on: January 12, 2019, 04:22:04 am »
This also worked for FI9816P (?49 off Amazon) camera

Offline minxster

  • Newbie
  • *
  • Posts: 12
  • Karma: +1/-0
Re: Adding a Foscam FI9900P to Vera Plus
« Reply #16 on: March 03, 2019, 02:06:13 pm »
After I restarted the router to take the port forwarding, it started to work using thxultra's method. Now I just need a list of commands to use LUUP to enable / disable motion sense etc.

Found the list of CGI Commands:

https://www.foscam.es/descarga/Foscam-IPCamera-CGI-User-Guide-AllPlatforms-2015.11.06.pdf

This is the code (below) I use on my UI5 from a few years ago. Essentially I made a sloppy way to collect the JSON from the webcam, break it up and then re-compile it back again to enable/disable the motion.

The problem I found was that the CGI/JSON has to be downloaded in full only because you have to send the whole lot back again. Even though you only want to change one thing, being the motion trigger!

I'm sure that someone with some proper JSON coding could clear all of this up. It's worked flawlessly on my Foscam cameras (FI9826W) for years  :o  ;D

This is run in my Scene to Enable motion detection!

Code: [Select]
local VContainerDeviceNo
local VC_SID
local func_return


function split(str, delim)
    local result,pat,lastPos = {},"(.-)" .. delim .. "()",1
    for part, pos in string.gfind(str, pat) do
        table.insert(result, part); lastPos = pos
    end
    table.insert(result, string.sub(str, lastPos))
    return result
end


function set_foscam_alarm(ip_address, alarm_set)
-- local ip_address, alarm_set
-- ip_address="192.168.66.241:881"
-- alarm_set=1

local serverurl=("http://" .. ip_address .. "/cgi-bin/CGIProxy.fcgi?")
local switchaddress=("cmd=getMotionDetectConfig&usr=<username>&pwd=<password>")
local fullcommand=(serverurl .. switchaddress)
local new_url
local armed

local VContainerDeviceNo
local VC_SID

local result, status
local http
local test1
local test2, loop1
local arr1, arr2



http = require("socket.http")
http.timeout = 5
result, status = http.request(fullcommand)

test1=result

test1 = string.gsub (result, "  ", " ")
test1 = string.gsub (test1, "  ", " ")
test1 = string.gsub (test1, "  ", " ")
test1 = string.gsub (test1, "  ", " ")
test1 = string.gsub (test1, " ", "")

test1 = string.gsub (test1, "<", "/")
test1 = string.gsub (test1, ">", "/")

test1 = string.gsub (test1, "//", "/")
test1 = string.gsub (test1, "//", "/")

test1 = string.gsub (test1, "\r\n", "!")
test1 = string.gsub (test1, "\n", "!")
test1 = string.gsub (test1, "\r", "!")

test1 = string.gsub (test1, "!!", "")


new_url=serverurl .. "cmd=setMotionDetectConfig"
armed=0


arr1=split(test1, "!")

for a,b in pairs(arr1)
do
   -- luup.log("Arr1:" .. a .. ":" .. b)
   if ((a>2) and (a<25)) then
  luup.log("Arr1:" .. a .. ":" .. b)
  arr2=split(b,"/")
  luup.log(" - Arr2:" .. arr2[2] .. ":" .. arr2[3])
  new_url = new_url .. "&" .. arr2[2] .. "=" .. arr2[3]
 
  -- collect current state
  if (a==3) then
      armed=arr2[3]
  end
   end
end



luup.log("armed vs. alarm set: !" .. armed.. "/" .. alarm_set.."!")



-- Note "~=" should be NOT?
if (tonumber(armed)==tonumber(alarm_set)) then
-- Do nothing as we want to use NOT (or ELSE)
else
new_url=new_url .. "&usr=<username>&pwd=<password>"

if (tonumber(alarm_set)==1) then
new_url = string.gsub (new_url, "isEnable=0", "isEnable=1")
end

if (tonumber(alarm_set)==0) then
new_url = string.gsub (new_url, "isEnable=1", "isEnable=0")
end

luup.log("New_URL:" .. new_url)

http = require("socket.http")
result, status = http.request(new_url)

luup.log("http request result/status: " .. result .. "/" .. status)
end

if (tonumber(armed)==tonumber(alarm_set)) then
return -10
else
return status
end

end

func_return = set_foscam_alarm("192.168.66.241:881", 1)
func_return = set_foscam_alarm("192.168.66.242:882", 1)


VContainerDeviceNo = 66
VC_SID = "urn:upnp-org:serviceId:VContainer1"

luup.variable_set(VC_SID,"VariableName1","Enable Date",VContainerDeviceNo)
luup.variable_set(VC_SID,"Variable1","",VContainerDeviceNo)
luup.variable_set(VC_SID,"Variable1",os.date('%H:%M:%S %Y-%m-%d'),VContainerDeviceNo)

luup.variable_set(VC_SID,"VariableName2","HTTP Status Code",VContainerDeviceNo)
luup.variable_set(VC_SID,"Variable2","",VContainerDeviceNo)

if (tonumber(func_return) == -10) then
luup.variable_set(VC_SID,"Variable2","No change required",VContainerDeviceNo)
else
if (tonumber(func_return) == 200) then
luup.variable_set(VC_SID,"Variable2","Change OK",VContainerDeviceNo)
else
luup.variable_set(VC_SID,"Variable2",func_return.."-Check!",VContainerDeviceNo)
end
end

luup.variable_set(VC_SID,"VariableName5","Current State",VContainerDeviceNo)
luup.variable_set(VC_SID,"Variable5","",VContainerDeviceNo)
luup.variable_set(VC_SID,"Variable5","Enabled",VContainerDeviceNo)