Login | Register 
FEATURES
APPLICATIONS
DOWNLOADS
EDUCATION
BLOG
WIKI
FORUM
STORE

looking for a simple relay controller

Discuss graphics hardware, computer hardware, and input/output devices to use with TouchDesigner

Re: looking for a simple relay controller

Postby jpkelly » Sat Jan 26, 2019 1:55 am

Recently used this. (with OSC)
https://shop.pimoroni.com/products/automation-hat + a Pi
The code is here (but not latest version PM me if you are interested)
https://github.com/jpkelly/autopi
Also had to hack the Automation Hat library a bit based on this post.
https://forums.pimoroni.com/t/automatio ... issue/6692
jpkelly
 
Posts: 5
Joined: Sat Sep 24, 2016 12:45 am

Re: looking for a simple relay controller

Postby harveymoon » Mon Jan 28, 2019 5:00 pm

I use ESP's for everything now.

you can get pre-packaged chips that work just like arduino, I like the D1 Mini series.

https://www.amazon.com/IZOKEE-NodeMcu-I ... ds=d1+mini

just like arduino they come with all sorts of shields

https://www.amazon.com/Diymore-ESP8266-Development-Shield-Arduino/dp/B01N9KG234/ref=sr_1_1?ie=UTF8&qid=1548708397&sr=8-1&keywords=d1+mini+relay


and with a little bit of code you can connect it to Touch in a few minutes.

I also use OSC for things like this.

[ below is arduino code ]


Code: Select all

#include <WiFiUdp.h>
#include <ESP8266WiFi.h>
#include <OSCMessage.h>
#include <OSCBundle.h>

WiFiUDP Udp;                                // A UDP instance to let us send and receive packets over UDP

IPAddress outIp(10, 0, 1, 5);     // remote IP of your computer
const unsigned int outPort = 9900;          // remote port to receive OSC
const unsigned int localPort = 9901;        // local port to listen for OSC packets (actually not used for sending)

const char* ssid     = "[YOUR SSID OF WIFI]";
const char* password = "[THE WIFI PASSWORD]";

unsigned int ledState = LOW;              // LOW means led is *on*

void setup() {
  pinMode(D1, OUTPUT);
  digitalWrite(D1, ledState);

  Serial.begin(115200);
  delay(100);

  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
 
  Serial.println("Starting UDP");
  Udp.begin(localPort);
  Serial.print("Local port: ");
  Serial.println(Udp.localPort());
  server.begin();
}

void oscMessage(int input) {
  Serial.print("oscMessage()");
  OSCMessage msg("/esp");
  msg.add(input);
  Udp.beginPacket(outIp, outPort);
  msg.send(Udp);
  Udp.endPacket();
  msg.empty();
  delay(50);
}

void relay(OSCMessage &msg, int addrOffset ) {
  ledState = msg.getInt(0);
  digitalWrite(D1, ledState);
  Serial.print("/led: ");
  Serial.println(ledState);
   oscMessage(ledState);
}

void loop() {
 OSCMessage msgIN;
  int size;
  if ( (size = Udp.parsePacket()) > 0)
  {
    while (size--)
      msgIN.fill(Udp.read());
    if (!msgIN.hasError()) {
      msgIN.route("/relay", relay);
    }
    outIp = Udp.remoteIP();
  }
}




in touch, use an OSCOUT dat and run this script

Code: Select all
op('oscout_relay').sendOSC('/relay',[1])


or this one

Code: Select all
op('oscout_relay').sendOSC('/relay',[0])




Heres something I literally threw together in 10 minutes.

https://photos.app.goo.gl/kUkHgcjHfreXy5DP7
harveymoon
 
Posts: 90
Joined: Mon Jun 29, 2015 8:42 pm

Re: looking for a simple relay controller

Postby cod65 » Tue Jan 29, 2019 7:13 pm

well my partner went with phidget relay so I'm tasked with figuring out, finding it to be a bit beyond me.

I've been hacking away at 'DigitalOutput' example script with intention of running in CHOPEXECUTE DAT, got standalone script below working with a hard-wired address in IDLE3.5.1, but within Touch099 its unreliable and slow. I'm not fully clear on open vs. attach channel requirements in script.

