Advertisement
hms11

ESP32ScaleSwitch0.3.9

Dec 6th, 2023
19
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.88 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.9"
  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. // Find the position of the space in the string
  124. int spaceIndex = receivedWeight.indexOf(' ');
  125. sentWeight = receivedWeight.substring(0, spaceIndex);
  126. for (int i = 0; i < sentWeight.length(); i++) {
  127. if (!isdigit(sentWeight[i])) {
  128. sentWeight.remove(i, 1);
  129. i--; // Adjust index after removal
  130. }
  131. }
  132. convertedWeight = sentWeight.toInt();
  133. receivedWeight.clear();
  134. }
  135. }
  136.  
  137. void realScaleSwitch() {
  138. realSwitch = digitalRead(realSwitchPin);
  139. if (realSwitch == 0) {
  140. scaleSwitchOn = true;
  141. }
  142. else if (realSwitch == 1) {
  143. scaleSwitchOn = false;
  144. }
  145. }
  146.  
  147.  
  148. void switchState() {
  149. if ((blynkScaleSwitchOn) || (scaleSwitchOn)) {
  150. if (!scaleRun) {
  151. scaleOff = false;
  152. scaleStart = true;
  153. }
  154. }
  155. if ((!scaleSwitchOn) && (!blynkScaleSwitchOn)) {
  156. scaleOff = true;
  157. }
  158. }
  159.  
  160.  
  161. void batteryMonitor() {
  162. adc_read = analogRead(batteryPin);
  163. batteryVoltage = (adc_read * 0.003241);
  164. batteryPercent = map(adc_read, 3708, 3920, 0, 100);
  165. }
  166.  
  167. void scaleOperation() {
  168. rs232Comm();
  169. oledDisplay();
  170. batteryMonitor();
  171. realScaleSwitch();
  172. switchState();
  173. if (scaleStart) {
  174. scaleBoot = true;
  175. digitalWrite(scaleMOSFET, HIGH);
  176. timer.setTimeout(30000, []()
  177. {
  178. scaleStart = false;
  179. scaleBoot = false;
  180. scaleRun = true;
  181. digitalWrite(scaleDisplayMOSFET, HIGH);
  182. digitalWrite(output3, HIGH);
  183. });
  184. }
  185. else if (scaleOff) {
  186. scaleRun = false;
  187. digitalWrite(scaleDisplayMOSFET, LOW);
  188. digitalWrite(scaleMOSFET, LOW);
  189. digitalWrite(output3, LOW);
  190. }
  191. }
  192.  
  193. void blynkData() {
  194. display.clearDisplay();
  195. if (scaleOff) {
  196. Blynk.virtualWrite(V1, LOW);
  197. Blynk.virtualWrite(V2, LOW);
  198. Blynk.virtualWrite(V12, LOW);
  199. Blynk.virtualWrite(V13, HIGH);
  200. }
  201. else if (scaleBoot) {
  202. Blynk.virtualWrite(V1, HIGH);
  203. Blynk.virtualWrite(V13, LOW);
  204. }
  205. else if (scaleRun) {
  206.  
  207. Blynk.virtualWrite(V1, LOW);
  208. Blynk.virtualWrite(V2, HIGH);
  209. Blynk.virtualWrite(V12, HIGH);
  210. }
  211.  
  212. Blynk.virtualWrite(V5, realSwitch);
  213. Blynk.virtualWrite(V9, adc_read);
  214. Blynk.virtualWrite(V4, batteryVoltage);
  215. Blynk.virtualWrite(V3, batteryPercent);
  216. Blynk.virtualWrite(V16, convertedWeight);
  217. Blynk.virtualWrite(V10, sentWeight);
  218. }
  219.  
  220.  
  221. BLYNK_WRITE (V0) {
  222. if (param.asInt() == 0) {
  223. blynkScaleSwitchOn = false;
  224. }
  225. else if (param.asInt() == 1) {
  226. blynkScaleSwitchOn = true;
  227. }
  228. }
  229.  
  230.  
  231.  
  232.  
  233.  
  234.  
  235. void loop() {
  236. BlynkEdgent.run();
  237. scaleOperation();
  238. timer.run();
  239. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement