#include "HX711.h"
#include <HX711_ADC.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <EEPROM.h>
#include "Ubidots.h" //Incluir librería Ubidots
#define EEPROM_SIZE 12
const char* UBIDOTS_TOKEN = "BBFF-Em2zlmR8XKaMMUYcdiQmnTbkSYkJlP" ; //TOKEN de Ubidots
const char* WIFI_SSID = "" ; //SSID Wi-Fi
const char* WIFI_PASS = "" ; //Contraseña de Wi-Fi
Ubidots ubidots(UBIDOTS_TOKEN, UBI_HTTP);
const int LOADCELL_DOUT_PIN = 19;
const int LOADCELL_SCK_PIN = 18;
const int calVal_eepromAdress = 0;
long t;
int lcdColumns = 16;
int lcdRows = 2;
//HX711 balanza;
LiquidCrystal_I2C lcd(0x27, lcdColumns, lcdRows);
HX711_ADC LoadCell(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
//WiFiClient client;
void setup() {
Serial.begin(115200);
lcd.init();
lcd.backlight();
if (!lcd.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
Serial.println(F("SSD1306 allocation failed"));
for (;;); // Don't proceed, loop forever
}
ubidots.wifiConnect(WIFI_SSID, WIFI_PASS);
ubidots.setDebug(true); // Descomenta esta línea para imprimir mensajes de depuración
LoadCell.begin();
float calibrationValue;// calibration value (see example file "Calibration.ino")
#if defined(ESP8266)|| defined(ESP32)
EEPROM.begin(EEPROM_SIZE); // uncomment this if you use ESP8266/ESP32 and want to fetch the calibration value from eeprom
#endif
EEPROM.write(calVal_eepromAdress, calibrationValue); // uncomment this if you want to fetch the calibration value from eeprom
long stabilizingtime = 2000; // preciscion right after power-up can be improved by adding a few seconds of stabilizing time
boolean _tare = true; //set this to false if you don't want tare to be performed in the next step
LoadCell.start(stabilizingtime, _tare);
if (LoadCell.getTareTimeoutFlag()) {
Serial.println("Timeout, check MCU>HX711 wiring and pin designations");
while (1);
}
else {
LoadCell.setCalFactor(420); // set calibration value (float)
Serial.println("Startup is complete");
}
}
void loop() {
static boolean newDataReady = 0;
const int serialPrintInterval = 0; //increase value to slow down serial print activity
// check for new data/start next conversion:
if (LoadCell.update()) newDataReady = true;
// get smoothed value from the dataset:
if (newDataReady) {
if (millis() > t + serialPrintInterval) {
float i = LoadCell.getData();
Serial.print("Load_cell output val: ");
Serial.println(i);
float w = 0.1; //peso de una sola pieza
lcd.clear(); //display.clearDisplay();
lcd.setCursor(1, 0); //display.setCursor(5, 0);
lcd.print("TOTAL: "); //display.print("TOTAL WEIGHT ");
lcd.setCursor(8, 0); //display.setCursor(10, 15);
lcd.print(i); //display.print(i);
lcd.print(" kg"); //display.print("gm");
lcd.display();
int k = i / w;
lcd.setCursor(1, 1); //display.setCursor(0, 30);
lcd.print("PIEZAS: "); //display.print("PIECES LEFT ");
lcd.setCursor(12, 1); //display.setCursor(30, 45);
lcd.print(k); // display.print(k);
//lcd.print(" Nos"); //display.print(" Nos");
lcd.display();
ubidots.add("Peso total", i);
ubidots.add("Piezas totales", k);
bool bufferSent = false;
bufferSent = ubidots.send(); // Se envian datos a ubidots (false/true)
if (bufferSent) {
//Do something if values were sent properly
Serial.println("Valores enviados al dispositivo");
}
delay(1000);
newDataReady = 0;
t = millis();
}
}
}