简体   繁体   English

Android与Arduino服务器通信

[英]Android to Arduino server communication

I'm a long time lurker, first time poster. 我是长期潜伏者,是第一次海报。 I'm working on a small home automation project using an Android device (Jelly Bean 4.3) and Arduino Uno w/ Wi-Fi shield. 我正在使用Android设备(Jelly Bean 4.3)和带Wi-Fi防护罩的Arduino Uno进行小型家庭自动化项目。 So far I have set up my arduino as a web server and can access the page via an IP address i'm given after the arduino connects to my home network. 到目前为止,我已经将arduino设置为Web服务器,并且可以在arduino连接到我的家庭网络后通过提供的IP地址访问页面。 I have coded the arduino so that I can turn on and off an LED by using commands like: http://IP Address/H (turns the Relay on) and http://IP Address/L (turns the Relay off). 我已经对arduino进行了编码,以便可以使用以下命令打开和关闭LED: http:// IP地址/ H(打开继电器)和http:// IP地址/ L(关闭继电器)。 The end goal is to use what I have learned to control my lights/water heater etc. I would like to be able to use my android device to send HTTP post requests to these exact IP addresses in an application so that I don't have to access these commands by my desktop. 最终目标是使用我学到的知识来控制灯光/热水器等。我希望能够使用我的Android设备向应用程序中的这些确切IP地址发送HTTP发布请求,这样我就不必在我的桌面上访问这些命令。 I am using Android Studio to write my application, and have been able to create a UI that has two buttons (ON and OFF). 我正在使用Android Studio编写我的应用程序,并且已经能够创建具有两个按钮(ON和OFF)的UI。 What would be a viable way to access these IP addresses? 访问这些IP地址的可行方法是什么? Sockets seem like a steep learning curve as I have little experience in JAVA programming. 套接字似乎是一个艰难的学习曲线,因为我对JAVA编程缺乏经验。 Any help is greatly appreciated. 任何帮助是极大的赞赏。

Here you are the simplest example of http call from android: 这是您从android进行HTTP调用的最简单示例:

String link = "http://www.google.com";
URL url = new URL(link);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
BufferedReader reader =new BufferedReader(new InputStreamReader(is, "UTF-8"));
String webPage = "",data="";
while ((data = reader.readLine()) != null){
    webPage += data + "\n";
}

this tutorial is very helpful and straight forward: http://www.tutorialspoint.com/android/android_network_connection.htm 本教程非常有帮助,也很直接: http : //www.tutorialspoint.com/android/android_network_connection.htm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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