If any phidget users could point to a bare-minimum script for Touch I'd be much obliged. I cant seem to pare it down to what is essential with all the exceptions and support for usb hub/network stuff.


Code: Select all
#import sys
import time
#import traceback
from Phidget22.Devices.DigitalOutput import *
from Phidget22.PhidgetException import *
from Phidget22.Phidget import *
from Phidget22.Net import *

'''try:
    from PhidgetHelperFunctions import *
except ImportError:
    sys.stderr.write("\nCould not find PhidgetHelperFunctions. Either add PhdiegtHelperFunctions.py to your project folder "
                      "or remove the import from your project.")
    sys.stderr.write("\nPress ENTER to end program.")
    readin = sys.stdin.readline()
    sys.exit()'''

'''
* Displays info about the attached Phidget channel.
* Fired when a Phidget channel with onAttachHandler registered attaches
*
* @param self The Phidget channel that fired the attach event
'''
def onAttachHandler(self):
   
    ph = self
    try:
        #If you are unsure how to use more than one Phidget channel with this event, we recommend going to
        #www.phidgets.com/docs/Using_Multiple_Phidgets for information
   
        print("\nAttach Event:")
       
        """
        * Get device information and display it.
        """
        serialNumber = ph.getDeviceSerialNumber()
        channelClass = ph.getChannelClassName()
        channel = ph.getChannel()
       
        deviceClass = ph.getDeviceClass()
        if (deviceClass != DeviceClass.PHIDCLASS_VINT):
            print("\n\t-> Channel Class: " + channelClass + "\n\t-> Serial Number: " + str(serialNumber) +
                  "\n\t-> Channel:  " + str(channel) + "\n")
        else:           
            hubPort = ph.getHubPort()
            print("\n\t-> Channel Class: " + channelClass + "\n\t-> Serial Number: " + str(serialNumber) +
                  "\n\t-> Hub Port: " + str(hubPort) + "\n\t-> Channel:  " + str(channel) + "\n")
                 
    except PhidgetException as e:
        print("\nError in Attach Event:")
        DisplayError(e)
        traceback.print_exc()
        return
"""
* Displays info about the detached Phidget channel.
* Fired when a Phidget channel with onDetachHandler registered detaches
*
* @param self The Phidget channel that fired the attach event
"""
def onDetachHandler(self):

    ph = self
    try:
        #If you are unsure how to use more than one Phidget channel with this event, we recommend going to
        #www.phidgets.com/docs/Using_Multiple_Phidgets for information
   
        print("\nDetach Event:")
       
        """
        * Get device information and display it.
        """
        serialNumber = ph.getDeviceSerialNumber()
        channelClass = ph.getChannelClassName()
        channel = ph.getChannel()
       
        deviceClass = ph.getDeviceClass()
        if (deviceClass != DeviceClass.PHIDCLASS_VINT):
            print("\n\t-> Channel Class: " + channelClass + "\n\t-> Serial Number: " + str(serialNumber) +
                  "\n\t-> Channel:  " + str(channel) + "\n")
        else:           
            hubPort = ph.getHubPort()
            print("\n\t-> Channel Class: " + channelClass + "\n\t-> Serial Number: " + str(serialNumber) +
                  "\n\t-> Hub Port: " + str(hubPort) + "\n\t-> Channel:  " + str(channel) + "\n")
       
    except PhidgetException as e:
        print("\nError in Detach Event:")
        DisplayError(e)
        traceback.print_exc()
        return

"""
* Writes Phidget error info to stderr.
* Fired when a Phidget channel with onErrorHandler registered encounters an error in the library
*
* @param self The Phidget channel that fired the attach event
* @param errorCode the code associated with the error of enum type ph.ErrorEventCode
* @param errorString string containing the description of the error fired
"""
def onErrorHandler(self, errorCode, errorString):

    sys.stderr.write("[Phidget Error Event] -> " + errorString + " (" + str(errorCode) + ")\n")

"""
* Creates, configures, and opens a DigitalOutput channel.
* Provides interface for setting the duty cycle of the DigitalOutput channel
* Closes out DigitalOutput channel
*
* @return 0 if the program exits successfully, 1 if it exits with errors.
"""
def main():

    ch = DigitalOutput()

       
    ch.setDeviceSerialNumber(499892)
    ch.setHubPort(0)
    ch.setIsHubPortDevice(0)
    ch.setChannel(0)

    print("\nSetting OnAttachHandler...")
    ch.setOnAttachHandler(onAttachHandler)
       
    print("Setting OnDetachHandler...")
    ch.setOnDetachHandler(onDetachHandler)
       
    print("Setting OnErrorHandler...")
    ch.setOnErrorHandler(onErrorHandler)
       
       

    ch.openWaitForAttachment(5000)
    ch.setDutyCycle(1.0)
    time.sleep(3)
    ch.setDutyCycle(0.0)
    time.sleep(3)
    ch.setDutyCycle(1.0)


main()

<.o'>
cod65
 
Posts: 555
Joined: Mon May 10, 2010 2:55 pm

Re: looking for a simple relay controller

Postby doctorhandshake » Tue Jan 29, 2019 7:34 pm

Can you send a .toe showing how you have this set up?

Looks like it's attaching callbacks to a persistent connection to the device after some blocking calls (sleep), both of which are a bad fit for running within your main TD instance -- this is why I was suggesting you might want to run this code in a freestanding Python process and communicate to it from your TD process with sockets or whatever suits.
Noah Norman
http://hardwork.party

codename: massapequa
User avatar
doctorhandshake
 
Posts: 132
Joined: Wed Aug 17, 2011 6:31 pm
Location: Brooklyn, NY

Re: looking for a simple relay controller

Postby cod65 » Wed Jan 30, 2019 4:21 am

thks Noah et al,

I guess I'm stubbornly not accepting the idea that most hardware that is Python controllable really is best controlled outside of touch's internal python environment. Seems counter-intuitive and wasteful, complicating, but I guess any 'performance hit' ( if it exists) even in the most critical situations is probably largely unnoticeable. I need to just accept this and master JSON, OSC or UDP etc. communication to external Python 'nodes'(if that is even the right term) :| (Ive done this sort of communication before and it worked efficiently, but now Ive forgotten most of it and will have to re-familiarize myself)

also, explaining to somebody who is even a lesser programmer than me ' yeah, you got to run this Python script outside of Touch simultaneously......'
( 'First-World problems', I know ...... )

It does seem like most of the suggested hardware solutions in this thread come to the same conclusion.
Maybe it is better this way, rather than trying to always adapt Python/hardware code to run in Touch .... that way all I have to do is adapt whatever code to receive JSON or the like ...
<.o'>
cod65
 
Posts: 555
Joined: Mon May 10, 2010 2:55 pm

Re: looking for a simple relay controller

Postby harveymoon » Fri Feb 01, 2019 12:59 pm

how to run a python file:

go to terminal/cmdprompt (outside of Touch)

type "python [path/name of file .py] "

press "return" or "enter"


------------


it seems like you are making this a lot more complicated with sub-process python scripts connecting to serial lines. I would try to reduce the number of components adding complexity to this system you are making.

A simple wireless relay with a boolean message sent over OSC is my simplest recommendation.

heres one for 8 bucks.
https://www.amazon.com/IZOKEE-Wireless- ... 8266+relay
harveymoon
 
Posts: 90
Joined: Mon Jun 29, 2015 8:42 pm

Re: looking for a simple relay controller

Postby cod65 » Sat Feb 02, 2019 1:14 am

thks Harvey I ordered one to check out . 8 bucks is a no-brainer.

the customer reviews seem to suggest there was hardware issue that has been resolved by manufacturer?

also is the cable in this vid just to provide power through USB to the ESP?
harveymoon wrote:I use ESP's for everything now.

Heres something I literally threw together in 10 minutes.

https://photos.app.goo.gl/kUkHgcjHfreXy5DP7
Last edited by cod65 on Sat Feb 02, 2019 2:36 am, edited 1 time in total.
<.o'>
cod65
 
Posts: 555
Joined: Mon May 10, 2010 2:55 pm

Re: looking for a simple relay controller

Postby cod65 » Sat Feb 02, 2019 2:28 am

matthewwachter wrote:We use the Phidget Python module directly inside TouchDesigner and haven't seen any issues. Relays a pretty easy to interface with as you are just setting values



matthew could you/ would you be willing to share script ? I've not been able to make this work purely inside touch. im using 004 relay unit - hope that is what u are using.
<.o'>
cod65
 
Posts: 555
Joined: Mon May 10, 2010 2:55 pm

Re: looking for a simple relay controller

Postby harveymoon » Sun Feb 03, 2019 2:11 am

cod65 wrote:thks Harvey I ordered one to check out . 8 bucks is a no-brainer.

the customer reviews seem to suggest there was hardware issue that has been resolved by manufacturer?

also is the cable in this vid just to provide power through USB to the ESP?
harveymoon wrote:I use ESP's for everything now.

Heres something I literally threw together in 10 minutes.

https://photos.app.goo.gl/kUkHgcjHfreXy5DP7




So, the $8 one will probably take some more effort to program. you will need a usb adapter in order to connect to it over serial USB.

or, looks like amazon has that too.

https://www.amazon.com/diymore-ESP8266- ... programmer

Then you will be able to re-program the esp with custom code that talks directly to touch.

I would recommend however, the first post I made. with the D1 Mini.
https://www.amazon.com/Makerfocus-Devel ... 145&sr=8-3

It is a better chip because it comes with a USB port for easy programming
( and yes power also, the code uploads through the cable, but the code I use only transmits over wifi. In the video you can also see serial debugging messages that are sent over USB in a different program showing on my screen, this is just for debugging.

I usually use them attached to phone USB power chargers, but you can also just wire up a wall-wart)

you would plug this ESP into this relay, and you are off to the races.

https://www.amazon.com/HiLetgo-Relay-Sh ... mini&psc=1

you can buy these chips and the relays for 1/8th of the price on Aliexpress

https://www.aliexpress.com/item/D1-mini-Mini-NodeMcu-4M-bytes-Lua-WIFI-Internet-of-Things-development-board-based-ESP8266-by/32651747570.html
harveymoon
 
Posts: 90
Joined: Mon Jun 29, 2015 8:42 pm

Re: looking for a simple relay controller

Postby bstv » Tue Mar 05, 2019 4:47 pm

thank you @harveymoon for the post!
i just stumbled over it and having an ESP32s i tried your script. But i am new to the arduino world and it gave me the following errors (see below)
If i understand correctly it loads something called ESP8266WiFi.h whcih in my case should be
ESP32Wifi.h i guess :roll:
what can i do to make the thing run ?!







ERROR FROM ARDUINO:
Multiple libraries were found for "WiFiUdp.h"
osc01:2:25: error: ESP8266WiFi.h: No such file or directory
Used: /Users/bst/Library/Arduino15/packages/esp32/hardware/esp32/1.0.1/libraries/WiFi
compilation terminated.
Not used: /private/var/folders/d4/wt5z02692j79hhy5t05dk4_40000gr/T/AppTranslocation/BB700A48-4FC1-4DCA-B445-EC97B56BE04F/d/Arduino.app/Contents/Java/libraries/WiFi
Not used: /private/var/folders/d4/wt5z02692j79hhy5t05dk4_40000gr/T/AppTranslocation/BB700A48-4FC1-4DCA-B445-EC97B56BE04F/d/Arduino.app/Contents/Java/libraries/WiFi
Not used: /private/var/folders/d4/wt5z02692j79hhy5t05dk4_40000gr/T/AppTranslocation/BB700A48-4FC1-4DCA-B445-EC97B56BE04F/d/Arduino.app/Contents/Java/libraries/WiFi
Not used: /private/var/folders/d4/wt5z02692j79hhy5t05dk4_40000gr/T/AppTranslocation/BB700A48-4FC1-4DCA-B445-EC97B56BE04F/d/Arduino.app/Contents/Java/libraries/WiFi
exit status 1
ESP8266WiFi.h: No such file or directory
bstv
 
Posts: 1
Joined: Thu Dec 21, 2017 7:09 pm

PreviousNext

Return to Hardware

Who is online

Users browsing this forum: No registered users and 1 guest