7 Replies Latest reply on Sep 2, 2015 11:40 PM by iamfarazahmad

    Send integer data to Edison via Wi-fi Server-Client operation.

    iamfarazahmad

      Dear All,

      I need to send some integer data from an HTML form to Edison running wi-fi server. How can I accept the incoming data from the form. Below code is for the HTML form that i am using for sending data to server"192.168.43.170:70". In Edison I have a client that I want it to receive this data and switch on that specified Digital Port "HIGH".

       

       

      <html>

      <body>

      <form action="http://192.168.43.170:70" method="post" >

        <p>

          <label for="1">Number</label>

          <input type="text" name="Number" id="1" />

        </p>

        <p>

          <input type="submit" name="2" id="2" value="Send" />

        </p>

      </form>

      </body>

      </html>

        • 1. Re: Send integer data to Edison via Wi-fi Server-Client operation.
          Intel_Peter

          Hello iamfarazahmad,

           

          I'm not certain on how to exactly complete this in the way you specifically asked. Nevertheless, I can give you a suggestion. You could upload the data you want to send to the Edison in a URL, download the data from that URL into an .html file, then make the Edison which is the data from that file that you need and finally make a new file with that data so you can use it as you need on the Edison.

          I did a short example on how to download and select the data that I want from an .html file. The example downloads the .html file from Wikipedia and then searches for the word "Kojima" (I'm a Hideo Kojima fan by the way) detects which paragraphs contain this word and then it copy them into a new file. The code can be modified simply and you can take it as beginning. I hope it helps.

           

          from subprocess import call
          def par():
              call(["wget","https://en.wikipedia.org/wiki/List_of_Metal_Gear_media"])
              call(["sleep","4"])
              f = open('/home/root/List_of_Metal_Gear_media','r+')
              l = f.readlines()
              n = []
              arr = []
              y = 0
              line1 = ''
              for x in l:
                  if 'Kojima' in x:
                      n.append(x)
              print len(n)
              for z in range(0,len(n)):
                  while y < len(n[z]):
                      if n[z][y] == '>':
                          y = y + 1
                          if n[z][y] != '<':
                              line1 = line1 + n[z][y]
                              y = y + 1
                              while y < len(n[z]) and n[z][y] != '<':
                                  line1 = line1 + n[z][y]
                                  y = y + 1
                      y = y + 1
                  arr.append(line1)
                  line1 = ''
                  y = 0
              nf = open('/home/root/New_file.txt','w')
              for i in range(0,len(arr)):
                  nf.write(arr[i])
              nf = open('/home/root/New_file.txt','r')
              print nf.read()
          par()
          

           

          Peter.

          • 2. Re: Send integer data to Edison via Wi-fi Server-Client operation.
            iamfarazahmad

            Thanks peter. I think appending the data with URL is a good idea but not sure whether it will work. I will try it out. But i wished if edison could accept a GET command from the HTML form and broadcast it to the client.

             

            Thanks again.

            Faraz Ahmad.

            • 3. Re: Send integer data to Edison via Wi-fi Server-Client operation.
              Frederick Blais

              If you don't mind doing it in Python, I can help you.

               

              You want to make a webserver on the Edison that will serve an HTML page with a form that allow changing the GPIO state? Right?

               

              I'm not sure I follow the part where you say you have a client you want to receive this data. What is the client you are talking about?

              • 4. Re: Send integer data to Edison via Wi-fi Server-Client operation.
                iamfarazahmad

                I have to send some integers (angle of servo motor and Pin Number of GPIO) over WIFI to Edison. I tried making it as a client but do not know how to proceed.

                 

                P.S. I don't know anything about Python.

                • 5. Re: Send integer data to Edison via Wi-fi Server-Client operation.
                  PeteB

                  There are quite a few ways to do this, but none so easy as you envision above, but it's all on Google. Your client, where you enter the data, can be a webserver, but it needs a way to send data that your edison server can understand. Your push with an HTTP server running on your edison may work with a lot of code on the edison, but this plan has lots of difficult compromises. Basically, you will need a full blown HTTP server implemented that can handle POST + headers + URL (what would be sent from your web interface). If you're clever, you could use a lightweight HTTP server with CGI that could decode your incoming request, and then execute your PWM or GPIO toggle. Again, this is a lot of work for something that can probably be done much easier.

                   

                  You have access to a machine that can run a webserver. Why not use PHP + MQTT to send the message, and have an MQTT listener on your edison (it supports Mosquitto natively). You could also create a REST interface on the Edison using Qt (google QT Webserver, then just extend it to handle in the incoming request) and send REST (and or JSON) messages. The Qt webserver could be made to handle your URL very easily without much work, but again, if you wanted to change anything about it, you need to do more work that isn't reused later.

                   

                  There is a fundamental disconnect here. You need to send complex data, but want it to be simple to receive it. However, the edison doesn't know anything about your data, so you need to help it out.

                   

                  Python is honestly your best bet here. It can build the server, decode your message, and act on the pins you want very quickly.

                  • 6. Re: Send integer data to Edison via Wi-fi Server-Client operation.
                    Frederick Blais

                    With Python and the Flask framework, it is pretty simple. Take a look at this single file example :

                     

                    Python script :

                     

                    from flask import Flask, request
                    app = Flask(__name__)
                    
                    
                    page = """
                    <!DOCTYPE html>
                    <html>
                    <body>
                    
                    
                    <form method="post">
                      Servo Value:<br>
                      <input type="text" name="Servo"><br>
                      Pin Value:<br>
                      <input type="text" name="Pin"><br><br>
                      <input type="submit" value="Send to Edison">
                    </form>
                    
                    
                    </body>
                    </html>
                    """
                    
                    
                    @app.route('/', methods=['GET', 'POST'])
                    def index():
                        if request.method == 'POST':
                            servo_value = int(request.form['Servo'])
                            pin_value = int(request.form['Pin'])
                            print "pin value = {} and servo value = {}".format(pin_value, servo_value)
                        return page
                    
                    
                    if __name__ == "__main__":
                        app.run(host="0.0.0.0")
                    
                    

                     

                    HTML page :

                    html.png

                     

                    Output from server :

                    ssh.png

                     

                    From there, you can just import mraa and do your stuff.

                    • 7. Re: Send integer data to Edison via Wi-fi Server-Client operation.
                      iamfarazahmad

                      Thanks All. Will have to work it out!