7 Replies Latest reply on Mar 23, 2016 4:52 PM by CMata_Intel

    Problem with Python script

    shriyansh.mit

      HI when i running my python script with simple print message its working fine with the below code.

      Capture.PNG

       

      But when i send call any rest api it reurns nothing create a blank file on root of SD card.

       

      Capture1.PNG

       

      Procedure for connectivity:

      - Connecting ethernet cable one port to galileo device and another to LAn port.

      - Connect Galileo board through COM port to ardunio IDE

      - Upload the sketch

       

      Is there any issue with coding please let me know?

       

       

      /*
      Execute python file
        
      */

      #include <SPI.h>
      #include <SD.h>

      File myFile;

      void setup()
      {
      // Open serial communications and wait for port to open:
        Serial.begin(9600);
         while (!Serial) {
          ; // wait for serial port to connect. Needed for Leonardo only
        }
        
        system("python /media/realroot/TestScript.py > /media/realroot/TestScriptOutput.txt");
       
        // re-open the file for reading:
        myFile = SD.open("TestScriptOutput.txt");
        if (myFile) {
          Serial.println("TestScriptOutput.txt:");   
          // read from the file until there's nothing else in it:
          while (myFile.available()) {
           Serial.write(myFile.read());
          }
          // close the file:
          myFile.close();
        } else {
         // if the file didn't open, print an error:
          Serial.println("error");
        }
      }

      void loop()
      {
      // nothing happens after setup
      }

        • 1. Re: Problem with Python script
          CMata_Intel

          Hi shriyansh.mit

           

          I was able to ran it fine, I'm using a Gen2, the SD card (with the Linux-image from Galileo Software Downloads) and I installed node.js with

          root@clanton://# opkg install http://repo.opkg.net/galileo/repo/i586/nodejs_0.10.32-r0_i586.ipk
          
          

          I used the same Python script and Arduino sketch. Let me know if after installing node you get better results.

           

          Regards;

          CMata

          • 2. Re: Problem with Python script
            shriyansh.mit

            Hi CMata,

            Thanks for your reply.

            Currently i am using Windows System and Linux image on my device and try to run python script but not working.

            Please let me know how i will run Javascript on my device with step by step. Please help me to run javascript code.

            Please share any link or doc.

             

            Thanks

            Shreyansh

            • 3. Re: Problem with Python script
              shriyansh.mit

              Hi CMata,

              Thanks for your reply.

              Currently i am using Windows System and Linux image on my device and try to run python script but not working.

              Please let me know how i will run Javascript on my device with step by step. Please help me to run javascript code.

              Please share any link or doc.

              • 4. Re: Problem with Python script
                CMata_Intel

                Hi shriyansh.mit

                 

                In order to run Javascript code. You only need:

                root@edison:~# node myCode.js
                

                After typing:

                root@edison:~# node --version
                root@edison:~# python version
                

                What is the outcome that you are receiving?

                 

                Regards;

                CMata

                • 5. Re: Problem with Python script
                  rgb

                  I am by no means an expert, but looking at your code I assume you are trying to capture the one line that gives the temperature for a city.

                  If so, it is easier to just do it in python

                  In fact you could do everything in Python and not need to run an Arduino sketch

                  But anyway, try the following

                   

                  import json
                  import urllib2
                  httpResponse = urllib2.urlopen('http://api.openweathermap.org/data/2.5/weather?id=1566083&units=metric')
                  jsonString = httpResponse.read()
                  jsonData = json.loads(jsonString)
                  CityName = jsonData['name']
                  CityTemp = str(jsonData['main']['temp'])
                  message = "Current temperature at "+ CityName + " is " + CityTemp + "(Celsius))"
                  fout = open('/media/realroot/TestScriptOutput.txtt', 'w')
                  fout.write(message)
                  fout.write('\n') # Writes carriage return
                  fout.close()

                   

                  rgb

                  PS Here is a link to someone who did the same thing you seem to be trying to do

                  http://blog.siliconstraits.vn/getting-start-intel-galileo-call-api-python/

                  • 6. Re: Problem with Python script
                    FrankBot

                    Hi,

                    I had the same issue when calling system("python ...") in arduino sketch. Seem like it doesn't execute, I put checking I/O write file but get nothing. Anyone figures out what is the root cause?

                    • 7. Re: Problem with Python script
                      CMata_Intel

                      Hi FrankBot,

                       

                      I would like to know more information about the problem you have and the configuration you are using.

                      Could you tell us the following information?

                      SD image on the board (on Terminal you can run the command uname –a)

                      Arduino IDE version

                      Python code

                      Arduino sketch

                       

                      With the image Linux galileo 3.8.7-yocto-standard #1 Tue Sep 29 22:16:33 GMT 2015 i586 GNU/Linux I was able to run the following sketch:

                      void setup() {
                        system("python /home/root/blink.py");// put your setup code here, to run once:
                      }
                      void loop() {
                      }
                      

                       

                      Having the blink.py:

                      import mraa #calls the MRAA library
                      import time #calls the time library
                      
                      #Setup - Allows to choose the pin 13 (Built-in LED) as an output.
                      x = mraa.Gpio(13)
                      x.dir(mraa.DIR_OUT)
                      #Process - The process is going to change the state of the pin every second
                      
                      try:
                        print "It's blinking"
                        while True:
                        x.write(1)
                        time.sleep(1)  #Maintain the status of the pin for a second
                        x.write(0)
                        time.sleep(1)
                      except KeyboardInterrupt:
                              print ""
                      

                      I ran it using the 0.9.6 MRAA version.

                       

                      Try with this and let me know the outcome.

                       

                      Regards,

                      Charlie