BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Yet Another Automated Plant box project. Please help me refine my code

I have been working over a year on a plant box. Im almost finished. I would like some help with my code, It works almost 100% but at times randomly disconnects and wont report data to blynk. Im really glad to have found Blynk, prior finding it i was trying to write a gui from scratch with a thing call Guino, I found on indestructibles.

This project is made up of many arduino boards,

Arduino Uno with Sparkfun Esp8266 wifi sheild.
x2 One Wire Digital Temperature Sensor - DS18B20
x1 DHT 22
x1 Atlas Scientific Industrial pH Probe
x1 Ultasonic Distance Sensor
x1 12v Light Winch System

1 Like

@cory3640 looks impressive, do you have any code?

how do i post it? properly in the form?

There is a link in here to code formatting Blynk for Beginners and help with your project

I have no coding experience what so ever, I made this code work, with lots of trial and error. copy and paste, and pulling bits and pieces from various examples, like blink with out delay . and many others.

Search the site for threads containing the work “Blynkify” which simply means converting a regular Arduino sketch to work with Blynk. There are some subtle differences but if you follow the guidance it’s really not that difficult.

#include <Blynk.h>

/**************************************************************
 * 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
 *   Blynk community:            http://community.blynk.cc
 *   Social networks:            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 shows how to use ESP8266 Shield via Hardware Serial
 * (on Mega, Leonardo, Micro...) to connect your project to Blynk.
 *
 * Note: Ensure a stable serial connection to ESP8266!
 *       Firmware version 1.0.0 (AT v0.22) or later is needed.
 *       You can change ESP baud rate. Connect to AT console and call:
 *           AT+UART_DEF=115200,8,1,0,0
 *
 * Change WiFi ssid, pass, and Blynk auth token to run :)
 * Feel free to apply it to any other example. It's simple!
 *
 **************************************************************/
//#define BLYNK_DEBUG
#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space

#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>

// Set ESP8266 Serial object
#define EspSerial Serial

char auth[] = "***";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "UNITE-91F4";
char pass[] = "48135613";

// Hardware Serial on Mega, Leonardo, Micro...
#define EspSerial Serial

// or Software Serial on Uno, Nano...
//#include <SoftwareSerial.h>
//SoftwareSerial EspSerial(2, 3); // RX, TX

// Your ESP8266 baud rate:
#define ESP8266_BAUD 9600

ESP8266 wifi(&EspSerial);

#include <SimpleTimer.h>
#include <DHT.h>
#define DHTPIN 9 //pin gpio 12 in sensor
#define DHTTYPE DHT22   // DHT 22 Change this if you have a DHT11
DHT dht(DHTPIN, DHTTYPE);
#include <OneWire.h>
#define ONE_WIRE_BUS 7
OneWire oneWire(ONE_WIRE_BUS);
#include <DallasTemperature.h>
DallasTemperature sensors(&oneWire);
unsigned long previousMillisLED12 = 0;
unsigned long previousMillisLED13 = 0;
unsigned long previousMillisLED14 = 0;
unsigned long previousMillisLED15 = 0;
unsigned long previousMillisLED16 = 0;
unsigned long previousMillisLED17 = 0;
unsigned long previousMillisLED20 = 0;
unsigned long previousMillisLED21 = 0;

const int buttonPin = 4;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin
int buttonState = 0;
int intervalLED12 = 8000;
int intervalLED13 = 10000;
int intervalLED14 = 60000;

#include "Arduino.h"
class Ultrasonic
{
  public:
    Ultrasonic(int pin);
    void DistanceMeasure(void);
    long microsecondsToCentimeters(void);
    long microsecondsToInches(void);
  private:
    int _pin;//pin number of Arduino that is connected with SIG pin of Ultrasonic Ranger.
    long duration;// the Pulse time received;
};
Ultrasonic::Ultrasonic(int pin)
{
  _pin = 5;
}
/*Begin the detection and get the pulse back signal*/
void Ultrasonic::DistanceMeasure(void)
{
  pinMode(_pin, OUTPUT);
  digitalWrite(_pin, LOW);
  delayMicroseconds(2);
  digitalWrite(_pin, HIGH);
  delayMicroseconds(5);
  digitalWrite(_pin, LOW);
  pinMode(_pin, INPUT);
  duration = pulseIn(_pin, HIGH);
}
/*The measured distance from the range 0 to 400 Centimeters*/
long Ultrasonic::microsecondsToCentimeters(void)
{
  return duration / 29 / 2;
}
/*The measured distance from the range 0 to 157 Inches*/
long Ultrasonic::microsecondsToInches(void)
{
  return duration / 74 / 2;
}

Ultrasonic ultrasonic(7);

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
SimpleTimer timer;

#include <SoftwareSerial.h>                           //we have to include the SoftwareSerial library, or else we can't use it.
#define rx 2                                          //define what pin rx is going to be.
#define tx 3                                          //define what pin tx is going to be.

SoftwareSerial myserial(rx, tx);                      //define how the soft serial port is going to work.


String inputstring = "";                              //a string to hold incoming data from the PC
String sensorstring = "";                             //a string to hold the data from the Atlas Scientific product
boolean input_stringcomplete = false;                 //have we received all the data from the PC
boolean sensor_stringcomplete = false;                //have we received all the data from the Atlas Scientific product
float ph;                                             //used to hold a floating point number that is the pH.

void setup()
{
   pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);

  // Set console baud rate
  Serial.begin(9600);
  
  // Set ESP8266 baud rate

  
EspSerial.begin(ESP8266_BAUD);
  Blynk.begin(auth, wifi, "UNITE-91F4", "48135613");
Serial.begin(9600);                                 //set baud rate for the hardware serial port_0 to 9600
  myserial.begin(9600);                               //set baud rate for software serial port_3 to 9600
  inputstring.reserve(10);                            //set aside some bytes for receiving data from the PC
  sensorstring.reserve(30);                           //set aside some bytes for receiving data from Atlas Scientific product
}


void serialEvent() {                                  //if the hardware serial port_0 receives a char
  char inchar = (char)Serial.read();                  //get the char we just received
  inputstring += inchar;                              //add it to the inputString
  if (inchar == '\r') {
    input_stringcomplete = true;                      //if the incoming character is a <CR>, set the flag
  }
}




void loop()
{


  Blynk.run();
  
    
       // read the state of the pushbutton value:
        buttonState = digitalRead(buttonPin);
      
        // check if the pushbutton is pressed.
        // if it is, the buttonState is HIGH:
        if (buttonState == LOW) {
          // turn LED on:
          digitalWrite(10, LOW);
          
        }

         
         unsigned long currentMillis = millis();
     
 if (currentMillis - previousMillisLED12 >= intervalLED12 & input_stringcomplete) {                         //if a string from the PC has been received in its entirety
    myserial.print(inputstring);                      //send that string to the Atlas Scientific product
    inputstring = "";                                 //clear the string
    input_stringcomplete = false;                     //reset the flag used to tell if we have received a completed string from the PC
         

  }

  if (myserial.available() > 0) {
   
   //if we see that the Atlas Scientific product has sent a character.
    char inchar = (char)myserial.read();              //get the char we just received
    sensorstring += inchar;
    if (inchar == '\r') {
      sensor_stringcomplete = true;                   //if the incoming character is a <CR>, set the flag
    }
  


  if (sensor_stringcomplete) {                        //if a string from the Atlas Scientific product has been received in its entirety
                        //send that string to the PC's serial monitor
    Blynk.virtualWrite (15, sensorstring);
    Blynk.virtualWrite (16, sensorstring);
    Blynk.virtualWrite (17, sensorstring);
    ph = sensorstring.toFloat();                      //convert the string to a floating point number so it can be evaluated by the Arduino


     sensorstring = "";                                //clear the string:
  
    sensor_stringcomplete = false;   
               
 previousMillisLED12 = currentMillis;
  
  }  

   
  if (currentMillis - previousMillisLED13 >= intervalLED13 ) {
    sensors.begin();


        sensors.requestTemperatures(); // Send the command to get temperatures


     Blynk.virtualWrite(7, sensors.getTempCByIndex (1) * 9 / 5 + 32);
        Blynk.virtualWrite(2, sensors.getTempCByIndex (0) * 9 / 5 + 32);
        Blynk.virtualWrite(3, sensors.getTempCByIndex (0) * 9 / 5 + 32);
        Blynk.virtualWrite(9, sensors.getTempCByIndex(0) * 9 / 5 + 32);
          float h = dht.readHumidity();
        float t = dht.readTemperature();
        t = t * 9 / 5 + 32;

        Blynk.virtualWrite(11, t); // virtual pin

        Blynk.virtualWrite(10, h); // virtual pin
 long RangeInInches;
        long RangeInCentimeters;
        ultrasonic.DistanceMeasure();// get the current signal time;
        RangeInInches = ultrasonic.microsecondsToInches();//convert the time to inches;
        RangeInCentimeters = ultrasonic.microsecondsToCentimeters();//convert the time to centimeters;
         int val = ultrasonic.microsecondsToInches();
         val = map(val, 4, 12, 100, 0);
                    Blynk.virtualWrite(5, val);

          
          previousMillisLED13 = currentMillis;




      }  }             }

Thank you for any help I can get, please look at my horrible code and help me structure everything properly,

@cory3640 you are Uno with ESP8266 WiFi shield, right?

I am guessing you can’t connect to Blynk with your sketch, as it is set up for Mega’s and Leonardo’s that have more than one serial port.

It can connect to Blynk and works fine, aside i cant seem to graph the output from Blynk.virtualWrite (17, sensorstring);

my ph readings stop at time and other times read 55.55, its almost as the the Blynk server is seeing in bound 5 readings and stacking them 5,5,5,5 = 55.55 ?

At rows 38 and 48 you have duplicated lines for “Hardware Serial” and that’s why I assumed you wouldn’t be able to connect but I see Software Serial set up elsewhere.

The stacking will be probably because of all the sleeping you are asking the Arduino to do.
Now it’s on Blynk it must work 24/7 and you need to call functions at timed intervals with SimpleTimer.

What widget are you using for V17?

I have it V17 linked to Historical Graph on the PH & RES TEMP tab.

Im surprised my code works at all. I really need to re-write it all.

Are you not displaying the data in a Display Widget before trying to graph it? I don’t do much graph work but I didn’t think you could just send the data directly to the History graph.

Ill try that, Display pH on V15 in the PH & RES TEMP Tab, also graph V15 in the Historical Graph and see if that works.

i will also try and remove Hardware Serial requests from my code and see if that helps, thank you very much for the help.

would you like to see the output from my serial monitor?

Might be useful.