I try to use IOT CLOUD to make a greenhouse temperature monitoring system, but there are some errors

Hi there,I am new to here , may make some mistakes ,thank you for the help!!
The device can automatically detect the room temperature and upload the data to the cloud,when temperature is too high, the red light will be on, the buzzer will work .
All code can run without error locally but it will have errors on IOT Cloud platform.It can see real-time temperature ,but when temperature too high the every device will work correctly 5 seconds,and then every devices go down. Here is the situation of the screen when error occure.


here is the Cloud Variables
Snipaste_2023-11-26_22-34-43
thank you for help!!!!!!!



#include "thingProperties.h"
#include <Arduino_LSM6DSOX.h>
#include <Servo.h>
#include <LiquidCrystal.h>
#define IN1 A1  
#define IN2 A0  
#define  ENA  A2 
// #define  Input  A6 
const int buzzerPin=3;
const int ledPin = 2;
const int ledPin2 = 4;
const int PIN_SERVO= 17;
LiquidCrystal lcd(0, 1, 8, 9, 10, 11);
double temperature;
int temperaturehigh=40;
int temperaturelow=10;
Servo myservo;


void setup() {
  Serial.begin(9600);
  
  myservo.attach(PIN_SERVO);
  pinMode(buzzerPin,OUTPUT);
  pinMode(ledPin, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(IN1,OUTPUT);
  pinMode(IN2,OUTPUT);
  pinMode(ENA,OUTPUT);
  pinMode(LEDR, OUTPUT);
  lcd.begin(16, 2);

  // 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(4);
  ArduinoCloud.printDebugInfo();
}
//get temperature
double gettemp(){
  double stepread=0;
  for(int i=0;i<10;i++){
    temperature=analogRead(A6);
    temperature=temperature*5;
    temperature-=0.5;
    temperature=temperature*100;
    temperature=temperature/1024;
    temperature+=3;
    stepread+=temperature;
    delay(10);
    
  }
  stepread=stepread/10;
  return stepread;
};

unsigned long previousMillis = 0;
const long interval = 1000;

void loop() {
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval) {
    
    previousMillis = currentMillis;
    ArduinoCloud.update();
    // myservo.write(0);
    // delay(1000);
    // digitalWrite(IN1,LOW); //stop
    // digitalWrite(IN2,LOW); 
    // analogWrite(ENA,255); 
    // digitalWrite(ledPin,LOW);
    // digitalWrite(ledPin2,LOW);
    // digitalWrite(LEDR, LOW);
    tone(buzzerPin,0);
    //system situation
    situation=true;
    
    //gain temperature
    temp=gettemp();
    //screen
    lcd.setCursor(0, 0);
    lcd.print("temperature");//print name
    lcd.setCursor(0, 1); // set the cursor to column 0, line 2
    lcd.print(temp);
    lcd.print("'C");
    //temperature too high
    while(temp>temperaturehigh){
      situation=false;
      digitalWrite(ledPin,HIGH);
  
      myservo.write(80);
      delay(1000);
      
      digitalWrite(IN1,HIGH);  
      digitalWrite(IN2,LOW);
      analogWrite(ENA,200);   
     
      for(int i=200;i<=800;i++)
      {
        tone(buzzerPin,i);
        delay(5);
      }
      delay(1000);
    
      for(int i=800;i>=200;i--)
      {
        tone(buzzerPin,i);
        delay(10);
      }
      temp=gettemp();
      if(temp<temperaturehigh){
        myservo.write(0);
        digitalWrite(ledPin,LOW);
        digitalWrite(IN1,LOW); 
        digitalWrite(IN2,LOW);
        situation=true;
      }
      ArduinoCloud.update();
    }
    //temperature too low
    while(temp<temperaturelow){
      situation=false;
      digitalWrite(ledPin2,HIGH);
      digitalWrite(LEDR, HIGH);
      for(int i=200;i<=800;i++)
      {
        tone(buzzerPin,i);
        delay(5);
      }
      delay(4000);
      for(int i=800;i>=200;i--)
      {
        tone(buzzerPin,i);
        delay(10);
      }
      temp=gettemp();
      if(temp>temperaturelow){
        digitalWrite(LEDR, LOW);
        digitalWrite(ledPin2,LOW);
        situation=true;
      }
      ArduinoCloud.update();
    }
  } 
}
  
void onTemphighChange()  {
  temperaturehigh=temphigh;
}

void onTemplowChange()  {
  temperaturelow=templow;
}

void onServoSwChange()  {
 if(servo_sw==false)
  {myservo.write(0);}
  else if(servo_sw==true)
  {myservo.write(80);}
}



and this is the dashboard.

I have found the reason ,there are something wrong on buzzer,I'm working on it now

1 Like

I finally find out that there maybe something wrong happen when one loop has more than 8300ms delay.I’m now finding other function to replace it.

Hi, you should avoid using delay in the loop function when connecting to the IoT Cloud, because this can interfere with the synchronization between the Cloud and your board.

You could try to follow this suggestion, which explains why you should avoid delay and gives a clear example of how to replace delay with millis:

thanks

hello,thank you for the reply , I use this method on a demo to tese the feasibility,but after i uplode there has something wrong,the red LEDs always flash at a frequency of 4 long and 4 short flashes and the buzzer dosen't sound.......and i can't connect to this board anymore unless I reset it. But this demo can work correctly on UNO R3 :thinking:
here is my demo code

bool go_to_sleep = false;
int sleep_timestamp = 0;
const int buzzerPin = 3;
void setup() {
  // put your setup code here, to run once:
pinMode(buzzerPin,OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
for(int i=200;i<=800;i++){
go_to_sleep = true;
if (go_to_sleep) {
  go_to_sleep = false;
  sleep_timestamp = millis() + 100;
}
while(millis() < sleep_timestamp) {
    tone(buzzerPin,i);
  
}
}
}

This light always flash at a frequency of 4 long and 4 short flashes. My board is Nano RP2040 Connect

Hi,
I think this last problem you're having with your demo is not related to the initial issue on the IoT Cloud.

Probably if you post it on a more appropriate category in the forum (e.g. the Development category), with a different title more specific to your current issue, you will get more chances to get support from other users.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.