我正在尝试使用arduino uno,ethernet shield和hc-sr06传感器连接服务器。它在不使用传感器代码的情况下连接到服务器,但是当我添加传感器代码时,它没有连接。
这是没有传感器代码的代码;我看到"在loop2"登录串口。
#include <Ethernet.h>
#include <SPI.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
EthernetClient client;
void setup() {
Serial.begin(9600);
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
}
delay(10000); // GIVE THE SENSOR SOME TIME TO START
}
void loop(){
Serial.println("In loop");
if (client.connect("www.xxxxx.com",80)) {
Serial.println("In loop2");
client.println("POST /update_data2.php HTTP/1.1");
client.println("Host: www.xxxxx.com"); // SERVER ADDRESS HERE TOO
client.println("Content-Type: application/x-www-form-urlencoded");
client.print("Content-Length: ");
client.println(txData.length());
client.println();
client.print(txData);
}
if (client.connected()) {
client.stop();
}
delay(30000);
}
以下是使用传感器代码的代码;我没有看到&#34;在loop2&#34;登录串口。
#include <Ethernet.h>
#include <SPI.h>
const int trigger_pin_1 = 12;
const int echo_pin_1 = 13;
const int trigger_pin_2 = 11;
const int echo_pin_2 = 10;
const int trigger_pin_3 = 9;
const int echo_pin_3 = 8;
const int led_1_r = 2;
const int led_1_g = 3;
const int led_2_r = 4;
const int led_2_g = 5;
const int led_3_r = 6;
const int led_3_g = 7;
int sure_1 ;
int mesafe_1 ;
int sure_2 ;
int mesafe_2 ;
int sure_3 ;
int mesafe_3 ;
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
EthernetClient client;
String txData ="";
void setup() {
Serial.begin(9600);
Serial.println("In setup");
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
}
delay(5000);
pinMode(led_1_r , OUTPUT);
pinMode(led_1_g , OUTPUT);
pinMode(led_2_r , OUTPUT);
pinMode(led_2_g , OUTPUT);
pinMode(led_3_r , OUTPUT);
pinMode(led_3_g , OUTPUT);
pinMode(trigger_pin_1 , OUTPUT);
pinMode(trigger_pin_2 , OUTPUT);
pinMode(trigger_pin_3 , OUTPUT);
pinMode(echo_pin_1 ,INPUT);
pinMode(echo_pin_2 ,INPUT);
pinMode(echo_pin_3 ,INPUT);
delay(3000);
}
void loop(){
Serial.println("In loop");
digitalWrite(trigger_pin_1 , HIGH);
digitalWrite(trigger_pin_2 , HIGH);
digitalWrite(trigger_pin_3 , HIGH);
delayMicroseconds(1000);
digitalWrite(trigger_pin_1 , LOW);
digitalWrite(trigger_pin_2 , LOW);
digitalWrite(trigger_pin_3 , LOW);
sure_1 = pulseIn(echo_pin_1 , HIGH);
mesafe_1 = (sure_1/2) / 29.1;
sure_2 = pulseIn(echo_pin_2 , HIGH);
mesafe_2 = (sure_2/2) / 29.1;
sure_3 = pulseIn(echo_pin_3 , HIGH);
mesafe_3 = (sure_3/2) / 29.1;
delay(100);
Serial.print("mesafe_1:");
Serial.println(mesafe_1);
Serial.print("mesafe_2:");
Serial.println(mesafe_2);
Serial.print("mesafe_3:");
Serial.println(mesafe_3);
if (mesafe_1 <= 10)
{
digitalWrite(led_1_r , HIGH);
delay(100);
clienConnect(1,0);
}else{
digitalWrite(led_1_g , HIGH);
delay(100);
clienConnect(1,1);
}
if (mesafe_2 <= 10)
{
digitalWrite(led_2_r , HIGH);
delay(100);
clienConnect(2,0);
}else{
digitalWrite(led_2_g , HIGH);
delay(100);
clienConnect(2,1);
}
if (mesafe_3 <= 10)
{
digitalWrite(led_3_r , HIGH);
delay(100);
clienConnect(3,0);
}else{
digitalWrite(led_3_g , HIGH);
delay(100);
clienConnect(3,1);
}
delay(100);
}
void clienConnect(int id, int valuex){
Serial.println("In clienConnect");
txData = "senid=" + (String)id + "&senvalue=" + (String)valuex;
Serial.print(" txData:");
Serial.println(txData);
Serial.println("In clienConnect2");
if (client.connect("www.xxxxx.com",80))
{
Serial.println("In loop2");
client.println("POST /update_data.php HTTP/1.1");
client.println("Host: www.xxxxx.com");
client.println("Content-Type: application/x-www-form-urlencoded");
client.print("Content-Length: ");
client.println(txData.length());
client.println();
client.print(txData);
}
Serial.println("In loop3");
if (client.connected()) {
Serial.println("In loop4");
client.stop();
Serial.println("In loop5");
}
Serial.println("In loop6");
delay(1000);
}
添加传感器代码时,为什么客户端没有连接到服务器?你能帮我解决这个问题吗?非常感谢。
答案 0 :(得分:1)
以太网屏蔽连接到SPI引脚,恰好是Arduino Uno上的引脚11,12和13。引脚10也用于Ethernet Shield CS引脚。 (参考:https://www.arduino.cc/en/Reference/Ethernet)
在你的第二个代码中:
git config --global http.postBuffer 524288000
git config --global http.maxRequestBuffer 100M
git config --global core.compression 0
您正在将这些引脚用于HC-SR06传感器。您需要为这些传感器使用不同的引脚。