#include <Adafruit_GFX.h>
#include <Wire.h>
#include <Adafruit_SSD1306.h>
#include <TOTP.h>
#include <swRTC.h>
#include <HX711.h>
#include <qrcode.h>
String display_stuff ="{\"token\":\"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ3ZWlnaHQiOiIyIiwiaWF0IjoxNjczNjk5MTQxfQ.mXsG3XicF3txetpieWLVRPkJ1uXwVsVzTAivR9Gey0c\",\"dustbinId\":\"63c144b2df1d3f6b0826d25d\",\"timeStamp\":1673699141000}\"";
HX711 scale;
swRTC rtc;
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define PIN_TRIG 12
#define PIN_ECHO 11
#define DETECT_THRESHOLD 30
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
String id = "someuniqueid";
uint8_t hmacKey[] = {0x4d, 0x79, 0x4c, 0x65, 0x67, 0x6f, 0x44, 0x6f, 0x6f, 0x72};
TOTP totp = TOTP(hmacKey, 6);
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
rtc.setTime(0,0,0);
rtc.startRTC();
scale.begin(A1, A0);
scale.tare();
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
pinMode(PIN_TRIG, OUTPUT);
pinMode(PIN_ECHO, INPUT);
display.display();
delay(2000);
display.clearDisplay();
// Not all the characters will fit on the display. This is normal.
// Library will draw what it can and the rest will be clipped.
}
int previous_distance = 0;
double previous_weight = 0.0;
void loop() {
// put your main code here, to run repeatedly:
// Start a new measurement:
digitalWrite(PIN_TRIG, HIGH);
delayMicroseconds(10);
digitalWrite(PIN_TRIG, LOW);
// Read the result:
int duration = pulseIn(PIN_ECHO, HIGH);
Serial.print("Distance in CM: ");
Serial.println(duration / 58);
Serial.print("Distance in inches: ");
Serial.println(duration / 148);
int dist_cm = duration / 58;
double weight = scale.get_units();
Serial.println(weight);
if (dist_cm != previous_distance || weight != previous_weight) {
Serial.println("Something changed");
previous_weight = weight;
previous_distance = dist_cm;
QRCode qrcode;
uint8_t qrcodeData[qrcode_getBufferSize(7)];
char *otp = totp.getCode(rtc.getTimestamp(1970));
String encode = id + String(".") + String(otp) + String(".") + String(weight);
qrcode_initText(&qrcode, qrcodeData, 7, ECC_HIGH, display_stuff.c_str());
display.clearDisplay();
for (uint8_t y = 0; y < qrcode.size; y++) {
for (uint8_t x = 0; x < qrcode.size; x++) {
if (qrcode_getModule(&qrcode, x, y)) {
display.drawPixel(x, y, SSD1306_WHITE);
}
}
}
display.display();
delay(30000);
} else {
Serial.println("Nothing changed");
display.clearDisplay();
display.display();
}
delay(1000);
}