arduino服务器_如何使用Arduino检查Web服务器的响应状态

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:)

  1. Arduino Uno

    Arduino Uno

  2. Ethernet Shield for Arduino (to connect the Arduino to the Internet)

    Arduino的以太网屏蔽 (将Arduino连接到Internet)

  3. Ethernet Cable

    以太网电缆
  4. A/B Type USB 2.0 Cable (Power cable for Arduino)

    A / B型USB 2.0电缆(Arduino的电源线)
  5. Male-to-Male Jumper Cables (x2)

    公对公跳线(x2)
  6. Breadboard

    面包板
  7. LED (x1, any color)

    LED(x1,任何颜色)
  8. Resistor (x1, >100 ohms works)

    电阻(x1,> 100欧姆可工作)

设置 (Setting It Up)

  1. Mount/Insert the Ethernet shield on the Arduino.

    在Arduino上安装/插入以太网屏蔽。
  2. Insert the positive (longer) end of the LED into the breadboard slot 6a and the negative (shorter) end into slot 5a.

    将LED的正(长)端插入面包板插槽6a,将负(短)端插入插槽5a。
  3. Insert one end of the resistor into the breadboard slot 1b and the other into slot 5b.

    电阻的一端插入面包板插槽1b,另一端插入插槽5b。

  4. 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插槽。

  5. 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中。

  6. 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 -> Examples -> Ethernet -> 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应用程序! :)

翻译自: https://www.freecodecamp.org/news/how-to-use-arduino-to-check-your-web-servers-response-status-9e47e02a61cc/

arduino服务器

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/393092.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

leetcode486. 预测赢家(dp)

给定一个表示分数的非负整数数组。 玩家 1 从数组任意一端拿取一个分数,随后玩家 2 继续从剩余数组任意一端拿取分数,然后玩家 1 拿,…… 。每次一个玩家只能拿取一个分数,分数被拿取之后不再可取。直到没有剩余分数可取时游戏结束…

linux怎么看文件状态,linux查看文件类型-file、状态-stat

linux查看文件类型-file、状态-stat首页 计算机相关 linux命令 linux查看文件类型-file、状态-statfile 命令可以用来查看文件类型-i mime type-s 读取字符或块设备文件最好指定[root192 tmp]# file freeclsfreecls: UTF-8 Unicode text[root192 tmp]# file -i freeclsfreecls:…

Linux课程笔记 Crond介绍

1. 定时任务比较及cron语法 Linux的任务调度可以分为两类: 系统自身执行的任务用户执行的工作Linux系统下另外两种定时任务软件: at:适合仅执行一次的调度任务,需要启动一个名为atd的服务 anacron:这个命令主要用于非…

Python 学习日记第二篇 -- 列表,元组

