My Arduino MKRIoT Wifi 1010 keeps disconnecting and reconnecting, I am not sure what the problem is and would like professional help. here is my code:
#include <Arduino_MKRIoTCarrier.h>
#include "thingProperties.h"
#include <math.h>
MKRIoTCarrier carrier;
uint32_t colorRed = carrier.leds.Color(255,0,0);
uint32_t colorGreen = carrier.leds.Color(0,0,255);
uint32_t colorYellow = carrier.leds.Color(255,255,0);
int moistPin = A5;
String relayState1 = "";
String relayState2 = "";
float p0 = 101.325;
float HH = 44330 * (1-pow((pressure / p0), (1 / 5.255)));
int r,g,b;
void setup() {
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);
setDebugMessageLevel(2);
ArduinoCloud.printDebugInfo();
}
void loop() {
ArduinoCloud.update();
if (carrier.Light.colorAvailable()) {
int none;//not gonna be used
carrier.Light.readColor(none, none, none, light);
}
pressure = carrier.Pressure.readPressure();
temperature = carrier.Env.readTemperature();
humidity = carrier.Env.readHumidity();
int rawMoistValue = analogRead(moistPin);
moistValue = map(rawMoistValue, 0, 1023, 100, 0);
if (temperature >= 30 && moistValue >= 30) {
carrier.leds.fill((colorRed), 0, 5);
carrier.Buzzer.sound(500);
carrier.leds.show();
} if (temperature < 20 || moistValue < 20) {
carrier.leds.fill(255, 255, 0);
carrier.leds.show();
} else {
carrier.Buzzer.noSound();
carrier.leds.fill((colorYellow), 0, 5);
carrier.leds.show();
}
if (relay_1 == true) {
carrier.Relay1.open();
relayState1 = "ON";
}
else {
carrier.Relay1.close();
relayState1 = "OFF";
}
if (relay_2 == true) {
carrier.Relay2.open();
relayState2 = "ON";
}
else {
carrier.Relay2.close();
relayState2 = "OFF";
}
}
/*
Since Light is READ_WRITE variable, onLightChange() is
executed every time a new value is received from IoT Cloud.
*/
void onLightChange() {
// Add your code here to act upon Light change
}
/*
Since Relay1 is READ_WRITE variable, onRelay1Change() is
executed every time a new value is received from IoT Cloud.
*/
void onRelay1Change() {
// Add your code here to act upon Relay1 change
}
/*
Since Relay2 is READ_WRITE variable, onRelay2Change() is
executed every time a new value is received from IoT Cloud.
*/
void onRelay2Change() {
// Add your code here to act upon Relay2 change
}
Please provide a detailed description of what you mean by this.
I'm not sure whether you are referring to the USB connection (an Arduino board can have a behavior where the operating system acts like you are repeatedly unplugging and then replugging the USB cable to your computer under certain conditions), or referring to the board's network to the Arduino Cloud server.
As I said, I am new to this forum, I am completely new to arduino forums, I did not know there were "stickies" or things that could help me with posting. I have a time sensitive problem, I saw "create post", I click. I do believe that is a very valid excuse, however, I do thank you for sending me the link. I just wish for some assistance in solving my problem and be on my way, I ask for your understanding.
Thank you for asking clarifying questions, that is very helpful. I'm not too sure of it, but it keeps clicking like when you tap the reset button twice to put it in boot loader, sometimes the port will show on the serial port, then it will begin the clicking, then it's not found on the port. I believe my USB cable is working as I was able to upload a sketch for a moment before it went back to clicking.
I pray its not the chip that is messed up. When I clicked the reset button it was not resetting, I think it was an orange light, possibly the relay, but whenever I clicked the reset button, the orange light was clicking simultaneously. It fixed itself though, I am not sure how.
The root cause of the problem is that you forgot to add a carrier.begin() call to the setup function of your sketch. You must call carrier.begin() when using the "Arduino_MKRIoTCarrier" library.
Because you didn't initialize the library by calling carrier.begin(), the sketch hung at the carrier.Light.colorAvailable(). By default, the ArduinoIoTCloud library used for Arduino Cloud Thing sketches sets a "watchdog timer". This automatically resets the board if too much time elapses between calls to ArduinoCloud.update(). This is intended to act as an emergency system to allow the Thing to recover from occasional unexpected hangs.
The "disconnecting and reconnecting" symptom you observed was caused by the board periodically resetting as the hang caused the watchdog time to time out.
So add a carrier.begin() call to the setup function of your sketch and then upload the sketch to your board. Hopefully this time it will work as expected.
You can learn more about how to use the "Arduino_MKRIoTCarrier" library from this guide: