Hi,
I have created code to control my sensor inputs and display them on a 20x4 LCD. I have also created code to connect to the Blynk app. These work seperatly but when i tried to combine them neither work. The LCD stops showing data once the Blynk code starts running in the setup and then the serial monitor does show that my board has connected to the wifi and is ready to start like it usually does.
Below is the code to connect to my Blynk app.
#define BLYNK_TEMPLATE_ID "TMPL7mLWZCzi"
#define BLYNK_DEVICE_NAME "Quickstart Device"
#define BLYNK_AUTH_TOKEN "SQKj9qqAVwTsrLHimtkkdvSPMFuj8Ss_"
// Comment this out to disable prints and save space
#define BLYNK_PRINT Serial
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "Tenda_3165";
char pass[] = "JDwRadYk";
// Hardware Serial on Mega, Leonardo, Micro...
//#define EspSerial Serial
// or Software Serial on Uno, Nano...
#include <SoftwareSerial.h>
SoftwareSerial EspSerial(8,10); // RX, TX
// Your ESP8266 baud rate:
#define ESP8266_BAUD 9600
ESP8266 wifi(&EspSerial);
BlynkTimer timer;
// This function sends Arduino's up time every second to Virtual Pin (5).
// In the app, Widget's reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.
void myTimerEvent()
{
// You can send any value at any time.
// Please don't send more that 10 values per second.
Blynk.virtualWrite(V5, millis() / 1000);
}
void setup()
{
// Debug console
Serial.begin(9600);
// Set ESP8266 baud rate
EspSerial.begin(ESP8266_BAUD);
delay(10);
Blynk.begin(BLYNK_AUTH_TOKEN,wifi, ssid, pass);
// You can also specify server:
//Blynk.begin(auth, wifi, ssid, pass, "blynk.cloud", 80);
//Blynk.begin(auth, wifi, ssid, pass, IPAddress(192,168,1,100), 8080);
// Setup a function to be called every second
timer.setInterval(1000L, myTimerEvent);
}
void loop()
{
Blynk.run();
timer.run(); // Initiates BlynkTimer
}
Below is the code i used for displaying readings on my serial monitor
#include <DHT.h> // Includes the library needed to read the humidity sensor
#define type DHT11 // Defines the type of humidity sensor
int HumidityPin = 7; // indicates which pin the humidity sensor is connected to
DHT HT(HumidityPin, type); // Defines HT
float humidity; // declares the float called humidity
// Include the libraries we need for soil temperature sensor
#include <OneWire.h>
#include <DallasTemperature.h>
// Data wire is plugged into port 9 on the Arduino for soil temperature sensor
#define ONE_WIRE_BUS 9
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
// arrays to hold device address for soil temp
DeviceAddress insideThermometer;
//Declare output pins for LEDs which will replace the humidifier, water pump and heat mat
int HumidityLED = 13; // LED acting in place of humidifier is powered from pin 13
int HeatLED = A4; // LED acting in place of heat mat is powered from pin 10
int WaterLED = A2; // LED acting in place of water pump is powered from pin 8
// Declare Desired values for sensor readings
int DesiredTemp = 24;
int DesiredHumidity = 80;
int DesiredMoisture = 20;
// Include LCD
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
// start for turning on and off the light
int Light = 6;
int LightState = LOW; // lightState used to set the Light on and off
unsigned long previousMillis = 0; // will store last time Light was turned on and off
const long LightOn = 10000; // Time light on in milliseconds
const long LightOff = 5000; // Time light off in milliseconds
void setup() {
Serial.begin(9600); // This tells the Arduino to get ready to exchange messages with the Serial Monitor at a data rate of 9600 bits per second
HT.begin(); // Begins reading from humidity sensor
sensors.begin(); // Starts soil temperature sensor
// report parasite power requirements for soil temp sensor
Serial.print("Parasite power is: ");
if (sensors.isParasitePowerMode()) Serial.println("ON");
else Serial.println("OFF");
// Assign address manually. The addresses below will beed to be changed
// to valid device addresses on your bus. Device address can be retrieved
// by using either oneWire.search(deviceAddress) or individually via
// sensors.getAddress(deviceAddress, index)
// Note that you will need to use your specific address here
//insideThermometer = { 0x28, 0x1D, 0x39, 0x31, 0x2, 0x0, 0x0, 0xF0 };
// Method:
// Search for devices on the bus and assign based on an index. Ideally,
// you would do this to initially discover addresses on the bus and then
// use those addresses and manually assign them (see above) once you know
// the devices on your bus (and assuming they don't change).
if (!sensors.getAddress(insideThermometer, 0)) Serial.println("Unable to find address for Device 0");
// set the resolution to 9 bit (Each Dallas/Maxim device is capable of several different resolutions)
sensors.setResolution(insideThermometer, 9);
Serial.print("Device 0 Resolution: ");
Serial.print(sensors.getResolution(insideThermometer), DEC);
Serial.println();
// Declare LEDs as ouputs
pinMode(HumidityLED, OUTPUT);
pinMode(HeatLED, OUTPUT);
pinMode(WaterLED, OUTPUT);
pinMode(Light, OUTPUT); // Sets the light as an output
// Set up LCD
lcd.begin(20, 4);
lcd.setCursor(0,0);
lcd.print("SYSTEM ON"); // print system on on LCD screen
delay(2000);
}
// function to print the soil temperature
void printTemperature(DeviceAddress deviceAddress)
{
float tempC = sensors.getTempC(deviceAddress);
if(tempC == DEVICE_DISCONNECTED_C)
{
Serial.println("Error: Could not read temperature data");
return;
}
// Print Temperature on row one of LCD
lcd.setCursor(0,0);
lcd.print("Soil Temp");
lcd.setCursor(10,0);
lcd.print(tempC);
lcd.setCursor(15,0);
lcd.print("C");
if(tempC < DesiredTemp){
digitalWrite(HeatLED, HIGH);
}
else{
digitalWrite(HeatLED, LOW);
}
if(tempC < DesiredTemp){
Serial.println("Soil Temperature below desired level");
}
else{
Serial.println("Soil Temperature above desired level");
}
}
void loop() {
// Humidity code
humidity = HT.readHumidity(); // reads humidity
// Print Humidity on row three of LCD
lcd.setCursor(0,2);
lcd.print("Humidity");
lcd.setCursor(9,2);
lcd.print(humidity);
lcd.setCursor(14,2);
lcd.print("%");
if(humidity < DesiredHumidity){
digitalWrite(HumidityLED, HIGH); // LED turned on
}
else{
digitalWrite(HumidityLED, LOW); // LED turned off
}
if(humidity < DesiredHumidity){
Serial.println("Humidity below desired level");
}
else{
Serial.println("Humidity above desired level");
}
delay(2000); // gives 2 second delay after humidity values are printed
// Soil moisture code
float reading = analogRead(A0); // Read pin A0
float Moisture = (reading/732)*100; // Convert reading from A0 to percentage moisture
// Print Moisture on row 2 of LCD
lcd.setCursor(0,1);
lcd.print("Soil Moisture");
lcd.setCursor(14,1);
lcd.print(Moisture);
lcd.setCursor(19,1);
lcd.print("%");
if(Moisture < DesiredMoisture){
digitalWrite(WaterLED, HIGH); // LED turned on
}
else{
digitalWrite(WaterLED, LOW); // LED turned off
}
if(Moisture < DesiredMoisture){
Serial.println("Soil Moisture below desired level");
}
else{
Serial.println("Soil Moisture above desired level");
}
delay(2000); // gives 2 second delay after soil moisture values are printed
// Soil Temerature code
sensors.requestTemperatures(); // Send the command to get temperature
printTemperature(insideThermometer); // Use a simple function to print out the data
delay(2000); // gives 2 second delay after soil temperature values are printed
// code for turning on and off light
unsigned long currentMillis = millis(); // Store current time Light has been on or off
if(LightState == LOW){
if (currentMillis - previousMillis >= LightOff) { //If the light has been off for longer than it was supposed to be
previousMillis = currentMillis; // reset time since light state was changed
LightState = HIGH; // turn light on
}
}
if(LightState == HIGH){
if (currentMillis - previousMillis >= LightOn) { // If the light has been on for longer than it was supposed to be
previousMillis = currentMillis; // reset time since light state was changed
LightState = LOW; // turn light off
}
}
digitalWrite(Light, LightState); // turn light on or off
}
This is then the combined code.
#include <DHT.h> // Includes the library needed to read the humidity sensor
#define type DHT11 // Defines the type of humidity sensor
int HumidityPin = 7; // indicates which pin the humidity sensor is connected to
DHT HT(HumidityPin, type); // Defines HT
float humidity; // declares the float called humidity
// Include the libraries we need for soil temperature sensor
#include <OneWire.h>
#include <DallasTemperature.h>
// Data wire is plugged into port 9 on the Arduino for soil temperature sensor
#define ONE_WIRE_BUS 9
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
// arrays to hold device address for soil temp
DeviceAddress insideThermometer;
//Declare output pins for LEDs which will replace the humidifier, water pump and heat mat
int HumidityLED = 13; // LED acting in place of humidifier is powered from pin 13
int HeatLED = A4; // LED acting in place of heat mat is powered from pin 10
int WaterLED = A2; // LED acting in place of water pump is powered from pin 8
// Declare Desired values for sensor readings
int DesiredTemp = 24;
int DesiredHumidity = 80;
int DesiredMoisture = 20;
// Include LCD
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
// start for turning on and off the light
int Light = 6;
int LightState = LOW; // lightState used to set the Light on and off
unsigned long previousMillis = 0; // will store last time Light was turned on and off
const long LightOn = 10000; // Time light on in milliseconds
const long LightOff = 5000; // Time light off in milliseconds
// Template ID, Device Name and Auth Token are provided by the Blynk.Cloud
// See the Device Info tab, or Template settings
#define BLYNK_TEMPLATE_ID "TMPL7mLWZCzi"
#define BLYNK_DEVICE_NAME "Quickstart Device"
#define BLYNK_AUTH_TOKEN "SQKj9qqAVwTsrLHimtkkdvSPMFuj8Ss_"
// Comment this out to disable prints and save space
#define BLYNK_PRINT Serial
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "Tenda_3165";
char pass[] = "JDwRadYk";
// Hardware Serial on Mega, Leonardo, Micro...
//#define EspSerial Serial
// or Software Serial on Uno, Nano...
#include <SoftwareSerial.h>
SoftwareSerial EspSerial(8,10); // RX, TX
// Your ESP8266 baud rate:
#define ESP8266_BAUD 9600
ESP8266 wifi(&EspSerial);
BlynkTimer timer;
// This function sends Arduino's up time every second to Virtual Pin (5).
// In the app, Widget's reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.
void myTimerEvent()
{
// You can send any value at any time.
// Please don't send more that 10 values per second.
Blynk.virtualWrite(V5, millis() / 1000);
}
void setup() {
Serial.begin(9600); // This tells the Arduino to get ready to exchange messages with the Serial Monitor at a data rate of 9600 bits per second
HT.begin(); // Begins reading from humidity sensor
sensors.begin(); // Starts soil temperature sensor
// report parasite power requirements for soil temp sensor
Serial.print("Parasite power is: ");
if (sensors.isParasitePowerMode()) Serial.println("ON");
else Serial.println("OFF");
// Assign address manually. The addresses below will beed to be changed
// to valid device addresses on your bus. Device address can be retrieved
// by using either oneWire.search(deviceAddress) or individually via
// sensors.getAddress(deviceAddress, index)
// Note that you will need to use your specific address here
//insideThermometer = { 0x28, 0x1D, 0x39, 0x31, 0x2, 0x0, 0x0, 0xF0 };
// Method:
// Search for devices on the bus and assign based on an index. Ideally,
// you would do this to initially discover addresses on the bus and then
// use those addresses and manually assign them (see above) once you know
// the devices on your bus (and assuming they don't change).
if (!sensors.getAddress(insideThermometer, 0)) Serial.println("Unable to find address for Device 0");
// set the resolution to 9 bit (Each Dallas/Maxim device is capable of several different resolutions)
sensors.setResolution(insideThermometer, 9);
Serial.print("Device 0 Resolution: ");
Serial.print(sensors.getResolution(insideThermometer), DEC);
Serial.println();
// Declare LEDs as ouputs
pinMode(HumidityLED, OUTPUT);
pinMode(HeatLED, OUTPUT);
pinMode(WaterLED, OUTPUT);
pinMode(Light, OUTPUT); // Sets the light as an output
// Set up LCD
lcd.begin(20, 4);
lcd.setCursor(0,0);
lcd.print("SYSTEM ON"); // print system on on LCD screen
delay(2000);
// Set ESP8266 baud rate
EspSerial.begin(ESP8266_BAUD);
delay(10);
Blynk.begin(BLYNK_AUTH_TOKEN,wifi, ssid, pass);
// You can also specify server:
//Blynk.begin(auth, wifi, ssid, pass, "blynk.cloud", 80);
//Blynk.begin(auth, wifi, ssid, pass, IPAddress(192,168,1,100), 8080);
// Setup a function to be called every second
timer.setInterval(1000L, myTimerEvent);
}
// function to print the soil temperature
void printTemperature(DeviceAddress deviceAddress)
{
float tempC = sensors.getTempC(deviceAddress);
if(tempC == DEVICE_DISCONNECTED_C)
{
Serial.println("Error: Could not read temperature data");
return;
}
// Print Temperature on row one of LCD
lcd.setCursor(0,0);
lcd.print("Soil Temp");
lcd.setCursor(10,0);
lcd.print(tempC);
lcd.setCursor(15,0);
lcd.print("C");
if(tempC < DesiredTemp){
digitalWrite(HeatLED, HIGH);
}
else{
digitalWrite(HeatLED, LOW);
}
if(tempC < DesiredTemp){
Serial.println("Soil Temperature below desired level");
}
else{
Serial.println("Soil Temperature above desired level");
}
}
void loop() {
Blynk.run();
timer.run(); // Initiates BlynkTimer
// Humidity code
humidity = HT.readHumidity(); // reads humidity
// Print Humidity on row three of LCD
lcd.setCursor(0,2);
lcd.print("Humidity");
lcd.setCursor(9,2);
lcd.print(humidity);
lcd.setCursor(14,2);
lcd.print("%");
if(humidity < DesiredHumidity){
digitalWrite(HumidityLED, HIGH); // LED turned on
}
else{
digitalWrite(HumidityLED, LOW); // LED turned off
}
if(humidity < DesiredHumidity){
Serial.println("Humidity below desired level");
}
else{
Serial.println("Humidity above desired level");
}
delay(2000); // gives 2 second delay after humidity values are printed
// Soil moisture code
float reading = analogRead(A0); // Read pin A0
float Moisture = (reading/732)*100; // Convert reading from A0 to percentage moisture
// Print Moisture on row 2 of LCD
lcd.setCursor(0,1);
lcd.print("Soil Moisture");
lcd.setCursor(14,1);
lcd.print(Moisture);
lcd.setCursor(19,1);
lcd.print("%");
if(Moisture < DesiredMoisture){
digitalWrite(WaterLED, HIGH); // LED turned on
}
else{
digitalWrite(WaterLED, LOW); // LED turned off
}
if(Moisture < DesiredMoisture){
Serial.println("Soil Moisture below desired level");
}
else{
Serial.println("Soil Moisture above desired level");
}
delay(2000); // gives 2 second delay after soil moisture values are printed
// Soil Temerature code
sensors.requestTemperatures(); // Send the command to get temperature
printTemperature(insideThermometer); // Use a simple function to print out the data
delay(2000); // gives 2 second delay after soil temperature values are printed
// code for turning on and off light
unsigned long currentMillis = millis(); // Store current time Light has been on or off
if(LightState == LOW){
if (currentMillis - previousMillis >= LightOff) { //If the light has been off for longer than it was supposed to be
previousMillis = currentMillis; // reset time since light state was changed
LightState = HIGH; // turn light on
}
}
if(LightState == HIGH){
if (currentMillis - previousMillis >= LightOn) { // If the light has been on for longer than it was supposed to be
previousMillis = currentMillis; // reset time since light state was changed
LightState = LOW; // turn light off
}
}
digitalWrite(Light, LightState); // turn light on or off
}
I know my code is very long but any suggestions on how to get this working would be much appreciated. I am using an Arduino Uno rev3 board with an ESP8266 shield.