Wifi Microscope

Here is a little snippet which makes up a simple GUI for an el-cheapo Wifi Microscope you can get for about 35 bucks on the Internets. The code is ported from a Python example found in github .

# Proof-of-concept code for reading data from a Wifi microscope.
# See https://www.chzsoft.de/site/hardware/reverse-engineering-a-wifi-microscope
#
# Copyright (c) 2020, Christian Zietz <[email protected]>
# Copyright (c) 2023, chw at ch minus werner dot de for the Tcl port
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

package require Tk
package require udp
package require Img

set HOST 192.168.29.1   ;# Microscope hard-wired IP address
set SPORT 20000         ;# Microscope command port
set RPORT 10900         ;# Receive port for JPEG frames

set JHCMD1000 [binary decode hex 4A48434D441000]
set JHCMD2000 [binary decode hex 4A48434D442000]
set JHCMDD001 [binary decode hex 4A48434D44D001]
set JHCMDD002 [binary decode hex 4A48434D44D002]

set IMG [image create photo]
$IMG configure -width 1280 -height 720

set JPEGBUF {}

proc setup {sock} {
    puts -nonewline $sock $::JHCMD1000
    puts -nonewline $sock $::JHCMD2000
    puts -nonewline $sock $::JHCMDD001
    puts -nonewline $sock $::JHCMDD001
    after 1000 [list heartbeat $sock]
}

proc finalize {sock} {
    after cancel [list heartbeat $sock]
    puts -nonewline $sock $::JHCMDD002
    close $sock
    exit 0
}

proc heartbeat {sock} {
    after 2000 [list heartbeat $sock]
    puts -nonewline $sock $::JHCMDD001
}

proc readsock {sock jpegvar img} {
    upvar $jpegvar jpegbuf
    set data [read $sock 1450]
    binary scan $data "SccIa*" frame _ packet _ blk
    if {![info exists blk]} {
        return
    }
    if {$packet == 0} {
        if {[string length $jpegbuf]} {
            catch {$img configure -format jpeg -data $jpegbuf}
        }
        set jpegbuf {}
    }
    append jpegbuf $blk
}

wm title . "Wifi Microscope"
label .l -image $IMG
pack .l -side top -fill both -expand 1

set SOCK [udp_open $RPORT reuse]
fconfigure $SOCK -remote [list $HOST $SPORT]
fconfigure $SOCK -buffering none -translation binary
fileevent $SOCK readable [list readsock $SOCK JPEGBUF $IMG]

setup $SOCK

wm protocol . WM_DELETE_WINDOW [list finalize $SOCK]
bind . <Escape> [list finalize $SOCK]