Remote GPIO with Xojo?

  1. ‹ Older
  2. 4 months ago

    Daniel W

    May 14 Australia

    @Scott R I'm trying to find a more Xojo-centric solution that does not require knowledge of additional languages (with the exception of a few new gpio library commands of course).

    I see... those bash commands are simply manipulating files. With the help of text streams Xojo's FolderItem should be do it. Perhaps I will build a class for this one day. I tire of partially compatible GPIO libraries that never work on different ARM devices. That is why I switched to using the gpio folders... no headaches since!

    Have fun Scott!

  3. Ulrich B

    May 14 Pre-Release Testers, Xojo Pro Europe (Germany, Berlin) · xo...

    As others here suggested, I believe you should be able to address the C daemon quite easily. It establishes a connection on a specified port, so you should be able to talk to it using TCPSocket(s) (and possibly ServerSockets if you the daemon can talk to the host too).
    I wrote a blog post about a similar project connecting FileMaker and Xojo via Sockets, but it is in German only and I am afraid the built-in Google translator will work as … hmm … usual: https://xojoblog.me/2017/11/23/1-x-filemaker-zu-xojo-und-zurueck-bitte/

    Anyway, I think a similar approach would work. Let me know if the translation is completely unreadable.

  4. Scott R

    is not verified May 14 Pre-Release Testers, Xojo Pro San Diego, California

    @Daniel W : 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 B , @Julia T : 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!

  5. Scott R

    is not verified May 14 Pre-Release Testers, Xojo Pro San Diego, California
    Edited 4 months ago

    So, I'm making progress.

    Special thanks to @Ulrich B , @Julia T , and @James D 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. :@)

  6. Scott R

    is not verified May 15 Pre-Release Testers, Xojo Pro San Diego, California
    Edited 4 months ago

    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.

  7. Julia T

    May 15 Sandy Hook, Connecticut

    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.

  8. Scott R

    is not verified May 15 Pre-Release Testers, Xojo Pro San Diego, California

    ¯\_(ツ)_/¯ Fair enough. Thanks.

  9. Scott R

    is not verified May 19 Pre-Release Testers, Xojo Pro Answer San Diego, California

    A big shout-out and "Thank you!" to @Tim S , @Wes W and @Daniel W, with special thanks to @WilliamReynolds , @Pedro I;Tellez Corella , @James D , @Julia T , and @Ulrich B 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!

  10. Julia T

    May 19 Sandy Hook, Connecticut
    Edited 4 months ago

    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 :)

  11. Scott R

    is not verified May 19 Pre-Release Testers, Xojo Pro San Diego, California
    Edited 4 months ago

    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.'

  12. Julia T

    May 20 Sandy Hook, Connecticut

    "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("\n"),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."

  13. Scott R

    is not verified May 20 Pre-Release Testers, Xojo Pro San Diego, California

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

  14. Jean-Yves P

    May 20 Pre-Release Testers, Xojo Pro Europe (France, Besançon)

    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 .

  15. Scott R

    is not verified May 20 Pre-Release Testers, Xojo Pro San Diego, California

    @Jean-YvesPochez : 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?

  16. Jean-Yves P

    May 20 Pre-Release Testers, Xojo Pro Europe (France, Besançon)
    Edited 4 months ago

    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) :
    -image-

    picture of the device :
    -image-

  17. Scott R

    is not verified May 20 Pre-Release Testers, Xojo Pro San Diego, California

    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.

  18. Jean-Yves P

    May 20 Pre-Release Testers, Xojo Pro Europe (France, Besançon)

    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.

  19. Scott R

    is not verified May 21 Pre-Release Testers, Xojo Pro San Diego, California

    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.

  20. Jean-Yves P

    May 21 Pre-Release Testers, Xojo Pro Europe (France, Besançon)
    Edited 4 months ago

    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...

    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);
            }
        }
        
    }

    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;
    }
  21. Scott R

    is not verified May 21 Pre-Release Testers, Xojo Pro San Diego, California

    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!

or Sign Up to reply!