Livecode mobile app interfacing with real-world hardware

Creating a graphics presentation or artists portfolio? Integrating artwork and code into something unique?

Moderators: Klaus, FourthWorld, heatherlaine, kevinmiller, robinmiller

Livecode mobile app interfacing with real-world hardware

Postby GSA_DC » Wed Jan 21, 2015 8:09 pm

Hi.

A student of mine is looking to build a Livecode app as an interface for his final year installation. The app should be able to control external hardware eg. switch on lights and control another Mac (being used to drive a faked TV). The app is being design to run on an iPad2 (I know that this question is mobile-specific, but also applies here!).

I'm looking for any pointers or advice to feed back. Can an app running on an iPad send OSC, Midi, or TCPIP messages over a closed wireless network? I'm thinking the Mac running the fake TV has an ad hoc Wifi network and the iPad is logged into this and they talk through this. It is simple messages (switch this on or off, change channel, etc.), no dynamic realtime (eg. fading lights). Am I on the right track with OSC? Is there an easy implementation out there which already exists?

Any help appreciated. Thanks.

Kind regards, Paul.
GSA_DC
 
Posts: 35
Joined: Thu Nov 27, 2014 2:07 pm

Re: Livecode mobile app interfacing with real-world hardware

Postby Mark » Tue Jan 27, 2015 11:58 am

Hi Paul,

LiveCode has sockets, which use the TCP/IP protocol. I wouldn't use OSC if this is a one-time project for in-house use. OSC is a lot of work for something simple like sending data from one device to another. Instead, use sockets to send text data over the network and have this text data interpreted by a script on the desktop machine. I would think that fading lights are easy to add. Simple visual effects have nothing to do with real-time or dynamics. I don't know of an existing implementation, but there surely are people who have created apps that let mobile devices communicate with desktop devices.

Kind regards,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/livecodebook
Mark
Livecode Opensource Backer
Livecode Opensource Backer
 
Posts: 5118
Joined: Thu Feb 23, 2006 9:24 pm

Re: Livecode mobile app interfacing with real-world hardware

Postby GSA_DC » Tue Jan 27, 2015 10:37 pm

Thanks Mark. Good advice! I'll let you know how I get on.

Kind regards, Paul.
GSA_DC
 
Posts: 35
Joined: Thu Nov 27, 2014 2:07 pm

Re: Livecode mobile app interfacing with real-world hardware

Postby GSA_DC » Mon Feb 02, 2015 11:41 pm

Hi again.

I'm struggling a bit with this issue still. Any advice on how to set it up would be really appreciated. Iam simply trying to make a mobile iOS app running a Livecode iOS app communicate with a Mac running a Livecode OSX app. Here's what I have done:

Server (OSX) card script:
Code: Select all
on openCard
   accept connections on port 1234 with message "clientConnected"
end openCard

on clientConnected pSocket
   read from socket pSocket with message "messageReceived"
   put "Connection on socket "&pSocket into fld "F_connection"
end clientConnected

on messageReceived pSocket, pMsg
    read from socket pSocket with message "messageReceived"
   put pMsg into fld "F_messages"
end messageReceived


Client (iOS) card script:
Code: Select all
local lSocket

on openCard
   open socket to "http://192.168.0.1:1234"
end openCard

on messageReceived pSocket, pMsg
   // write pMsg to socket pSocket
   read from socket pSocket with message "messageReceived"
   put pMsg into fld "F_messages"
   put pSocket into lSocket
end messageReceived

on sendMessage pMsg
   put "MESG," & the number of chars in pMsg & return & pMsg into tMsg
   write tMsg to socket lSocket
end sendMessage


I'm running it on a separate LAN (an ad hoc network from the Mac). Is this all I should have to do to make a connection? Anyone got any pointers?

Kind regards, Paul.
GSA_DC
 
Posts: 35
Joined: Thu Nov 27, 2014 2:07 pm

Re: Livecode mobile app interfacing with real-world hardware

Postby jacque » Tue Feb 03, 2015 8:52 pm

I don't see any mobile support for sockets in the dictionary. It looks like that is desktop-only.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com
jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
 
Posts: 3796
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN

Re: Livecode mobile app interfacing with real-world hardware

Postby GSA_DC » Thu Feb 05, 2015 1:56 pm

Hi.

Thanks for your response here. That explains why I can't get anything running between mobile and desktop.

The question is, if sockets are not in fact supported in mobile, then do you have any suggestions on a way to do this?
I suppose I could run the desktop machine as a webserver on a private LAN with a fixed IP, and have the mobile client speak to a PHP script. Or something. Ultimately I need a mobile client on a private LAN to be able to affect a desktop application. Any advice on a best practice here? This is for a staged exhibition context.

Kind regards, Paul.
GSA_DC
 
Posts: 35
Joined: Thu Nov 27, 2014 2:07 pm

Re: Livecode mobile app interfacing with real-world hardware

Postby jacque » Thu Feb 05, 2015 10:00 pm

I think using a private server would work great. You could either use a PHP script there or install LC server and use LiveCode scripting everywhere. Both the mobile app and the desktop app could easily send and receive html requests. This way has the advantage of using pretty simple LC commands too; communicating with a server is basically a one-liner.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com
jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
 
Posts: 3796
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN

Re: Livecode mobile app interfacing with real-world hardware

Postby GSA_DC » Mon Feb 09, 2015 11:06 pm

Hi again Jacque.

Thanks for your feedback. How do I have the Livecode desktop app running fullscreen on the server receive the message from the PHP script? I'm totally lame when it comes to server-side scripting - no clue. Sorry to keep looking for help on this - keen to know a preferred route - would be great of you knew of any examples I could work from.

