0

The code below, as it stands, works correctly. It connects to a local server (so Token and everything else is no problem sharing) and transmits the data via post.

However, when I uncomment the lines to add a diplay (// # include ), just adding the library, the code stops passing.

The console message is this:

The sketch uses 12010 bytes (37%) of program storage space. The maximum is 32256 bytes. Global variables use 806 bytes (39%) of dynamic memory, leaving 1242 bytes for local variables. The maximum is 2048 bytes.

I don't know if it can be something from memory.

#include <SoftwareSerial.h>
#include <DHT.h>
//#include <Wire.h> 
//#include <LiquidCrystal_I2C.h>
#include <ArduinoJson.h>

SoftwareSerial esp8266(8, 9);
DHT dht(A2, DHT22);
//LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); 
//LiquidCrystal_I2C lcd(0x27); 

int pulseAccount;

void setup() {
  Serial.begin(9600);
  esp8266.begin(115200);
  dht.begin();
  //lcd.begin(16, 2);

  //Mensagem de inicialização
  //displayInfo("INICIANDO PCDH...", -1);

  setupESP8266(2000);
  setupSensorWaterFlow();

  connectToWiFi("PicoloAP", "14260822", 2000);
}

void loop() {
  String hydroponic_id = "5e1e7e8c87a8360eabfe415a";
  String server = "192.168.1.12";
  String path = "/api/v1/hydroponics_data?token=3c55299961ae724f54bb";
  String json;

  const size_t capacity = JSON_OBJECT_SIZE(10);
  DynamicJsonDocument root(capacity);

  root["hydroponic_id"] = hydroponic_id;
  root["humidity"] = dht.readHumidity();
  root["temperature"] = dht.readTemperature();
  root["waterflow"] =  getWaterFlow();
  root["ph"] = getPh();
  root["ce"] = 23.3;

  serializeJson(root, json);

  // Mostra dados no LCD
//  displayInfo("TEMP. AMBIENTE", dht.readTemperature());
//  displayInfo("UMIDADE", dht.readHumidity());
//  displayInfo("VAZAO", getWaterFlow());
//  displayInfo("PH", getPh());
//  displayInfo("CE", getCe());

  String request;
  //request.reserve(200);

  request = "POST " + path + " HTTP/1.1\r\n";
  request += "Host: " + server + "\r\n";
  request += "Accept: application/json\r\n";
  request += "Content-Length: ";
  request += json.length();
  request += "\r\n";
  request += "Content-Type: application/json\r\n";
  request += "\r\n";
  request += json;

  Serial.println("Antes");
  Serial.println(request);

  startTCPConnection(server, 2000);
  sendGetRequest(request, 2000);
  closeTCPConnection(2000);
}

// ================= ESP8266 ===================
String atCommand(String command, int timeout) {
  String response = "";
  esp8266.println(command);

  long int time = millis();

  while( (time+timeout) > millis() ) {
    while(esp8266.available()) {
      char c = esp8266.read();
      response += c;
    }
  }

  Serial.println(response);
  return response;
}

void setupESP8266(const int timeout) {
  atCommand("AT+RST", timeout);
  atCommand("AT+CWMODE=1", timeout);
}

void connectToWiFi(const String& ssid, const String& password, const int timeout) {
  String connect = "AT+CWJAP=\"" +ssid+"\",\"" + password + "\"";
  atCommand(connect, 6000);
  atCommand("AT+CIFSR", timeout);
}

void startTCPConnection(const String& server, const int timeout) {
  String connect = "AT+CIPSTART=\"TCP\",\"" + server + "\",3000";
  atCommand(connect, timeout);
}

void closeTCPConnection(const int timeout) {
  atCommand("AT+CIPCLOSE", timeout);
  delay(5000);
}

String sendGetRequest(const String& request, const int timeout) {
  Serial.println("Depois");
  Serial.println(request);
  atCommand("AT+CIPSEND=" + String(request.length()), timeout);
  String response = atCommand(request, 6000);
  return response;
}

// =================== Sensor de Fluxo ===================
void setupSensorWaterFlow(){
  pinMode(4, INPUT);
  attachInterrupt(digitalPinToInterrupt(4), pulseInc, RISING);  
}

float getWaterFlow(){
  pulseAccount = 0;
  //sei();
  delay (1000);
  //cli();
  float flow = pulseAccount / 5.5;
  return flow;
}

void pulseInc()  {
  pulseAccount++;
}

// =================== Sensor de PH ===================
float getPh(){
  auto measure = analogRead(A0);
  auto voltage = 5 / 1024.0 * measure; 
  auto ph = 7 + ((2.5 - voltage) / 0.18);
  return ph;
}

// =================== Sensor de CE ===================
float getCe(){
  // Implementar
  float ce = 23.3;
  return ce;
}

// =================== Diplay ===================
//void displayInfo(String Title, float info){
//  lcd.clear();
//  lcd.setCursor(0, 0);
//  lcd.print(Title);
//  lcd.setCursor(0, 1);
//  if (info != -1 ){
//    lcd.print(info);
//  }
//  delay(4000);
//}
  • 2
    What LCD_I2C library do you use? There are plenty of them and most of them differ in their constructor method. What do you mean with the code stops passing. Are there compiler errors or warnings? I don't see a memory problem. You can scroll up the console message, perhaps there is an error shown in the output. – Peter Paul Kiefer Jan 17 at 13:04
  • Please clarify your question and clearly explain what problem you are trying to solve. The compiler message you show indicates successful compilation of the code and contains no error messages. – StarCat Jan 22 at 16:29

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.