Using Multicam and Sonoff - General Help and Assistance on using CHDK stable releases - CHDK Forum  

Using Multicam and Sonoff

  • 12 Replies
  • 2606 Views
Using Multicam and Sonoff
« on: 23 / September / 2021, 12:47:54 »
Advertisements
Hello Denizens of CHDK,

Thank you so much for posting all the great information and leaving the trail for many others to follow.  I have done my wanderings, set my paths and now, after successes and failures, i'm in over my head and need some guidance.

I have been working on developing a photogrammetry setup with 8 canon cameras running chdk, controlled by multicam, and a dumb turntable controlled by a Sonoff switch.  I can issues commands to the switch to turn on and off via a script in a http request.

I am new to the world of LUA and Python scripting.  I have installed LUA and Python for Windows but getting all the dependencies correctly installed and working together has been difficult for me.  Therefore I have been unable to get the CLI operation working.
 I have been using chdkptp GUI to connect the cameras. I am able to run multicam from Notepad++ and execute it through the 'LUA local Macro' and this has been successful for running the cameras.     

What I am endeavoring to accomplish is to get a running program that will send commands to take pictures as well as stop and start the turntable via a HTTP request.  This is what I have so far:
Code: [Select]
--[[
This is to start and run Multicam
]]

mc=require('multicam')
mc:connect()
mc:start()
mc:cmdwait('rec')
mc:cmdwait('preshoot')
mc:cmdwait('shoot')
mc:cmd('exit')
--[[
This is to turn on the table
]]
http.request (<URL>/cm?cmnd=Power%20TOGGLE)
--[[
This is to pause for the turning of the table 3 seconds for the table to advance
]]

sys.sleep(3000)

--[[
This is to turn off the table
]]
http.request (http://<URL>/cm?cmnd=Power%20TOGGLE)


--[[
This is to start and run Multicam
]]

mc=require('multicam')
mc:connect()
mc:start()
mc:cmdwait('rec')
mc:cmdwait('preshoot')
mc:cmdwait('shoot')
mc:cmd('exit')
note: I add my IP address in the URL

This is nothing revolutionary, code wise, but if one of you intelligent, bright minds could help me to get a script that functions, I would be eternally grateful to you. 
Also, if you could provide me with some pointers on the best way to install LUA, Python, and the dependencies for a noob, I would really appreciate that too. 

Thank you all very much for the great things you do!

*

Offline reyalp

  • ******
  • 14117
Re: Using Multicam and Sonoff
« Reply #1 on: 23 / September / 2021, 16:04:44 »
I am new to the world of LUA and Python scripting.  I have installed LUA and Python for Windows but getting all the dependencies correctly installed and working together has been difficult for me.  Therefore I have been unable to get the CLI operation working.
I'm not sure if I completely understand, but FWIW, the chdkptp binaries from https://app.assembla.com/spaces/chdkptp/documents include a complete Lua interpreter and support both the CLI and GUI mode.
To use the CLI, start chdkptp (from command prompt, powershell etc) with chdkptp -i

You can run your own code or modules from there using the "!" command, but 3rd party modules built in DLL are unlikely to work.

Quote
I have been using chdkptp GUI to connect the cameras. I am able to run multicam from Notepad++ and execute it through the 'LUA local Macro' and this has been successful for running the cameras.     
I'm unclear how you are running multicam from notepad++. Do you somehow have chdkptp built as a modules running within the notepad++ Lua environment, or are you just executing it as an external .exe?

If you want to run your own Lua scripts inside chkdptp, you don't need notepad++, you can just run them with !dofile(...) or use require and call functions with !.

Quote
What I am endeavoring to accomplish is to get a running program that will send commands to take pictures as well as stop and start the turntable via a HTTP request.
FWIW, if you want to run everything from the chdkptp Lua interpreter, you could just use standard Lua os.execute or io.popen (or the chdkptp wrapper fsutil.popen) to execute curl or something like that to make the http requests.

Quote
Also, if you could provide me with some pointers on the best way to install LUA, Python, and the dependencies for a noob, I would really appreciate that too. 
Without knowing what you want to accomplish with them, this isn't straightforward question.

If you just want python and easy access to python packages on windows, anaconda is a straightforward standalone install https://www.anaconda.com/products/individual

MSYS2 https://www.msys2.org/ also provides python, along with a complete gcc development environment and large selection of *nix-ish tools. This is the environment I use to develop chdkptp on windows, and I also use it for various python things. However, python in msys2 can have confusing compatibility issues. There are separate python packages for the  "msys" and the "mingw" environments, which are incompatible with each other and have different behavior. "msys" is a unix-ish (more or less) "posix" environment, while the mingw environments are more windows-ish.

MSYS2 provides various Lua packages as well, but the chdkptp executables I distribute use a statically linked standalone build from the the official Lua source, so MSYS2 Lua is not directly relevant to chdkptp.

You can also get Python and Lua on windows using WSL (https://docs.microsoft.com/en-us/windows/wsl/) but I have never tried to run chdkptp through WSL and don't know if it's possible to make the USB connection work.
« Last Edit: 23 / September / 2021, 18:18:03 by reyalp »
Don't forget what the H stands for.

Re: Using Multicam and Sonoff
« Reply #2 on: 23 / September / 2021, 16:58:01 »
Thank you very much for your reply, reyalp, and guidance.
re:
Quote
I'm not sure if I completely understand, but FWIW, the chdkptp binaries from https://app.assembla.com/spaces/chdkptp/documents include a complete Lua interpreter and support both the CLI and GUI mode.
To use the CLI, start chdkptp (from command prompt, powershell etc) with chdkptp -i

You can run your own load code or modules from there using the "!" command, but 3rd party modules built in DLL are unlikely to work.
I will go ahead with that and see if I have any problems, i'll consult the above documents or reply if something isn't working properly.

Quote
I'm unclear how you are running multicam from notepad++. Do you somehow have chdkptp built as a modules running within the notepad++ Lua environment, or are you just executing it as an external .exe?
I suppose necessity is the mother of invention.  Honestly, I don't know how I got it working either. But I was working on editing the LUA on Notepad++ I ran this chdkptp macro and the program worked!  (I added an Screenshot here)


Quote
If you want to run your own Lua scripts inside chkdptp, you don't need notepad++, you can just run them with !dofile(...) or use require and call functions with !.
Ok, will do.  I'm hoping once I do what you mention in point 1, I'll be able to get this running as you mention here.

Quote
FWIW, if you want to run everything from the chdkptp Lua interpreter, you could just use standard Lua os.execute or io.popen (or the chdkptp wrapper fsutil.popen) to execute curl or something like that to make the http requests.
This statement will be the most difficult for me to make sense of.  Ideally, I would like to write a program in a .lua file and then execute it via the command line.  If the chdkptp LUA interpreter gets me the ability to make the HTTP request then I will proceed that way.  Is there a way to embed the request in the same lua program as multicam that I can execute it from the command line? 

Quote
Without knowing what you want to accomplish with them, this isn't straightforward question.

If you just want python and easy access to python packages on windows, anaconda is a straightforward standalone install https://www.anaconda.com/products/individual

MSYS2 https://www.msys2.org/ also provides python, along with a complete gcc development environment and large selection of *nix-ish tools. This is the environment I use to develop chdkptp on windows, and I also use it for various python things. However, python in msys2 can have confusing compatibility issues. There are separate python packages for the  "msys" and the "mingw" environments, which are incompatible with each other and have different behavior. "msys" is a unix-ish (more or less) "posix" environment, while the mingw environments are more windows-ish.

MSYS2 provides various Lua packages as well, but the chdkptp executables I distribute use a statically linked standalone build from the the official Lua source, so MSYS2 Lua is not directly relevant to chdkptp.

You can also get Python and Lua on windows using WSL (https://docs.microsoft.com/en-us/windows/wsl/) but I have never tried to run chdkptp through WSL and don't know if it's possible to make the USB connection work.

Thank you very much for this guidance.  I will work in the direction you have indicated above and will come back with more clarification questions. Thanks again! 

*

Offline reyalp

  • ******
  • 14117
Re: Using Multicam and Sonoff
« Reply #3 on: 23 / September / 2021, 18:34:23 »
I suppose necessity is the mother of invention.  Honestly, I don't know how I got it working either. But I was working on editing the LUA on Notepad++ I ran this chdkptp macro and the program worked!  (I added an Screenshot here)
OK, I guess you used the environment from https://chdk.setepontos.com/index.php?topic=12961.0 ?

I don't personally use that, but I assume in that case it's just running the code in chdkptp normally, equivalent to saving the file and then executing it in chdkptp from the command line.

Quote
This statement will be the most difficult for me to make sense of.  Ideally, I would like to write a program in a .lua file and then execute it via the command line. 
You can do this from the command prompt like
Code: [Select]
chdkptp -e"exec dofile('somefile.lua')"
USAGE.TXT in chdkptp directory has more details.

Quote
If the chdkptp LUA interpreter gets me the ability to make the HTTP request then I will proceed that way.  Is there a way to embed the request in the same lua program as multicam that I can execute it from the command line? 
chdkptp Lua does not directly have the ability to make HTTP requests. But as I mentioned, you can use os.execute to call a program like curl (https://curl.se/windows/)

something like
Code: [Select]
os.execute('curl <URL>/cm?cmnd=Power%20TOGGLE')
Note os.execute will just print any output from the command, which in the case of curl with default options, will be the server response plus some status output on stderr. If you need to read the server response, you should use popen instead (see http://www.lua.org/manual/5.2/manual.html#6.9)
Don't forget what the H stands for.


Re: Using Multicam and Sonoff
« Reply #4 on: 24 / September / 2021, 08:59:02 »
Reyalp, you are truly a Titan among mortals.  Thank you very much for providing this whole platform and guiding our projects.  I  hope to be as benevolent in my own work.

re:
Quote
OK, I guess you used the environment from https://chdk.setepontos.com/index.php?topic=12961.0 ?

I don't personally use that, but I assume in that case it's just running the code in chdkptp normally, equivalent to saving the file and then executing it in chdkptp from the command line.
Thank you for the guidance.  I believe as I proceed as instructed above and run things from the chdkptp CLI that will be success.  The Notepad++ was more of a workaround. 

Quote
USAGE.TXT in chdkptp directory has more details.
Once again thank you for the guidance in syntax and showing me where I can find more information.  So very helpful.

Quote
chdkptp Lua does not directly have the ability to make HTTP requests. But as I mentioned, you can use os.execute to call a program like curl (https://curl.se/windows/)

something like
Code: [Select]
os.execute('curl <URL>/cm?cmnd=Power%20TOGGLE')
Note os.execute will just print any output from the command, which in the case of curl with default options, will be the server response plus some status output on stderr. If you need to read the server response, you should use popen instead (see http://www.lua.org/manual/5.2/manual.html#6.9)

Thank you so much for this easy breakdown and the websites to get Curl.  I may have to circle back on the last point when I get to that. But I believe you have given me an understandable blueprint with which to proceed. 

Please let me know if I can contribute to a Patreon page or a good cause in your honor.  Your work is greatly appreciated.

Re: Using Multicam and Sonoff
« Reply #5 on: 26 / September / 2021, 11:06:37 »
Hello Reyalp,

I have done as you recommended and I have began running chdkptp from the command line using the "chdkptp -i" and that works beautifully.  I checked that I have CURL and LUA 5.2 and everything correct.  My problems start when I try to run my Lua file when I try
Code: [Select]
chdkptp -e"exec dofile(basicTest.lua)"  I get all kinds of errors (Screenshot here).  I tried giving the whole path to the file and it gave an "ERROR: compile failed:[String "dofile(C:\...)]".  I'm ashamed to ask what I'm missing, to the master, but I have no one else to ask.

Also after I checked that CURL was installed, I modified the code with the simple call you suggested.
In the future, V2, I would like to perform callbacks to assist with the timings but I have to start with something that works first.  Does this script look like it should work?

Code: [Select]

connect --[[Not sure about this one]]

--[[
This is to start and run Multicam
]]

mc=require('multicam')
mc:connect()
mc:start()
mc:cmdwait('rec')
mc:cmdwait('preshoot')
mc:cmdwait('shoot')
mc:cmd('exit')


os.execute('curl <url>/cm?cmnd=Power%20TOGGLE')
--[[
This is to pause for the turning of the table 3 seconds
]]

sys.sleep(3000)

os.execute('curl <url>/cm?cmnd=Power%20TOGGLE')

mc=require('multicam')
mc:connect()
mc:start()
mc:cmdwait('rec')
mc:cmdwait('preshoot')
mc:cmdwait('shoot')
mc:cmd('exit')


Thank you again for your time.
« Last Edit: 26 / September / 2021, 11:46:14 by anaxantax »

*

Offline Caefix

  • *****
  • 947
  • Sorry, busy deleting test shots...
Re: Using Multicam and Sonoff
« Reply #6 on: 26 / September / 2021, 12:10:16 »
Quote
chdkptp -e"exec dofile(basicTest.lua)"
;) ... Probably it should look like...  :-*
Code: [Select]
chdkptp -e"exec dofile( 'basicTest.lua' )"
All lifetime is a loan from eternity.

*

Offline reyalp

  • ******
  • 14117
Re: Using Multicam and Sonoff
« Reply #7 on: 26 / September / 2021, 15:05:08 »
I get all kinds of errors (Screenshot here).
caefix answer is correct. The filename needs to be quoted.

Quote
In the future, V2, I would like to perform callbacks to assist with the timings but I have to start with something that works first.
I'm not sure what you mean by callbacks here. I assume you want to get some information back from the the table, but how to do that would depend on the interface, which I know nothing about.

Quote
Does this script look like it should work?
There are some things I'd suggest doing differently
Code: [Select]
connect --[[Not sure about this one]]
"connect" is a chdkptp CLI command, so it can't appear like that in a Lua file. In any case, you don't need to use CLI connect with multicam, it uses mc:connect() instead.

If you want to shoot more than once in a single script, you should only repeat the shooting calls, not all of the initialization stuff. I'd suggest something like
Code: [Select]
-- multicam initialization
mc=require('multicam')
mc:connect()
mc:start()
mc:cmdwait('rec')

-- first shot
mc:cmdwait('preshoot')
mc:cmdwait('shoot')

-- turn table
os.execute('curl <url>/cm?cmnd=Power%20TOGGLE')
--[[
This is to pause for the turning of the table 3 seconds
]]

sys.sleep(3000)

os.execute('curl <url>/cm?cmnd=Power%20TOGGLE')

-- second shot
mc:cmdwait('preshoot')
mc:cmdwait('shoot')

-- cleanup
mc:cmdwait('play')
mc:cmd('exit')
Don't forget what the H stands for.


Re: Using Multicam and Sonoff
« Reply #8 on: 27 / September / 2021, 13:20:48 »
I am deeply indebted to this community for helping me with this project.
Thank you, Caefix, and Reyalp so much for your largess and assistance.
 
Quote
I'm not sure what you mean by callbacks here. I assume you want to get some information back from the the table, but how to do that would depend on the interface, which I know nothing about.
The idea would be to trigger the cameras with a signal from the table once it has stopped rather than timing out the process.  Make the functioning more event based rather than timing based. This would be via the HTTP request.  Tasmota (the software running on the Sonoff) would take the request and then output a JSON, which I was thinking, could be used to trigger the cameras.  Currently, I will give power to the motor for enough time to rotate the table 1/8th of a full rotation, stop, give a few milliseconds of the taking of the pictures, and then start the table again for enough time to rotate another 1/8, everything being time based.  I was thinking it would be better if it were event based.  Cameras take their first pictures, that is a queue to start the turntable for its 1/8th turn, when it's finished it says "off", which is a cue for the cameras to take their pictures and so on.  Do you think this is a reasonable plan? If so, what might be some challenges?  Do you have any recommendations on approaches?

Finally, thank you very much for tidying up my code.  When I ran it from
Code: [Select]
chdkptp -e"exe dofile('test2.lua')"I got parts of the system to work.   In my testing, I was able to control the Sonoff to turn on and off when the camera wasn't on. I kept getting an "1: failed I/O error"  when the camera was on even though it did recognize and give the correct USB information.  (screenshot here) Could this be due to low batteries?  (Got them on the charger, now)

This community has made something I could only dream of come near to my grasp.  I am very grateful to receive your replies.  It's like getting prayers answered.
« Last Edit: 27 / September / 2021, 14:51:07 by anaxantax »

*

Offline reyalp

  • ******
  • 14117
Re: Using Multicam and Sonoff
« Reply #9 on: 27 / September / 2021, 16:34:15 »
The idea would be to trigger the cameras with a signal from the table once it has stopped rather than timing out the process.  Make the functioning more event based rather than timing based. This would be via the HTTP request.  Tasmota (the software running on the Sonoff) would take the request and then output a JSON, which I was thinking, could be used to trigger the cameras.  Currently, I will give power to the motor for enough time to rotate the table 1/8th of a full rotation, stop, give a few milliseconds of the taking of the pictures, and then start the table again for enough time to rotate another 1/8, everything being time based.  I was thinking it would be better if it were event based.  Cameras take their first pictures, that is a queue to start the turntable for its 1/8th turn, when it's finished it says "off", which is a cue for the cameras to take their pictures and so on.  Do you think this is a reasonable plan? If so, what might be some challenges?  Do you have any recommendations on approaches?
That makes sense conceptually, but I don't know enough about Sonoff and Tasmota suggest an approach. Does it somehow know the position of the table?

If you need a more complicated interaction with the HTTP API than can easily be done with curl, you could use os.execute to run something else like a python script or whatever.

Quote
I got parts of the system to work.   In my testing, I was able to control the Sonoff to turn on and off when the camera wasn't on. I kept getting an "1: failed I/O error"  when the camera was on even though it did recognize and give the correct USB information.  (screenshot here) Could this be due to low batteries?  (Got them on the charger, now)
What does the camera do in this case? Is the screen on? Does the lens extend? Does the camera turn itself off?
If the battery is very low, it can turn off when you try switch to rec. Usually it will briefly display a message like "Charge the battery" and then turns off.

If this isn't it, you can add run your command like this to get information about exactly where the error occurred
Code: [Select]
chdkptp -e"set err_trace=always" -e"exec dofile('test2.lua')"
Don't forget what the H stands for.

 

Related Topics


SimplePortal 2.3.6 © 2008-2014, SimplePortal