Hi,
I managed to set up a couple of sensors. But now I got stuck. I have 2 DHT22’s, a MAX6675, and a digittal out channel coupled and listed as things: Works fine. But if I add another thing, no matter whether it is an LED to control or another sensor, the things crashe and are corrupted:
The things lose their infos and values:
If I access them directly via the browser (http://192.168.0.100/things/DHT_WEST/properties), I can perfectly see the JSON-object and even the properites are updated (http://192.168.0.100/things/DHT_WEST/properties)
Is there a limited number of things per adapter or am I missing something else?
Here the code:
// ***************************Libraries***************************
// Defintions for WebThings
#define LARGE_JSON_BUFFERS 1
#define ARDUINOJSON_USE_LONG_LONG 1
// Necessary libraries to use WebThings
#include <Arduino.h>
#include "Thing.h"
#include "EthernetWebThingAdapter.h" // Switch to the Wifi Adapter if you go wireless
// Necessary library to use DHT11/DHT22 sensors
#include <DHT.h>;
//Define pins fot DHT sensors
#define DHTPIN1 2 // connect a DHT22 from the WEST wall to this plug [D2-D3]
#define DHTPIN2 6 // connect a DHT22 from the EAST wall to this plug [D6-D7]
// Instance of the DHT class, name = dht
DHT dht[] = {
{DHTPIN1, DHT22},
{DHTPIN2, DHT22},
};
// Necessary libraries to use thermo couples via voltage amplifier MAX6675
#include "max6675.h"
// Define input pins as constants
const int snPin2 = A0; // connect grove moisture sensor 1 of the WEST wall to this Pin [D4-D5]
//const int snPin3 = 0; // connect grove moisture sensor 2 of the WEST wall to this Pin [D4]
const int snPin5 = 8; // connect grove moisture sensor 1 of the EAST wall to this Pin [D8-D9]
const int snPin6 = 10; // connect grove moisture sensor 2 of the EAST wall to this Pin [D10-D11]
const int snPin7 = 12; // connect grove moisture sensor of the NORTH wall to this Pin [D12-D13]
//Define the 3 pins where the MAY6675 is connected:
const int max6675SO = 3; // Serial Output am PIN [D3]
const int max6675CS = 5; // Chip Select am PIN [D5]
const int max6675CLK =7; // Serial Clock am PIN [D7]
// Define output pins as constants
const int snPin8 = 9; // connect switch for fan WEST/NORTH channel to this Pin [D9]
const int snPin9 = 11; // connect switch for fan EAST channel to this Pin [D11]
// Define the interval length to read the sensors
const long interval = 10000; // interval at which the sensor block is read (milliseconds)
// Initialisation of variables
double moisW = -1; // Moisture West wall 1
//double moisW2 = -1; // Moisture West wall 2
double moisE1 = -1; // Moisture East wall 1
double moisE2 = -1; // Moisture East wall 2
double moisN = -1; // Moisture North wall 1
double tempF = -1; // Temperature Floor
bool fan1lastOn = false;
bool fan2lastOn = false;
unsigned long previousMillis = 0; // will store last time the sensors were updated
float humidity[2];
float temperature[2];
// Initialisation of ktc, a MAX6675 instance according to the pin definition up here
MAX6675 ktc(max6675CLK, max6675CS, max6675SO);
// ***************************MOZILLA-WebThings***************************
WebThingAdapter *adapter;
//Set up DHT-thing 1
const char *dhtType1[] = {"TemperatureSensor", "MultiLevelSensor", nullptr};
ThingDevice DHT1("DHT_WEST", "DHT sensor guest room, west channel", dhtType1);
ThingProperty TempProp1("Temperature", "Digital Input Pin", NUMBER, "TemperatureProperty");
ThingProperty HumProp1("Humidity", "Digital Input Pin", NUMBER, "LevelProperty");
//Set up DHT-thing 2
const char *dhtType2[] = {"TemperatureSensor", "MultiLevelSensor", nullptr};
ThingDevice DHT2("DHT_EAST", "DHT sensor guest room, east channel", dhtType2);
ThingProperty TempProp2("Temperature", "Digital Input Pin", NUMBER, "TemperatureProperty");
ThingProperty HumProp2("Humidity", "Digital Input Pin", NUMBER, "LevelProperty");
//Set up MAX6675-thing 2
const char *MAX6675type[] = {"TemperatureSensor", nullptr};
ThingDevice MAX6675_1("Floor", "Thermo couple guest room, bath in the floor", MAX6675type);
ThingProperty TempProp3("Temperature", "Digital Input Pins", NUMBER, "TemperatureProperty");
//Set up Seed moisture sensor 1
const char *seedMoistureTypes[] = {"MultiLevelSensor", nullptr};
ThingDevice SeedMoisture1("Moist_WEST", "Seed moisture sensor guest room, west channel", seedMoistureTypes);
ThingProperty MoisProp1("Moisture", "Analog Input Pin", NUMBER, "LevelProperty");
////Set up Seed moisture sensor 2
//ThingDevice SeedMoisture2("Moist_EAST1", "Seed moisture sensor guest room, east channel", seedMoistureTypes);
//ThingProperty MoisProp2("Moisture", "Digital Input Pin", NUMBER, "LevelProperty");
//
////Set up Seed moisture sensor 3
//ThingDevice SeedMoisture3("Moist_EAST2", "Seed moisture sensor guest room, east channel", seedMoistureTypes);
//ThingProperty MoisProp3("Moisture", "Digital Input Pin", NUMBER, "LevelProperty");
//
////Set up Seed moisture sensor 4
//ThingDevice SeedMoisture4("Moist_North", "Seed moisture sensor guest room, north channel", seedMoistureTypes);
//ThingProperty MoisProp4("Moisture", "Digital Input Pin", NUMBER, "LevelProperty");
//Set up Fan-thing 1
const char *fanTypes[] = {"Fan", nullptr};
ThingDevice fan1("fanWEST", "Fan in the west channel", fanTypes);
ThingProperty fanOn1("on", "", BOOLEAN, "OnOffProperty");
//Set up Fan-thing 2
//ThingDevice fan2("fanEAST", "Fan in the west channel", fanTypes);
//ThingProperty fanOn2("on", "", BOOLEAN, "OnOffProperty");
// ***************************Ehternet part***************************
byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0xFB, 0x80 };
IPAddress ip(192, 168, 0, 100);
void setup(void) {
pinMode(snPin8, OUTPUT);
digitalWrite(snPin8, HIGH);
pinMode(snPin9, OUTPUT);
digitalWrite(snPin9, HIGH);
Serial.begin(9600);
Serial.println("");
Ethernet.begin(mac, ip);
Serial.print("Server started. IP: ");
Serial.println(Ethernet.localIP());
for (auto& sensor : dht) {
sensor.begin();
}
adapter = new WebThingAdapter("w25", Ethernet.localIP());
TempProp1.unit = "degree celsius";
DHT1.addProperty(&TempProp1);
HumProp1.unit = "percent";
HumProp1.title = "Humidity";
HumProp1.readOnly = true;
DHT1.addProperty(&HumProp1);
TempProp2.unit = "degree celsius";
DHT2.addProperty(&TempProp2);
HumProp2.unit = "percent";
HumProp2.title = "Humidity";
HumProp2.readOnly = true;
DHT2.addProperty(&HumProp2);
TempProp3.unit = "degree celsius";
MAX6675_1.addProperty(&TempProp3);
MoisProp1.unit = "percent";
MoisProp1.title = "Moisture";
MoisProp1.readOnly = true;
SeedMoisture1.addProperty(&MoisProp1);
// MoisProp2.unit = "percent";
// MoisProp2.title = "Moisture";
// MoisProp2.readOnly = true;
// SeedMoisture2.addProperty(&MoisProp2);
//
// MoisProp3.unit = "percent";
// MoisProp3.title = "Moisture";
// MoisProp3.readOnly = true;
// SeedMoisture3.addProperty(&MoisProp3);
//
// MoisProp4.unit = "percent";
// MoisProp4.title = "Moisture";
// MoisProp4.readOnly = true;
// SeedMoisture4.addProperty(&MoisProp4);
fan1.addProperty(&fanOn1);
// fan2.addProperty(&fanOn2);
adapter->addDevice(&DHT1);
adapter->addDevice(&DHT2);
adapter->addDevice(&MAX6675_1);
adapter->addDevice(&SeedMoisture1);
// adapter->addDevice(&SeedMoisture2);
// adapter->addDevice(&SeedMoisture3);
// adapter->addDevice(&SeedMoisture4);
adapter->addDevice(&fan1);
// adapter->addDevice(&fan2);
adapter->begin();
Serial.println("HTTP server started with the following adresses to Things:");
Serial.println("DHT1:");
Serial.print("http://");
Serial.print(Ethernet.localIP());
Serial.print("/things/");
Serial.println(DHT1.id);
Serial.println("DHT2:");
Serial.print("http://");
Serial.print(Ethernet.localIP());
Serial.print("/things/");
Serial.println(DHT2.id);
Serial.println("Thermocouple:");
Serial.print("http://");
Serial.print(Ethernet.localIP());
Serial.print("/things/");
Serial.println(MAX6675_1.id);
Serial.println("Moisture1:");
Serial.print("http://");
Serial.print(Ethernet.localIP());
Serial.print("/things/");
Serial.println(SeedMoisture1.id);
// Serial.println("Moisture2:");
// Serial.print("http://");
// Serial.print(Ethernet.localIP());
// Serial.print("/things/");
// Serial.println(SeedMoisture2.id);
//
// Serial.println("Moisture3:");
// Serial.print("http://");
// Serial.print(Ethernet.localIP());
// Serial.print("/things/");
// Serial.println(SeedMoisture3.id);
//
// Serial.println("Moisture4:");
// Serial.print("http://");
// Serial.print(Ethernet.localIP());
// Serial.print("/things/");
// Serial.println(SeedMoisture4.id);
Serial.println("Fan west:");
Serial.print("http://");
Serial.print(Ethernet.localIP());
Serial.print("/things/");
Serial.println(fan1.id);
// Serial.println("Fan east:");
// Serial.print("http://");
// Serial.print(Ethernet.localIP());
// Serial.print("/things/");
// Serial.println(fan2.id);
}
void loop(void) {
// It takes quite some time to read the sensors. In order to find the things quickly,
// I read them out only every once in a while:
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
for (int i = 0; i < 2; i++) {
temperature[i] = dht[i].readTemperature();
humidity[i] = dht[i].readHumidity();
}
tempF = ktc.readCelsius();
moisW = 3.1415; //getMoistureValue(snPin2);
// moisE1 = getMoistureValue(snPin5);
// moisE2 = getMoistureValue(snPin6);
// moisN = getMoistureValue(snPin7);
previousMillis = currentMillis;
}
ThingPropertyValue Humidity1;
Humidity1.number = humidity[0];
HumProp1.setValue(Humidity1);
ThingPropertyValue Temperature1;
Temperature1.number = temperature[0];
TempProp1.setValue(Temperature1);
ThingPropertyValue Humidity2;
Humidity2.number = humidity[1];
HumProp2.setValue(Humidity2);
ThingPropertyValue Temperature2;
Temperature2.number = temperature[1];
TempProp2.setValue(Temperature2);
ThingPropertyValue Temperature3;
Temperature3.number = tempF;
TempProp3.setValue(Temperature3);
ThingPropertyValue Moisture1;
Moisture1.number = moisW;
MoisProp1.setValue(Moisture1);
// ThingPropertyValue Moisture2;
// Moisture2.number = moisE1;
// MoisProp2.setValue(Moisture2);
//
// ThingPropertyValue Moisture3;
// Moisture3.number = moisE2;
// MoisProp3.setValue(Moisture3);
//
// ThingPropertyValue Moisture4;
// Moisture4.number = moisN;
// MoisProp4.setValue(Moisture4);
adapter->update();
bool on = fanOn1.getValue().boolean;
digitalWrite(snPin8, on ? LOW : HIGH); // active low led
if (on != fan1lastOn) {
Serial.print(fan1.id);
Serial.print(": ");
Serial.println(on);
}
fan1lastOn = on;
}
// getMoistureValue, reads the moisture sensor value i times and returns the average.
// Takes the sensor pin as an input.
double getMoistureValue(int sensorPin){
double sensorValue = 0;
sensorValue = analogRead(sensorPin);
return sensorValue/12;
}