Advertisement
hms11

ESP32ScaleSwitch0.4.1

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