I am having trouble with random false readings from my input pins. The project is an indicator for the state of the garage doors (his and her). There is a set of leds that are visible inside the house to show if they're open or closed. I had the sketch running flawlessly on the final hardware sitting on my desk but without the wire leads used to connect to the magnetic door sensors. After I installed everything in the garage, the leds did not match the door state consistently. I suspected the magnetic switches might be the issue but removing the switches completely (leaving wires completely disconnected) is still generating erroneous values. I used an exact between the terminals on the board in case there was residue conducting current. It seems like it works if I remove the wire completely. I think the pull up resistors are enabled correctly (although using older code). Board is an ESP-32 (Elegoo).
General functions - check wifi and flash leds if not connected
Check time and turn off leds after 11pm local time (using very rough dst calculation)
Check sensors and turn leds on/off as appropriate - problem area in final install
My first wifi project so i am really bummed i couldn't cross the line with it.
/*
Sketch generated by the Arduino IoT Cloud Thing "Untitled"
https://create.arduino.cc/cloud/things/af63c06b-c7b8-4f1e-ace1-7f30240c4d10
Arduino IoT Cloud Variables description
The following variables are automatically generated and updated when changes are made to the Thing
String timeStringCloud;
bool ledHerCloud;
bool ledHisCloud;
Variables which are marked as READ/WRITE in the Cloud Thing will also have functions
which are called when their values are changed from the Dashboard.
These functions are generated with the Thing and added at the end of this sketch.
*/
#include "thingProperties.h"
#include "time.h"
int sensorLeft = 34;
int sensorRight = 23;
int ledHerRed = 12;
int ledHerGreen = 27;
int ledHisRed = 18;
int ledHisGreen = 21;
int wifiCounter = 0;
String timeString = "Daytime";
struct tm timeinfo; // time related...
int dstBeginMonth = 3; // DST Starts March 8th (on or about) - using DST begins in March and ends in November
int dstEndMonth = 11; // DST Ends November 1st (on or about)
void setup() {
// Initialize serial and wait for port to open:
Serial.begin(9600);
// This delay gives the chance to wait for a Serial Monitor without blocking if none is found
delay(1500);
// My Adds:
pinMode(sensorLeft, INPUT);
digitalWrite(sensorLeft, HIGH); // enable pull up resistor
pinMode(sensorRight, INPUT);
digitalWrite(sensorRight, HIGH); // enable pull up
pinMode(ledHerRed, OUTPUT);
pinMode(ledHerGreen, OUTPUT);
pinMode(ledHisRed, OUTPUT);
pinMode(ledHisGreen, OUTPUT);
// Defined in thingProperties.h
initProperties();
// Connect to Arduino IoT Cloud
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
/*
The following function allows you to obtain more information
related to the state of network and IoT Cloud connection and errors
the higher number the more granular information you’ll get.
The default is 0 (only errors).
Maximum is 4
*/
setDebugMessageLevel(2);
ArduinoCloud.printDebugInfo();
}
void loop() {
ArduinoCloud.update();
// Your code here
wifiCounter ++; // counter for wifi connection test
if (timeString == "Daytime") {
ledUpdate(sensorLeft);
ledUpdate(sensorRight);
}
else {
ledsOff();
}
if (!getLocalTime(&timeinfo)) {
Serial.println("No time available (yet)");
}
else {
timeString = timeChecker();
Serial.println("time check:");
Serial.println(timeString);
}
cloudUpdates(); // set the cloud variables for the dashboard
delay(500);
Serial.println(ledHisCloud);
Serial.println(ledHerCloud);
Serial.println(WiFi.status());
if (wifiCounter >= 20) {
if (WiFi.status() != WL_CONNECTED) {
noWiFiConnection();
}
wifiCounter = 0;
}
}
void ledUpdate(int x) { // Based on the sensor state, we set the led lights
if (x == sensorLeft) {
digitalWrite(ledHerGreen, digitalRead(x));
digitalWrite(ledHerRed, !digitalRead(x));
}
if (x == sensorRight){
digitalWrite(ledHisGreen, digitalRead(x));
digitalWrite(ledHisRed, !digitalRead(x));
}
}
void ledsOff() { // to turn off the physical leds overnight
digitalWrite(ledHerGreen, LOW);
digitalWrite(ledHerRed, LOW);
digitalWrite(ledHisGreen, LOW);
digitalWrite(ledHisRed, LOW);
}
void cloudUpdates() { // Based on sensor state, update cloud variables, update time value
ledHerCloud = digitalRead(sensorLeft);
ledHisCloud = digitalRead(sensorRight);
timeStringCloud = timeString;
}
void noWiFiConnection() {
for (int i = 0; i < 10; i++)
{
Serial.println("no_wifi");
digitalWrite(ledHerGreen, HIGH);
digitalWrite(ledHisGreen, HIGH);
digitalWrite(ledHerRed, LOW);
digitalWrite(ledHisRed, LOW);
delay(500);
digitalWrite(ledHerGreen, LOW);
digitalWrite(ledHisGreen, LOW);
digitalWrite(ledHerRed, HIGH);
digitalWrite(ledHisRed, HIGH);
delay(500);
}
}
String timeChecker() {
String t;
int timeOffset = 0; // vs UTC
int myHour = 0; // local hour
Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S");
if ((timeinfo.tm_mon >= dstBeginMonth) && (timeinfo.tm_mon < dstEndMonth)) {
Serial.println("it's Daylight Savings Time");
timeOffset = 4;
}
else if ((timeinfo.tm_mon < dstBeginMonth) || (timeinfo.tm_mon >= dstEndMonth)) {
Serial.println("it's not Daylight Savings Time");
timeOffset = 5;
}
if (timeinfo.tm_hour > timeOffset) {
myHour = timeinfo.tm_hour - timeOffset;
Serial.println("Hour is " + String(myHour));
}
else {
myHour = timeinfo.tm_hour + 24 - timeOffset;
Serial.println("Hour is " + String(myHour));
}
if ((myHour >= 23) || (myHour < 8)) { // hours for overnight - 11 pm - 8 am
t = "Overnight";
}
else {
t = "Daytime";
}
return t;
}

