Hi, may someone please help me understand where my error is connecting the DS18B20 waterproof sensor to the Arduino Opla through the MKR Wifi 1010?
I have correct temperature readings from the sensor using UNO but when I connect it to MKR 1010 and run a modified sketch (originally the Smart Garden project) to also take the DS18B20 input, I can't get the temperature, just reading -127 (the error code?).
The sensor cable is properly connected through a PlugableTerminal to MKR's VCC (3.3V), GND and PIN 6.
I went through many related posts but couldn't pin the issue, also just starting with Arduino IoT, I would really appreciate any suggestions, thank you!
#include <OneWire.h>
#include <DallasTemperature.h>
#include "thingProperties.h"
#include <Arduino_MKRIoTCarrier.h>
MKRIoTCarrier carrier;
#define ONE_WIRE_BUS 6
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature sensor
DallasTemperature sensors(&oneWire);
int moistPin;
String waterPumpState;
String coolingFanState;
String lightState;
uint32_t lightsOn = carrier.leds.Color(82, 118, 115);
uint32_t lightsOff = carrier.leds.Color(0, 0, 0);
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);
sensors.begin();
sensors.setResolution(12);
// Defined in thingProperties.h
initProperties();
// Connect to Arduino IoT Cloud
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
//Get Cloud Info/errors , 0 (only errors) up to 4
setDebugMessageLevel(2);
ArduinoCloud.printDebugInfo();
//Wait to get cloud connection to init the carrier
while (ArduinoCloud.connected() != 1) {
ArduinoCloud.update();
delay(500);
}
delay(500);
CARRIER_CASE = false;
carrier.begin();
moistPin = carrier.getBoardRevision() == 1 ? A5 : A0; //assign A0 or A5 based on HW revision
carrier.display.setRotation(0);
delay(1500);
}
void loop() {
//Update the Cloud
ArduinoCloud.update();
sensors.requestTemperatures();
//read temperature and humidity
temperature = carrier.Env.readTemperature();
humidity = carrier.Env.readHumidity();
water = waterTemperature();
//read raw moisture value
int raw_moisture = analogRead(moistPin);
//map raw moisture to a scale of 0 - 100
moisture = map(raw_moisture, 0, 1023, 0, 100);
//read ambient light
while (!carrier.Light.colorAvailable()) {
delay(5);
}
int none; //We dont need RGB colors
carrier.Light.readColor(none, none, none, light);
Serial.print("Celsius temperature: ");
Serial.print(sensors.getTempCByIndex(0));
Serial.print(" - Fahrenheit temperature: ");
Serial.println(sensors.getTempFByIndex(0));
//water = sensors.getTempCByIndex(0);
delay(100);
}
float waterTemperature() {
sensors.requestTemperatures();
float waterTemp = sensors.getTempCByIndex(0);
return waterTemp;
}
void onWaterpumpChange() {
if (waterpump == true) {
carrier.Relay2.open();
waterPumpState = "PUMP: ON";
} else {
carrier.Relay2.close();
waterPumpState = "PUMP: OFF";
}
updateScreen();
}
void onCoolingFanChange() {
if (cooling_fan == true) {
carrier.Relay1.open();
//coolingFanState = "FAN: ON";
coolingFanState = "TEMP: C";
} else {
carrier.Relay1.close();
//coolingFanState = "FAN: OFF";
coolingFanState = "TEMP: F";
}
updateScreen();
}
void onArtificialLightChange() {
if (artificial_light == true) {
carrier.leds.fill(lightsOn, 0, 5);
carrier.leds.show();
lightState = "BLACK: 100C";
} else {
carrier.leds.fill(lightsOff, 0, 5);
carrier.leds.show();
lightState = "GREEN: 70C";
}
updateScreen();
}
//Update displayed Info
void updateScreen() {
carrier.display.fillScreen(ST77XX_BLACK);
carrier.display.setTextColor(ST77XX_WHITE);
carrier.display.setTextSize(3);
carrier.display.setCursor(40, 50);
carrier.display.print(water);
carrier.display.setCursor(40, 90);
carrier.display.print(coolingFanState);
carrier.display.setCursor(40, 130);
carrier.display.print(lightState);
}
Here is also the thingProperties.h:
// Code generated by Arduino IoT Cloud, DO NOT EDIT.
#include <ArduinoIoTCloud.h>
#include <Arduino_ConnectionHandler.h>
const char SSID[] = SECRET_SSID; // Network SSID (name)
const char PASS[] = SECRET_OPTIONAL_PASS; // Network password (use for WPA, or use as key for WEP)
void onArtificialLightChange();
void onCoolingFanChange();
void onWaterpumpChange();
float humidity;
float temperature;
float water;
int light;
int moisture;
bool artificial_light;
bool cooling_fan;
bool waterpump;
void initProperties(){
ArduinoCloud.addProperty(humidity, READ, ON_CHANGE, NULL);
ArduinoCloud.addProperty(temperature, READ, ON_CHANGE, NULL);
ArduinoCloud.addProperty(water, READ, 2 * SECONDS, NULL);
ArduinoCloud.addProperty(light, READ, ON_CHANGE, NULL);
ArduinoCloud.addProperty(moisture, READ, ON_CHANGE, NULL);
ArduinoCloud.addProperty(artificial_light, READWRITE, ON_CHANGE, onArtificialLightChange);
ArduinoCloud.addProperty(cooling_fan, READWRITE, ON_CHANGE, onCoolingFanChange);
ArduinoCloud.addProperty(waterpump, READWRITE, ON_CHANGE, onWaterpumpChange);
}
WiFiConnectionHandler ArduinoIoTPreferredConnection(SSID, PASS);
