Stray cat shelter monitor and control

/* */
  const int Dimmer_Light = 5;   //D1
  const int Relay_1 = 13;       //D2
  const int Relay_2 = 15;       //D3
  const int Relay_3 = 14;       //D5
  const int Relay_4 = 16;       //D0
  const int Blue_LED = 2;       //D4
  bool Connected2Blynk = false;
  
#define BLYNK_PRINT Serial
#define TRIGGER_PIN 0
#define DHTPIN 4 // Digital pin connected to the DHT sensor 

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>
#include <ESP8266WebServer.h>
#include <DNSServer.h>
#include <WiFiManager.h>          //https://github.com/tzapu/WiFiManager

char auth[] = "AUTH KEY GOES HERE";// auth code for app b

#define DHTTYPE DHT22   // DHT 22, AM2302, AM2321

DHT_Unified dht(DHTPIN, DHTTYPE);
uint32_t delayMS;

char ssid[] = "SSID GOES HERE";
char pass[] = "PSK GOES HERE";
BlynkTimer timer;
//********************* LED DETAILS *********************************************
int PWM_LED1;
int RELAY_1;
int RELAY_2;
int RELAY_3;
int RELAY_4;
//*********************************************************************************
//########################################
// 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 sendSensor()
{
  // Delay between measurements.
  delay(delayMS);
  float fahrenheittemp = 70;
  // Get temperature event and print its value.
  sensors_event_t event;
  dht.temperature().getEvent(&event);
  if (isnan(event.temperature)) {
    Serial.println(F("Error reading temperature!"));
  }
  else {
  float celsius = event.temperature;
  float fahrenheit = (celsius * 1.8) + 32;
  fahrenheittemp = fahrenheit;
    Serial.print(F("Temperature: "));
//    Serial.print(event.temperature);
    Serial.print(fahrenheit);
    Blynk.virtualWrite(V6, fahrenheit);
  }
  // Get humidity event and print its value.
  dht.humidity().getEvent(&event);
  if (isnan(event.relative_humidity)) {
    Serial.println(F("Error reading humidity!"));
  }
  else {
    Serial.print(F(" Humidity: "));
    Serial.print(event.relative_humidity);
    Serial.println(F("%"));
    Blynk.virtualWrite(V5, (event.relative_humidity));
  }

    if(fahrenheittemp > 100){
    Blynk.email("kimbrec@cdw.com", "ESP8266 Alert", "Temperature over 100!!");
    Blynk.notify("ESP8266 Alert - Temperature over 100F!");
  }
}
//#########################################
BLYNK_WRITE(V0) //slider in Virtual Pin 0 (0...256)
{
 PWM_LED1 = param.asInt();
analogWrite(Dimmer_Light,PWM_LED1);
}
BLYNK_WRITE(V1) //on off for additional light
{
RELAY_1 = param.asInt();
digitalWrite(Relay_1,RELAY_1);
 }
BLYNK_WRITE(V2) //on off for additional light
{
RELAY_2 = param.asInt();
digitalWrite(Relay_2,RELAY_2);
 }
BLYNK_WRITE(V3) //on off for additional light
{
RELAY_3 = param.asInt();
 digitalWrite(Relay_3,RELAY_3);
 }
BLYNK_WRITE(V4) //on off for additional light
{
RELAY_4 = param.asInt();
 digitalWrite(Relay_4,RELAY_4);
}
//########################################
void sendWifi() {
  int wifisignal = WiFi.RSSI();
  Blynk.virtualWrite(V10, wifisignal);
}
//########################################
void CheckConnection(){
  Connected2Blynk = Blynk.connected();
  if(!Connected2Blynk){
    Serial.println("Not connected to Blynk server"); 
    Blynk.connect(3333);  // timeout set to 10 seconds and then continue without Blynk
  }
  else{
    Serial.println("Connected to Blynk server");     
  }
}

//########################################
void setup()
{
//  pinMode(Wlan_Setup, INPUT);
  pinMode(Dimmer_Light, OUTPUT);
  pinMode(Relay_1, OUTPUT);
  pinMode(Relay_2, OUTPUT);
  pinMode(Relay_3, OUTPUT);
  pinMode(Relay_4, OUTPUT);
  pinMode(Blue_LED, OUTPUT);
  Serial.begin(9600);
  void wifisetup();
  WiFi.begin(WiFi.SSID().c_str(), WiFi.psk().c_str());
  delay(1000);
  Serial.println("");
  Serial.println("####################");
  Serial.println("WiFi connected");  
  Serial.println("controller IP address: ");
  Serial.println(WiFi.localIP());
  Serial.println("controller WLAN: ");
  Serial.println(WiFi.SSID());
  Serial.println("controller Signal Strength: ");
  Serial.println(WiFi.RSSI()); 
  Serial.println("controller set PSK: ");

  if ( digitalRead(TRIGGER_PIN) == LOW ) {
    WiFiManager wifiManager;
    //reset settings - for testing
    wifiManager.resetSettings();
    Serial.println("WiFi settings have been cleared!!!!!!");  
digitalWrite(Blue_LED,0);
    //sets timeout until configuration portal gets turned off
    //useful to make it all retry or go to sleep
    //in seconds
    //wifiManager.setTimeout(120);
    //WITHOUT THIS THE AP DOES NOT SEEM TO WORK PROPERLY WITH SDK 1.5 , update to at least 1.5.1
    //WiFi.mode(WIFI_STA);
    
    if (!wifiManager.startConfigPortal("CatHouseController")) {
      Serial.println("failed to connect and hit timeout");
      delay(3000);
      //reset and try again, or maybe put it to deep sleep
      ESP.reset();
      delay(5000);
    }
    //if you get here you have connected to the WiFi
    Serial.println("connected...Meow Meow Meow :)");
  }
  Serial.println(WiFi.psk()); 
  Serial.println("####################");
  delay(1000);

//  Blynk.begin(auth, ssid, pass);
  Blynk.begin(auth, WiFi.SSID().c_str(), WiFi.psk().c_str());
  Blynk.config(auth);
  Blynk.connect(3333);
if (Blynk.connect() == false) {
// Wait until connected
digitalWrite(Blue_LED,0);
}
else{
  digitalWrite(Blue_LED,1);
}
  Serial.println("Connected to Blynk server");
  timer.setInterval(11000L, CheckConnection); // check if still connected every 11 seconds 
#define TRIGGER_PIN 0
pinMode(TRIGGER_PIN, INPUT);

  Serial.begin(9600);
  // Initialize device.
  dht.begin();
  Serial.println(F("DHTxx Unified Sensor Example"));
  // Print temperature sensor details.
  sensor_t sensor;
  dht.temperature().getSensor(&sensor);
  Serial.println(F("------------------------------------"));
  Serial.println(F("Temperature Sensor"));
  Serial.print  (F("Sensor Type: ")); Serial.println(sensor.name);
  Serial.print  (F("Driver Ver:  ")); Serial.println(sensor.version);
  Serial.print  (F("Unique ID:   ")); Serial.println(sensor.sensor_id);
  Serial.print  (F("Max Value:   ")); Serial.print(sensor.max_value); Serial.println(F("°C"));
  Serial.print  (F("Min Value:   ")); Serial.print(sensor.min_value); Serial.println(F("°C"));
  Serial.print  (F("Resolution:  ")); Serial.print(sensor.resolution); Serial.println(F("°C"));
  Serial.println(F("------------------------------------"));
  // Print humidity sensor details.
  dht.humidity().getSensor(&sensor);
  Serial.println(F("Humidity Sensor"));
  Serial.print  (F("Sensor Type: ")); Serial.println(sensor.name);
  Serial.print  (F("Driver Ver:  ")); Serial.println(sensor.version);
  Serial.print  (F("Unique ID:   ")); Serial.println(sensor.sensor_id);
  Serial.print  (F("Max Value:   ")); Serial.print(sensor.max_value); Serial.println(F("%"));
  Serial.print  (F("Min Value:   ")); Serial.print(sensor.min_value); Serial.println(F("%"));
  Serial.print  (F("Resolution:  ")); Serial.print(sensor.resolution); Serial.println(F("%"));
  Serial.println(F("------------------------------------"));
  // Set delay between sensor readings based on sensor details.
  delayMS = sensor.min_delay / 1000;

timer.setInterval(300L, sendSensor);
timer.setInterval(300L, sendWifi);
}
//########################################
void loop()
{
  if(Connected2Blynk){
    Blynk.run();
  }
    timer.run();
}
//########################################
2 Likes

