Ошибка при отправке данных на облачный сервер и отставание arduino - PullRequest
1 голос
/ 06 января 2020

Код, над которым я работаю, должен показывать температуру, влажность и способен измерять частоту пульса на ЖК-дисплее. После того, как данные показаны, он отправит данные в «ThingSpeak». После отправки будет ошибка http-кода -401, которая в порядке, так как она может отправлять только данные очень 15 se c. Но через некоторое время он изменит ошибку с кодом http -301, а затем зависнет. Другая проблема заключается в том, что когда я пытаюсь использовать датчик температуры с датчиком сердечного ритма, ЖК-дисплей зависает, и он не будет работать, пока я не выполню сброс.

    #include "ThingSpeak.h"
    #include "SPI.h"
    #include "DHT.h"
    #include <Ethernet.h>
    #include <LiquidCrystal.h>
    LiquidCrystal lcd(10, 9, 5, 4, 3, 2); //numbers of interface pins
    #define redLED 8
    int sensorPin = A8;
    float tempC;

    #define DHTPIN 6
    #define DHTTYPE DHT11
    DHT dht(DHTPIN, DHTTYPE);
    float h;

    #define USE_ARDUINO_INTERRUPTS true    // Set-up low-level interrupts for most acurate BPM math.
    #include <PulseSensorPlayground.h>     // Includes the PulseSensorPlayground Library.   

    //  Variables
    const int PulseWire = A9;       // PulseSensor PURPLE WIRE connected to ANALOG PIN 0
    const int blinkPin = 22;          // The on-board Arduino LED, close to PIN 13.
    int Threshold = 550;           // Determine which Signal to "count as a beat" and which to ignore.

    PulseSensorPlayground pulseSensor;  // Creates an instance of the PulseSensorPlayground object called "pulseSensor"


    byte mac[] = {0x90, 0xA2, 0xDA, 0x10, 0x40, 0x4F};
    unsigned long myChannelNumber = ;
    const char * myWriteAPIKey = "";

    // Set the static IP address to use if the DHCP fails to assign
    IPAddress ip(172, 17, 171, 199);
    IPAddress myDns(172, 17, 171, 254);

    float get_temperature(int pin)
    {
      float temperature = analogRead(pin); // Calculate the temperature based on the reading and send that value back
      float voltage = temperature * 5.0;
      voltage = voltage / 1024.0;
      return ((voltage - 0.5) * 100);
    }

    EthernetClient client;

    void setup()
    {
      lcd.begin(16, 2);
      pinMode(redLED, OUTPUT);
      pulseSensor.analogInput(PulseWire);
      pulseSensor.blinkOnPulse(blinkPin);       //auto-magically blink Arduino's LED with heartbeat.
      pulseSensor.setThreshold(Threshold);
      pulseSensor.begin();
      dht.begin();
      Ethernet.init(10);  // Most Arduino Ethernet hardware
      Serial.begin(9600);  //Initialize serial
      // start the Ethernet connection:
      Serial.println("Initialize Ethernet with DHCP:");
      if (Ethernet.begin(mac) == 0)
      {
        Serial.println("Failed to configure Ethernet using DHCP");
        // Check for Ethernet hardware present
        if (Ethernet.hardwareStatus() == EthernetNoHardware)
        {
          Serial.println("Ethernet shield was not found.  Sorry, can't run without hardware. :(");
          while (true)
          {
            delay(10); // do nothing, no point running without Ethernet hardware
          }
        }
        if (Ethernet.linkStatus() == LinkOFF)
        {
          Serial.println("Ethernet cable is not connected.");
        }
        // try to congifure using IP address instead of DHCP:
        Ethernet.begin(mac, ip, myDns);
      }
      else
      {
        Serial.print("  DHCP assigned IP ");
        Serial.println(Ethernet.localIP());
      }
      // give the Ethernet shield a second to initialize:
      delay(1000);

      ThingSpeak.begin(client);  // Initialize ThingSpeak
    }

    void loop()
    {
      h = dht.readHumidity();
      {
        tempC = get_temperature(sensorPin);
      }

      if (tempC < 31)
      {
        lcd.setCursor(0, 0);
        lcd.print(tempC);
        lcd.print(" "); //print the temp
        lcd.print((char)223); // to get ° symbol
        lcd.print("C");
        lcd.print(" ");
        lcd.print(h);
        lcd.print("%");
        delay(750);
      }
      else if (tempC > 31)
      {
        lcd.setCursor(0, 0);
        lcd.print(tempC);
        lcd.print(" "); //print the temp
        lcd.print((char)223); // to get ° symbol
        lcd.print("C");
        lcd.print(" ");
        lcd.print(h);
        lcd.print("%");
        delay(750);
      }

       int myBPM = pulseSensor.getBeatsPerMinute();  // Calls function on our pulseSensor object that returns BPM as an "int".
      // "myBPM" hold this BPM value now.

      if (pulseSensor.sawStartOfBeat()) 
      {   
        lcd.setCursor(0,1);
        lcd.print("BPM:");                        // Print phrase "BPM: "
        lcd.println(myBPM);                        // Print the value inside of myBPM.
        lcd.print("    ");
        delay(100);
      }

      // Write to ThingSpeak channel.
      ThingSpeak.setField(1, tempC);
      ThingSpeak.setField(2, h);
      ThingSpeak.setField(3, myBPM);

      int x = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
      if (x == 200)
      {
        Serial.println("Channel update successful.");

      }
      else
      {
        Serial.println("Problem updating channel. HTTP error code " + String(x));
      }

    }
...