Wednesday, April 16, 2014

Use Notepad++ to remotely edit your scripts from your RPi

So your main development platform is your Windows machine.
Your preferred editor is Notepad++ but you are developing programs to work with your Raspberry Pi.

What do you do up to now?

  1. Either you SSH to your RPi and work through VIM, Nano, Emacs etc. without Notepadd++ or,
  2. You work on your Windows machine through Notepad++ and you upload your scripts to the RPi, SSH and execute them to check if everything is working as should.
This is working and you might have already got used to it but why not to make your life easier?

You can enjoy Notepad++ and all the features provided by this beloved editor and at the same time work "directly" on your RPi. 

This can be done with the use of the NppFTP plugin.

After installing the plugin choose Show NppFTP window. The console will open at the RHS of the Notepad++ interface.


Click on the gear icon and choose profile settings:


In the pop up window, click on Add new to create a new profile. Set your settings accordingly:


Make sure to choose SFTP as your connection type (FTP over SSH) and port 22. Type in your RPi username & password and set your desired Initial remote directory. I store my scripts in the folder myscripts on the Desktop of "pi" user thus my initial remote directory is set to /home/pi/Desktop/myscripts.
When done click close. Having chosen the desired profile, click on the connect button .

Your RPi's filesystem is now populated:

Browse to your scripts location and double click the desired script.

The file will be cached on your Windows machine and each time you click on save will be uploaded to your RPi.

Note that in case you lose connection to your pi, your working file can be located at a path similar to:
C:\Users\<user>\AppData\Roaming\Notepad++\plugins\config\NppFTP\Cache\pi@192.168.2.31\


How to configure Notepad++ to run a python script



Supposing that Notepad++ & Python (v2.7 in my case) are installed on your computer:
  • Run CMD to open a command prompt
  • Run the following command to set Python location in your system's PATH variable: 
set PATH=%PATH%;C:\Python27
    Note: Amend python installation path based on your setup.
    Use the following command to check that python path was successfully added to the PATH variable:
    echo %PATH% 
    You should see the location of the python directory at the end of the output.
    • Start Notepad++, select Run menu, and then select Run... (F5)
    • Insert the following in the pop up text box that appears: 

    python.exe -i "$(FULL_CURRENT_PATH)"
    • Click Save...
    • Configure ALT+SHIFT+P (or any of your choice) as the command shortcut.  Give it a distinctive name (e.g. Execute Python), and press OK.
    With this setup whenever you want to run the python program you're working on, press ALT+SHIFT+P or chose "Execute Python" from the Run Menu list. Note that because of the -i flag the Python console window stays open after the program execution for debugging purposes. If you don't want that just remove the -i flag.

    Friday, April 4, 2014

    But what actually is between the Raspberry Pi and the AC Motor?

    ATTENTION - IN THIS POST MAINS (AC) CURRENT IS USED. AC CURRENT CAN KILL YOU. MAKE SURE THAT YOU KNOW WHAT YOU ARE DOING IF YOU FOLLOW THE BELOW INSTRUCTIONS.

    In order to drive the AC Motor, apart from the S/W part of this post I need a H/W part too.
    This should be a circuit that must be triggered from my low voltage microcontroller (RPi) and drive a high voltage bidirectional motor.

    Hmmm...I must get my RPi "in touch" with AC and must be sure that I will not burn it. This means that I need a way to isolate it from the AC part of the circuit...OK got it...OPTOCOUPLER.
    That's a component that can be triggered from low voltage and drive its output without having any connection between input & output. Its input is an IR LED, and its output is a photo sensitive transistor which gets conductive when the LED emits its light. Thus what ever happens at its output, nothing will pass to the input, where our RPi is connected.

    OK, what's next then? I have a conductive transistor each time my Optocoupler's (optoisolator) input is triggered. The Optocoupler's output must now drive another component that will let AC current pass through my motor. This is done with a TRIAC.

    Each time the Optocoupler's output is conductive, current passes through it to the Gate of the TRIAC, the TRIAC is triggered and gets conductive too and let AC current pass through it towards the motor's winding. This way one direction is triggered each time one PRi GPIO Pin is set to HIGH.
    To be able to trigger the motor's other winding in order to make the motor move towards the other direction, we just have to double the same circuit which will be triggered by another RPi GPIO Pin.

    The result is the following schematic:

    The complete circuit I used is same to the one described in the Solid State Control for
    Bi-Directional Motors Application Note from On Semiconductor. In this Application Note you will be able to understand what is all the other components use.

    Thursday, April 3, 2014

    Using MQTT protocol and Mosquitto Message Broker to control an AC motor through a web app with Python.

    ATTENTION - IN THIS POST MAINS (AC) CURRENT IS USED. AC CURRENT CAN KILL YOU. MAKE SURE THAT YOU KNOW WHAT YOU ARE DOING IF YOU FOLLOW THE BELOW INSTRUCTIONS.

    The concept is to be able to control an AC motor through a web based application using Python.

    There are a lot of ways to implement something like this. Initially, I have chosen to use a Python/CGI script that would do that. I created a Web Form that would pass the user's input to the CGI script running on my RPi Apache Server, and the CGI script would do the rest. The problem I faced with this setup is that the permissions on the CGI script where not enough to be able to trigger the RPi GPIO pins. As I didn't want the Apache to run as root I had to find a different way.

    The way to go was MQTT & Mosquitto.

    "Before going any further I would like to thank Dougie Lawson, whose post inspired me and from which I have used some code too & Roger Light who is developing Mosquitto and also replied to my questions."

    This project's S/W part consists of 3 parts:
    1. Installation of all needed modules.
    2. The HTML Form that the user is accessing in order to control the motor.
    3. The S/W that receives user's form input and publishes it as an MQTT message to the Broker.
    4. The S/W that subscribes to the Broker, runs as a service at the background, reads the MQTT messages published to the Broker and acts accordingly, based on the MQTT messages.
    I will now explain thoroughly each one of the above 4 parts.

    Part 1. Modules Installation

    First of all we need to install the Debian Mosquitto broker module and the Python Mosquitto module.
    A "sudo apt-get install mosquitto" will handle the first one. After installation the broker will automatically start as service. A "sudo pip install mosquitto" will handle the second one. If you don't have PIP for Python installed please refer to this link Install PIP for Python before proceeding.
    Now that we have all the prerequisites in place lets write some code!

    Part 2. The HTML form

    The PHP form is a simple form that consists of a vertical slider. Since the motor to be controller is used to open/close the window shades at my home I have emulated the vertical windows shades with a vertical HTML5 slider. Based on the user's input, a different message is sent to the mosquitto broker that emulates the opening or closing of the window shades.

    
    

    <head>
    <style type="text/css">
    input[type=range].vertical
    {
        writing-mode: bt-lr; /* IE */
        -webkit-appearance: slider-vertical; /* WebKit */
        width: 8px;
        height: 200px;
        padding: 0 5px;
    }
    </style>
    </head>
    
    <body>
    <form name="email" action="/cgi-bin/mqtt.py" method="get">
    <input type="range" name="dir" min="0" max="2" value="0" step="1"
    class="vertical" orient="vertical"/>
    <input type="submit" value="Submit">
    </form>
    </body>
    The css part of the HTML code above is used in order to rotate the slider vertically.
    The input type = "range" is the actual slider. What it looks like in Chrome is the following:

    As shown in the code above it's initiated at value = "0", emulating WINDOW CLOSED, has a max value of 2, emulating WINDOW OPEN and an intermediate value of 1, emulating WINDOW HALF.

    There is also a submit button that will trigger the form submission.
    The vital part of the form though is it's action: action="/cgi-bin/mqtt.py"
    Based on the above we have set the form to use method GET in order to pass to the mqtt.py script located in the web server's cgi-bin folder the value of the slider. I am running apache2 and the default location of cg-bin on my RPi is located at /usr/lib/cgi-bin/

    Up to this point we have created the Web Form that the user will use in order to send her input to the script that will pick it up and publish it to the Broker.

    Part 3 - The Publisher

    Now we need to create the script that will pick the user's input and will publish our MQTT messages to the Broker, make them available for the subscribers. This is our mqtt.py file located in the "cgi-bin" folder as stated above. Before proceeding I propose to open next to this page the documentation of the mosquitto python module. This will help you understand in depth all the following.

    The actual code is the following:
    #!/usr/bin/env python
    
    # Import modules for CGI handling & Debugging
    import cgi
    import cgitb; cgitb.enable()
    
    #Import Python Broker Module
    import mosquitto
    
    #Import Other Python Modules
    import os
    import time
    
    broker = "127.0.0.1"
    port = 1883
    
    mypid = os.getpid()
    client_uniq = "pubclient_"+str(mypid)
    mqttc = mosquitto.Mosquitto(client_uniq)
    
    #connect to broker
    mqttc.connect(broker, port, 60, True)
    mqttc.loop()
    
    # Create instance of FieldStorage
    form = cgi.FieldStorage()
    
    # Get data from form fields
    dir = form.getvalue('dir')
    
    #Print the result in HTML
    print "Content-type:text/html\r\n\r\n"
    print "<html>"
    print "<head>"
    print "<title>Publish MQTT Program</title>"
    print "</head>"
    print "<body>"
    print "<h2>Message %s sent to MQTT</h2>" %(dir)
    mqttc.publish("motor/action", dir)
    print "</body>"
    print "</html>"

    Line 1 states that our script must be executed by Python program
    Lines 4-5 imports all the needed CGI related modules so that the program is executed by our Web Server
    Line 8 imports the Mosquitto Python modules
    Lines 11-12 imports additional Python modules needed in our program
    Lines 14-15 sets variables broker & port to our needs
    Lines 17-18 help us create a unique id per client that connects to the broker
    Line 19 creates a mosquitto client instance
    Line 22 connects our client to the broker
    Line 23 executes a continuous loop which is need for all Python clients so that the function properly
    Line 26 creates a FieldStorage CGI instance so that we can pick form values
    Line 29 assigns form value to a variable
    Lines 32 - 41 prints the results of the CGI script execution to the browser
    Line 39 Will use this pattern mqttc.publish(msg.topic, msg.payload) and will publish "dir" to "motor/action"

    Since we have our MQTT messages coming from our HTML form, published to the Broker, we need to create the software that will read these by subscribing to the corresponding MQTT topic and use them accordingly.

    Part 4 - The Subscriber & Action Taker

    The script below will subscribe to the predefined MQTT topic, will read the topic's payload, will decode it and based on that will execute some GPIO actions. We will set this script to run as a service so that each time a new MQTT message is received (the user is opening/closing the window shades) the script re-executes accordingly.
    #!/usr/bin/env python
    
    import time
    import sys
    import os
    import mosquitto
    import RPi.GPIO as GPIO
    
    GPIO.setwarnings(False)
    GPIO.setmode(GPIO.BOARD)
    
    #GPIO pin to drive OPEN
    GPIO.setup(7, GPIO.OUT)
    #GPIO pin to drive CLOSE
    GPIO.setup(22, GPIO.OUT)
    
    #Motor Move execution function
    def trigger(x, y):
     GPIO.output(x, 1)
     time.sleep(y)
     GPIO.output(x, 0)
     GPIO.cleanup()
     
    def on_message(mqtts, userd, msg):
     #GPIO pin to drive OPEN
     GPIO.setup(7, GPIO.OUT)
     #GPIO pin to drive CLOSE
     GPIO.setup(22, GPIO.OUT)
                    
            topic_payload = []
     topic_payload.append(msg.payload)
    
     if topic_payload[0] == "1":
      print "Changing Window to HALF"
      directionPin = 7
      durationSecs = 6
      trigger(directionPin, durationSecs)
     elif topic_payload[0] == "2":
      print "Changing Window to FULLY OPEN"
      directionPin = 7
      durationSecs = 12
      trigger(directionPin, durationSecs)
     elif topic_payload[0] == "0":
      print "Changing Window to FULLY CLOSED"
      directionPin = 22
      durationSecs = 12
      trigger(directionPin, durationSecs)
    
    def main():
     broker = "127.0.0.1"
     port = 1883
    
     mypid = os.getpid()
     sub_uniq = "subclient_"+str(mypid)
     mqtts = mosquitto.Mosquitto(sub_uniq)
     mqtts.on_message = on_message
     mqtts.connect(broker, port, 60)
     mqtts.subscribe("motor/action", 0)
    
     try:
      rc = 0
      while rc == 0:
       rc = mqtts.loop()
      GPIO.cleanup()
      return 0
            #CTRL+C keyboard interrupt
     except KeyboardInterrupt:
                    # resets all GPIO ports used
      GPIO.cleanup()             
      return 4
    
    if __name__ == "__main__":
     sys.exit(main())
    Line 1-15 Are self explanatory
    Line 30-31 I write the payload to a list, even if not needed for this version of the script, in order to be able to use it in the future versions of the script.
    Line 24-47 Each time a message is received the on_message function is called. Based on the message the corresponding GPIO pins are picked (7 for Open, 22 for Close) and corresponding delay times are set (12 secs for complete Open/Close and 6 secs for half) 
    Line 17-22 Is the function that is responsible for the AC Motor movement.
    Line 50-58 Same as in the previous script
    Line 56 On Message receipt call on_message
    Line 58 Subscription to motor/action topic
    Line 60-70 Loop for ever unless keyboard interrupt
    Line 72 Call main - Execute Program
    Line 73 sys.exit()

    But what actually is between the Raspberry Pi and the AC Motor?

    Sunday, March 30, 2014

    AC Motor Direction Control Circuit

    Introduction

    Hey! Just wanted to introduce my Blog to all the #RPi addicts around the world. It's been a while that I am a RPi v2 owner and have alredy created some funcy Projects. Since knowledge is to be shared I started this blog in order to share with all of you that have the same passion my projects. Exchange opinions, make proposals, help each other resolve issues that might be facing while playing with RPi.
    So let's start :)