I’ve edited your post to add triple backticks at the start and end of your code so that the code displays correctly.

It would help if you provided some information about what this project does, what the relays control and what the app looks like.

Pete.

thanks… still trying to figure out how to do that… like add a picture

This all started when we build this awesome stray cat shelter so i decided to add some remote control and monitoring capability.
I have 4 channels of relay control, a 12 amp PWM LED dimmer… and a temperature and humidity sensor.
the PWM LED dimmer will control the indoor lighting and the relays will be for outdoor light, heating pads and fan / supplemental heat

3 Likes

I know of several forum members who live in worse situations than the stray cats in your area!
Don’t be surprised if you find one of them curled-up in your cat house one morning (especially the ones that think that ‘cat house’ is a euphemism for something else).

Just one comment about your code. You’re using WiFiManager, but seem to be missing some of the code needed to allow it to store the credentials and Auth Code in SPIFFS. Is this something that you dabbled with then forgot to remove fully, or am I overlooking something?

Pete.

3 Likes

it seems to be working and storing
shrug

You have SSID, Wifi Password and Auth Code hard-coded into your sketch, and once SSID and Password have been used by the ESP they will be stored anyway and it will continue to connect using these even if they are then not specified in code.
I think that if you took your device out of range of your Wi-Fi and tried to connect to a different Wi-Fi network using WiFiManager then it would probably fail. It would almost certainly not retain any credentials for the second Wi-Fi network after a reboot.

It’s only really an issue if you plan on giving one to someone else to use with their Wi-Fi and Blynk Auth Code.

Pete.

1 Like

im a network engineer by trade
i have several test SSID at the house and i have been switching between them.
// Blynk.begin(auth, ssid, pass); is commented out so
Blynk.begin(auth, WiFi.SSID().c_str(), WiFi.psk().c_str()); must be working yes?

1 Like

Explain please :smile:

Top left is a beer stain and top right is a piece of popcorn.
Obviously the stray cats aren’t very domesticated (unlike me of course :angel:) and they spill their beer and drop their popcorn when they’re watching TV in their 5 star cat shelter :open_mouth:

Pete.

3 Likes

3 Likes

its a stray cat shelter and i want to monitor temp and humidity as well as have a dimmable interior LED light as well and the ability to turn on and off outdoor lighting

Is the black cat saying “Honestly Kimberley, that’s not my used hypodermic, it’s that ginger tom from down the block…”?

Pete.

1 Like

My cat would be swatting at and stealing all those wires sooo quick… I am often chasing her down to retrieve something I was working on :stuck_out_tongue:

1 Like

i keep flux in hypo needles… works great

2 Likes

one of my tuxedo cats loves to steal arduino wires and oddly places them in the food bowl after chewing on them
i have to lock her out of the lab… she does the same thing to heat shrink

1 Like

That black cat is beautiful!

Are those flat brown things electric heating pads?

And I have got to ask, what is with the ball in the hoop thing?

Your trade skills show up in the orderly wiring :smiley: