Easy IOT- Internet of Things

There are lots of ways to access hardware interfaced devices across the internet. You can buy ready-to-go hardware easily. I've always enjoyed this sort of thing. However unless you work via a third-party server it can be messy programming, since most of us have an external dynamically-issued internet address which can change, so we may need to have an account on a DNS server to cover this, and may need to know about opening ( and protecting!) ports. And client and control machines will generally be off ( or offline) for significant amounts of time so a signal may be missed unless hosted on a third machine which is always on.

NB If interfacing to mains-driven kit be very aware of safety, loading and wiring considerations. I bought a web-enabled mains switch off the internet a few weeks back- works beautifully via a mobile phone interface. But although it uses a claimed 10 amp switching relay, it is wired via 3A connecting screw-blocks, and supplied with 2A twin-core cable, no earth, and few warnings. Buyer beware!

However we all use a web browser without needing to know such details. LB makes it pretty easy to webscrape a web page ( at client end), and also to ftp a file to a site ( at control- server- end). I've used this for several years for things like turning on a heater in a remote property 150 miles away, where I have a low power PC running continuously, or sometimes at pre-arranged times. This periodically scrapes the webpage and parses the contents and acts accordingly. Of course I could instead ftp to that remote machine and have the client program read the latest local command file. But I like being able to see the control file on a webpage from anywhere as well as being able to alter it from anywhere. For a simple guy like me it is even fun to see one of my home computers turn something on from another of my machines in the house!

LB code to scrape and parse a website and act appropriately

This uses the httpget$() command to read the entire command file once a minute from a website. It then parses out the bit which tells it what a particular attached device is supposed to do. Here a graphics window simply changes color- normally it would actuate an external device.

    timer 60000, [checkWeb]
    wait

[checkWeb]
    htmlText$ = httpget$( "http://www.diga.me.uk/command.txt")	'	a file on my website..
    'print upto$( afterlast$( htmlText$, ">command<"), ">/command<") ' for debug/testing
    c$ =upto$( afterlast$( htmlText$, ">command<"), ">/command<")
    if c$ ="on" then #wg "fill red" else #wg "fill darkblue"
    wait

    end

Typical control file

An easy file structure is fun to invent. Here I use a name for the device to identify it and follow it with the command. I also use things like 'lamp1|on' or 'lamp1|off', with CRLF separators in the file. This example commands just one device.

<command>off</command>

LB code to ftp a control file to site

Uses ftp4w32.dll which works nicely on my Linux/Wine setup. Perfectly possible to do this instead outside LB, by suitable ftp ( or sftp) software on a computer or mobile phone.)

    nomainwin

    dim error$(6000)

    error$(   0) ="No problem.."
    error$(1003) ="user not connected to server"
    error$(1004) ="cannot open file"
    error$(1008) ="action not taken"
    error$(1010) ="server cannot open file"
    error$(3001) ="3001"
    error$(5002) ="5002"

    WindowWidth  =  100
    WindowHeight =   40
    UpperLeftX   =    1
    UpperLeftY   =    1

    port          = 21                       'port to use
    pasv          = 1                        'PASV mode? 1 is yes, 0 is no
    ftphost$      = "zzzzzzzzzz"             'ftp site address
    login$        = "zzzzzzzzz"              'ftp site username login
    password$     = "zzzzzzzz"               'ftp site password
    initpath$     = ""                       'leave blank if you want the site's default
    upload$       = "command.txt"            'full path & file info for file to upload
    remotefile$   = "command.txt"            'what you want it to be called on the ftp server

    TRUE          = 1
    FALSE         = 0

    ftpsession = FALSE

    open "LB" for graphics as #main

    #main, "trapclose [quit.main]"

    hW =hwnd( #main)

[upload]
    parentftp =hW   'nd( #session)

    open "ftp4w32.dll" for dll as #ftp

    ftpsession = TRUE

    calldll #ftp, "FtpInit", parentftp as long, res as long
    'print res; " "; error$( res)

    calldll #ftp, "FtpSetSynchronousMode", void as long
    calldll #ftp, "FtpSetDefaultPort", port as long, void as long
    calldll #ftp, "FtpSetPassiveMode", pasv as long, void as long
    t =time$( "seconds")
    calldll #ftp, "FtpLogin", ftphost$ as struct, login$ as struct, password$ as struct, parentftp as long, 0 as long, res as long
    calldll #ftp, "FtpCWD", initpath$ as struct, res as long
    print res; " "; error$( res); " ";
    calldll #ftp, "FtpSendFile", upload$ as struct, remotefile$ as struct, 0 as long, 0 as long, parentftp as long, 0 as long, res as long
    'print res; " "; error$( res);

wait

[closeftp]
    calldll #ftp, "FtpCloseConnection", void as long
    calldll #ftp, "FtpRelease", void as long
    close #ftp
    ftpsession = FALSE
    'print "ftp call over: delaying 600000ms ie 600s or 10 minutes."

    wait

[quit.main]
    close #main
    if ftpsession = TRUE then
        calldll #ftp, "FtpCloseConnection", void as long
        calldll #ftp, "FtpRelease", void as long
        close #ftp
    end if
    END