I'm running an Uno r4 Wifi with code generated by Arduino for IoT. I have added my own code to add a web server that can be accessed locally. The code controls 2 valves and 1 blower for my boat lift control. The Arduino outputs are going to 3 relays. The code works flawlessly except for some reason when the Arduino first boots I can't communicate with it by Arduino Cloud or on the local network. If I manually hit the reset button on the Arduino, it begins to function properly and works great until a power outage and then I have to hit the reset button again to get it to function. I have some experience coding in C++, but very little in HTML. I initially had just the web server on the Arduino without the IoT code and did not have this issue. I believed I've messed something up when combining the 2 pieces of code. Any help would be appreciated.
Full serial error message
"***** Arduino IoT Cloud - configuration info *****
Device ID: 46adc2e7-83ac-44c0-8aff-3f5e6d9c69e3
MQTT Broker: iot.arduino.cc:8883
WiFi.status(): 0
Current WiFi Firmware: 0.4.1
Connected to "PBLake"
ArduinoIoTCloudTCP::handle_SyncTime could not get valid time. Retrying now.
ArduinoIoTCloudTCP::handle_ConnectMqttBroker could not connect to iot.arduino.cc:8883 Error: -2
ArduinoIoTCloudTCP::handle_ConnectMqttBroker could not connect to iot.arduino.cc:8883 Error: -2
ArduinoIoTCloudTCP::handle_ConnectMqttBroker could not connect to iot.arduino.cc:8883 Error: -2"
Below is my code:
include "arduino_secrets.h"
/*
Sketch generated by the Arduino IoT Cloud Thing "Untitled"
https://create.arduino.cc/cloud/things/691d9121-75f9-4f30-88f0-1d92394a9910
Arduino IoT Cloud Variables description
The following variables are automatically generated and updated when changes are made to the Thing
bool liftValve;
bool lowerValve;
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 "ArduinoGraphics.h"
#include "Arduino_LED_Matrix.h"
#include "thingProperties.h"
#define Lower 7
#define Raise 8
#define Blower 9
int CLift = 8;
int CBlower = 9;
int CLower = 7;
ArduinoLEDMatrix matrix;
// Set to ON
const uint32_t hi[] = {
0xFB3FB3DB,
0xBDBFDBFD,
0xB7FB3FB3
};
String output1 = "off";
String output2 = "off";
String header;
String Lift = " Raise ";
String Drop = " Lower ";
String Stop = " Stop ";
unsigned long currentTime = millis();
unsigned long previousTime = 0;
const long timeoutTime = 2000;
WiFiServer server(80);
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);
// Defined in thingProperties.h
initProperties();
// Connect to Arduino IoT Cloud
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
//if (ArduinoCloud.connected())
matrix.begin();
//Set pin properties
pinMode(CLift, OUTPUT);
pinMode(CLower, OUTPUT);
pinMode(CBlower, OUTPUT);
pinMode(Lower, OUTPUT);
pinMode(Raise, OUTPUT);
pinMode(Blower, OUTPUT);
//set valves to off position
digitalWrite(CBlower, LOW);
digitalWrite(CLift, LOW);
digitalWrite(CLower, LOW);
digitalWrite(Lower, LOW);
digitalWrite(Raise, LOW);
digitalWrite(Blower, LOW);
server.begin();
/*
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();
webServer();
LEDMatrix();
}
void LEDMatrix() {
matrix.loadFrame(hi);
}
void webServer() {
WiFiClient client = server.available();
if (client) {
Serial.println("New Client.");
String currentLine = "";
currentTime = millis();
previousTime = currentTime;
while (client.connected() && currentTime - previousTime <= timeoutTime) {
currentTime = millis();
if (client.available()) {
char c = client.read();
Serial.write(c);
header += c;
if (c == '\n') {
if (currentLine.length() == 0) {
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println("Connection: close");
client.println();
if (header.indexOf("GET /LowerOn") >= 0) {
output1 = "on";
digitalWrite(Lower, HIGH);
matrix.beginDraw();
matrix.textScrollSpeed(50);
matrix.textFont(Font_5x7);
matrix.beginText(0, 1, 255, 0, 0);
matrix.println(Drop);
matrix.endText(SCROLL_LEFT);
matrix.endDraw();
} else if (header.indexOf("GET /LowerOff") >= 0) {
output1 = "off";
digitalWrite(Lower, LOW);
matrix.beginDraw();
matrix.textScrollSpeed(50);
matrix.textFont(Font_5x7);
matrix.beginText(0, 1, 255, 0, 0);
matrix.println(Stop);
matrix.endText(SCROLL_LEFT);
matrix.endDraw();
}
if (header.indexOf("GET /RaiseOn") >= 0) {
output2 = "on";
digitalWrite(Raise, HIGH);
delay(1);
digitalWrite(Blower, HIGH);
matrix.beginDraw();
matrix.textScrollSpeed(50);
matrix.textFont(Font_5x7);
matrix.beginText(0, 1, 255, 0, 0);
matrix.println(Lift);
matrix.endText(SCROLL_LEFT);
matrix.endDraw();
} else if (header.indexOf("GET /RaiseOff") >= 0) {
output2 = "off";
digitalWrite(Blower, LOW);
delay(1);
digitalWrite(Raise, LOW);
matrix.beginDraw();
matrix.textScrollSpeed(50);
matrix.textFont(Font_5x7);
matrix.beginText(0, 1, 255, 0, 0);
matrix.println(Stop);
matrix.endText(SCROLL_LEFT);
matrix.endDraw();
}
client.println("<!DOCTYPE html><html>");
client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
client.println("<link rel=\"icon\" href=\"data:,\">");
client.println("<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}");
client.println(".on { background-color: #E74C3C; border: 5px; color: white; padding: 16px 40px; border-radius: 20px;");
client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}");
client.println(".off {background-color: #27AE60;border: 5px; color: white; padding: 16px 40px; border-radius: 20px;");
client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}</style></head>");
client.println("<body><h1>Presto Lift</h1>");
client.println("<p>Lower " + output1 + "</p>");
if (output1 == "off") {
client.println("<p><a href=\"/LowerOn\"><button class=\"off\">Lower</button></a></p>");
} else {
client.println("<p><a href=\"/LowerOff\"><button class=\"on\">Stop</button></a></p>");
}
client.println("<p>Raise " + output2 + "</p>");
if (output2 == "off") {
client.println("<p><a href=\"/RaiseOn\"><button class=\"off\">Raise</button></a></p>");
} else {
client.println("<p><a href=\"/RaiseOff\"><button class=\"on\">Stop</button></a></p>");
}
client.println("</body></html>");
client.println();
break;
} else {
currentLine = "";
}
} else if (c != '\r') {
currentLine += c;
}
}
}
header = "";
client.stop();
Serial.println("Client disconnected.");
Serial.println("");
}
}
/*
Since LiftValve is READ_WRITE variable, onLiftValveChange() is
executed every time a new value is received from IoT Cloud.
*/
void onLiftValveChange() {
// Add your code here to act upon LiftValve change
Serial.println(liftValve);
if (liftValve) {
digitalWrite(CLift, HIGH);
digitalWrite(CBlower, HIGH);
} else {
digitalWrite(CLift, LOW);
digitalWrite(CBlower, LOW);
}
}
/*
Since LowerValve is READ_WRITE variable, onLowerValveChange() is
executed every time a new value is received from IoT Cloud.
*/
void onLowerValveChange() {
// Add your code here to act upon LowerValve change
Serial.println(lowerValve);
if (lowerValve) {
digitalWrite(CLower, HIGH);
} else {
digitalWrite(CLower, LOW);
}
}