Arduino没有响应Web请求

时间:2019-06-07 07:03:34

标签: arduino

请,我的代码有问题。我想让我的arduino测量温度和湿度,从NTP服务器获取时间和日期,并将这些数据发送到网站,PHP脚本将从这些数据中捕获它们并将其发送到服务器。

我认为我的问题在于我的状况,而不是工作原理。

现在如何工作: Arduino正在从DHT22传感器(温度和湿度)读取数据,并从NTP服务器更新时间,并且所有这些都将在LCD监视器16x2上显示。 但是,当我尝试通过Internet浏览器(Chrome和iExplorer)通过其IP地址连接到它时,它尝试连接,但过一会儿,打印该服务器未发送任何要加载的数据或网页,或者它加载了多个H​​TTP标头( HTTP / 1.1 200 OK内容类型:text / html连接:close)并将它们打印到屏幕上...

我尝试修复了一个多月,但未成功。

我的硬件:Arduino Uno,带SD卡的Arduino以太网屏蔽,DHT22传感器,LCD显示器。

我的代码:

#include <Ethernet.h>
#include <SPI.h>
#include <SD.h>
#include <LiquidCrystal.h>
#include <DHT.h>
#include <EthernetUdp.h>
#include <TimeLib.h>

#define DHTPIN 3
#define DHTTYPE DHT22
DHT dht (DHTPIN,DHTTYPE);

File webPage;
String HTTP_req="";

const int rs = 5, en = 6, d4 = 7, d5 =8, d6 = 9, d7 = 2;
LiquidCrystal lcd(rs,en,d4,d5,d6,d7);

byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
IPAddress ip(192,168,243,253);
IPAddress subnet(255,255,252,0);
IPAddress gateway(192,168,240,2);
IPAddress dns(192,168,240,55);
IPAddress timeServer(192,168,240,55);

EthernetServer server(80);

const int timeZone = 2;
EthernetUDP Udp;
unsigned int localPort = 8888;

void setup() {  
  Ethernet.begin(mac,ip,dns,gateway,subnet);
  lcd.begin(16,2);
  lcd.display();
  dht.begin();
  Serial.begin(9600);
  Udp.begin(localPort);
  setSyncProvider(getNtpTime);

  }

void loop() {

  EthernetClient client = server.available();
  float t = dht.readTemperature();
  float h = dht.readHumidity();

  if (client) {
    Serial.println("new client");
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read(); // read 1 byte (character) from client
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if ( HTTP_req.length() < 80)
          HTTP_req += c;  // save the HTTP request 1 char at a time
        if (c == '\n' && currentLineIsBlank) {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println("Connection: close");
          client.println();
          // send web page
          if (HTTP_req.indexOf("ajaxrefresh") >= 0 ) {
            ajaxRequest(client);  //update the analog values
            break;
            }
          else {
            webPage = SD.open("index.htm");        // open web page file
        if (webPage) {
          while (webPage.available()) {
            client.write(webPage.read()); // send web page to client
          }
          webPage.close();
        }
        break;
      }
      if (c == '\n') {
        // you're starting a new line
        currentLineIsBlank = true;
      } else if (c != '\r') {
        // you've gotten a character on the current line
        currentLineIsBlank = false;
      }
    }
  }else{
    client.stop();
    }
}
// give the web browser time to receive the data
delay(5);
// close the connection:

HTTP_req = "";
Serial.println("client disconnected");
  } // end if (client)



else {

  if (isnan(h) || isnan(t)) {
    lcd.setCursor(0,0);
    if (second()==1) {
      digitalClockDisplay();
    }
else{
lcd.setCursor(0,1);
lcd.print("  SENSOR ERROR  ");
 }
}
else {

lcd.setCursor(0,0);
if (second()==1) {
  digitalClockDisplay();
  Serial.println("Time and Date uploaded.");
  delay(1001);
    }
    else {
      lcd.setCursor(0,1);     // Sets cursor on the next line
      lcd.print(h,1);         // Prints the humidity value from the sensor
      lcd.print(" %   ");
      lcd.setCursor(9,1);
      lcd.print(t,1);
      lcd.print(" ");
      lcd.print(char(223));
      lcd.print("C");
      }
    }
  }
}

void digitalClockDisplay() {
  printDigits(hour());
  lcd.print(":");
  printDigits(minute());
  lcd.print("  ");
  lcd.print(day());
  lcd.print(".");
  lcd.print(month());
  lcd.print(".");
  lcd.print(year()); 
}

void printDigits(int digits) {
  if(digits < 10)
    lcd.print('0');
    lcd.print(digits);
}

const int NTP_PACKET_SIZE = 48;
byte packetBuffer[NTP_PACKET_SIZE];

time_t getNtpTime() {
  while (Udp.parsePacket() > 0) ; // discard any previously received packets
  sendNTPpacket(timeServer);
  uint32_t beginWait = millis();
  while (millis() - beginWait < 1500) {
    int size = Udp.parsePacket();
    if (size >= NTP_PACKET_SIZE) {
      Udp.read(packetBuffer, NTP_PACKET_SIZE);  // read packet into the buffer
      unsigned long secsSince1900;
      // convert four bytes starting at location 40 to a long integer
      secsSince1900 =  (unsigned long)packetBuffer[40] << 24;
      secsSince1900 |= (unsigned long)packetBuffer[41] << 16;
      secsSince1900 |= (unsigned long)packetBuffer[42] << 8;
      secsSince1900 |= (unsigned long)packetBuffer[43];
      return secsSince1900 - 2208988800UL + timeZone * SECS_PER_HOUR;
    }
  }
  return 0; // return 0 if unable to get the time
}

void sendNTPpacket(IPAddress &address) {
  // set all bytes in the buffer to 0
  memset(packetBuffer, 0, NTP_PACKET_SIZE);
  // Initialize values needed to form NTP request
  packetBuffer[0] = 0b11100011;   // LI, Version, Mode
  packetBuffer[1] = 0;     // Stratum, or type of clock
  packetBuffer[2] = 6;     // Polling Interval
  packetBuffer[3] = 0xEC;  // Peer Clock Precision
  // 8 bytes of zero for Root Delay & Root Dispersion
  packetBuffer[12]  = 49;
  packetBuffer[13]  = 0x4E;
  packetBuffer[14]  = 49;
  packetBuffer[15]  = 52;
  // all NTP fields have been given values, now
  // you can send a packet requesting a timestamp:                 
  Udp.beginPacket(address, 123); //NTP requests are to port 123
  Udp.write(packetBuffer, NTP_PACKET_SIZE);
  Udp.endPacket();
}


void ajaxRequest(EthernetClient client)
{
  for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
    int sensorReading = analogRead(analogChannel);
    client.print("analog input ");
    client.print(analogChannel);
    client.print(" is ");
    client.print(sensorReading);
    client.println("<br />");
  }
}

请帮助我,我真的很需要它。

0 个答案:

没有答案