After uploading the sketch through the full editor, the connectivity status of the Arduino Nano 33 IoT is still offline.
I also found a forum with the same problem (Status of Arduino Nano 33 IoT is offline even though successfully uploaded the sketch in full editor) but the solution ain't working for me. After successfully uploading, the arduino also keeps on disconnecting and connecting on its own.
I am guessing it could be because of my long code or it could also be something else. I hope someone here could help me.
Also, here is my code for this project. Basically, it monitors and automates certain parameters in a fish tank.
/*
Sketch generated by the Arduino IoT Cloud Thing "Untitled"
https://create.arduino.cc/cloud/things/a82579f0-6aa9-43a2-b817-23fa66471e0b
Arduino IoT Cloud Variables description
The following variables are automatically generated and updated when changes are made to the Thing
float DisOxy;
float pH;
float TDS;
float Temp;
int WaterLevel;
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 <Arduino.h> // Library for DO
#include <OneWire.h> // Library for Temp
#include <DallasTemperature.h> // Library for Temp
#include <Wire.h> // Library for pH
#include <NusabotSimpleTimer.h> // Library for pH
#define ONE_WIRE_BUS 13 // Temp Sensor Pin Assignment
#define TdsSensorPin A3 // TDS Pin Assignment
//pH pin Assignment A1
//Waterlevel Pin Assignment A2
#define DO_PIN A0 // DO pin assignment
#define VREF 5 // analog reference voltage(Volt) of the ADC // For TDS
#define SCOUNT 30 // sum of sample point // For TDS
#define VREF1 5000 //VREF (mv) // For DO
#define ADC_RES 1024 //ADC Resolution // For DO
#define TWO_POINT_CALIBRATION 0 //DO Single-point calibration Mode=0 (1 if Two-point)
#define READ_TEMP (25) //Current water temperature ℃, Or temperature sensor function // For DO
//Single point calibration needs to be filled CAL1_V and CAL1_T
#define CAL1_V (1000) //mv *For Calibration*
#define CAL1_T (26) //℃
//Two-point calibration needs to be filled CAL2_V and CAL2_T
//CAL1 High temperature point, CAL2 Low temperature point
#define CAL2_V (1300) //mv
#define CAL2_T (15) //℃
OneWire oneWire(ONE_WIRE_BUS); // For Temp Sensor
DallasTemperature sensors(&oneWire); // For Temp Sensor
NusabotSimpleTimer timer; // For ph Sensor
const int intakeRelayPin = 2; // Relay pin for intake solenoid valve
const int outtakeRelayPin = 3; // Relay pin for outtake solenoid valve
const int chillerRelayPin = 4; // Relay pin for aquarium chiller
const int aeratorRelayPin = 5; // Relay pin for aerator
int WaterLevelpin = A2; // WaterLevel Sensor Pin Assignment
float calibration_value = 21.34 - 0.2; //Calibration value for pH
int phval = 0;
unsigned long int avgval;
int buffer_arr[10], temp;
int analogBuffer[SCOUNT]; // store the analog value in the array, read from ADC // For TDS
int analogBufferTemp[SCOUNT]; // For TDS
int analogBufferIndex = 0, copyIndex = 0; // For TDS
float averageVoltage = 0, temperature = 25; // For TDS
float ideal_DO_lower = 6000;
float ideal_DO_upper = 8000;
float ideal_Temp_lower = 26;
float ideal_Temp_upper = 28;
float ideal_pH_lower = 7;
float ideal_pH_upper = 8;
float ideal_TDS_lower = 200;
float ideal_TDS_upper = 500;
bool adjustWaterLevel = false; // Flag to control water level adjustments
// Setup Calibration for DO
const uint16_t DO_Table[41] = {
14460, 14220, 13820, 13440, 13090, 12740, 12420, 12110, 11810, 11530,
11260, 11010, 10770, 10530, 10300, 10080, 9860, 9660, 9460, 9270,
9080, 8900, 8730, 8570, 8410, 8250, 8110, 7960, 7820, 7690,
7560, 7430, 7300, 7180, 7070, 6950, 6840, 6730, 6630, 6530, 6410
};
uint8_t Temperaturet;
uint16_t ADC_Raw;
uint16_t ADC_Voltage;
uint16_t DO;
int16_t readDO(uint32_t voltage_mv, uint8_t temperature_c)
{
#if TWO_POINT_CALIBRATION == 0
uint16_t V_saturation = (uint32_t)CAL1_V + (uint32_t)35 * temperature_c - (uint32_t)CAL1_T * 35;
return (voltage_mv * DO_Table[temperature_c] / V_saturation);
#else
uint16_t V_saturation = (int16_t)((int8_t)temperature_c - CAL2_T) * ((uint16_t)CAL1_V - CAL2_V) / ((uint8_t)CAL1_T - CAL2_T) + CAL2_V;
return (voltage_mv * DO_Table[temperature_c] / V_saturation);
#endif
}
void setup() {
sensors.begin();
Wire.begin();
// Initialize serial and wait for port to open:
Serial.begin(9600);
pinMode(TdsSensorPin, INPUT);
// Resposne: pin2=valve in; pin3=valve out; pin4= STC200; pin5= AP226
for (int pinNumber = 2; pinNumber < 6; pinNumber ++) {
pinMode(pinNumber, OUTPUT);
digitalWrite(pinNumber, HIGH);
}
// 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
WaterLevel = analogRead(WaterLevelpin); // Stores the WaterLevel variable
sensors.requestTemperatures(); // For Temp Sensor
Temp = sensors.getTempCByIndex(0); // Stores the Temp in Celsius
DisOxy = (readDO(ADC_Voltage, Temperaturet));
// void loop for DO
Temperaturet = (uint8_t)READ_TEMP;
ADC_Raw = analogRead(DO_PIN);
ADC_Voltage = uint32_t(VREF1) * ADC_Raw / ADC_RES;
// void loop for pH
timer.run(); // Initiates SimpleTimer
for (int i = 0; i < 10; i++)
{
buffer_arr[i] = analogRead(A1);
delay(30);
}
for (int i = 0; i < 9; i++)
{
for (int j = i + 1; j < 10; j++)
{
if (buffer_arr[i] > buffer_arr[j])
{
temp = buffer_arr[i];
buffer_arr[i] = buffer_arr[j];
buffer_arr[j] = temp;
}
}
}
avgval = 0;
for (int i = 2; i < 8; i++)
avgval += buffer_arr[i];
float volt = (float)avgval * 3.3 / 1024 / 6;
pH = -5.70 * volt + calibration_value;
// End of void loop for pH
// void loop for TDS
static unsigned long analogSampleTimepoint = millis();
if (millis() - analogSampleTimepoint > 40U) //every 40 milliseconds,read the analog value from the ADC
{
analogSampleTimepoint = millis();
analogBuffer[analogBufferIndex] = analogRead(TdsSensorPin); //read the analog value and store into the buffer
analogBufferIndex++;
if (analogBufferIndex == SCOUNT)
analogBufferIndex = 0;
}
static unsigned long printTimepoint = millis();
if (millis() - printTimepoint > 800U)
{
printTimepoint = millis();
for (copyIndex = 0; copyIndex < SCOUNT; copyIndex++)
analogBufferTemp[copyIndex] = analogBuffer[copyIndex];
averageVoltage = getMedianNum(analogBufferTemp, SCOUNT) * (float)VREF / 1024.0; // read the analog value more stable by the median filtering algorithm, and convert to voltage value
float compensationCoefficient = 1.0 + 0.02 * (temperature - 25.0); //temperature compensation formula: fFinalResult(25^C) = fFinalResult(current)/(1.0+0.02*(fTP-25.0));
float compensationVolatge = averageVoltage / compensationCoefficient; //temperature compensation
TDS = (133.42 * compensationVolatge * compensationVolatge * compensationVolatge - 255.86 * compensationVolatge * compensationVolatge + 857.39 * compensationVolatge) * 0.57; //convert voltage value to tds value
Serial.print("TDS Value: ");
Serial.print(TDS, 0);
Serial.print(" WL: ");
Serial.print(WaterLevel);
Serial.print(" C: ");
Serial.print(Temp);
Serial.print(" pH: ");
Serial.print(pH);
Serial.println(" DO:\t" + String(readDO(ADC_Voltage, Temperaturet)) + "\t");
delay(1000);
}
}
int getMedianNum(int bArray[], int iFilterLen)
{
int bTab[iFilterLen];
for (byte i = 0; i < iFilterLen; i++)
bTab[i] = bArray[i];
int i, j, bTemp;
for (j = 0; j < iFilterLen - 1; j++)
{
for (i = 0; i < iFilterLen - j - 1; i++)
{
if (bTab[i] > bTab[i + 1])
{
bTemp = bTab[i];
bTab[i] = bTab[i + 1];
bTab[i + 1] = bTemp;
}
}
}
if ((iFilterLen & 1) > 0)
bTemp = bTab[(iFilterLen - 1) / 2];
else
bTemp = (bTab[iFilterLen / 2] + bTab[iFilterLen / 2 - 1]) / 2;
return bTemp;
// End of void loop for TDS
// Monitor sensors and automate the system
if (DisOxy < ideal_DO_lower || DisOxy > ideal_DO_upper) {
// Activate aerator until ideal DO is reached
digitalWrite(5, HIGH);
} else {
digitalWrite(5, LOW);
}
if (Temp < ideal_Temp_lower || Temp > ideal_Temp_upper) {
// Activate chiller until ideal temperature is reached
digitalWrite(4, HIGH);
} else {
digitalWrite(4, LOW);
}
if (pH < ideal_pH_lower || pH > ideal_pH_upper || TDS < ideal_TDS_lower || TDS > ideal_TDS_upper) {
// Adjust water level based on pH and TDS
adjustWaterLevel = true;
}
if (adjustWaterLevel && WaterLevel > 400) {
digitalWrite(3, HIGH); // Open outtake solenoid valve
if (WaterLevel <= 50) {
digitalWrite(3, LOW); // Close outtake solenoid valve
digitalWrite(2, HIGH); // Open intake solenoid valve
adjustWaterLevel = false;
}
}
if (!adjustWaterLevel && WaterLevel < 400) {
digitalWrite(3, LOW); // Close outtake solenoid valve
digitalWrite(2, LOW); // Close intake solenoid valve
}
}```
