0

I'm working on a school project and using Arduino Uno. The basic working method for the project is as follows:

  1. Read temperature from LM-35.
  2. Read CO2 level in air using MH-Z14A.
  3. Upload data to server via an HTTP request.

Now each piece of functionality above works fine when tested alone (If I only read temp it reads correctly. If I send data to database it gets sent correctly.) But when reading values and trying to send them to the database the HTTP request doesn't succeed. Also sometimes the Serial Monitor stops printing logs and the sketch stops executing.

I tried adding some delay between things but that didn't work also. Here is my code:

#include <SPI.h>
#include <Ethernet.h>

// Global Variables:
EthernetClient client; // Initialize the Ethernet client library.
const int cyclesLimit = 250;
int cycles = 0;
int temp = 0; // Initial value for temperature.
int co2Level = 0;

float getTemp(){
  const int TEMP_PIN = 0; // Analog.
  Serial.println("Reading Temperature...");
  float tempSensorRawReading = analogRead(TEMP_PIN); // Reading directly from sensor.
  Serial.print("Raw LM35 Reading in mV: ");
  Serial.println(tempSensorRawReading);
  float tempCelsius = (5.0 * tempSensorRawReading * 100.0) / 1024.0; // Convert LM35 reading into celsius.
  Serial.print("Celsius Temperature: ");
  Serial.println(tempCelsius);
  return tempCelsius;
}

int getCO2Level(){
  const int CO2_PIN = 1; // Analog.
  Serial.println("Reading CO2 Level...");
  int ppm = analogRead(CO2_PIN); // Reading directly from sensor.
  Serial.print("CO2 Level in ppm: ");
  Serial.println(ppm);
  return ppm;
}

void updateFirebaseData(int temp, int co2Level){
  const char server[] = "NAME-ADDRESS";
  const String query = String("QUERY-TEXT");
  // Call API.
  Serial.println("Sending request to Database...");
  if (client.connect(server, 80)) { // Default port for HTTP is 80.
    Serial.print("Connected to ");
    Serial.println(client.remoteIP());
    // Make an HTTP request:
    client.println(query);
    client.println("Host: NAME-ADDRESS");
    client.println("Connection: close");
    client.println();
    Serial.println("Data sent to database successfully.");
  } else {
    // If you didn't get a connection to the server:
    Serial.println("Connection failed. Database not updated.");
  }
}

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // Wait for serial port to connect. Needed for native USB port only.
  }

  byte mac[] = {0xE2, 0xBA, 0xFE, 0xAD, 0x16, 0x23}; // This should be unique for every chip.
  // Start the Ethernet connection:
  Serial.println("Initializing Ethernet with DHCP...");
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP.");
    if (Ethernet.hardwareStatus() == EthernetNoHardware) {
      Serial.println("Ethernet shield was not found.  Sorry, can't run without hardware.");
    } else if (Ethernet.linkStatus() == LinkOFF) {
      Serial.println("Ethernet cable is not connected.");
    }
    // No point in carrying on, so do nothing forevermore:
    while (true) {
      delay(1);
    }
  }
  // print your local IP address:
  Serial.print("IP address: ");
  Serial.println(Ethernet.localIP());
}

void loop() {
  cycles++;
  Serial.println(cycles);
  if(cycles >= cyclesLimit){
    Serial.println("Time is up...");
    temp = int(getTemp());
    co2Level = getCO2Level();
    updateFirebaseData(temp, co2Level);
    cycles = 0;
  }
}

I really find this so weird as every piece works alone but when combining things together everything gets messed up.

  • Try to use millis() function for manage delays between data send. Follow the example "Blink Without Delay" for using millis(). arduino.cc/en/tutorial/BlinkWithoutDelay – leoc7 Jan 17 at 11:53
  • Using a counter variable like "cycles" inside loop() function is not a good approach, because you're not able to control the time elapsed between data send. – leoc7 Jan 17 at 11:56
  • close() the client after sending data – Juraj Jan 17 at 12:51
  • 1
    and don't use String to build the request url – Juraj Jan 17 at 13:54
  • 2
    @Juraj I did it and it worked, the String object was taking a lot of memory and messed up everything, please post an answer so I can vote up for you. Thanks. – Ameer Taweel Jan 17 at 19:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.