Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Fill-in information from your Blynk Template here
- #define BLYNK_TEMPLATE_ID "TMPLiTcSd8qR"
- #define BLYNK_TEMPLATE_NAME "ScaleSwitch"
- #define BLYNK_FIRMWARE_VERSION "0.3.5"
- #define BLYNK_PRINT Serial
- //#define BLYNK_DEBUG
- #define APP_DEBUG
- // Uncomment your board, or configure a custom board in Settings.h
- //#define USE_WROVER_BOARD
- //#define USE_TTGO_T7
- #include "BlynkEdgent.h"
- #include <Wire.h>
- #include <Adafruit_GFX.h>
- #include <Adafruit_SSD1306.h>
- #define SCREEN_WIDTH 128 // OLED display width, in pixels
- #define SCREEN_HEIGHT 64 // OLED display height, in pixels
- #define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
- #define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
- Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
- WidgetLED led1(V1);
- WidgetLED led2(V2);
- WidgetLED led3(V12);
- WidgetLED led4(V13);
- BlynkTimer timer;
- char receivedChar;
- String receivedWeight = "";
- int convertedWeight;
- int realSwitch = 0;
- const int realSwitchPin = 27;
- const int batteryPin = 36;
- int adc_read = 0;
- float batteryVoltage = 0;
- int batteryPercent = 0;
- bool scaleSwitchOn = false;
- bool blynkScaleSwitchOn = false;
- bool scaleStart = false;
- bool scaleBoot = false;
- bool scaleRun = false;
- bool scaleOff = false;
- const int output1 = 4;
- const int scaleDisplayMOSFET = 16;
- const int output3 = 17;
- const int scaleMOSFET = 18;
- void setup()
- {
- Serial.begin(115200);
- Serial2.begin(9600, SERIAL_8N1, 13, 14); // RX pin: 17, TX pin: 16
- Wire.begin(32, 33);
- display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // Address 0x3C for 128x64
- display.setTextColor(SSD1306_WHITE); // Draw white text
- display.clearDisplay();
- display.display();
- delay(100);
- BlynkEdgent.begin();
- pinMode(realSwitchPin, INPUT);
- pinMode(batteryPin, INPUT);
- pinMode(scaleMOSFET, OUTPUT);
- pinMode(output1, OUTPUT);
- pinMode(output3, OUTPUT);
- pinMode(scaleDisplayMOSFET, OUTPUT);
- digitalWrite(output1, HIGH);
- digitalWrite(scaleDisplayMOSFET, LOW);
- digitalWrite(scaleMOSFET, LOW);
- digitalWrite(output3, LOW);
- Blynk.syncAll();
- Blynk.virtualWrite(V2, LOW);
- timer.setInterval(750,blynkData);
- }
- void oledDisplay() {
- if (scaleRun) {
- display.setTextSize(2);
- display.setCursor(0,0); // Start at top-left corner
- display.println(F("BATTERY:"));
- display.drawLine(0, 20, display.width()-45, 20, SSD1306_WHITE);
- display.setTextSize(2);
- display.setCursor(1,25);
- display.print(batteryVoltage);
- display.setCursor(100,30);
- display.println(F("V"));
- display.setCursor(1,45);
- display.setTextSize(2);
- display.print(batteryPercent);
- display.setCursor(100,50);
- display.println(F("%"));
- display.display();
- }
- else if (scaleOff) {
- display.clearDisplay();
- display.display();
- }
- else if (scaleBoot) {
- display.setTextSize(2);
- display.setCursor(30,20); // Start at top-left corner
- display.println(F("SCALE"));
- display.setCursor(15,40); // Start at top-left corner
- display.println(F("STARTING"));
- display.display();
- }
- }
- void rs232Comm() {
- while (Serial2.available() > 0) {
- // Read the incoming byte
- receivedChar = Serial2.read();
- receivedWeight += receivedChar;
- }
- convertedWeight = receivedWeight.toInt();
- receivedWeight.clear();
- }
- void realScaleSwitch() {
- realSwitch = digitalRead(realSwitchPin);
- if (realSwitch == 0) {
- scaleSwitchOn = true;
- }
- else if (realSwitch == 1) {
- scaleSwitchOn = false;
- }
- }
- void switchState() {
- if ((blynkScaleSwitchOn) || (scaleSwitchOn)) {
- if (!scaleRun) {
- scaleOff = false;
- scaleStart = true;
- }
- }
- if ((!scaleSwitchOn) && (!blynkScaleSwitchOn)) {
- scaleOff = true;
- }
- }
- void batteryMonitor() {
- adc_read = analogRead(batteryPin);
- batteryVoltage = (adc_read * 0.003241);
- batteryPercent = map(adc_read, 3708, 3920, 0, 100);
- }
- void scaleOperation() {
- rs232Comm();
- oledDisplay();
- batteryMonitor();
- realScaleSwitch();
- switchState();
- if (scaleStart) {
- scaleBoot = true;
- digitalWrite(scaleMOSFET, HIGH);
- timer.setTimeout(30000, []()
- {
- scaleStart = false;
- scaleBoot = false;
- scaleRun = true;
- digitalWrite(scaleDisplayMOSFET, HIGH);
- digitalWrite(output3, HIGH);
- });
- }
- else if (scaleOff) {
- scaleRun = false;
- digitalWrite(scaleDisplayMOSFET, LOW);
- digitalWrite(scaleMOSFET, LOW);
- digitalWrite(output3, LOW);
- }
- }
- void blynkData() {
- display.clearDisplay();
- if (scaleOff) {
- Blynk.virtualWrite(V1, LOW);
- Blynk.virtualWrite(V2, LOW);
- Blynk.virtualWrite(V12, LOW);
- Blynk.virtualWrite(V13, HIGH);
- }
- else if (scaleBoot) {
- Blynk.virtualWrite(V1, HIGH);
- Blynk.virtualWrite(V13, LOW);
- }
- else if (scaleRun) {
- Blynk.virtualWrite(V1, LOW);
- Blynk.virtualWrite(V2, HIGH);
- Blynk.virtualWrite(V12, HIGH);
- }
- Blynk.virtualWrite(V5, realSwitch);
- Blynk.virtualWrite(V9, adc_read);
- Blynk.virtualWrite(V4, batteryVoltage);
- Blynk.virtualWrite(V3, batteryPercent);
- Blynk.virtualWrite(V16, convertedWeight);
- }
- BLYNK_WRITE (V0) {
- if (param.asInt() == 0) {
- blynkScaleSwitchOn = false;
- }
- else if (param.asInt() == 1) {
- blynkScaleSwitchOn = true;
- }
- }
- void loop() {
- BlynkEdgent.run();
- scaleOperation();
- timer.run();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement