Advertisement
hms11

ESP32ScaleSwitch0.3.4

Dec 4th, 2023
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.42 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.4"
  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 convertedWeight;
  38. int realSwitch = 0;
  39. const int realSwitchPin = 27;
  40. const int batteryPin = 36;
  41. int adc_read = 0;
  42. float batteryVoltage = 0;
  43. int batteryPercent = 0;
  44. bool scaleSwitchOn = false;
  45. bool blynkScaleSwitchOn = false;
  46. bool scaleStart = false;
  47. bool scaleBoot = false;
  48. bool scaleRun = false;
  49. bool scaleOff = false;
  50.  
  51. const int output1 = 4;
  52. const int scaleDisplayMOSFET = 16;
  53. const int output3 = 17;
  54. const int scaleMOSFET = 18;
  55.  
  56.  
  57. void setup()
  58. {
  59. Serial.begin(115200);
  60. Serial2.begin(9600, SERIAL_8N1, 14, 13); // RX pin: 17, TX pin: 16
  61. Wire.begin(32, 33);
  62. display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // Address 0x3C for 128x64
  63. display.setTextColor(SSD1306_WHITE); // Draw white text
  64. display.clearDisplay();
  65. display.display();
  66. delay(100);
  67. BlynkEdgent.begin();
  68.  
  69. pinMode(realSwitchPin, INPUT);
  70. pinMode(batteryPin, INPUT);
  71. pinMode(scaleMOSFET, OUTPUT);
  72. pinMode(output1, OUTPUT);
  73. pinMode(output3, OUTPUT);
  74. pinMode(scaleDisplayMOSFET, OUTPUT);
  75.  
  76. digitalWrite(output1, HIGH);
  77. digitalWrite(scaleDisplayMOSFET, LOW);
  78. digitalWrite(scaleMOSFET, LOW);
  79. digitalWrite(output3, LOW);
  80. Blynk.syncAll();
  81. Blynk.virtualWrite(V2, LOW);
  82. timer.setInterval(750,blynkData);
  83. }
  84.  
  85. void oledDisplay() {
  86. if (scaleRun) {
  87. display.setTextSize(2);
  88. display.setCursor(0,0); // Start at top-left corner
  89. display.println(F("BATTERY:"));
  90. display.drawLine(0, 20, display.width()-45, 20, SSD1306_WHITE);
  91. display.setTextSize(2);
  92. display.setCursor(1,25);
  93. display.print(batteryVoltage);
  94. display.setCursor(100,30);
  95. display.println(F("V"));
  96. display.setCursor(1,45);
  97. display.setTextSize(2);
  98. display.print(batteryPercent);
  99. display.setCursor(100,50);
  100. display.println(F("%"));
  101. display.display();
  102. }
  103. else if (scaleOff) {
  104. display.clearDisplay();
  105. display.display();
  106. }
  107. else if (scaleBoot) {
  108. display.setTextSize(2);
  109. display.setCursor(30,20); // Start at top-left corner
  110. display.println(F("SCALE"));
  111. display.setCursor(15,40); // Start at top-left corner
  112. display.println(F("STARTING"));
  113. display.display();
  114. }
  115. }
  116.  
  117. void rs232Comm() {
  118. if (Serial2.available()) {
  119. // Read the incoming byte
  120. receivedWeight = Serial2.read();
  121. convertedWeight = atoi(&receivedWeight);
  122. }
  123. }
  124.  
  125. void realScaleSwitch() {
  126. realSwitch = digitalRead(realSwitchPin);
  127. if (realSwitch == 0) {
  128. scaleSwitchOn = true;
  129. }
  130. else if (realSwitch == 1) {
  131. scaleSwitchOn = false;
  132. }
  133. }
  134.  
  135.  
  136. void switchState() {
  137. if ((blynkScaleSwitchOn) || (scaleSwitchOn)) {
  138. if (!scaleRun) {
  139. scaleOff = false;
  140. scaleStart = true;
  141. }
  142. }
  143. if ((!scaleSwitchOn) && (!blynkScaleSwitchOn)) {
  144. scaleOff = true;
  145. }
  146. }
  147.  
  148.  
  149. void batteryMonitor() {
  150. adc_read = analogRead(batteryPin);
  151. batteryVoltage = (adc_read * 0.003241);
  152. batteryPercent = map(adc_read, 3708, 3920, 0, 100);
  153. }
  154.  
  155. void scaleOperation() {
  156. rs232Comm();
  157. oledDisplay();
  158. batteryMonitor();
  159. realScaleSwitch();
  160. switchState();
  161. if (scaleStart) {
  162. scaleBoot = true;
  163. digitalWrite(scaleMOSFET, HIGH);
  164. timer.setTimeout(30000, []()
  165. {
  166. scaleStart = false;
  167. scaleBoot = false;
  168. scaleRun = true;
  169. digitalWrite(scaleDisplayMOSFET, HIGH);
  170. digitalWrite(output3, HIGH);
  171. });
  172. }
  173. else if (scaleOff) {
  174. scaleRun = false;
  175. digitalWrite(scaleDisplayMOSFET, LOW);
  176. digitalWrite(scaleMOSFET, LOW);
  177. digitalWrite(output3, LOW);
  178. }
  179. }
  180.  
  181. void blynkData() {
  182. display.clearDisplay();
  183. if (scaleOff) {
  184. Blynk.virtualWrite(V1, LOW);
  185. Blynk.virtualWrite(V2, LOW);
  186. Blynk.virtualWrite(V12, LOW);
  187. Blynk.virtualWrite(V13, HIGH);
  188. }
  189. else if (scaleBoot) {
  190. Blynk.virtualWrite(V1, HIGH);
  191. Blynk.virtualWrite(V13, LOW);
  192. }
  193. else if (scaleRun) {
  194.  
  195. Blynk.virtualWrite(V1, LOW);
  196. Blynk.virtualWrite(V2, HIGH);
  197. Blynk.virtualWrite(V12, HIGH);
  198. }
  199.  
  200. Blynk.virtualWrite(V5, realSwitch);
  201. Blynk.virtualWrite(V9, adc_read);
  202. Blynk.virtualWrite(V4, batteryVoltage);
  203. Blynk.virtualWrite(V3, batteryPercent);
  204. Blynk.virtualWrite(V16, convertedWeight);
  205.  
  206. }
  207.  
  208.  
  209. BLYNK_WRITE (V0) {
  210. if (param.asInt() == 0) {
  211. blynkScaleSwitchOn = false;
  212. }
  213. else if (param.asInt() == 1) {
  214. blynkScaleSwitchOn = true;
  215. }
  216. }
  217.  
  218.  
  219.  
  220.  
  221.  
  222.  
  223. void loop() {
  224. BlynkEdgent.run();
  225. scaleOperation();
  226. timer.run();
  227. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement