Author: feurig

  • Making Stephen Wright’s Light Switch with the Adafruit Huzzah!

    23439940833_31be1e9c50_o

     

    Things are Different

    In 2012 I was struggling to put together a stm32 based wireless datalogger using leaflabs libmaple and a $20 wifi module. I had to write most of the code myself because libmaple was based on arduino-0022 and most of the wifi libraries made extensive use of the new classes available after arduino 1.xx (String for instance). It was painful and in the end the project was a complete failure except that I had a premade platform to make an art piece called Stephen Wrights Light Switch.

     

    8346730548_dede0fa72e_n img_0935_23439975343_o

    Nowadays you can get an Arduino capable wifi module for less than 10 bucks.

    Huzzah!

    (look it up on the internets!)


    /*-----------------------------------StephenWrightsLightSwitch.ino
     This is a hack of the provided WifiClient example.
     This is free software (BSD License)
     (C) Donald Delmar Davis , SuspectDevices.
    
     ---------------------------------------------------------------*/
    
    
    #include <ESP8266WiFi.h>
    
    
    #define SWITCH_OFF 1
    #define SWITCH_ON 0
    #define LED_OFF 0
    #define LED_ON 1
    
    
    const char* ssid = "MYWIFI";
    const char* password = "MYSECRET";
    const char* host = "www.suspectdevices.com";
    const int httpPort = 80;
    
    const char* offstate = "OFF";
    const char* onstate = "ON";
    
    #ifdef ESP8266_DEV_BOARD
    const int switchPin = 4;
    const int ledPin = 5;
    #else
    const int switchPin = 4;
    const int ledPin = 5;
    #endif 
    /*
     global variables
    */
    
    bool newSwitchValueFlag = false;
    bool oldSwitchValue = SWITCH_OFF;
    bool lightValue = SWITCH_OFF;
    
    
    /*---------------------------------------------------setup()
    
    */
    void setup() {
     bool flipFlop = SWITCH_OFF;
    
     Serial.begin(115200);
     pinMode(switchPin, INPUT_PULLUP);
     pinMode(ledPin, OUTPUT);
     digitalWrite(ledPin, LED_ON);
     delay(10);
    
     // We start by connecting to a WiFi network
    
     Serial.println();
     Serial.println();
     Serial.print("Connecting to ");
     Serial.println(ssid);
     WiFi.begin(ssid, password);
    
     while (WiFi.status() != WL_CONNECTED) {
     flipFlop = !flipFlop;
     digitalWrite(switchPin, flipFlop);
     delay(500);
     Serial.print(".");
     }
    
     digitalWrite(ledPin, LED_OFF);
    
     Serial.println("");
     Serial.println("WiFi connected");
     Serial.println("IP address: ");
     Serial.println(WiFi.localIP());
    
    }
    
    /*-------------------------------------------------------------------------loop()
    
    */
    void loop() {
     
     bool newSwitchValue;
     
     newSwitchValue = (digitalRead(switchPin)==SWITCH_ON);
     
     Serial.print("Switch="); Serial.println(newSwitchValue?"ON":"OFF");
     
     if (newSwitchValue != oldSwitchValue) {
     newSwitchValueFlag = true;
     } else {
     newSwitchValueFlag = false;
     }
     
     Serial.print("connecting to ");
     Serial.println(host);
    
     // Use WiFiClient class to create TCP connections
     WiFiClient client;
     if (!client.connect(host, httpPort)) {
     Serial.println("connection failed");
     return;
     }
    
     // We now create a URI for the request
     String url = "/art2013/settheswitch/";
     if (newSwitchValueFlag) {
     url += "?state=";
     url += newSwitchValue ? onstate : offstate;
     oldSwitchValue=newSwitchValue;
     }
    
     Serial.print("Requesting URL: ");
     Serial.println(url);
    
     // This will send the request to the server
     client.print(String("GET ") + url + " HTTP/1.1\r\n" +
     "Host: " + host + "\r\n" +
     "Connection: close\r\n\r\n");
     
     for (int t = 8; t > 0 && !client.available(); t--)
     delay(100);
    
     // Read all the lines of the reply from server and print them to Serial
     while (client.available()) {
     String line = client.readStringUntil('\r');
     if (line.lastIndexOf("ON") >= 0) {
     Serial.println("FOUND AN ON!!!");
     lightValue = SWITCH_ON;
     } else {
     lightValue = SWITCH_OFF;
     }
    
     Serial.print(line);
     }
    
     digitalWrite(ledPin, lightValue ? LED_ON : LED_OFF);
    
     Serial.println();
     Serial.println("closing connection");
     // client.close();
    
    }
    
    
  • The Adafruit Huzzah and the Sparkfun Esp8266 dev board.

    Background.

    I was asked to put together a 2 day class on the internet of things at PNCA so I suggested that we try out the Adafruit huzzah.  while I was looking for it I came across the Sparkfun Esp8266 “thing” and the Esp8266 dev board. I used the Sparkfun Esp8266 dev board to implement a light socket that queried the internet for its State.

    IMG_1259

    Connections

    The most important thing you can do with your Huzzah is to make sure you have at least 250 ma of power available to the wifi part of the module. There is nothing more frustrating than having everything worked as advertised until you turn the radio on. The Sparkfun boards have usb power which is good enough. The Huzzah has an on board regulator for both external battery and can be powered by USB’s 5v and it detects an d uses whichever is greater.

    After working through some code on the Huzzah with the students, I went to the Hardware store and purchased and externally mounted light socket, I had brought with me a solid state relay from my bench pile. For all practical purposes an SSR can be thought of as an LED. In fact the SSR that I chose did not have a current reducing circuit and I let the smoke out of the first one. To power the board I purchased a mini usb charger which without the external casing fit fine in the enclosure.

    IMG_1256

    The Server from ByteMe 2013

    The web server for the afru show is still presenting the state of Stephen Wrights Light Switch from the AFRU Gallery’s annual Byte Me show in 2013. The code to present the state is a small pile of python. The most important part is the 3 non standard ways to tell your browser to stop caching the data since no-one seems to follow the established standards (..that means you safari..)

    ...$ cat index.wsgi 
    import webapp2
    import sqlite3
    
    class MainPage(webapp2.RequestHandler):
        def get(self):
    
        con=sqlite3.connect('/home/newcourse/suspectdevices/www/art2013/swls/swls.db')
        cur=con.cursor()
        
        cur.execute("select state from switch")
        swstate=cur.fetchone()[0]    
        self.response.headers['Content-Type'] = 'text/plain'
        self.response.headers['Cache-Control'] = 'no-cache, no-store, must-revalidate'
        self.response.headers['Pragma'] = 'no-cache'
        self.response.headers['Expires'] = '0'
        self.response.out.write(swstate)
    
    application = webapp2.WSGIApplication([('/art2013/swls/index.wsgi', MainPage)],
                                  debug=True)
    
    

    Creating the Light Socket using the esp8266 dev board

     
    The arduino code for creating the light switch is remarkably simple. After setting up the wifi connection the device repeatedly does does an http get which returns the header information and a body containing either ON or OFF. Since the standard headers do not contain the word ON we look for that using the arduino String class’ indexOf method.

     

    /*----------------------FrauleinMartinaFranksLightSocket.ino
     This is a hack of the provided WifiClient example.
     This is free software (BSD License)
     (C) 2015 Donald Delmar Davis , SuspectDevices.
    ----------------------------------------------------------*/
    
    
    #include <ESP8266WiFi.h>
    
    /*
     Declarations and constants
    */
    
    #define ESP8266_DEV_BOARD 1
    
    #define SWITCH_OFF 1
    #define SWITCH_ON 0
    #define LED_OFF 0
    #define LED_ON 1
    #define RELAY_OFF 0
    #define RELAY_ON 1
    
    
    const char* ssid = "MYSSID";
    const char* password = "myWifiPassword";
    const char* host = "www.suspectdevices.com";
    const int httpPort = 80;
    
    const char* offstate = "OFF";
    const char* onstate = "ON";
    #ifdef ESP8266_DEV_BOARD
    // esp8266 dev board 0,4,5(LED) and 14 are available as General Purpose pins
    // on board led is at pin 5
    // avoid pin 15 messes with downloading
    const int switchPin = 4;
    const int ledPin = 5;
    const int relayPin = 2;
    #elseif defined(ADAFRUIT_HUZZAH)
    const int switchPin = 4;
    const int ledPin = 13;
    const int relayPin = 2;
    #else 
    const int switchPin = 4;
    const int ledPin = 13;
    const int relayPin = 2;
    #endif 
    /*
     global variables
    */
    
    bool oldSwitchValue = SWITCH_OFF;
    
    
    /*-----------------------------------------------------------setup()
    
    */
    void setup() {
     bool flipFlop = SWITCH_OFF;
    
     Serial.begin(115200);
     pinMode(switchPin, INPUT_PULLUP);
     pinMode(ledPin, OUTPUT);
     digitalWrite(ledPin, LED_ON);
     pinMode(relayPin, OUTPUT);
     digitalWrite(relayPin, RELAY_ON);
     delay(10);
    
     // We start by connecting to a WiFi network
    
     Serial.println();
     Serial.println();
     Serial.print("Connecting to ");
     Serial.println(ssid);
     WiFi.begin(ssid, password);
    
     while (WiFi.status() != WL_CONNECTED) {
     flipFlop = !flipFlop;
     digitalWrite(ledPin, flipFlop);
     delay(500);
     Serial.print(".");
     }
    
     digitalWrite(ledPin, LED_OFF);
    
     Serial.println("");
     Serial.println("WiFi connected");
     Serial.println("IP address: ");
     Serial.println(WiFi.localIP());
    
    }
    
    /*---------------------------------------------------loop()
    
    */
    void loop() {
     delay(1000);
    
     Serial.print("connecting to ");
     Serial.println(host);
    
     // Use WiFiClient class to create TCP connections
     WiFiClient client;
     if (!client.connect(host, httpPort)) {
     Serial.println("connection failed");
     return;
     }
    
     // We now create a URI for the request
     String url = "/art2013/swls/";
    
     Serial.print("Requesting URL: ");
     Serial.println(url);
    
     // This will send the request to the server
     client.print(String("GET ") + url + " HTTP/1.1\r\n" +
     "Host: " + host + "\r\n" +
     "Connection: close\r\n\r\n");
     
     for (int t = 5; t > 0 && !client.available(); t--)
     delay(100);
    
     // Read all the lines of the reply from server and print them to Serial
     while (client.available()) {
     String line = client.readStringUntil('\r');
     if (line.lastIndexOf("ON") >= 0) {
     Serial.println("FOUND AN ON!!!");
     oldSwitchValue = SWITCH_ON;
     } else {
     oldSwitchValue = SWITCH_OFF;
     }
     Serial.print(line);
     }
    
     digitalWrite(ledPin, oldSwitchValue ? LED_ON : LED_OFF);
     digitalWrite(relayPin, oldSwitchValue ? RELAY_ON : RELAY_OFF);
    
     Serial.println();
     Serial.println("closing connection");
    
    }
    
    

    IMG_1254

    References

  • Communicating between the Arduino and pure data (on the raspberry pi) Pt. 1

    The simplest interaction between the arduino and pure data is by using the comport object in pure data and sending data to it using the Arduino’s Serial object. Below is an illustration of two way communication between pure data and the arduino.  On the right the comport object receives values as bytes and puts them on the first vslider. The values from the second slider are sent to the comport. On the left you can see the code for the arduino. The arduino reads the potentiometer and if the value has changed writes the value (scaled to fit in a byte) to the serial port. Then it checks to see if a value has been sent to it and if its different from the last value it sets the led pins pwm value accordingly.

     

    8 bit wonder comms

     

  • Am letting my intermediate arduino class hijack this site temporarily.

    Will shut off the twitter feed to cut down on the noise.

     

     

     

    IMAG0325feel free to watch this space…

  • Intermediate Arduino Day 1

    Leading Questions:

    • What do you know?
    • What do you not know?
    • What Would be most helpful for you as an artist?

    Whiteboard from “what do you know?”

    IMAG0315

    (as a bullet list)

    • I/O -> Massage -> Outputs (james)
    • AD -> I/O -> Midi (christiano)
    • Sensors -> Control movement / sound (gail)
    • The Unit is robust/stable (julie)
    • I/O ->  Lights -> Sequences/Processing (nance)
    • The hardware is diverse (nance)
    • Max/MSP/Processing -> Motors/Blinkin Lights

    The Original Proposed  Topics.

    • Midi / Sound
    • Sensors
    • Motors and Motion Control
    • High Voltage
    • Build Your Own Arduino / Soldering,
    • Advanced communication /Networking
    • Programming, Datasheets, Hardware

    (Projects whiteboard)IMAG0316

    Not sure how to go from 3 hour whiteboard session to wordpress site. But we are going to figure this out.

     

     

     

     

     

     

     

     


    Categories.

    • Video Processing -> BL
    • Rotating Projector
    • Hacking Existing Hardware
    • El/paint -> Sound Processing -> Display
    • leap motion -> motor control
    • sound processing -> output
    • sound processing with control

     

    IMAG0318

     

     

     

     

     

     

     

     

     

     

     Therefore Pi!

    Out of the 7 projects 6 of them would best be done with a computer and the arduino. For that reason we suggested (and decided) to go with a Raspberry Pi / Arduino combination.

     

    IMAG0315

     

     

     

     

     

     

     

     

     

     

     

     

     


    Resulting proposed lectures

    • Pi Setup /PD
    • Communication
    • Sensors
    • Motion Control

    IMAG0320

  • Your Own Protocol (Chapter 7 of Physical Computing)

    Once again find myself in a windowless cube. So I 3d printed a varient of a compact camera gimbal and attached an rpi camera to it.  I needed to set up a serial based protocol to talk to it. Its a pretty good example of creating your own protocol as described in the Physical Computing book. The gimbal is servo based and there is one servo for rotation or “yaw” and one for attitude or “pitch”. So the protocol I came up with was:

    yXXX<cr>
    pXXX<cr>

    where XXX represents a number between 0 and 180 (servo values)

    Here is the code.

    
    /*
      demonstration of a private protocol.
      we have a serial connected device that has two servos attached to it. 
          one controls the pitch (up and down) 
          one controls the yaw (round and round)
      we send it lines of text in the form 
      s<cr>
      where s is:
        pnnn -- pitch value (0-180)
        ynnn -- yaw value (0-180) 
      Normally it doesnt respond unless weWANNADEBUGTHIS or if it doesnt understand us
      in this case it replys with ?????
     */
    
    #include <Servo.h>
    #include <stdlib.h>
    
    #define YAW_PIN 10
    #define PITCH_PIN 9
    #define YAW_HOME 60
    
    #define PITCH_HOME 35
    #define MAXCHARS 10
    //#define IWANNADEBUGTHIS 1
    #ifdef  IWANNADEBUGTHIS
    #define HEY_ARE_YOU_LISTENING_TO_ME 1
    #define HEY_I_AM_TALKING_TO_YOU 1
    #endif
    
    Servo yaw;
    Servo pitch;
    char inputBuffer[MAXCHARS];
    boolean inputBufferReady;
    int inputBufferIndex;
    
    void setup()
    {
      // initialize the serial communication:
      Serial.begin(9600);
      yaw.attach(YAW_PIN);
      pitch.attach(PITCH_PIN);
      yaw.write(YAW_HOME);
      pitch.write(PITCH_HOME);
    
    }
    
    boolean gotDataLine (void){
      char inch;
      if (inputBufferIndex>=MAXCHARS) {
        inputBufferIndex=0;
      }
      if (Serial.available()){
        inputBuffer[inputBufferIndex++]=inch=Serial.read();
        inputBuffer[inputBufferIndex]='\0';
        //Serial.print("I received: "); Serial.println(inch);
        if ((inch == '\n') && (inputBufferIndex>0)) {      // I dont want this charachter
          inputBufferIndex--;    // "dtmfa" - dan savage
          return false; 
        }
        if ((inch == '\r') && (inputBufferIndex>1)) {
            inputBuffer[inputBufferIndex-1]='\0';
            inputBufferIndex=0;
            return true;
        }
      }
      return false;
    }
    
    void loop() {
      int myValue;
      if (gotDataLine()) {
        myValue=atoi(inputBuffer+1);
        if (myValue>180) myValue=180;
        if (myValue<0) myValue=0;
        switch(inputBuffer[0]) {
          case 'p' : 
    #ifdef HEY_ARE_YOU_LISTENING_TO_ME
                     Serial.print("Setting pitch to ");
                     Serial.println(myValue);
    #endif
                     pitch.write(myValue);
                     break;
          case 'y' : 
    #ifdef HEY_ARE_YOU_LISTENING_TO_ME
                     Serial.print("Setting yaw to ");
                     Serial.println(myValue);
    #endif
                     yaw.write(myValue);
                     break;
          default:  Serial.print("?"); Serial.print(inputBuffer); Serial.println("????");
        }
      }
    }

     

  • Using the Timer1 and Timer3 libraries with the Arduino Leonardo.

    (NOTE: PJRC HAS SINCE UPDATED THE LIBRARY TO INCLUDE THE LEONARDO)

    I have been teaching Physical Computing for Artists with the Arduino Uno for over a year now and this year I decided to move to the Leonardo with the hope that a USB-Midi based core will be finished sometime soon. Its cheaper than the uno and it has a serial port that isn’t busy and best of all it doesn’t reboot every time you talk to it.

    I usually introduce background tasks using the Timer1 library since it makes timers manageable for the novice. However the existing Timer1 and Timer3 libraries are not currently avaliable for the leonardo (or any of the 4 32u4 based processors). Fortunately there is a “port” of the library for the Teensy2.0 at http://www.pjrc.com/teensy/arduino_libraries/TimerOne.zip, for some reason the leonardo is missing from the boards supported but no matter the fix is pretty easy and now I can get back to teaching.

    
    diff ~/Downloads/TimerOne/config/known_16bit_timers.h TimerOne/config/known_16bit_timers.h 
    11,22c11,34
    < 
    < // Teensy 2.0
    < //< 
    < // Teensy 2.0
    < //
    < #elif defined(__AVR_ATmega32U4__) && defined(CORE_TEENSY)
    <   #define TIMER1_A_PIN   14
    <   #define TIMER1_B_PIN   15
    <   #define TIMER1_C_PIN   4
    <   #define TIMER1_ICP_PIN 22
    <   #define TIMER1_CLK_PIN 11
    <   #define TIMER3_A_PIN   9
    <   #define TIMER3_ICP_PIN 10
    < 
    ---
    > // mega34u4
    > // Ehem: it would be nice to figure out the sparkfun micro-pro
    > // and the adafruit u4 stuff as well.
    > #elif defined(__AVR_ATmega32U4__) 
    >     // Teensy 2.0
    >     #if defined(CORE_TEENSY)
    >         #define TIMER1_A_PIN   14
    >         #define TIMER1_B_PIN   15
    >         #define TIMER1_C_PIN   4
    >         #define TIMER1_ICP_PIN 22
    >         #define TIMER1_CLK_PIN 11
    >         #define TIMER3_A_PIN   9
    >         #define TIMER3_ICP_PIN 10
    >     // otherwise assume Leonardo/micro/esplora
    >     #else 
    >         #define TIMER1_A_PIN   9
    >         #define TIMER1_B_PIN   10
    >         #define TIMER1_C_PIN   11
    >         #define TIMER1_ICP_PIN 4
    >         #define TIMER1_CLK_PIN 12
    >         #define TIMER3_A_PIN   5
    >         #define TIMER3_ICP_PIN 13
    >     #endif
    > 

    http://www.suspectdevices.com/reference/Timer1.tgz

  • Two New Continuing Education Courses for the Rainy Season.

    Arduino for Artists.

    (The class formerly known as Physical Computing for Artists)

    During the summer break Donald Delmar Davis went to ITP Summer Camp 30 days of more intersection between art and technology than anyone can take in. He is hoping that he can use what he learned to teach more artists about how to integrate the arduino into their tool set. This year he will be working with the Arduino Leonardo with the intention of bringing midi and other usb clsses into the Arduino’s capabilities.

    https://cereg.pnca.edu/p/adult/s/407

    Tube Amplifiers.

    During the summer Mark Keppinger piloted a new class that he is offering this fall. In the class you get to build and take home a tube amplifier (for your guitar, iPhone/iPod, etc). This includes both the electronics and the cabinet to hold it.

    https://cereg.pnca.edu/p/adult/s/405