Kind regards, Paul.
GSA_DC
 
Posts: 35
Joined: Thu Nov 27, 2014 2:07 pm

Re: Livecode mobile app interfacing with real-world hardware

Postby jacque » Tue Feb 10, 2015 8:07 pm

I'd use a php script or a LiveCode server script to catch the incoming data. The client app on the mobile device would post the data to the server script URL. The server script would process the data (if necessary) and then make it available to the desktop app. That part is a little trickier; one of the easier ways is to write the data to a text file and have the desktop app periodically check to see if there is new data in the file. If it sees there is, it reads the file and uses the data in whatever way it needs to.

Checking for new data can be done in several ways. One frequently used way is to create a separate "flag" file that the desktop app would look for. If the desktop app sees that the flag file exists, it reads the data file and then deletes the flag file. The server would create a new flag file the next time the data changes.

One thing to watch out for is a race condition, where the desktop app and the server script are trying to access the data file at the same time. To avoid that, the desktop app should not read the data until the flag file exists (meaning the server is done writing to it,) and the server app should not write to the data file while the flag file exists (because the app is still reading it.) That means both the server and the app may need to retry their respective actions a few times until the flag is in the correct state.

If you know for sure that there will only be a single user and the data will be infrequently updated, you may be able to just ignore race conditions. It wouldn't work for a commercial app or one with multiple users, but for a one-student practice project it might work well enough.

Edit: Now that I think about it, the data file could serve as the flag file, so you wouldn't need two files. If the data file exists, the app hasn't read it yet. The app would delete the data file when it's done, and the server would just create it again the next time.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com
jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
 
Posts: 3796
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN

Re: Livecode mobile app interfacing with real-world hardware

Postby SparkOut » Tue Feb 10, 2015 10:25 pm

Didn't Andre have a complete http server written in LiveCode (or transcript, or revolution, or whenever it was) ?

You could run the server stack on the TV machine, and on the client when you press the "channel up" button you post the channel up value to the server. The server could process the parameter and hand off to a handler that does the necessary action.
SparkOut
 
Posts: 1627
Joined: Sun Sep 23, 2007 4:58 pm

Re: Livecode mobile app interfacing with real-world hardware

Postby jacque » Wed Feb 11, 2015 8:37 pm

Didn't Andre have a complete http server written in LiveCode (or transcript, or revolution, or whenever it was) ?


Yeah, actually, he did. I think it was set up for fast CGI and he said it was broken now. There's a minimal MetaCard http server somewhere. But I think the easiest thing these days is just to install LC Community Server.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com
jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
 
Posts: 3796
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN

Re: Livecode mobile app interfacing with real-world hardware

Postby FourthWorld » Wed Feb 11, 2015 11:14 pm

In its simplest form. a Web sever is just an app that responds to requests on port 80. To show off MetaCard's socket support Scott Raney made mchttd.mc, a simple stack that acts as a Web server.

Andre made extensive enhancements to mchttpd, but he's interested in a level of scaling for public sites, which can't be done with conventional CGI in a single-threaded engine, so he's moved on to other projects.

The problem isn't that FastCGI became broken, but that it was never truly feasible, since it requires any app using it to support multiple threads to handle concurrent connections.

For small workground mchttpd and similar socket servers can be a good solution.

With Dr. Raney's permission, I've added a necessary header-output adjustment to his original stack, and have posted it here, under MIT license:
http://www.fourthworld.net/lc/mchttpd-4W.zip

All that said, Apache is a very good Web server, and CGI is efficient enough for a wide range of public web sites. While there's interest in the community in exploring threading additions to the engine needed for FastCGI for large-scale deployments, for most of us CGI is fine.
Richard Gaskin
Community volunteer LiveCode Community Liaison

LiveCode development, training, and consulting services: Fourth World Systems: http://FourthWorld.com
LiveCode User Group on Facebook : http://FaceBook.com/groups/LiveCodeUsers/
FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
 
Posts: 5305
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles

Re: Livecode mobile app interfacing with real-world hardware

Postby GSA_DC » Mon Feb 23, 2015 7:07 pm

HI again all.

Heads up on this. I now have something working quite well. I have Apache and mySQL running on this Mac (easiest for beginners to download MAMP). And I have written a small PHP script to take an input and write it out to a local text file. This is then read by a Livecode desktop app. All fast and great. Thanks for advice on this!

Final issue is getting the file creation date of the text file and comparing it to the previous - so application only responds to changes. It seems there is 'the detailed files' but no examples on how to parse, that I can find. Trying to resolve now. Any simple advice on this would be appreciated.

Kind regards, Paul.
GSA_DC
 
Posts: 35
Joined: Thu Nov 27, 2014 2:07 pm

Re: Livecode mobile app interfacing with real-world hardware

Postby GSA_DC » Mon Feb 23, 2015 8:07 pm

Ok all. Resolved it - as it happens the help on 'files' was very useful! Thanks.
GSA_DC
 
Posts: 35
Joined: Thu Nov 27, 2014 2:07 pm

Re: Livecode mobile app interfacing with real-world hardware

Postby dave.kilroy » Mon Jul 20, 2015 10:12 am

@GSA_DC as of LiveCode 7.1(dp1) sockets work on iOS and Android - too late for you just now but maybe if you ever come back and revamp this project?
"...this is not the code you are looking for..."
dave.kilroy
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
 
Posts: 824
Joined: Wed Jun 24, 2009 1:17 pm
Location: Plymouth, UK


Return to Digital Art and Portfolios

Who is online

Users browsing this forum: No registered users and 2 guests