Advertisement
hms11

ESP32ScaleSwitch0.3.5

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