# Video -- So close!



## Shdwdrgn (Dec 23, 2014)

I finally got around to hooking up a camera to one of the arduino-type computers last night. I have 320x240 streaming video at about 25 frames per second, which isn't bad at all, however the bottom half of the frame is corrupted...










Been digging around this morning and found someone with nearly the same issue, plus a couple suggested solutions. Can't wait to try it out again tonight!

In the meantime, a bit of explanation of what I have here. I am using an ESP8266-12F, which programs like an arduino, but it has built-in wifi and a clock speed 10x faster. Because the camera uses so many of the I/O lines, I only have enough left over to control the loco speed and direction, plus a couple of lights (however there is a new ESP just released which has a LOT more I/O lines!)... This is enough for basic operations though, so I can test out the concepts.

The camera is an ArduCAM mini 2mp. The entire board is only 25x35mm, not quite small enough to fit in a cab, but absolutely microscopic compared to most setups I see using a video camera mounted on a flat car.

The goal with this little setup is to make the loco controllable from a web page, with full video showing. Imagine if you had such a rig at a show, allowing people to connect with their cell phones? (Yes, I will have collision-avoidance implemented before that happens!) The layout will use a raspberry pi as the primary computer, providing the wifi access point and keeping track of train positions, however the ESP chips on board each loco will create their own web page that you connect to. For example, with the video test I set up last night I would just point my browser at http://drgw321/ and it brings up the live feed (this only works if you are attached to the network that the trains are on).

Anyway, thought I would share my near-success. Once I can get the video stream working, I'll take a break from that and get my test track put together, then come back to the electronics and put together a loco with the full complement of new hardware.


----------



## gunrunnerjohn (Nov 10, 2010)

That's great, waiting for updates.


----------



## Shdwdrgn (Dec 23, 2014)

Woot! Got it working. Oddly, the solution was to create a _smaller_ buffer size.

So here's what I can tell so far... 

Occasionally it gets a bad frame of data, which gets sent to the web page and crashes the playback. I can probably fix that by determining how large a full frame should be, and not sending anything of a different size.

There is a noticeable lag in the video stream... not much, maybe 0.1 second, but if I were running a train from this video I would want to take it a bit slower.

The lens on this is pretty decent, considering the low resolution. It is a manual focus, but one setting can provide a decent view of the area.

This camera will wok with infrared lighting for night shots, but you can tell it's kind of grainy in lower light

There is a ton of extra code in the example because it was made to work with several different versions of two different camera boards. Most of that is in the setup, though, and I don't think there's much of anything to remove that would help the speed once the code begins streaming the video feed.

Ideally I would like to crop the final image into a 1:2 frame, something like 320x160 so it shows a nice panoramic view of the layout without showing the walls and ceiling. To do that, I have a lot to learn about the images being generated. The output is a stream of JPEG images, which means the chip on the camera board is doing a lot of processing to format each image. Since there are higher-resolution boards available, I'm thinking they probably process the images even faster, and that may be the key to eliminating the lag in my low-res video feed.

Still a lot of work to do to get this code wrapped up into something I can use, but at least it is actually working tonight. So for now, I leave you with a wave...


----------



## gunrunnerjohn (Nov 10, 2010)

I bought a WiFi camera on eBay that streams HD video to any WiFi device, I can watch it run on my phone. It has a pretty small sensor that could fit in a locomotive, but the body is a bit larger.


----------



## Shdwdrgn (Dec 23, 2014)

Some of these board have the camera chip on a ribbon cable, so it might be possible to lay the board down flat and then pull the camera up where you need it. The particular board I have here has the camera soldered on directly though.

So I made progress on the video delay. I found the setting to change the bus clock speed and doubled it to 8Mhz, which seems about the maximum it will run stably. However there's another issue, in that I noticed the longer the program runs, the slower the refresh gets. Seems to be a hiccup in the code, but I haven't had time to troubleshoot it yet. When the setup is first started, the video response is very quick, so I know the camera *can* work well, and is likely perfectly fine at higher resolution, if I can figure out what's wrong with the code.


----------

