Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*************************************************************
- Download latest Blynk library here:
- https://github.com/blynkkk/blynk-library/releases/latest
- Blynk is a platform with iOS and Android apps to control
- Arduino, Raspberry Pi and the likes over the Internet.
- You can easily build graphic interfaces for all your
- projects by simply dragging and dropping widgets.
- Downloads, docs, tutorials: http://www.blynk.cc
- Sketch generator: http://examples.blynk.cc
- Blynk community: http://community.blynk.cc
- Follow us: http://www.fb.com/blynkapp
- http://twitter.com/blynk_app
- Blynk library is licensed under MIT license
- This example code is in public domain.
- *************************************************************
- This example runs directly on ESP32 chip.
- Note: This requires ESP32 support package:
- https://github.com/espressif/arduino-esp32
- Please be sure to select the right ESP32 module
- in the Tools -> Board menu!
- Change WiFi ssid, pass, and Blynk auth token to run :)
- Feel free to apply it to any other example. It's simple!
- *************************************************************/
- /* Comment this out to disable prints and save space */
- #define BLYNK_PRINT Serial
- /* Fill-in your Template ID (only if using Blynk.Cloud) */
- #define BLYNK_TEMPLATE_ID "TMPLiTcSd8qR"
- #define BLYNK_DEVICE_NAME "ScaleSwitch"
- #define BLYNK_AUTH_TOKEN "Pfp1YQBre1f6_RChG8-ej7jd_y0M_6oO"
- #include <WiFi.h>
- #include <WiFiClient.h>
- #include <BlynkSimpleEsp32.h>
- // You should get Auth Token in the Blynk App.
- // Go to the Project Settings (nut icon).
- char auth[] = "Pfp1YQBre1f6_RChG8-ej7jd_y0M_6oO";
- // Your WiFi credentials.
- // Set password to "" for open networks.
- char ssid[] = "HotchsYardWifi";
- char pass[] = "Coldbeer84";
- // Fill-in information from your Blynk Template here
- #define BLYNK_TEMPLATE_ID "TMPLgBRoLHL2"
- #define BLYNK_DEVICE_NAME "Scale Switch"
- #define BLYNK_PRINT Serial
- //#define BLYNK_DEBUG
- #define APP_DEBUG
- WidgetLED led1(V1);
- WidgetLED led2(V2);
- BlynkTimer timer;
- int realSwitch = 0;
- const int realSwitchPin = 27;
- bool scaleSwitchOn = false;
- bool blynkScaleSwitchOn = false;
- bool blynkScaleSwitchFirst = false;
- bool scaleOn = false;
- bool scaleRunning = false;
- const int batteryVoltagePin = 36;
- int batteryVoltageRaw;
- int batteryVoltageMap;
- const int output1 = 4;
- const int scaleDisplayMOSFET = 16;
- const int scaleMOSFET = 17;
- const int output4 = 18;
- void setup()
- {
- Serial.begin(115200);
- delay(100);
- Blynk.begin(auth, ssid, pass);
- pinMode(realSwitchPin, INPUT);
- pinMode(scaleMOSFET, OUTPUT);
- pinMode(scaleDisplayMOSFET, OUTPUT);
- pinMode(output1, OUTPUT);
- pinMode(output4, OUTPUT);
- digitalWrite(output1, LOW);
- digitalWrite(scaleDisplayMOSFET, LOW);
- digitalWrite(scaleMOSFET, LOW);
- digitalWrite(output4, LOW);
- Blynk.syncAll();
- timer.setInterval(500,blynkData);
- }
- void realScaleSwitch() {
- realSwitch = digitalRead(realSwitchPin);
- if (realSwitch == 0) {
- scaleSwitchOn = true;
- }
- else if (realSwitch == 1) {
- scaleSwitchOn = false;
- }
- }
- void switchState() {
- if ((blynkScaleSwitchOn) || (scaleSwitchOn)) {
- if (scaleRunning) {
- }
- else if (!scaleRunning) {
- scaleOn = true;
- }
- }
- if ((!scaleSwitchOn) && (!blynkScaleSwitchOn)) {
- scaleOn = false;
- scaleRunning = false;
- }
- }
- void scaleOperation() {
- realScaleSwitch();
- switchState();
- if (scaleOn) {
- digitalWrite(scaleMOSFET, HIGH);
- digitalWrite(output4, HIGH);
- timer.setTimeout(45000, []()
- {
- scaleOn = false;
- scaleRunning = true;
- digitalWrite(scaleDisplayMOSFET, HIGH);
- });
- }
- else if ((!scaleOn) && (!scaleRunning)) {
- digitalWrite(scaleDisplayMOSFET, LOW);
- digitalWrite(scaleMOSFET, LOW);
- digitalWrite(output4, LOW);
- }
- }
- void blynkData() {
- if (scaleOn) {
- Blynk.virtualWrite(V1, HIGH);
- }
- else if (scaleRunning) {
- Blynk.virtualWrite(V1, LOW);
- Blynk.virtualWrite(V2, HIGH);
- }
- else if ((!scaleOn) && (!scaleRunning)) {
- Blynk.virtualWrite(V1, LOW);
- Blynk.virtualWrite(V2, LOW);
- }
- batteryVoltageRaw = analogRead(batteryVoltagePin);
- batteryVoltageMap = map(batteryVoltageRaw, 3946, 4096, 0, 100);
- Blynk.virtualWrite(V3, batteryVoltageRaw);
- Blynk.virtualWrite(V4, batteryVoltageMap);
- }
- BLYNK_WRITE (V0)
- {
- if (param.asInt() == 0) {
- blynkScaleSwitchOn = false;
- }
- else if (param.asInt() == 1) {
- blynkScaleSwitchOn = true;
- }
- }
- void loop() {
- Blynk.run();
- scaleOperation();
- timer.run();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement