7 Replies Latest reply on Nov 26, 2014 7:43 AM by mhahn

    Python Test Setup

    toadaze

      Here's a simple little Python program using a pot, led, touch sensor and a rgb-lcd

      with the mraa/upm libraries.

       

      The pot is on A0, the led is on D4, the touch sensor is on D3 and the lcd is plugged into i2c.

       

      Whenever the touch sensor is pressed, the led lights and the value of the pot is printed/displayed once per second.

      Other analog and digital sensors can be substituted.

       

      I ought to include a more graceful way to exit than Control-C, but you get the idea.

       

      # pot_touch_led_lcd.py

       

      import pyupm_i2clcd as lcd

      import mraa

      import time

      import sys

       

      # analog input - pot

      potPin = 0

      pot = mraa.Aio(potPin)

      potVal = 0

       

      # digital output - led

      ledPin = mraa.Gpio(4)

      ledPin.dir(mraa.DIR_OUT)

      ledPin.write(0)

       

      # digital input - touch

      touchPin = mraa.Gpio(3)

      touchPin.dir(mraa.DIR_IN)

       

      # display - lcd

      lcdDisplay = lcd.Jhd1313m1(0, 0x3E, 0x62)

       

      while 1:

       

          while touchPin.read() == 1:   

       

              # turn led on

              ledPin.write(1)

             

              # read pot/print/convert to string/display on lcd

              potVal = float(pot.read())

              print potVal   

              potStr = str(potVal)

              lcdDisplay.setCursor(0, 0)

              lcdDisplay.write(potStr)

                     

              time.sleep(1)

       

          # turn led off

          ledPin.write(0)

       

      pot_sensor_led_lcd.jpg

        • 1. Re: Python Test Setup
          Swigert

          Hi Toadaze thats a cool setup you got going there where can I get that power shield? and cables thanks.

          • 2. Re: Python Test Setup
            mhahn

            as far as I can see the "base shield" is part of the Buy Grove starter kit plus – Intel IoT Edition for Intel Galileo Gen 2 [110020002] | Seeedstudio - likewise the cables, the sensors and the display.

             

            toadaze You might want to use an interrupt function rather than checking the touch sensor in a while loop. You'd find an example in the mraa/UPM samples.

            Likewise you might want to use an interrupt function to react on <CTRL>+C

            • 3. Re: Python Test Setup
              toadaze

              Yep, mhahn is correct.  That's just a Grove Starter Kit.  I didn't buy one at first. The last thing I needed was more sensors. But when I couldn't get any of my lcd displays to work on the Edison-Arduino, I decided to get a Grove rgb-lcd.  Problem was I would have had to order one directly from them in China.  Since Trossen had the kit locally which included the display, I bought the entire kit.  It's actually very convenient and I can adapt other sensors to its cables.

               

              Also, mhahn - good suggestion on using interrupts instead of loops.  I'll rework it to use the touch sensor to exit the program when pressed.

              • 4. Re: Python Test Setup
                Swigert

                Thanks alot for the info my kit will arrive tomorrow...I noticed you are using python...can you guide me towards which website for the code because its

                so confusing...I've done a lot of research but when it comes down to the correct software..I get lost.

                thanks.

                • 5. Re: Python Test Setup
                  mhahn

                  I noticed you are using python...can you guide me towards which website for the code because its

                  so confusing...I've done a lot of research but when it comes down to the correct software..I get lost.

                  thanks.

                  Some steps I would recommend:

                  1. update libmraa0 and upm (see other threads on that)
                  2. check python examples coming with libmraa0 / UPM (opkg files upm ...)
                  3. have a look at the docs on mraa: Main Page and  upm: Main Page
                  • 6. Re: Python Test Setup
                    toadaze

                    Swigert - here's my take on the big picture.  There are several languages and ways to use them on the Edison supported by Intel; C, C++, Javascript and Python. Other languages could be used, too, but these 4 have IDE's, libraries, examples, etc. on various Intel web pages.

                     

                    C and C++ can be used with an interactive IDE (IoTDK) from your desktop. It has a cross compiler that compiles C and C++ programs and uploads them to the Edison.  The Edison itself also has a compiler that can do this directly. The mraa-upm libraires let you control GPIO and sensors.   IoT - Home | Intel® Developer Zone

                     

                    Javascript can also be used interactively with the Edison from your desktop using Intel's XDK. Since Javascript is an interpreted language, no compiling is involved. The Edison has node.js on it, so you can run Javascript programs directly on the Edison. The mraa-upm libraires let you control GPIO and sensors. There are also other 3rd party libraries available like Cylon and Galileo-IO.  Intel® XDK IoT Edition | Intel® Developer Zone

                     

                    Python is handled a bit differently.  Intel doesn't have an IDE for this, although you could use Eclipse with a Python plug-in. But since Python is already installed on the Edison, you can use your own editor on either your desktop or the Edison and then run Python programs directly on the Edison. Libraries have been built into Python on the Edison to support GPIO and various sensors. You can find Python examples at both of the above links.

                     

                    While each of these are well supported within their own environment (well maybe not Python so much), in my opinion, Intel could do a bit better bringing all this together under one umbrella, at least for those new to the Edison.

                     

                    In any case, from my experience using several other popular boards, Intel's IoTDK and XDK are vastly superior to what those other board builders provide for their products.

                     

                    Intel guys - feel free to correct or add.

                    • 7. Re: Python Test Setup
                      mhahn

                      good feedback - even twice

                      Thanks