I have a project I'm working on for a chicken coop. I want to control the door with timing of the sunrise and sunset. Also looking to a few other things but somewhere along the way, I must have screwed up the placement of setup()
, loop()
and other void statements. I'm not longer getting Serial.print
statements to print. I've done quite a bit of borrowing from other codes (which might be apparent to those that are much better at this than me). For some of those borrowed codes, I'm getting serial prints. I'm using this as my debug, so I can verify the values that I'm passing and to determine if they are what I'm expecting, specifically for the times of sunrise and sunset, to control my door actuator.
Any ideas why these various serial.print lines are not printing?
I've indicated some of the serial.print lines with <------------------------
.
/*************************************************************
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.
*************************************************************
WARNING!
It's very tricky to get it working. Please read this article:
http://help.blynk.cc/hardware-and-libraries/arduino/esp8266-with-at-firmware
This example shows how value can be pushed from Arduino to
the Blynk App.
NOTE:
BlynkTimer provides SimpleTimer functionality:
http://playground.arduino.cc/Code/SimpleTimer
App project setup:
Value Display widget attached to Virtual Pin V5
*************************************************************/
/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include "DHT.h"
#include <Wire.h>
#include <time.h>
#include <TimeLib.h>
#include <Dusk2Dawn.h>
#include <DS1307RTC.h>
#define TIMEZONE -6
#define LATITUDE 38.3970
#define LONGITUDE -90.6412
//The type of DHT that is being used
#define DHTTYPE DHT11 // DHT 11
//Which pin the DHT output is connected to the Arduino
#define DHTPIN 5
DHT dht(DHTPIN, DHTTYPE);
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "d*********************************0";
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "P**********k";
char pass[] = "m*****************a";
int DisconnectCount = 0;
int ReCnctFlag;
int ReCnctCount = 0;
Dusk2Dawn HouseSprings(LATITUDE,LONGITUDE, TIMEZONE);
tmElements_t tm;
//Changes Mins to the same format as Sunrise/Sunset (minutes after midnight)
int Mins = tm.Hour * 60 + tm.Minute;
int Sunrise = HouseSprings.sunrise(tm.Year, tm.Month, tm.Day, true);
int Sunset = HouseSprings.sunset(tm.Year, tm.Month, tm.Day, true);
//char Str_door_status[] = "";
// Hardware Serial on Mega, Leonardo, Micro...
//#define EspSerial Serial1
// or Software Serial on Uno, Nano...
#include <SoftwareSerial.h>
SoftwareSerial EspSerial(2, 3); // RX, TX
// Your ESP8266 baud rate: adjust per project
#define ESP8266_BAUD 9600
ESP8266 wifi(&EspSerial);
BlynkTimer timer;
//Section of code that will handle getting the humidity/temp data and where to send it
void send_sensor()
{
//Set the variable h to the value for humidity
float h = dht.readHumidity();
// Read temperature as Fahrenheit (isFahrenheit = true)
//Set the variable f to the value for temp
float f = dht.readTemperature(true);
//delay(1000);
//Perform a check if either h or f has no value
if (isnan(h) || isnan(f)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
//These would be used to send data to the serial window
Serial.println(h); // <------------------------
Serial.println(f); // <------------------------
//Set the virtual pin 3 of the blynk to the temperature value
Blynk.virtualWrite(V3, f);
//Set the virtual pin 4 of the blynk to the humidity value
Blynk.virtualWrite(V4, h);
}
// 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.
}
int in1 = 11;
int in2 = 12;
void motor_stop()
{
digitalWrite(in1,LOW);
digitalWrite(in2,LOW);
Blynk.virtualWrite(V6,0);
Blynk.virtualWrite(V7,0);
}
BLYNK_WRITE(V6)
{
int pinValue = param.asInt();
pinMode(in1,OUTPUT);
pinMode(in2,OUTPUT);
if (pinValue = 1);
{
digitalWrite(in1,HIGH);
digitalWrite(in2,LOW);
Blynk.virtualWrite(V0,1);
//delay(35000);
//digitalWrite(in1,LOW);
//Blynk.virtualWrite(V6,0);
timer.setTimeout(35000L, motor_stop);
Blynk.virtualWrite(V0,2);
}
}
BLYNK_WRITE(V7)
{
int pinValue = param.asInt();
pinMode(in1,OUTPUT);
pinMode(in2,OUTPUT);
if (pinValue = 1);
{
digitalWrite(in1,LOW);
digitalWrite(in2,HIGH);
Blynk.virtualWrite(V0,1);
//removed this delay function as Blynk doesn't like it
//delay(35000);
//digitalWrite(in1,LOW);
//Blynk.virtualWrite(V6,0);
//Secondary method of delay that Blynk appears to prefer
timer.setTimeout(35000L, motor_stop);
Blynk.virtualWrite(V0,0);
}
}
void setup()
{
// Debug console
Serial.begin(9600);
// Set ESP8266 baud rate
EspSerial.begin(ESP8266_BAUD);
delay(10);
Blynk.begin(auth, wifi, ssid, pass);
// You can also specify server:
//Blynk.begin(auth, wifi, ssid, pass, "blynk-cloud.com", 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);
dht.begin();
timer.setInterval(1000L, send_sensor);
//RTC.read(tm);
}
BLYNK_CONNECTED() {
Serial.println("Connected");
ReCnctCount = 0;
}
//Serial.println(Sunrise); // 418
//Serial.println(Sunset); // 1004
void open_door()
{
}
void close_door()
{
Serial.println("info"); // <------------------------
Serial.println(Mins); // <------------------------
Serial.println(Sunrise); // <------------------------
Serial.println(Sunset); // <------------------------
char time2[] = "00:00";
Dusk2Dawn::min2str(time2, Sunrise);
Serial.print("Sunrise: "); // <------------------------
Serial.println(time2); // 16:53 // <------------------------
char time3[] = "00:00";
Dusk2Dawn::min2str(time3, Sunset);
Serial.print("Sunset: "); // <------------------------
Serial.println(time3); // 16:53 // <------------------------
}
void loop()
{
Serial.println(Mins); // <------------------------
Serial.println(Sunrise); // <------------------------
Serial.println(Sunset); // <------------------------
if (Blynk.connected())
{
Blynk.run(); // Starts Blynk connection
}
else if (ReCnctFlag == 0)
{
ReCnctFlag = 1; // Set reconnection Flag
Serial.println("Starting reconnection timer in 3 seconds...");
timer.setTimeout(3000L, []() { // Lambda Reconnection Timer Function
ReCnctFlag = 0; // Reset reconnection Flag
ReCnctCount++; // Increment reconnection Counter
Serial.print("Attempting reconnection #");
Serial.println(ReCnctCount);
Blynk.connect(); // Try to reconnect to the server
});
}
timer.run(); // Initiates BlynkTimer
//if (Mins > Sunrise) {
//Serial.println("Door is open");
//}
//if (Mins > Sunset) {
// Serial.println("Door is closed");
//}
}
This is what I'm getting in the serial window:
[45108333] Ready (ping: 53ms).
Connected
3
inSoftwareSerial EspSerial(2, 3);
which seems to beSerial
's RX pin. But a severely cut down version of your code doing nothing but this didn't seem cause the problem. My only suggestion is that you start removing code from your question, starting with things that people who might try to help you are not going to want to deal with. Blynk, the DHT sensor, whatever you can remove while still demonstrating the problem.