arduino服务器
by Harshita Arora
通过Harshita Arora
如何使用Arduino检查Web服务器的响应状态 (How to use Arduino to check your web server’s response status)
Last year, I created Crypto Price Tracker (an app which was acquired by Redwood City Ventures this year). A back end member of my team had been using an Arduino setup to check web server response statuses continuously to get updates all the time. I found that setup to be pretty useful and interesting.
去年,我创建了Crypto Price Tracker (一个应用程序,今年被Redwood City Ventures收购)。 我团队的一个后端成员一直在使用Arduino设置来连续检查Web服务器响应状态以始终获取更新。 我发现该设置非常有用且有趣。
I researched about it and recreated the setup for myself. In this article, I’ll show you how you can build it yourself, too.
我对此进行了研究,并为自己重新创建了设置。 在本文中,我将向您展示如何自己构建它。
您需要的东西: (Things that you need:)
Ethernet Shield for Arduino (to connect the Arduino to the Internet)
Arduino的以太网屏蔽 (将Arduino连接到Internet)
- Ethernet Cable 以太网电缆
- A/B Type USB 2.0 Cable (Power cable for Arduino) A / B型USB 2.0电缆(Arduino的电源线)
- Male-to-Male Jumper Cables (x2) 公对公跳线(x2)
- Breadboard 面包板
- LED (x1, any color) LED(x1,任何颜色)
- Resistor (x1, >100 ohms works) 电阻(x1,> 100欧姆可工作)
设置 (Setting It Up)
- Mount/Insert the Ethernet shield on the Arduino. 在Arduino上安装/插入以太网屏蔽。
- Insert the positive (longer) end of the LED into the breadboard slot 6a and the negative (shorter) end into slot 5a. 将LED的正(长)端插入面包板插槽6a,将负(短)端插入插槽5a。
Insert one end of the resistor into the breadboard slot 1b and the other into slot 5b.
将电阻的一端插入面包板插槽1b,另一端插入插槽5b。
Insert one end of the first jumper cable into the breadboard slot 1e. Insert the other end into the GND slot of the Ethernet shield.
将第一根跳线的一端插入面包板插槽1e中。 将另一端插入以太网屏蔽的GND插槽。
Insert one end of the second jumper cable into the breadboard slot 6e. Insert the other end into pin slot 2 of the Ethernet shield.
将第二根跨接电缆的一端插入面包板插槽6e中。 将另一端插入以太网屏蔽的插针插槽2中。
Connect the Ethernet cable from your router to your Ethernet shield.
将以太网电缆从路由器连接到以太网屏蔽。
This is what my setup looks like:
这是我的设置:
7. Open a command line interface on your machine and check and note your default gateway. This can be done using ipconfig
command on Windows or the netstat -nr | grep default
command on Linux/Mac.
7.在计算机上打开命令行界面,然后检查并记下默认网关。 可以使用ipconfig
完成 Windows或netstat -nr | grep default
使用netstat -nr | grep default
netstat -nr | grep default
Linux / Mac上的命令。
8. Download and install the Arduino IDE if you haven’t already.
8.如果尚未下载并安装Arduino IDE,请下载并安装。
9. Open the IDE and go to Files -&
gt; Examples
-> Ether
net -> WebClientRepeating. You should see the following code:
9.打开IDE,然后转到“文件” -&
> 为例es
- >的Eth er
网- > WebClientRepeating。 您应该看到以下代码:
10. Edit the line 37 to be an IP address in the range (1–254) of your default gateway IP. So for example, if my default gateway is 10.0.0.1, then I can use an IP address from 10.0.0.2 to 10.0.0.254. It is, however, important to make sure that the IP that you’re using doesn’t conflict with any other IP addresses on your network.
10.将第37行编辑为默认网关IP范围(1-254)中的IP地址。 因此,例如,如果我的默认网关是10.0.0.1,则可以使用从10.0.0.2到10.0.0.254的IP地址。 但是,重要的是要确保您使用的IP地址与网络上的任何其他IP地址都没有冲突。
For this example, I changed the line of code to be:
对于此示例,我将代码行更改为:
IPAddress ip(10, 0, 0, 2);
IPAddress ip(10, 0, 0, 2);
11. Change the DNS in line 40 to be 8.8.8.8 (this is the Google Public DNS and is just something I prefer, you may use a DNS that you prefer).
11.将第40行的DNS更改为8.8.8.8 (这是Google Public DNS,这只是我喜欢的东西,您可以使用自己喜欢的DNS)。
For this example, I changed the line of code to be:
对于此示例,我将代码行更改为:
IPAddress myDns(8, 8, 8, 8);
IPAddress myDns(8, 8, 8, 8);
12. Change the URL in line 45 to a URL matching your web server. If you would like to use an IP address instead, then comment line 45 and uncomment line 46. Since I am using a web server that I’m hosting locally, for this example, I will use an IP address.
12.将第45行中的URL更改为与您的Web服务器匹配的URL。 如果您想改用IP地址,请在注释行45和注释行46中取消注释。 由于我使用的是本地托管的Web服务器,因此在此示例中,我将使用IP地址。
For this example, I changed the line of code to be:
对于此示例,我将代码行更改为:
//char server[] = “www.arduino.cc";
IPAddress server(127,0,0,1);
//char server[] = “ www.arduino.cc ";
IPAddress server(127,0,0,1);
Note that the port or the path here is not important yet. Just the IP Addressis needed. If you would like to change the port that is used for the GETrequest, you may change it on line 94.
请注意,此处的端口或路径并不重要。 仅需要IP地址。 如果您想更改用于GETrequest的端口,则可以在第94行进行更改。
For this example, I have hosted my local webserver on port 3000. Thus, I will change the code in line 94 to something like this:
对于此示例,我将本地Web服务器托管在端口3000上。因此,我将在第94行中的代码更改为如下所示:
if (client.connect(server, 3000)) {
if (client.connect(server, 3000)) {
13. Edit the GET request that is pre-written in lines 97 - 100 to follow this pattern:
13.编辑在第97-100 行中预先编写的GET请求,以遵循以下模式:
client.println(“GET /path_to_url HTTP/1.1”);
client.println(“Host: 127.0.0.1”);
client.println(“Connection: close”);
client.println();
client.println(“GET /path_to_url HTTP/1.1”);
client.println(“Host: 127.0.0.1”);
client.println(“Connection: close”);
client.println();
14. We can now start programming the behaviour of the LED depending on the web server status and response. To do this, we must first declare the pin we’re using for the LED on our Ethernet shield.
14.现在,我们可以开始根据Web服务器的状态和响应对LED的行为进行编程。 为此,我们必须首先在以太网屏蔽上声明用于LED的引脚。
Add the following line of code after the first two include statements of the program:
在前两个包括之后添加以下代码行 该计划的陈述:
int LED = 2;
int LED = 2;
15. Add the following lines of code at the beginning of the setup() function.
15.在setup()函数的开头添加以下代码行。
pinMode(LED, OUTPUT);
pinMode(LED, OUTPUT);
digitalWrite(LED, LOW); //program starts with the LED turned off
digitalWrite(LED, LOW); //program starts with the LED turned off
16. Add the following line of code after the GET request line that we previously edited:
16.在我们先前编辑的GET请求行之后添加以下代码行:
digitalWrite(LED, LOW);
digitalWrite(LED, LOW);
17. Finally, add this line of code at the beginning of the else statement of the same conditional:
17.最后,在else的开头添加这一行代码 相同条件的语句:
digitalWrite(LED, HIGH);
digitalWrite(LED, HIGH);
And voilà, you’re done!
瞧,您完成了!
Upload the program to your Arduino. Open the serial monitor from the top right part of the IDE and watch the response. If your server doesn’t respond, the LED glows, if it does, the LED stays off :)
将程序上传到您的Arduino。 从IDE的右上方打开串行监视器,然后观察响应。 如果您的服务器没有响应,则LED发光,如果发光,则LED保持熄灭:)
You can check out my final code here.
您可以在此处查看我的最终代码。
检查响应 (Checking Response)
If you would also like to validate the response that you receive from yourweb server, then you may add them inside the following conditional of theprogram.
如果您还想验证从Web服务器收到的响应,则可以将它们添加到程序的以下条件中。
if (client.available()) {
char c = client.read();
Serial.write(c);
}
if (client.available()) {
char c = client.read();
Serial.write(c);
}
The variable c is where the response is stored.You could check it like this:
变量c是响应的存储位置,您可以像这样检查它:
if (client.available()) {
if (client.available()) {
char c = client.read();
if(c == “arduino is great”){
digitalWrite(LED, LOW); //correct response
}
else{
digitalWrite(LED, HIGH); //wrong response
}
Serial.write(c);
}
char c = client.read();
if(c == “arduino is great”){
digitalWrite(LED, LOW); //correct response
digitalWrite(LED, LOW); //correct response
}
else{
digitalWrite(LED, HIGH); //wrong response
digitalWrite(LED, HIGH); //wrong response
}
Serial.write(c);
}
Do note that, if you’re trying to do this, then it’s best to get rid of thedigitalWrite statement after the GET request. Depending on your response,you may also have to parse JSON values. There are several ways to do thisand plenty of tutorials/articles around for it too! Make sure to check themout!
请注意,如果您尝试执行此操作,则最好在GET请求之后删除digitalWrite语句。 根据您的响应,您可能还必须解析JSON值。 有几种方法可以做到这一点,并且还有很多教程/文章! 请务必检查一下!
Have fun! Feel free to email me at harshita (at) harshitaapps.com
for any questions, feedback, or ideas!
玩得开心! 如有任何问题,反馈或想法,请随时通过harshita (at) harshitaapps.com
给我发送电子邮件!
Make sure to check out Crypto Price Tracker app if you’re interested/invested in cryptocurrencies! :)
如果您对加密货币感兴趣/投资了,请确保签出Crypto Price Tracker应用程序! :)
arduino服务器