如何连接Arduino Mega 2560+;esp8266+;HTML?

如何连接Arduino Mega 2560+;esp8266+;HTML?,arduino,esp8266,atmega16,Arduino,Esp8266,Atmega16,我想知道这段在互联网上使用最多的代码中出现了什么故障,但我无法连接到我的Mega 2560 #include <SoftwareSerial.h> #define DEBUG true SoftwareSerial esp8266(2,3); // make RX Arduino line is pin 2, make TX Arduino line is pin 3. // This means that you need

我想知道这段在互联网上使用最多的代码中出现了什么故障,但我无法连接到我的Mega 2560

#include <SoftwareSerial.h>

#define DEBUG true

SoftwareSerial esp8266(2,3); // make RX Arduino line is pin 2, make TX   Arduino line is pin 3.
                         // This means that you need to connect the TX line from the esp to the Arduino's pin 2
                         // and the RX line from the esp to the Arduino's pin 3
 void setup()
 {
  Serial.begin(9600);
  esp8266.begin(9600); // your esp's baud rate might be different

    pinMode(11,OUTPUT);
    digitalWrite(11,LOW);

    pinMode(12,OUTPUT);
    digitalWrite(12,LOW);

    pinMode(13,OUTPUT);
    digitalWrite(13,LOW);

     sendData("AT+RST\r\n",2000,DEBUG); // reset module
     sendData("AT+CWMODE=2\r\n",1000,DEBUG); // configure as access point
     sendData("AT+CIFSR\r\n",1000,DEBUG); // get ip address
     sendData("AT+CIPMUX=1\r\n",1000,DEBUG); // configure for multiple connections
     sendData("AT+CIPSERVER=1,80\r\n",1000,DEBUG); // turn on server on port 80
 }

 void loop()
{
 if(esp8266.available()) // check if the esp is sending a message 
 {


if(esp8266.find("+IPD,"))
{
 delay(1000); // wait for the serial buffer to fill up (read all the serial data)
 // get the connection id so that we can then disconnect
 int connectionId = esp8266.read()-48; // subtract 48 because the read() function returns 
                                       // the ASCII decimal value and 0 (the first decimal number) starts at 48

 esp8266.find("pin="); // advance cursor to "pin="

 int pinNumber = (esp8266.read()-48)*10; // get first number i.e. if the pin 13 then the 1st number is 1, then multiply to get 10
 pinNumber += (esp8266.read()-48); // get second number, i.e. if the pin number is 13 then the 2nd number is 3, then add to the first number

 digitalWrite(pinNumber, !digitalRead(pinNumber)); // toggle pin    

 // make close command
 String closeCommand = "AT+CIPCLOSE="; 
 closeCommand+=connectionId; // append connection id
 closeCommand+="\r\n";

 sendData(closeCommand,1000,DEBUG); // close connection
}
}
}

  /*
  * Name: sendData
  * Description: Function used to send data to ESP8266.
   * Params: command - the data/command to send; timeout - the time to wait for a response; debug - print to Serial window?(true = yes, false = no)
    * Returns: The response from the esp8266 (if there is a reponse)
   */
   String sendData(String command, const int timeout, boolean debug)
  {
   String response = "";

esp8266.print(command); // send the read character to the esp8266

long int time = millis();

while( (time+timeout) > millis())
{
  while(esp8266.available())
  {

    // The esp has data so display its output to the serial window 
    char c = esp8266.read(); // read the next character.
    response+=c;
  }  
}

if(debug)
{
  Serial.print(response);
}

return response;
}
#包括
#定义调试为真
软件系列esp8266(2,3);//使RX Arduino线路为引脚2,使TX Arduino线路为引脚3。
//这意味着您需要将发送线路从esp连接到Arduino的针脚2
//以及从esp到Arduino引脚3的RX线路
无效设置()
{
Serial.begin(9600);
esp8266.begin(9600);//您的esp的波特率可能不同
pinMode(11,输出);
数字写入(11,低);
pinMode(12,输出);
数字写入(12,低);
pinMode(13,输出);
数字写入(13,低);
sendData(“AT+RST\r\n”,2000,调试);//重置模块
sendData(“AT+CWMODE=2\r\n”,1000,调试);//配置为访问点
sendData(“AT+CIFSR\r\n”,1000,调试);//获取ip地址
sendData(“AT+CIPMUX=1\r\n”,1000,调试);//配置多个连接
sendData(“AT+CIPSERVER=1,80\r\n”,1000,调试);//打开端口80上的服务器
}
void循环()
{
if(esp8266.available())//检查esp是否正在发送消息
{
如果(esp8266.find(“+IPD”))
{
延迟(1000);//等待串行缓冲区填满(读取所有串行数据)
//获取连接id以便我们可以断开连接
int connectionId=esp8266.read()-48;//减去48,因为read()函数返回
//ASCII十进制值和0(第一个十进制数)从48开始
esp8266.find(“pin=”);//将光标前进到“pin=”
int pinNumber=(esp8266.read()-48)*10;//获取第一个数字,即如果引脚13,则第一个数字为1,然后相乘得到10
pinNumber+=(esp8266.read()-48);//获取第二个数字,即如果管脚编号为13,则第二个数字为3,然后添加到第一个数字
digitalWrite(pinNumber,!digitalRead(pinNumber));//切换pin
//关闭命令
String closeCommand=“AT+CIPCLOSE=”;
closeCommand+=connectionId;//追加连接id
closeCommand+=“\r\n”;
sendData(closeCommand,1000,DEBUG);//关闭连接
}
}
}
/*
*名称:sendData
*描述:用于向ESP8266发送数据的功能。
*Params:command—要发送的数据/命令;超时-等待响应的时间;调试-打印到串行窗口?(真=是,假=否)
*返回:来自esp8266的响应(如果有响应)
*/
字符串sendData(字符串命令、常量int超时、布尔调试)
{
字符串响应=”;
打印(命令);//将读取的字符发送到esp8266
长整数时间=毫秒();
而((时间+超时)>毫秒()
{
而(esp8266.available())
{
//esp有数据,因此将其输出显示到串行窗口
char c=esp8266.read();//读取下一个字符。
响应+=c;
}  
}
如果(调试)
{
串行打印(响应);
}
返回响应;
}
HTML


ESP8266 LED控制
拨动销11
拨动销12
拨动销13
$(文档).ready(函数(){
$(“.led”)。单击(函数(){
var p=$(this.attr('id');//获取id值(即pin13、pin12或pin11)
//使用参数“pin”和值“p”向IP地址发送HTTP GET请求,然后执行该函数
$.get(”http://192.168.4.1:80/“,{pin:p});//执行get请求
});
});

我试图发送GET参数,但我没有成功,Arduino程序的控制台发送AT,但没有应答

但这会创建一个要连接的LAN


有什么不兼容的地方吗?

为什么我们要关注一个未知的、未经测试的链接?我投票决定结束这个问题,因为它会将我们带到一个未知的领域。对不起,我已经做出了决定​​更改您必须将应用程序降低到显示行为的最低级别。您的格式设置使这变得不可能。基本故障排除告诉您,您必须验证所有连接是否正常工作,至少从arduino到esp8266。先到那里,然后继续。顺便说一句,您可以使用超级终端(或任何其他终端程序)在arduino尝试之前验证通信。
 <html>
<head>
    <title>ESP8266 LED Control</title>
</head>
<body>

<!-- in the <button> tags below the ID attribute is the value sent to the arduino -->

<button id="11" class="led">Toggle Pin 11</button> <!-- button for pin 11 -->
<button id="12" class="led">Toggle Pin 12</button> <!-- button for pin 12 -->
<button id="13" class="led">Toggle Pin 13</button> <!-- button for pin 13 -->

<script src="jquery.min.js"></script>
<script type="text/javascript">
    $(document).ready(function(){
        $(".led").click(function(){
            var p = $(this).attr('id'); // get id value (i.e. pin13, pin12, or pin11)
            // send HTTP GET request to the IP address with the parameter "pin" and value "p", then execute the function
            $.get("http://192.168.4.1:80/", {pin:p}); // execute get request
        });
    });
</script>
</body>