Advertisement
hms11

ShopScaleNoOTA

Nov 30th, 2022
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.69 KB | None | 0 0
  1. /*************************************************************
  2. Download latest Blynk library here:
  3. https://github.com/blynkkk/blynk-library/releases/latest
  4.  
  5. Blynk is a platform with iOS and Android apps to control
  6. Arduino, Raspberry Pi and the likes over the Internet.
  7. You can easily build graphic interfaces for all your
  8. projects by simply dragging and dropping widgets.
  9.  
  10. Downloads, docs, tutorials: http://www.blynk.cc
  11. Sketch generator: http://examples.blynk.cc
  12. Blynk community: http://community.blynk.cc
  13. Follow us: http://www.fb.com/blynkapp
  14. http://twitter.com/blynk_app
  15.  
  16. Blynk library is licensed under MIT license
  17. This example code is in public domain.
  18.  
  19. *************************************************************
  20. This example runs directly on ESP32 chip.
  21.  
  22. Note: This requires ESP32 support package:
  23. https://github.com/espressif/arduino-esp32
  24.  
  25. Please be sure to select the right ESP32 module
  26. in the Tools -> Board menu!
  27.  
  28. Change WiFi ssid, pass, and Blynk auth token to run :)
  29. Feel free to apply it to any other example. It's simple!
  30. *************************************************************/
  31.  
  32. /* Comment this out to disable prints and save space */
  33. #define BLYNK_PRINT Serial
  34.  
  35. /* Fill-in your Template ID (only if using Blynk.Cloud) */
  36. #define BLYNK_TEMPLATE_ID "TMPLiTcSd8qR"
  37. #define BLYNK_DEVICE_NAME "ScaleSwitch"
  38. #define BLYNK_AUTH_TOKEN "Pfp1YQBre1f6_RChG8-ej7jd_y0M_6oO"
  39.  
  40.  
  41. #include <WiFi.h>
  42. #include <WiFiClient.h>
  43. #include <BlynkSimpleEsp32.h>
  44.  
  45. // You should get Auth Token in the Blynk App.
  46. // Go to the Project Settings (nut icon).
  47. char auth[] = "Pfp1YQBre1f6_RChG8-ej7jd_y0M_6oO";
  48.  
  49. // Your WiFi credentials.
  50. // Set password to "" for open networks.
  51. char ssid[] = "HotchsYardWifi";
  52. char pass[] = "Coldbeer84";
  53.  
  54.  
  55. // Fill-in information from your Blynk Template here
  56. #define BLYNK_TEMPLATE_ID "TMPLgBRoLHL2"
  57. #define BLYNK_DEVICE_NAME "Scale Switch"
  58.  
  59.  
  60. #define BLYNK_PRINT Serial
  61. //#define BLYNK_DEBUG
  62.  
  63. #define APP_DEBUG
  64.  
  65.  
  66.  
  67. WidgetLED led1(V1);
  68. WidgetLED led2(V2);
  69.  
  70. BlynkTimer timer;
  71.  
  72. int realSwitch = 0;
  73. const int realSwitchPin = 27;
  74. bool scaleSwitchOn = false;
  75. bool blynkScaleSwitchOn = false;
  76. bool blynkScaleSwitchFirst = false;
  77. bool scaleOn = false;
  78. bool scaleRunning = false;
  79. const int batteryVoltagePin = 36;
  80. int batteryVoltageRaw;
  81. int batteryVoltageMap;
  82.  
  83. const int output1 = 4;
  84. const int scaleDisplayMOSFET = 16;
  85. const int scaleMOSFET = 17;
  86. const int output4 = 18;
  87.  
  88.  
  89.  
  90. void setup()
  91. {
  92. Serial.begin(115200);
  93. delay(100);
  94. Blynk.begin(auth, ssid, pass);
  95.  
  96. pinMode(realSwitchPin, INPUT);
  97. pinMode(scaleMOSFET, OUTPUT);
  98. pinMode(scaleDisplayMOSFET, OUTPUT);
  99. pinMode(output1, OUTPUT);
  100. pinMode(output4, OUTPUT);
  101.  
  102. digitalWrite(output1, LOW);
  103. digitalWrite(scaleDisplayMOSFET, LOW);
  104. digitalWrite(scaleMOSFET, LOW);
  105. digitalWrite(output4, LOW);
  106. Blynk.syncAll();
  107. timer.setInterval(500,blynkData);
  108.  
  109. }
  110.  
  111. void realScaleSwitch() {
  112. realSwitch = digitalRead(realSwitchPin);
  113. if (realSwitch == 0) {
  114. scaleSwitchOn = true;
  115. }
  116. else if (realSwitch == 1) {
  117. scaleSwitchOn = false;
  118. }
  119. }
  120.  
  121.  
  122. void switchState() {
  123. if ((blynkScaleSwitchOn) || (scaleSwitchOn)) {
  124. if (scaleRunning) {
  125.  
  126. }
  127. else if (!scaleRunning) {
  128. scaleOn = true;
  129. }
  130. }
  131. if ((!scaleSwitchOn) && (!blynkScaleSwitchOn)) {
  132. scaleOn = false;
  133. scaleRunning = false;
  134. }
  135. }
  136.  
  137.  
  138. void scaleOperation() {
  139. realScaleSwitch();
  140. switchState();
  141. if (scaleOn) {
  142. digitalWrite(scaleMOSFET, HIGH);
  143. digitalWrite(output4, HIGH);
  144. timer.setTimeout(45000, []()
  145. {
  146. scaleOn = false;
  147. scaleRunning = true;
  148. digitalWrite(scaleDisplayMOSFET, HIGH);
  149. });
  150. }
  151. else if ((!scaleOn) && (!scaleRunning)) {
  152. digitalWrite(scaleDisplayMOSFET, LOW);
  153. digitalWrite(scaleMOSFET, LOW);
  154. digitalWrite(output4, LOW);
  155. }
  156. }
  157.  
  158. void blynkData() {
  159. if (scaleOn) {
  160. Blynk.virtualWrite(V1, HIGH);
  161. }
  162. else if (scaleRunning) {
  163. Blynk.virtualWrite(V1, LOW);
  164. Blynk.virtualWrite(V2, HIGH);
  165. }
  166. else if ((!scaleOn) && (!scaleRunning)) {
  167. Blynk.virtualWrite(V1, LOW);
  168. Blynk.virtualWrite(V2, LOW);
  169. }
  170. batteryVoltageRaw = analogRead(batteryVoltagePin);
  171. batteryVoltageMap = map(batteryVoltageRaw, 3946, 4096, 0, 100);
  172. Blynk.virtualWrite(V3, batteryVoltageRaw);
  173. Blynk.virtualWrite(V4, batteryVoltageMap);
  174.  
  175. }
  176.  
  177. BLYNK_WRITE (V0)
  178. {
  179. if (param.asInt() == 0) {
  180. blynkScaleSwitchOn = false;
  181. }
  182. else if (param.asInt() == 1) {
  183. blynkScaleSwitchOn = true;
  184. }
  185. }
  186.  
  187.  
  188.  
  189. void loop() {
  190. Blynk.run();
  191. scaleOperation();
  192. timer.run();
  193. }
  194.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement