Advertisement
hms11

ESP32ScaleSwitch0.3.3

Dec 3rd, 2023
17
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.78 KB | None | 0 0
  1.  
  2. // Fill-in information from your Blynk Template here
  3. #define BLYNK_TEMPLATE_ID "TMPLiTcSd8qR"
  4. #define BLYNK_TEMPLATE_NAME "ScaleSwitch"
  5.  
  6. #define BLYNK_FIRMWARE_VERSION "0.3.3"
  7.  
  8. #define BLYNK_PRINT Serial
  9. //#define BLYNK_DEBUG
  10.  
  11. #define APP_DEBUG
  12.  
  13. // Uncomment your board, or configure a custom board in Settings.h
  14. //#define USE_WROVER_BOARD
  15. //#define USE_TTGO_T7
  16.  
  17. #include "BlynkEdgent.h"
  18. #include <Wire.h>
  19. #include <Adafruit_GFX.h>
  20. #include <Adafruit_SSD1306.h>
  21.  
  22. #define SCREEN_WIDTH 128 // OLED display width, in pixels
  23. #define SCREEN_HEIGHT 64 // OLED display height, in pixels
  24.  
  25. #define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
  26. #define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
  27. Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
  28.  
  29. WidgetLED led1(V1);
  30. WidgetLED led2(V2);
  31. WidgetLED led3(V12);
  32. WidgetLED led4(V13);
  33.  
  34. BlynkTimer timer;
  35.  
  36. char receivedWeight = 0;
  37. int realSwitch = 0;
  38. const int realSwitchPin = 27;
  39. const int batteryPin = 36;
  40. int adc_read = 0;
  41. float batteryVoltage = 0;
  42. int batteryPercent = 0;
  43. bool scaleSwitchOn = false;
  44. bool blynkScaleSwitchOn = false;
  45. bool scaleStart = false;
  46. bool scaleBoot = false;
  47. bool scaleRun = false;
  48. bool scaleOff = false;
  49.  
  50. const int output1 = 4;
  51. const int scaleDisplayMOSFET = 16;
  52. const int output3 = 17;
  53. const int scaleMOSFET = 18;
  54.  
  55.  
  56. void setup()
  57. {
  58. Serial.begin(115200);
  59. Serial2.begin(9600, SERIAL_8N1, 14, 13); // RX pin: 17, TX pin: 16
  60. Wire.begin(32, 33);
  61. display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // Address 0x3C for 128x64
  62. display.setTextSize(2);
  63. display.setTextColor(SSD1306_WHITE); // Draw white text
  64. display.clearDisplay();
  65. display.setCursor(0,0); // Start at top-left corner
  66. display.println(F("WEIGHT:"));
  67. display.drawLine(0, 20, display.width()-45, 20, SSD1306_WHITE);
  68. display.drawLine(0, 60, display.width()-1, 60, SSD1306_WHITE);
  69. delay(100);
  70. BlynkEdgent.begin();
  71.  
  72. pinMode(realSwitchPin, INPUT);
  73. pinMode(batteryPin, INPUT);
  74. pinMode(scaleMOSFET, OUTPUT);
  75. pinMode(output1, OUTPUT);
  76. pinMode(output3, OUTPUT);
  77. pinMode(scaleDisplayMOSFET, OUTPUT);
  78.  
  79. digitalWrite(output1, HIGH);
  80. digitalWrite(scaleDisplayMOSFET, LOW);
  81. digitalWrite(scaleMOSFET, LOW);
  82. digitalWrite(output3, LOW);
  83. Blynk.syncAll();
  84. Blynk.virtualWrite(V2, LOW);
  85. timer.setInterval(750,blynkData);
  86. }
  87.  
  88.  
  89. void rs232Comm() {
  90. if (Serial2.available()) {
  91. // Read the incoming byte
  92. receivedWeight = Serial2.read();
  93. }
  94. display.setTextSize(2);
  95. display.setCursor(1,40);
  96. display.write(receivedWeight);
  97. display.setTextSize(1);
  98. display.setCursor(105,45);
  99. display.println(F("LBS"));
  100. display.display();
  101. }
  102.  
  103. void realScaleSwitch() {
  104. realSwitch = digitalRead(realSwitchPin);
  105. if (realSwitch == 0) {
  106. scaleSwitchOn = true;
  107. }
  108. else if (realSwitch == 1) {
  109. scaleSwitchOn = false;
  110. }
  111. }
  112.  
  113.  
  114. void switchState() {
  115. if ((blynkScaleSwitchOn) || (scaleSwitchOn)) {
  116. if (!scaleRun) {
  117. scaleOff = false;
  118. scaleStart = true;
  119. }
  120. }
  121. if ((!scaleSwitchOn) && (!blynkScaleSwitchOn)) {
  122. scaleOff = true;
  123. }
  124. }
  125.  
  126.  
  127. void batteryMonitor() {
  128. adc_read = analogRead(batteryPin);
  129. batteryVoltage = (adc_read * 0.003241);
  130. batteryPercent = map(adc_read, 3708, 3920, 0, 100);
  131. }
  132.  
  133. void scaleOperation() {
  134. rs232Comm();
  135. batteryMonitor();
  136. realScaleSwitch();
  137. switchState();
  138. if (scaleStart) {
  139. scaleBoot = true;
  140. digitalWrite(scaleMOSFET, HIGH);
  141. timer.setTimeout(30000, []()
  142. {
  143. scaleStart = false;
  144. scaleBoot = false;
  145. scaleRun = true;
  146. digitalWrite(scaleDisplayMOSFET, HIGH);
  147. digitalWrite(output3, HIGH);
  148. });
  149. }
  150. else if (scaleOff) {
  151. scaleRun = false;
  152. digitalWrite(scaleDisplayMOSFET, LOW);
  153. digitalWrite(scaleMOSFET, LOW);
  154. digitalWrite(output3, LOW);
  155. }
  156. }
  157.  
  158. void blynkData() {
  159. if (scaleOff) {
  160. Blynk.virtualWrite(V1, LOW);
  161. Blynk.virtualWrite(V2, LOW);
  162. Blynk.virtualWrite(V12, LOW);
  163. Blynk.virtualWrite(V13, HIGH);
  164. }
  165. else if (scaleBoot) {
  166. Blynk.virtualWrite(V1, HIGH);
  167. Blynk.virtualWrite(V13, LOW);
  168. }
  169. else if (scaleRun) {
  170.  
  171. Blynk.virtualWrite(V1, LOW);
  172. Blynk.virtualWrite(V2, HIGH);
  173. Blynk.virtualWrite(V12, HIGH);
  174. }
  175.  
  176. Blynk.virtualWrite(V5, realSwitch);
  177. Blynk.virtualWrite(V9, adc_read);
  178. Blynk.virtualWrite(V4, batteryVoltage);
  179. Blynk.virtualWrite(V3, batteryPercent);
  180. Blynk.virtualWrite(V16, receivedWeight);
  181.  
  182. }
  183.  
  184.  
  185. BLYNK_WRITE (V0) {
  186. if (param.asInt() == 0) {
  187. blynkScaleSwitchOn = false;
  188. }
  189. else if (param.asInt() == 1) {
  190. blynkScaleSwitchOn = true;
  191. }
  192. }
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199. void loop() {
  200. BlynkEdgent.run();
  201. scaleOperation();
  202. timer.run();
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement