Load Cell and Wifi Integration

Hello all,

I am attempting to hook up 5 load cells, a thermocouple, a RTC clock, a microusb (used for datalogging), an ESP8266 Wifi module and a 4x20 LCD screen on an arduino MEGA.

I have gotten the various aspects working, and now its time for full integration. I apologize in advance for throwing so much at you all at once.

What seems to not be working now, is that I cannot get the Serial monitor stream to pass into my "Sendcommand" function, which essentially passes a string (comma delimited) through ESP to an online service which logs the data over wifi as well. I have tried Stream &Serial1, but then it throws errors that Serial1 is not defined in this scope for both the void loop, and void setup (Where it is actually declared).

Posed is my ENTIRE code [in two parts, max post char count ](Again sorry for so much at once, and its probably atrocious).

Thank you so so much for any help, I've been working on this for months...

#include "HX711.h"
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
#include "max6675.h"
#include <SD.h>
#include <SPI.h>
#include <DS3231.h>
#include <Adafruit_ssd1306syp.h>

//Calibration constant for load cell 1-5 - This value is obtained using the SparkFun_HX711_Calibration sketch
#define calibration_factor1 -7050.0
#define calibration_factor2 -7050.0
#define calibration_factor3 -7050.0
#define calibration_factor4 -7050.0
#define calibration_factor5 -7050.0

//Pin definitions
#define LoadCellFiveDOUT  11
#define LoadCellFiveCLK  10
#define LoadCellFourDOUT 9
#define LoadCellFourCLK 8  
#define LoadCellThreeDOUT  7
#define LoadCellThreeCLK  6
#define LoadCellTwoDOUT  5
#define LoadCellTwoCLK  4
#define LoadCellOneDOUT  3
#define LoadCellOneCLK  2

//Load cell object allocations
HX711 LoadCellOne(LoadCellOneDOUT, LoadCellOneCLK);
HX711 LoadCellTwo(LoadCellTwoDOUT, LoadCellTwoCLK);
HX711 LoadCellThree(LoadCellThreeDOUT, LoadCellThreeCLK);
HX711 LoadCellFour(LoadCellFourDOUT, LoadCellFourCLK);
HX711 LoadCellFive(LoadCellFiveDOUT, LoadCellFiveCLK);

//LCD Object
LiquidCrystal_I2C lcd(0x27,20,21);  // set the LCD address to 0x27 for a 16 chars and 2 line display

//OLED Object
Adafruit_ssd1306syp display(37,35);
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET

//Wifi Access Point Name and Password
String AP = "temp";       // CHANGE ME
String PASS = "temp"; // CHANGE ME
int counter = 0;

//Counting Variables for wifi processing
int countTrueCommand;
int countTimeCommand; 
boolean found = false; 

String API = "temp";   // CHANGE ME
String HOST = "api.thingspeak.com";
String PORT = "80";
String field1 = "field1";
String field2 = "field2";
String field3 = "field3";
String field4 = "field4";
String field5 = "field5";
String field6 = "field6";
String field7 = "field7";

//Water value variable
int watervalue = 0;

//Water level variable
int adc_id = 0;

//Clock object
DS3231 rtc(A4, A5);

//Pin allocation for thermocouple
int ktcSO = 49;
int ktcCS = 47;
int ktcSCK = 45;

//Constant for sd card
const int chipSelect = 53;

//Thermocouple object
MAX6675 ktc(ktcSCK, ktcCS, ktcSO);

void setup() {
  
  //wifi initialization
  Serial1.begin(115200);
  sendCommand("AT",5,"OK");
  sendCommand("AT+CWMODE=1",5,"OK");
  sendCommand("AT+CWJAP=\""+ AP +"\",\""+ PASS +"\"",20,"OK");
  
  //Initializes LCD
  lcd.init();
  lcd.backlight();

  //Initializes OLED
  display.initialize();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  
  //Initializes SD Card
  pinMode(chipSelect, OUTPUT);

  //Starts day and time clock
  rtc.begin();
  
//Initializes date and time - CHANGE IF CLOCK IS OFF, LOAD PROGRAM WITH THIS UNCOMMENTED (update day of week, time and date fields), THEN COMMENT OUT AGAIN
//---------------------------
  //rtc.setDOW(WEDNESDAY);
  //rtc.setTime(14,06,20);
  //rtc.setDate(3,1,2019);
//---------------------------

  //Sets load cell calibrations and tares
  LoadCellOne.set_scale(calibration_factor1); //This value is obtained by using the SparkFun_HX711_Calibration sketch
  LoadCellOne.tare(); //Assuming there is no weight on the scale at start up, reset the scale to 0
  LoadCellTwo.set_scale(calibration_factor2); //This value is obtained by using the SparkFun_HX711_Calibration sketch
  LoadCellTwo.tare(); //Assuming there is no weight on the scale at start up, reset the scale to 0
  LoadCellThree.set_scale(calibration_factor3); //This value is obtained by using the SparkFun_HX711_Calibration sketch
  LoadCellThree.tare(); //Assuming there is no weight on the scale at start up, reset the scale to 0
  LoadCellFour.set_scale(calibration_factor4); //This value is obtained by using the SparkFun_HX711_Calibration sketch
  LoadCellFour.tare(); //Assuming there is no weight on the scale at start up, reset the scale to 0
  LoadCellFive.set_scale(calibration_factor5); //This value is obtained by using the SparkFun_HX711_Calibration sketch
  LoadCellFive.tare(); //Assuming there is no weight on the scale at start up, reset the scale to 0
  delay(500);
}

void loop() {
  
  //Clears OLED display
  display.clear();
  
  //Make a string for assembling the data to log to SD:
  String dataString = "";

//-------------------------------Wifi Status------------------------------------------

  //String displaying wifi status
  String wificonnected = "";

  if(Serial1)
  {
    String wificonnected = "Wifi Connected.";
  }
  else
  {
    String wificonnected = "Wifi Disconnected.";
  }

  //OLED Display Wifi Status
  display.setCursor(0,0);
  display.println(wificonnected);
  display.update();

//----------------------------------Date & Time----------------------------------------

  //Sets position on LCD and prints date and time
  lcd.setCursor(9,4);
  lcd.print(rtc.getDateStr());
  lcd.setCursor(0,4); 
  lcd.print(rtc.getTimeStr());

  //Adds date and time to datafile string for SD Card
  dataString += String(rtc.getDateStr());
  dataString += " -- ";
  dataString += String(rtc.getTimeStr());
  dataString += ",";

//-------------------------------------Load Cell 1--------------------------------------

  //Sets position and prints load cell 1 value to LCD
  lcd.setCursor(0,1);
  lcd.print("1: ");
  lcd.print(LoadCellOne.get_units(), 1);

  //Adds load cell value to datafile string for SD Card
  float LoadCellOneReading = LoadCellOne.get_units();
  dataString += String(LoadCellOneReading);
  dataString += ",";

  //Adds load cell 1 value to thingspeak field 1
  String getDataLoad1 = "GET /update?api_key="+ API +"&"+ field1 +"="+LoadCellOneReading;
  sendCommand("AT+CIPMUX=1",5,"OK");
  sendCommand("AT+CIPSTART=0,\"TCP\",\""+ HOST +"\","+ PORT,15,"OK");
  sendCommand("AT+CIPSEND=0," +String(getDataLoad1.length()+4),4,">");
  Serial1.println(getDataLoad1);countTrueCommand++;
  sendCommand("AT+CIPCLOSE=0",5,"OK");

//-------------------------------------Load Cell 2--------------------------------------

  //Sets position and prints load cell 2 value
  lcd.setCursor(9,1);
  lcd.print("2: ");
  lcd.print(LoadCellTwo.get_units(), 1);

  //Adds load cell value to datafile string
  float LoadCellTwoReading = LoadCellTwo.get_units();
  dataString += String(LoadCellTwoReading);
  dataString += ",";

  //Adds Load cell 2 value to thingspeak field 2
  String getDataLoad2 = "GET /update?api_key="+ API +"&"+ field2 +"="+LoadCellTwoReading;
  sendCommand("AT+CIPMUX=1",5,"OK");
  sendCommand("AT+CIPSTART=0,\"TCP\",\""+ HOST +"\","+ PORT,15,"OK");
  sendCommand("AT+CIPSEND=0," +String(getDataLoad2.length()+4),4,">");
  Serial1.println(getDataLoad2);countTrueCommand++;
  sendCommand("AT+CIPCLOSE=0",5,"OK");
// NEXT PART OF CODE ATTACHED IN NEXT POST

This is a continuation of the code from last post (character limit)

//-------------------------------------Load Cell 3--------------------------------------

  //Sets position and prints load cell 3 value to LCD
  lcd.setCursor(0,2);
  lcd.print("3: ");
  lcd.print(LoadCellThree.get_units(), 1);

  //Adds load cell value to datafile string for SD Card
  float LoadCellThreeReading = LoadCellThree.get_units();
  dataString += String(LoadCellThreeReading);
  dataString += ",";

  //Adds load cell 3 value to thingspeak field 3
  String getDataLoad3 = "GET /update?api_key="+ API +"&"+ field3 +"="+LoadCellThreeReading;
  sendCommand("AT+CIPMUX=1",5,"OK");
  sendCommand("AT+CIPSTART=0,\"TCP\",\""+ HOST +"\","+ PORT,15,"OK");
  sendCommand("AT+CIPSEND=0," +String(getDataLoad3.length()+4),4,">");
  Serial1.println(getDataLoad3);countTrueCommand++;
  sendCommand("AT+CIPCLOSE=0",5,"OK");

//-------------------------------------Load Cell 4--------------------------------------

  //Sets position and prints load cell 4 value to LCD
  lcd.setCursor(9,2);
  lcd.print("4: ");
  lcd.print(LoadCellFour.get_units(), 1);

  //Adds load cell value to datafile string for SD Card
  float LoadCellFourReading = LoadCellFour.get_units();
  dataString += String(LoadCellFourReading);
  dataString += ",";

  //Adds load cell 4 value to thingspeak field 4
  String getDataLoad4 = "GET /update?api_key="+ API +"&"+ field4 +"="+LoadCellFourReading;
  sendCommand("AT+CIPMUX=1",5,"OK");
  sendCommand("AT+CIPSTART=0,\"TCP\",\""+ HOST +"\","+ PORT,15,"OK");
  sendCommand("AT+CIPSEND=0," +String(getDataLoad4.length()+4),4,">");
  Serial1.println(getDataLoad4);countTrueCommand++;
  sendCommand("AT+CIPCLOSE=0",5,"OK");

//-------------------------------------Load Cell 5--------------------------------------

  //Sets position and prints load cell 5 value to LCD
  lcd.setCursor(0,3);
  lcd.print("5: ");
  lcd.print(LoadCellFive.get_units(), 1);

  //Adds load cell value to datafile string for SD Card
  float LoadCellFiveReading = LoadCellFive.get_units();
  dataString += String(LoadCellFiveReading);
  dataString += ",";

  //Adds load cell 5 value to thingspeak field 5
  String getDataLoad5 = "GET /update?api_key="+ API +"&"+ field5 +"="+LoadCellFiveReading;
  sendCommand("AT+CIPMUX=1",5,"OK");
  sendCommand("AT+CIPSTART=0,\"TCP\",\""+ HOST +"\","+ PORT,15,"OK");
  sendCommand("AT+CIPSEND=0," +String(getDataLoad5.length()+4),4,">");
  Serial1.println(getDataLoad5);countTrueCommand++;
  sendCommand("AT+CIPCLOSE=0",5,"OK");

//--------------------------------Water Level------------------------------------------

  //Gets water level and prints to LCD
  float value = analogRead(adc_id); // get adc value
  lcd.setCursor(0,0);
  lcd.print("W: ");
  lcd.print(watervalue);

  //Adds water level to datafile string
  dataString += String(watervalue);
  dataString += ",";

  //Adds water level to thingspeak field 6
  String getDataWater = "GET /update?api_key="+ API +"&"+ field6 +"="+watervalue;
  sendCommand("AT+CIPMUX=1",5,"OK");
  sendCommand("AT+CIPSTART=0,\"TCP\",\""+ HOST +"\","+ PORT,15,"OK");
  sendCommand("AT+CIPSEND=0," +String(getDataWater.length()+4),4,">");
  Serial1.println(getDataWater);countTrueCommand++;
  sendCommand("AT+CIPCLOSE=0",5,"OK");

//----------------------------Tempearture-----------------------------------------------

  //Sets position and prints temperature in celsius
  lcd.setCursor(9,0);
  lcd.print("C: ");
  lcd.print(ktc.readCelsius());

  //Adds temperature to datafile string
  int ktcread = ktc.readCelsius();
  dataString += String(ktcread);

  //Adds temperature to thingspeak field 7
  String getDataTemp = "GET /update?api_key="+ API +"&"+ field7 +"="+ktcread;
  sendCommand("AT+CIPMUX=1",5,"OK");
  sendCommand("AT+CIPSTART=0,\"TCP\",\""+ HOST +"\","+ PORT,15,"OK");
  sendCommand("AT+CIPSEND=0," +String(getDataTemp.length()+4),4,">");
  Serial1.println(getDataTemp);countTrueCommand++;
  sendCommand("AT+CIPCLOSE=0",5,"OK");

//--------------------------------------SD Card Writing----------------------------------

  //Open datafile 
  File dataFile = SD.open("DataLog.txt", FILE_WRITE);

  //If the datafile is available, write to it:
  if (dataFile) 
  {
    dataFile.println(dataString);
    dataFile.close();
  } 
  //If the file isn't open, pop up an error:
  else 
  {
    
  }
}

//------------------------------Function for sending wifi commands------------------------

void sendCommand(String command, int maxTime, char readReplay[], Stream &Serial1) {
  while(countTimeCommand < (maxTime*1))
  {
    Serial1.println(command);//at+cipsend
    if(Serial1.find(readReplay))//ok
    {
      found = true;
      break;
    }
  
    countTimeCommand++;
  }
  
  if(found == true)
  {
    countTrueCommand++;
    countTimeCommand = 0;
  }
  
  if(found == false)
  {
    countTrueCommand = 0;
    countTimeCommand = 0;
  }
  
  found = false;
 }

First, what board are you using? Does it really have another hardware serial port?

Next, you’re calling sendCommand() like this:

sendCommand("AT",5,"OK");

But, the functions signature is:

void sendCommand(String command, int maxTime, char readReplay[], Stream &Serial1);

They don’t match each other. There’s no need to pass Serial1 as a parameter. It’s globally defined (assuming, of course, that it exists on your board).

Finally, your code has a massive amounts of repetition because of this:

HX711 LoadCellOne(LoadCellOneDOUT, LoadCellOneCLK);
HX711 LoadCellTwo(LoadCellTwoDOUT, LoadCellTwoCLK);
HX711 LoadCellThree(LoadCellThreeDOUT, LoadCellThreeCLK);
HX711 LoadCellFour(LoadCellFourDOUT, LoadCellFourCLK);
HX711 LoadCellFive(LoadCellFiveDOUT, LoadCellFiveCLK);

You should instead instantiate an array of HX711 objects and iterate through them with ‘for’ loops.

Im using the MEGA 2560, and no it only has one hardware serial.

To your second point, I was playing around with Stream &Serial1, it was not part of my original code. I wasnt running into errors yet with the function call not matching its signature, I was first running into errors that stated that Serial1 was not defined in Void loop or Void setup. I only tried passing it to the function with 'Stream &Serial1' because it first was stating that it was not defined within the function.

My solution was trying to pass it to the function, which then I ran into errors about it not being declared in loop and setup as I had described.

Im super confused at this point.

(Thank you for also pointing out the repetitious nature of my code - its more my style as I am writing code, I tend to consolidate with loops once functionality is established - that likely seems stupid to most).

Thank you again for your time, I appreciate it greatly!

The Mega 2560 has 4 Hardware Serial Ports: Serial, Serial1, Serial2, Serial3.
Do you have the proper target board selected in the IDE?

Meaning for the COM Port? I believe so (I will check soon) - and I think this is a compiling error (not declared in this scope) more then a uploading error.

UPDATE: Checked and was actually on wrong COM - however still the same error is popping up.

reingoldm7:
Meaning for the COM Port?

No, I mean just what I said. Do you have the proper target board selected?

Compiles fine for me:

void setup() {
  Serial1.begin(115200);
  Serial1.println("Hello World");
}

void loop() {
}

Ah man, I feel stupid.

It's been a while since I've been working on this project - that was the fix...

Thank you very much!
I'm sure I'll have more questions about this project coming up soon...

Thank you!