简体   繁体   中英

How to send data from python script through Internet to android app and vice versa.

I am coding on raspberry pi in python. I want to control my pi through internet for that I need to send data from my android app to the running python script on my Pi. For example, if I click a button B on my android app the raspberry pi's python script should get the data "12345" as string or integer through Internet. I will compare the received data and take the needed action in python. Similarly, vice versa. If I click a button in python created by tkinter, python should send some data say "67890" as string or integer to my android app via Internet. I will compare that received data my android app and take necessary action. Constant process of sending and receiving data between android app created in java and python script on Pi should continue. I already created python program and android app without this communication part. How will it be possible to do data sending??

You shouldn't start with communication over Internet. For the beginning its okay to use your local WiFi for that (the code stays the same, but you must not set up your RPi as a Server with DynDNS and stuff like that. Which is as well dangerous if you don't know what you're doing).

Then start with the python-part. Your Pi acts as a Server for your application. So have a look at ServerSockets with Python: like this example . Here you define a Port on which your service runs. Your python-code will now wait on that Port for an incoming Connection.

Now on your Android-device you need to connect to that Socket.

Socket sock = new Socket("IP-ADDRESS_OF_YOUR_RPi", PORT);

Now your Android is connected with your RPi via your local WiFi (assuming that both are in the same network)

Then you can communicate over Input- and Outputstreams between you Android-App and your Python-Server.

But you should definitely do some research about server-client and TCP socket communication. And connecting this application to the Internet is the very last step you should do (because open ports on servers in the internet are a invitation for criminals).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM