Remote GPIO with Xojo?

@Daniel Wilson : I must admit, I do admire the simplicity and cleverness of your solution. Unfortunately, I also have to deal with unreliable network connectivity issues, power issues, and resource availability schedules that would prevent continuous network connectivity over NFS. With the socket method, I can manage a more passive approach by simply pinging all daemon ports to check for availability and then send directives via host name or IP.

@Ulrich Bogun , @Julia Truchsess : It looks like we’re on to something here. Thanks for pointing me in more directions to explore. I now have plenty of homework and feel very optimistic I’ll have a solution soon (in spite of the fact that the only German I know is from Die Hard and Indiana Jones movies).

If all else fails, and I can’t find a way to connect to the existing pigpio daemon, I’m intrigued by something similar I read in the “Introduction to Programming Xojo” book called EasyTCPSocket that apparently allows you to create your own socket protocol. I’ll back-burner that one for the moment though.

Thanks again everyone!

So, I’m making progress.

Special thanks to @Ulrich Bogun , @Julia Truchsess , and @James Dooley for all your help and pointing me toward TCPSocket.

I was able to get the socket to connect. Now i just need to figure out how to send 4-byte UInt32 commands converted into Little-Endian Hex(8) over the socket (or at least I think that’s what’s going on).

I tried to emulate the “pigpio.py” function which uses…

sl.s.send(struct.pack('IIII', cmd, p1, p2, 0))

to pack the four comma-delimited bytes for command (cmd), parameter 1 (p1), Parameter 2 (p2), and a single ‘0’ for most of the simpler commands into a hex string. For example…

struct.pack('IIII', 0, 20, 1, 0)

produces:

b'\\x00\\x00\\x00\\x00\\x14\\x00\\x00\\x00\\x01\\x00\\x00\\x00\\x00\\x00\\x00\\x00'

I was able to approximate this with the following code, but it’s coming out Big-Endian “00000000000000140000000100000000” and I just feel like I’m doing more than I need to; like I’m unaware of a more efficient command or method. (And if it looks like my code is goofy, it’s because I’m still very new at this. I’m still translating from Sinclair BASIC from 1983 in my head.)

dim h as string
dim cmd() as integer = Array(0, 20, 1, 0)
for each n as Integer in cmd
  n = Uint32(n)
  h = n.tohex(8)
  TextArea1.AppendText(h)
next

Any ideas?

As always, much appreciated. :@)

I can always just flip it around with:

dim mynew as string
for word as integer = 1 to len(h) step 2
  mynew = mid(h,word,2) + mynew
next
TextArea1.text = mynew

It just seems Xojo would have thought of this already.

Go with what works for now and don’t sweat the optimization. If there’s a more elegant way it’ll probably occur to you later.

¯\(?)/¯ Fair enough. Thanks.

A big shout-out and “Thank you!” to @Tim Seyfarth , @Wes Westhaver and @Daniel Wilson, with special thanks to @William Reynolds , @Pedro Ivan Tellez Corella , @James Dooley , @Julia Truchsess , and @Ulrich Bogun for all your kind help and support. And of course special mention to Joan for her original work on the pigpio library and daemon (http://abyz.me.uk/rpi/pigpio/sif.html).

After a solid week of staring at Python code, endless Googling, and seemingly endless trial and error, I was finally able to achieve the first steps toward achieving my goal of native Xojo control of remote Raspi GPIO function over Wi-Fi without an intermediate Python layer.

In answer to my original questions:

  1. Einhugur’s Python 3 script plug-in is fine work, but it uses Python (of course) and it does not address remote GPIO functionality.

  2. Xojo’s own GPIO module is also excellent work, but it too does not provide remote GPIO functionality.

In the end, I was able to communicate with the pigpio daemon remotely via TCPSocket natively in Xojo with surprisingly little code.

This code connects to the daemon:

TCPSocket1.Port = 8888
TCPSocket1.Address = "192.168.2.15"
TCPSocket1.Connect

This formats the socket message:

Function Pack(cmd, p1, p2, p3, ext)
    Dim pad as string = chrb(0)+chrb(0)+chrb(0)
    Dim cmdCode as string = chrb(cmd) + pad + chrb(p1) + pad + chrb(p2) + pad + chrb(p3) + pad + chrb(ext) + pad
    if p3 = 0 then cmdCode = left(cmdCode,16)
    return cmdCode
End Function

And this sends the message to control the GPIO pin to turn on an LED:

Dim setmode as String = Pack(0,20,1,0,0)
TCPSocket1.Write(setmode)
Dim write as string = Pack(4,20,1,0,0)
TCPSocket1.Write(write)

Of course, my code is probably a bit sloppy since I am still very inexperienced, but it’s simple and it works well.

Amazing! How is Xojo not at the top of the list of the most popular programming languages?!

I’ve marked this reply as the answer to my original question. I hope that is not breach of protocol or appears vain in any way. I just thought the question should be properly closed with a detailed answer for anyone else who might stumble upon the same issue.

Thanks again!

Excellent! Glad you worked it out and I’m sure it will be helpful to others at some point, myself probably included. Sockets can seem intimidating at first but as you found out, they generally end up being almost trivial to use.

Xojo’s relative obscurity is a mystery to me as well. Sometimes I can’t help but think that some people prefer arcane, cryptic, and unreadable languages because it makes them feel superior.

Marking your own solution as “the answer” is not at all a breach of protocol :slight_smile:

Thank you Julia. I agree regarding users who insist on the supposed superiority arcane languages presumptuously impart. Now, after writing that last sentence, I may suffer the same illness with the English language. How’s that for irony? I guess what I should have said was, “I know, right?! Yeah! 'Cuz…Man! Really? ‘Ooh, look at me. I can type semi-colons and curly braces all day.’” Meh. Not impressed. Single source an app in C++ and then compile it for Windows, Mac and Linux by click…ing…a…check…box. Then I might be impressed.

Sadly, I think a large part of the problem is branding. Xojo is an amazing IDE and language but “XOJO” isn’t as much a name or brand as it is merely a description; X-platform ObJect Oriented. That’s like saying “Hey, what ingredients did you use for that award-winning souffl?” - ‘Umm…food.

"CREATORS ADMIT UNIX, C HOAX

In an announcement that has stunned the computer industry, Ken Thompson, Dennis Ritchie and Brian Kernighan admitted that the Unix operating system and C programming language created by them is an elaborate April Fool’s prank kept alive for over 20 years. Speaking at the recent UnixWorld Software Development Forum, Thompson revealed the following:

"In 1969, AT&T had just terminated their work with the GE/Honeywell/AT&T Multics project. Brian and I had just started working with an early release of Pascal from Professor Nichlaus Wirth’s ETH labs in Switzerland and we were impressed with its elegant simplicity and power. Dennis had just finished reading ‘Bored of the Rings’, a hilarious National Lampoon parody of the great Tolkien ‘Lord of the Rings’ trilogy. As a lark, we decided to do parodies of the Multics environment and Pascal. Dennis and I were responsible for the operating environment. We looked at Multics and designed the new system to be as complex and cryptic as possible to maximize casual users’ frustration levels, calling it Unix as a parody of Multics, as well as other more risque allusions. Then Dennis and Brian worked on a truly warped version of Pascal, called ‘A’. When we found others were actually trying to create real programs with A, we quickly added additional cryptic features and evolved into B, BCPL and finally C. We stopped when we got a clean compile on the following syntax:

for(;P("
“),R–;P(”|"))for(e=C;e–;P("_"+(*u++/8)%2))P("|"+(*u/4)%2);

To think that modern programmers would try to use a language that allowed such a statement was beyond our comprehension! We actually thought of selling this to the Soviets to set their computer science progress back 20 or more years. Imagine our surprise when AT&T and other US corporations actually began trying to use Unix and C! It has taken them 20 years to develop enough expertise to generate even marginally useful applications using this 1960’s technological parody, but we are impressed with the tenacity (if not common sense) of the general Unix and C programmer. In any event, Brian, Dennis and I have been working exclusively in Pascal on the Apple Macintosh for the past few years and feel really guilty about the chaos, confusion and truly bad programming that have resulted from our silly prank so long ago."

Major Unix and C vendors and customers, including AT&T, Microsoft, Hewlett-Packard, GTE, NCR, and DEC have refused comment at this time. Borland International, a leading vendor of Pascal and C tools, including the popular Turbo Pascal, Turbo C and Turbo C++, stated they had suspected this for a number of years and would continue to enhance their Pascal products and halt further efforts to develop C.

An IBM spokesman broke into uncontrolled laughter and had to postpone a hastily convened news conference concerning the fate of the RS-6000, merely stating ‘VM will be available Real Soon Now’. In a cryptic statement, Professor Wirth of the ETH institute and father of the Pascal, Modula 2 and Oberon structured languages, merely stated that PT Barnum was correct.

In a related late-breaking story, usually reliable sources are stating that a similar confession may be forthcoming from William Gates concerning the MS-DOS and Windows operating environments, and IBM spokesmen have begun denying that the Virtual Machine (VM) product is an internal prank gone awry."

That’s hilarious. Sadly, notice the date: https://www.gnu.org/fun/jokes/unix-hoax.html

FWIW, I would not use a Pi to remote control simple devices like a led or a switch. an arduino (esp8266) is more than enough for that. a Pi is overkill. even a zero.
and you can still send commands to it with xojo ! almost the same way you did .

@Jean-Yves Pochez : Ordinarily, I would agree with you. Arduino’s are ideal for small tasks like that. My intention at this time was just to get it to work at all, so I chose to light an LED as proof of concept. However, my goals with each Raspi station extend much further to include touchscreen functionality, multi-sensor input, multiple device control, camera, news feed, etc.

That being said, for future projects I would be interested in knowing more about how to communicate directly with the arduino using Xojo. I’ve seen one forum thread discussing serial communication via USB. Are you aware of any efforts to connect via Wi-Fi for remote operation?

the esp8266 behaves like a (wifi) web server. so it’s just a GET to some web page that activates the command.
an arduino with a wiznet ethernet module can do the same in ethernet wired mode.

here is a web screen of my soon-to-be hvac control system based on one esp8266 (and one arduino to control the motors) :

picture of the device :

Impressive. Wi-Fi and GPIO on a small form factor. And from my preliminary research, it appears you can pick one up for about 3 bucks. Nice. However, please excuse my ignorance, but I was under the impression that the Arduino IDE is programmed with C++ exclusively. If that’s true, then are you programming the Arduino in one language and then developing the interface for the Web with Xojo? The GUI design does not look familiar.

I’m just looking for the Xojo connection because my experience as a programmer can be counted in days, not years, and I find C++ daunting and unnecessarily verbose. I can hold my own with electronics, barring surface mount (my hands shake terribly and my eyesight is very poor), but if the Arduino can’t be programmed with Xojo, I’m afraid I would be in dark territory indeed.

yes, the arduino is programmed via c++ only. but it can be simple enough as modifying some existing examples.
there is a complete web server example with the ide.
what I love about the arduino is the number of available ressources on the web, you always find some example to help you.

I plan to use a raspberry (and a touch screen and a xojo program) to master all the house.
but all peripheral can be simple arduino like this one.

the interface I showed is only a simple web page, no “programming” involved just some html text that the esp8266 sends to any internet browser.

Well, every tool has its purpose, I suppose. And everyone has their preferences. I make no judgements. However, for me, at this point in my exploration of programming, I have little interest in C++. In fact, I’m not even crazy about Python. I chose Xojo because it is a well-designed evolution of the BASIC language for modern object-oriented programming and is cross-compatible with just about everything. Not only that, but with a head full of 1983 BASIC from my 8-bit days, I was able to quickly Help File/Forum/Google my way through some pretty complicated programming tasks with Xojo and have written a handful of fully-functional desktop apps with relatively little effort.

I understand your point of view. this was just mine …
however don’t be afraid by c++ : most of the time it just looks like basic !

this is some example I use to pilot some stepper motor in c++ arduino ide, it doesn’t look like C…

[code]
void loop() {
// put your main code here, to run repeatedly:
Serial.println ( actual_switch_state);

if (stop_motor_at_startup)
{
    while (actual_switch_state != stopped) {
        actual_switch_state = readSwitchState();
    }
    stop_motor_at_startup = false;
}
else
{
    actual_switch_state = readSwitchState();
    if ((actual_switch_state == fastback) || (actual_switch_state == back)) {
        digitalWrite( motor_direction, HIGH);
    }
    if ((actual_switch_state == fastforw) || (actual_switch_state == forw)) {
        digitalWrite( motor_direction, LOW);
    }
    
    if ((actual_switch_state == fastforw) || (actual_switch_state == fastback)) {
        actual_frequency = max_frequency;
    }
    else
    {
       potar_frequence = analogRead( potar_pin);
       actual_frequency = potar_frequence*(max_frequency-min_frequency);
       actual_frequency = actual_frequency/1023;
       actual_frequency = min_frequency + actual_frequency;
   }
    actual_period = 1/(2*actual_frequency);
    actual_period = 1000000 * actual_period; // en microseconde
    
    if (actual_switch_state != stopped) {
        digitalWrite( motor_clock, HIGH);
        delayMicroseconds( actual_period);
        digitalWrite( motor_clock, LOW);
        delayMicroseconds( actual_period);
    }
}

}[/code]

but it can be quite obscure too :

float InvSqrt (float x){ float xhalf = 0.5f*x; int i = *(int*)&x; i = 0x5f3759df - (i>>1); x = *(float*)&i; x = x*(1.5f - xhalf*x*x); return x; }

I must admit, it is encouraging to see C++ code that includes familiar commands like “if” and “else”, but that second example makes my teeth itch. And all of those curly braces and semi-colons! So unnecessary.

My first introduction to programming was actually with Dartmouth BASIC V on a Teletype terminal in high school in 1981. I was supposed to be learning electronics, but the teacher allowed me to play on the disused terminal in the corner of his office since I already knew how to wire up a relay to the mains without killing myself.

Later, I grew up learning “street” BASIC on a Sinclair ZX81 with only 1K of RAM. Efficient code was a must or it simply wouldn’t fit. Now of course we have gobs of memory and most programmers don’t know how to program efficiently, but more to the point, they don’t care - and it shows, with bloated software that runs poorly.

Then it was Gfa BASIC on the Atari ST, Visual Basic on the PC (until Visual .NET screwed it all up), Liberty BASIC, a short stint with BBC BASIC, and now Xojo. You just can’t beat that check box cross-platform build and standalone web app capability with iOS just begging to be explored.

I tried to follow the new generation of kids and learn Python, but the for-loops were stupid and the editor kept complaining about missing indentation. (Shuddup!) When it first came out, I tried my hand at Swift in Xcode and was still put off by the language structure - they got rid of the semi-colons, but still too many curly braces. Come on! Both the programmer and the computer know when a line of code ends.

Ironically, I must admit that all of that is sneaking up on me though. I started with line numbered BASIC and resisted structured BASIC. Then once I embraced indented code replacing line numbers, I resisted all that “dot stuff”. Once I saw the logic (and necessity) of dot delimited class events, properties and methods in a modern object-oriented, GUI-driven BASIC, my eyes began to clear to begin to finally embrace other languages. And when you look at them from a distance, Xojo, C++, Swift, Python…they all kind of start looking the same after a while. But Xojo doesn’t bog you down with unnecessary punctuation while still keeping commands in an easy-to-remember format and automatically handles the indentations.

I can see the value of C++ (as much as any other language). But those curly braces gotta go!