Hello, I am looking for help with my project. I am willing to help pay compensation for the person’s time. I have about 80 to 90 percent done (im guessing). I am posting my code here (note that some of the function names have been changed to protect any patent information.
Please be aware I am a novice… so be easy on me. I have learned a great deal from reading these forums and have tried to mimic my code as such. I try to comment it out as much as possible and keep it in same “format” as others. This seems to be an awesome community and I hope to use Blynk in my future products.
Please comment how long you think it would take to get my idea into a working Blynk app and any idea code you have. Please comment out your code. I don’t want someone to just do this FOR me and I cannot understand what the code is doing. My main goal is to learn from this. I wanna know what each line does.
hardware… Photon, mosfet switches, INA219 (for power monitoring, I want for battery life prediction)
Generic terms… Pump, liquid level.
/*
Blynk Virtual Pins -------------
V1 Shunt Voltage
V2 Bus Voltage
V3 Current mA
V4 Load Voltage
V5 Fluid Level
V6 Humidity
V7 Temperature
V8 Wifi Signal
V10 Terminal Widget //not sure if I am going to use this or not.
Photon Pins ------------------
D0 SDA -for ina219
D1 SCL -for ina219
D3 Mosfet 1
D4 Mosfet 2
D5 Temp/humid sensor
D7 Onboard LED
*/
// This #include statement was automatically added by the Particle IDE.
#include <blynk.h>
#include <Adafruit_DHT.h>
#include "Particle.h" //not entirely sure I need this.
#include "adafruit-ina219.h"
#include <Wire.h> //not sure if I need this
PRODUCT_ID(7222); //
PRODUCT_VERSION(2); //playing with particle products feature
STARTUP(System.enableFeature(FEATURE_RETAINED_MEMORY));//so I can store counters when it goes to deep sleep.
#define BLYNK_PRINT Serial
#define DHTPIN 5
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
const int pumpPin1 = 3; //set to D3
const int pumpPin2 = 4; //set to D4
const int ledPin = 7; // set to D7 with onboard LED
int ledState = LOW;
unsigned long previousMillis = 0;
const long interval = 1000; // interval at which to blink (milliseconds)
char auth[] = "40a58175**************aa78";
float f; // Temperature
int h; // Humidity
int signal; // Wifi strength (dB)
retained int numberofDays = 0; //used for day counter
retained int pumpTimes = 0; //used for liquid level counter
retained int fluidLevel = 26; //26 being 100%
WidgetTerminal terminal(V10); // Blynk Terminal
/* ************************************** VOLTAGE READ SETTINGS FOR INA219 Board **********************************/
Adafruit_INA219 ina219;
float shuntvoltage = 0.00;
float busvoltage = 0.00;
float current_mA = 0.00;
float loadvoltage = 0.00;
float energy = 0.00;
float power = 0.00;
/* *************************************** TIME SETTINGS FOR SLEEP ***************************************** */
int secondOfDay = 11*60*60 + 25*60 + 30; //This is an exact time but I may change this so I can ajust it in Blynk.
int currentSecond = Time.local() % 86400;
int tomorrow = 86400 + secondOfDay;
int secondsToSleep = tomorrow - currentSecond;
/* ********************************************** SETUP ************************************************* */
void setup() {
delay(5000); // Allow board to settle.... so they say
(WiFi.selectAntenna(ANT_AUTO)); //I have an external ant and want to use it for better signal
Blynk.begin(auth);
Serial.begin(115200); //not sure if I need this or not
dht.begin(); // DHT22
uint32_t currentFrequency;
ina219.begin();
Particle.syncTime();
Time.zone(-5); //Eastern Time Zone, but this needs to be selectable in Blynk app.
Time.local();
pinMode(pumpPin1, OUTPUT);
pinMode(pumpPin2, OUTPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(pumpPin1, LOW); //start off with pumps set to off
digitalWrite(pumpPin2, LOW); //start off with pumps set to off
}
//End of setup
/* ***************************************** VOID FUNCTIONS ********************************** */
void Blynk_Delay(int milli){ // timer delay instead of DELAY
int end_time = millis() + milli;
while(millis() < end_time){
Blynk.run();
}
}
void pump(){
digitalWrite(pumpPin1, HIGH); //run pump 1
while(pumpPin1 == HIGH){
getINA219values();
}
Blynk_Delay(5000);
digitalWrite(pumpPin1, LOW);
Blynk_Delay(500); //this delay is needed to allow pump 1 to completely stop before starting pump 2.
digitalWrite(pumpPin2, HIGH); //run pump 2
Blynk_Delay(5000);
digitalWrite(pumpPin2, LOW);
}
void GoToSleep(){
System.sleep(SLEEP_MODE_DEEP, secondsToSleep);//sleep till time next day
}
void sendInfo(){
signal = abs(WiFi.RSSI()); // Wifi -1 db strong, -127 db weak. Make positive for easy charting
h = dht.getHumidity(); // V6
f = dht.getTempFarenheit(); // V7
Blynk.virtualWrite(V5,fluidLevel); //Amount of fluid left
Blynk.virtualWrite(V6,h); // Humidity (%)
Blynk.virtualWrite(V7,f); // Temperature (F)
Blynk.virtualWrite(V8,signal); // Wifi strength (dB)
Blynk.syncVirtual(V10); // Sync terminal. Tends to not update if app is running on phone
terminal.flush(); // Maybe this will keep the terminal screen updated
}
void getINA219values(){
// get the INA219 values and throw some basic math at them
shuntvoltage = ina219.getShuntVoltage_mV();
busvoltage = ina219.getBusVoltage_V();
current_mA = ina219.getCurrent_mA();
loadvoltage = busvoltage + (shuntvoltage / 1000); // V
power = (current_mA / 1000) * loadvoltage * 1000; // mW
energy = energy + (power / 1000 / 1000);
// nothing connected? set all to 0, otherwise they float around 0.
if (loadvoltage < 1.1 && loadvoltage > 1 && current_mA < 2 && power < 2) {
loadvoltage = 0;
current_mA = 0;
power = 0;
energy = 0;
}
Blynk.virtualWrite(V1,shuntvoltage); // I may need to define these in setup... testing.....currently not working
Blynk.virtualWrite(V2,busvoltage);
Blynk.virtualWrite(V3,current_mA);
Blynk.virtualWrite(V4,loadvoltage);
/* ****************************************For Troubleshooting********************************
Serial.print("Bus Voltage: "); Serial.print(busvoltage); Serial.println(" V");
Serial.print("Shunt Voltage: "); Serial.print(shuntvoltage); Serial.println(" mV");
Serial.print("Load Voltage: "); Serial.print(loadvoltage); Serial.println(" V");
Serial.print("Current: "); Serial.print(current_mA); Serial.println(" mA");
Serial.println("");
*/
}
void blinkLED() { //might use this to blink the onboard LED on D7
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}
digitalWrite(ledPin, ledState);
}
}
/* ********************************************* LOOP ******************************************* */
void loop(){
Blynk.run();
// Gotta put all the IF triggers here to get/send information to BLYNK
// I want temp/humidity data to be sent daily.
// I want voltage/battery life to be sent during the pumping cycle.
// I only want it to run pumps every 2 weeks.
// Fluid level is FULL at 26 and empty on 0. 26 pump times. Once every 2 weeks. Empty and end of 1 year.
// Notify user when batteries are low. (Using 4 alkaline batteries for 6V. Measures around 6.35 with new batteries.)
// Notify user when Fluid is almost empty (1 pump left) and when empty. (zero)
if (numberofDays == 0){ //testing ideas........
pump();
++numberofDays;
++pumpTimes;
--fluidLevel;
Blynk_Delay(2000);
sendInfo();
Blynk_Delay(2000);
GoToSleep();
}
if (numberofDays == 14){ //testing ideas......
pump();
++numberofDays;
--fluidLevel;
Blynk_Delay(2000);
sendInfo();
Blynk_Delay(2000);
GoToSleep();
}
}