一、列表 列表是一个可以包含所以数据类型的对象的位置有序集合,它是可以改变的。 1、列表的序列操作(Python3) 123456789101112131415161718192021222324>>> one_list [1,2,3,4]>>> two_list ["jonny","…

【Gamma】PhyLab 测试报告

PhyLab Gamma测试报告 测试中发现的bug Gamma阶段新Bug Bug可能原因部分错误码设置与原先抛异常的逻辑冲突原先代码中使用了一些特殊的办法处理异常Beta未发现Bug Bug可能原因控制台新建实验编号不能以0开头后端处理编号会将其前导0去除,以数字形式存储,…

如何使用Node.js,Express和MongoDB设置GraphQL服务器

by Leonardo Maldonado莱昂纳多马尔多纳多(Leonardo Maldonado) 如何使用Node.js,Express和MongoDB设置GraphQL服务器 (How to set up a GraphQL Server using Node.js, Express & MongoDB) 从GraphQL和MongoDB开始的最直接的方法。 (The most straightforward…

leetcode954. 二倍数对数组(treemap)

给定一个长度为偶数的整数数组 A&#xff0c;只有对 A 进行重组后可以满足 “对于每个 0 < i < len(A) / 2&#xff0c;都有 A[2 * i 1] 2 * A[2 * i]” 时&#xff0c;返回 true&#xff1b;否则&#xff0c;返回 false。 示例 1&#xff1a; 输入&#xff1a;[3,1,…

linux文件内容打印成二进制,如何在二进制文件中只打印可打印字符(相当于Linux下的字符串)?...

在Python3中&#xff0c;以二进制模式打开文件会得到bytes的结果。迭代一个bytes对象可以得到0到255(包括0到255)的整数&#xff0c;而不是字符。从^{} documentation&#xff1a;While bytes literals and representations are based on ASCII text, bytes objects actually b…

1098 均分纸牌

1098 均分纸牌 2002年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description有 N 堆纸牌&#xff0c;编号分别为 1&#xff0c;2&#xff0c;…, N。每堆上有若干张&#xff0c;但纸牌总数必为 N 的倍数。可以在任一堆上取若于张纸…

轻松学习分布式|系列3|分布式数据库。

我们继续来讲分布式&#xff0c;回到我们的创业游戏。 我们的业务规模上来了&#xff0c;客户也越来越忠诚了。很多客户都通过我们的订票服务&#xff0c;来方便自己的行程。 那对这些老客户&#xff0c;我们的宗旨是&#xff1a;要不断超越客户的期待。 所以&#xff0c;我们要…

量子运算 简单通俗例子_什么是量子计算机? 用一个简单的例子解释。

量子运算 简单通俗例子by YK Sugi由YK Sugi 什么是量子计算机&#xff1f; 用一个简单的例子解释。 (What is a quantum computer? Explained with a simple example.) Hi everyone!嗨&#xff0c;大家好&#xff01; The other day, I visited D-Wave Systems in Vancouver…

linux增加端口失败,端口没被占用,怎么会bind失败呢?

今天在一个服务器上部署一个webserver的时候&#xff0c;提示我bind端口失败&#xff0c;我习惯性的用netstat看了下&#xff0c;没有被占用啊&#xff01;把问题分享出来后&#xff0c;给力的同事们搜索到了ip_local_port_range这个东西这个东西对应的是/proc/sys/net/ipv4/ip…

leetcode面试题 17.15. 最长单词

给定一组单词words&#xff0c;编写一个程序&#xff0c;找出其中的最长单词&#xff0c;且该单词由这组单词中的其他单词组合而成。若有多个长度相同的结果&#xff0c;返回其中字典序最小的一项&#xff0c;若没有符合要求的单词则返回空字符串。 示例&#xff1a; 输入&am…

Restful API 设计

1. 简介 目前 "互联网软件"从用客户端/服务端模式&#xff0c;建立在分布式体系上&#xff0c;通过互联网通讯&#xff0c;具有高延时、高开发等特点。但是软件开发和网络是两个不同的领域&#xff0c;交集很少。要使得两个融合&#xff0c;就要考虑如何在互联网环境…

sql行数少于10_如何用少于100行的代码创建生成艺术

sql行数少于10by Eric Davidson埃里克戴维森(Eric Davidson) 如何用少于100行的代码创建生成艺术 (How to Create Generative Art In Less Than 100 Lines Of Code) Generative art, like any programming topic, can be intimidating if you’ve never tried it before. I’v…

安装输入发

直接在系统 ——系统管理 ——语言支持 选——中文从新启动 sudo apt-get install scim-pinyin安装JAVA环境支持 sudo apt-get install sun-java-jre()要是 apt -get 命令不能用 可能是你 的 源有问题 可以 更新一下 在系统 &#xff0d;系统管理 源设置 选这台湾的 就可以 …

(第2篇)一篇文章教你轻松安装hadoop

摘要: 这篇文章将会手把手教你安装hadoop&#xff0c;只要你细心按照文章中的步骤操作&#xff0c;hadoop肯定能正确安装&#xff0c;绝对不会让你崩溃 博主福利 给大家赠送一套hadoop视频课程 授课老师是百度 hadoop 核心架构师 内容包括hadoop入门、hadoop生态架构以及大型ha…

linux cpio到指定目录,linux cpio命令存取归档包中的文件

功能描述使用cpio命令可以通过重定向的方式将文件进行打包备份及还原恢复&#xff0c;它可以解压缩以“.cpio”或者“.tar”结尾的文件。命令语 法cpio [选项] [目标目录]选项含义选项含义-o执行 copy-out 模式&#xff0c;建立备份档。-i执行 copy-in 模式&#xff0c;还原备份…

leetcode剑指 Offer 20. 表示数值的字符串

请实现一个函数用来判断字符串是否表示数值&#xff08;包括整数和小数&#xff09;。例如&#xff0c;字符串"100"、“5e2”、"-123"、“3.1416”、"-1E-16"、“0123"都表示数值&#xff0c;但"12e”、“1a3.14”、“1.2.3”、"…

python接口自动化2-发送post请求

前言 发送post的请求参考例子很简单&#xff0c;实际遇到的情况却是很复杂的&#xff0c;首先第一个post请求肯定是登录了&#xff0c;但登录是最难处理的。登录问题解决了&#xff0c;后面都简单了。 一、查看官方文档 1.学习一个新的模块&#xff0c;其实不用去百度什么的&am…