Issue in Smart Farming System with Arduino IoT Cloud

Hello There…

I am Johan, I am currently working on an automated farming system using the IoT cloud platform. I am using an Arduino nano 33 IoT, a DHT22 sensor attached to pin 2, a LDR sensor on pin A0 and an analog temperature sensor (KY-013) on analog pin A1. And the sensors are powered from an external breadboard power supply as I am not sure if the nano 33 IoT has a 5v pin. But I am facing an issue with my code as my DHT22 and LDR sensor is not switching on, the readings from the temperature sensor are going haywire and the <DHT.h> and <SimpleDHT.h> libraries are not working. I would like to understand whether it is with my code or any other issues in my code.

This is my code

#include "thingProperties.h";
#include <SimpleDHT.h>;
#define pinDHT22 A2;
SimpleDHT22 dht22(pinDHT22);
#define TEMP_PIN A1;
#define LIGHT_PIN A0;


int Vo;
float R1 = 10000; // value of R1 on board
float logR2, R2, T;
float c1 = 0.001129148, c2 = 0.000234125, c3 = 0.0000000876741;


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);

  /*
     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();

}



void onTemperatureChange() {
  Vo = analogRead(TEMP_PIN);
  R2 = R1 * (1023.0 / (float)Vo - 1.0); //calculate resistance on thermistor
  logR2 = log(R2);
  T = (1.0 / (c1 + c2*logR2 + c3*logR2*logR2*logR2)); // temperature in Kelvin
  temperature = T - 273.15;
  Serial.print(temperature);
}


void onHumidityChange() {
  int humidity = 0;
  Serial.print(pinDHT22);
}

void onLightChange() {
  analogWrite(LIGHT_PIN, INPUT);
  Serial.print(analogRead(LIGHT_PIN));
}

This is mt thingProperties.h code

#include <ArduinoIoTCloud.h>
#include <Arduino_ConnectionHandler.h>


const char THING_ID[] = "daf1adac-56e2-48b4-bb72-ee92aee4ccbf";

const char SSID[]     = SECRET_SSID;    // Network SSID (name)
const char PASS[]     = SECRET_PASS;    // Network password (use for WPA, or use as key for WEP)

void onLightChange();
void onTemperatureChange();

int light;
CloudRelativeHumidity humidity;
float temperature;

void initProperties(){

  ArduinoCloud.setThingId(THING_ID);
  ArduinoCloud.addProperty(light, READWRITE, 3 * SECONDS, onLightChange);
  ArduinoCloud.addProperty(humidity, READ, 3 * SECONDS, NULL);
  ArduinoCloud.addProperty(temperature, READWRITE, 3 * SECONDS, onTemperatureChange);

}

WiFiConnectionHandler ArduinoIoTPreferredConnection(SSID, PASS);

Please find the below attachment to see what happened to my Dashboard

Please let me know if you find any issue in my code. I would really appreciate your help

Thank you

do the sensors all work Ok when you test them individually?

I don't know that this is your problem, but it is unlikely to be correct:

void onLightChange() {
  analogWrite(LIGHT_PIN, INPUT);
  Serial.print(analogRead(LIGHT_PIN));
}

Setting something other than a pin mode to a value of INPUT is wrong.
analogWrite sets a PWM value, so reading the value with analogRead, which uses an ADC, is wrong.
I don't know the Arduino nano 33 IoT but I sure hope that LIGHT_PIN does PWM if this is going to work at all.

Hi,

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Tom… :slight_smile:

TomGeorge:
Hi,

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Tom… :slight_smile:

Yes sure, please find the attached file

Thank you for helping me

horace:
do the sensors all work Ok when you test them individually?

Yes they do.............And I also have one more query...........Can we take both humidity and temperature readings from the DHT22 and display it separately in the dashboard (as I would like to do this to remove the analog temperature sensor)

Hi,
OPs circuit;


You will need a 10k or 4k7 resisitor between signal and supply on the DHT22.
You need to label the pins on each of the components, a hand drawn pencil and paper would have been easier I think.

Tom... :slight_smile:

johansanju06:
Yes they do.............

when you tested the sensors individually did you use the circuit of post #4 ?

to draw simple schematic diagrams have a look at TinyCAD

horace:
when you tested the sensors individually did you use the circuit of post #4 ?

to draw simple schematic diagrams have a look at TinyCAD

yes I did

Hello There,
Since my code has a lot of issues.....I would appreciate if (and only if) you would be able to let me know if you had seen something similar with the code to mine which was using the IoT cloud platform as I am a beginner and feel that correcting my code would be a bigger problem to you guys.

P.S Thank you @horace for the link to TinyCAD, and @TomGeorge for giving me valuable advice on the resistors as it helped me in using the sensor individually while I was testing it.

Thanking you
Johan