12 Replies Latest reply on Jun 20, 2015 4:29 AM by AlexT_Intel

    My Python script program on Edison

    mbox

      Hello everyone, just want to share my first python program running on edison. Please check out this link(Intel Edison Demo - YouTube). Thank you to this forum I'm learning and the same time enjoying . And now I'm searching to start playing with nodejs which I think is very cool, and would be great for my next mini project.

       

      Kind regards and more power,

      tacbanon

        • 1. Re: My Python script program on Edison
          Frederick Blais

          Hello mbox,

           

          Congrats for your little project. It seems to work great! By the way, was that a rooster that I hear in the background?

          • 2. Re: My Python script program on Edison
            mbox

            Hello Frederick, Thanks for taking the time looking at my demo. Oops.. Yes it was a rooster(my neighbor's) , ever since our house was wrecked by the super typhoon we have to relocate and live for a small area to stay.

            • 3. Re: My Python script program on Edison
              Frederick Blais

              I have a small challenge for you. In Python

               

              1. Install Python on your Windows 7 computer (if it is not already done).
              2. Install ZeroMQ library on both your Edison and computer ( Python binding - zeromq )
              3. ZeroMQ is a great library for networking.
              4. Take a look at this example for client server : PAIR — Learning 0MQ with examples
              5. Install PySide or PyQT on your windows computer. PySide Binaries Windows - Qt Wiki
              6. Look at PySide tutorial : First programs in PySide
              7. Create a client program with user interface on your Windows computer to control your Edison
              8. Modify the Python program on your Edison to listen to a client connection and control the oulet.


              Fred

              • 4. Re: My Python script program on Edison
                mbox

                Good morning from the Philippines Frederick, Thank you so much for these information and guide. I will definitely do my best to learn and make this work on my setup.

                • 5. Re: My Python script program on Edison
                  mbox

                  I'm still new in linux. I'm having trouble installing pyzmq for intel edison in the command line. I tried the following..

                  wget http://download.zeromq.org/zeromq-3.2.4.tar.gz
                  tar -xf zeromq-3.2.4.tar.gz
                  cd zeromq-3.2.4
                  ./configure --prefix=$(readlink -f ..)
                  make && make install
                  export CXXFLAGS="-I $(readlink -f ../include)"
                  export LDFLAGS="-L $(readlink -f ../lib) -Wl,-rpath=$(readlink -f ../lib)"
                  npm install zmq
                            
                  

                  Everything went okay except step 08 of the installation. Gives out so many errors. can anyone kindly tell what I'm doing wrong?

                   

                  Thank you in advance,

                  mbox

                  • 6. Re: My Python script program on Edison
                    Frederick Blais

                    Hello,

                     

                    as far as I know, npm is fore Node.js

                    You should use Python pip. Try this command:

                    pip install pyzmq

                     

                    You need to have pip installed. I don't remember if it is installed by default on the Edison but if it is not, I think you can get it with opkg.

                     

                    I'll take a look at that this week-end.

                     

                    Fred

                    • 7. Re: My Python script program on Edison
                      Frederick Blais

                      To install Python ZMQ :

                       

                      1. Follow tutorial there to configure opkg repository : Edison Package Repo Configuration Instructions
                      2. execute this command to install setup tools : wget https://bitbucket.org/pypa/setuptools/raw/bootstrap/ez_setup.py -O - | python
                      3. run this if pip is not installed yet : opkg install python-pip
                      4. run this to install pyzmq package : pip install pyzmq (this takes a couple minutes because it needs to compile ZMQ)

                       

                      I did this and it works on a fresh install of Yocto on my Edison (version 146)

                      • 8. Re: My Python script program on Edison
                        Frederick Blais

                        Just some code to get you started :

                         

                        client.py

                        from PyQt4 import QtGui
                        import zmq
                        import sys
                        
                        
                        class Toggle(QtGui.QWidget):
                            def __init__(self, labelText, number, socket):
                                super(Toggle, self).__init__()
                                self.s = socket
                                self.n = number
                                self.initUI(labelText)
                                
                            def initUI(self, labelText):      
                                btn = QtGui.QPushButton('OFF', self)
                                btn.setCheckable(True)
                        
                        
                                lbl = QtGui.QLabel(labelText, self)
                                lbl.setMinimumWidth(lbl.sizeHint().width()+20)        
                        
                        
                                hbox = QtGui.QHBoxLayout()
                                hbox.addWidget(lbl)
                                hbox.addWidget(btn)
                                
                                self.setLayout(hbox)
                                btn.clicked[bool].connect(self.sendMessage)
                                
                            def sendMessage(self, pressed):
                                
                                source = self.sender()
                                
                                if pressed:
                                    source.setText("ON")
                                    self.s.send(str(self.n)+':'+"ON")
                                else:
                                    source.setText("OFF")
                                    self.s.send(str(self.n)+':'+"OFF")
                                                
                        class Window(QtGui.QWidget):
                            def __init__(self, socket):
                                super(Window, self).__init__()
                                self.s = socket
                                self.initUI()
                                
                            def initUI(self):      
                                t1 = Toggle("Outlet #1", 1, self.s)
                                t2 = Toggle("Outlet #2", 2, self.s)
                        
                        
                                vbox = QtGui.QVBoxLayout()
                                vbox.addWidget(t1)
                                vbox.addWidget(t2)
                                
                                self.setLayout(vbox)
                                self.setWindowTitle('ZeroMQ Test')
                                self.show()
                                
                        def main():
                            ctx = zmq.Context.instance()
                            s = ctx.socket(zmq.PUSH)
                            url = 'tcp://192.168.0.187:5555'
                            s.connect(url)
                        
                        
                            app = QtGui.QApplication(sys.argv)
                            ex = Window(s)
                            sys.exit(app.exec_())
                        
                        
                        
                        
                        if __name__ == '__main__':
                            main() 
                        

                         

                        server.py

                        import time
                        import zmq
                        
                        
                        ctx = zmq.Context.instance()
                        s = ctx.socket(zmq.PULL)
                        url = 'tcp://*:5555'
                        s.bind(url)
                        
                        
                        poller = zmq.Poller()
                        poller.register(s)
                        
                        
                        while True:
                            events = dict(poller.poll(0))
                            if s in events:
                                msg = s.recv()
                                print("received {}".format(msg))
                            else:
                                time.sleep(1)
                        

                         

                        Here is a picture of a terminal running the server (on Edison) and the client on my Linux computer

                        client_server.png

                        • 9. Re: My Python script program on Edison
                          mbox

                          Hi Frederick, I really appreciate & I'm grateful to your effort and time. I tried the first step

                          1. Follow tutorial there to configure opkg repository : Edison Package Repo Configuration Instructions

                            .. but each time I try I get same results.

                          ToForum5.png

                          probably I messed up somewhere during trial and error in the command line.

                          And here is my base-feeds.conf

                          ToForum6.png

                          Thank you in advance,

                          mbox

                          • 10. Re: My Python script program on Edison
                            Frederick Blais

                            It's weird, you are getting two errors :

                            4 : Network failure.

                            8 : Server issued an error response

                             

                            maybe AlexT_Intel could help us?

                             

                            In the worst case, you could backup the data on your Edison, reflash it with the latest firmware and try again...

                            • 11. Re: My Python script program on Edison
                              mbox

                              I will try to flash it then this weekend.

                              Thanks,

                              mbox

                              • 12. Re: My Python script program on Edison
                                AlexT_Intel

                                Yes, I would say this looks simply as if the board doesn't have Internet access. mbox try pinging e.g. google.com to see if that's the case or just connect it to the network which has Internet access if you intendedly set it up to not being able to connect to the outside. The repo is ok, I can freely access it from my board right now.