This is my first foray into the Gateway - I am trying to set up an irrigation system for multiple planters using Arduinos and ESP83266s from Adafruit. I have seen an example of a MultiLevelSensor and one for OnOffSwitch but none have both. And it seems that each one uses different sets of libraries which has made understanding difficult.
When I run this code, I can see from the serial monitor that it comes online and is receiving moisture readings from the Arduino, but the Gateway can’t find it - even if I enter the URL.
#include <Arduino.h>
#include <Thing.h>
#include <WebThingAdapter.h>
const char* ssid = “xxxxxxx”;
const char* password = “xxxxx”;
const int valvePin = 13;
const int PinA = 12;
const int PinB = 14;
const int PinC = 16;
int MDSIndex = 0;
boolean newData = false;
String mDNSHostname = “”;
String URLAbbrev = “”;
WebThingAdapter *adapter = NULL;
const char *deviceTypes[] = {“MultiLevelSensor”, “OnOffSwitch”, nullptr};
ThingDevice MultiLevelSensor(“Soil Moisture”, “Moisture Sensor”, deviceTypes);
ThingDevice OnOffSwitch(“Valve”, “Valve”, deviceTypes);
ThingProperty LevelProperty(“Soil Moisture Sensor”, “SensorX”, NUMBER, “LevelProperty”);
ThingProperty valveOn(“Water On”, “ValvePin”, BOOLEAN, “OnOffProperty”);
int moistLevel = 0;
const byte numChars = 4;
char receivedChars[numChars]; // an array to store the received data
String DNSName[] = {“BlueberryWest”, “BlueberryCenter”, “BlueberryEast”, “LowWest”, “HighWest”, “HighEast”, “LowEast”};
String URLName[] = {"/BBW/", “/BBC/”, “/BBE/”, “/LPW/”, “/HPW/”, “/HPE/”, “/LPE/”};
void setup() {
pinMode(valvePin, OUTPUT);
Serial.begin(115200);
MDSIndex = determineAddress(); // Address set by DIP switches on pins 12, 14, 16
mDNSHostname = DNSName[MDSIndex];
URLAbbrev = URLName[MDSIndex];
Serial.println(" “);
Serial.println(MDSIndex);
Serial.print(mDNSHostname);
Serial.print(” ");
Serial.println(URLAbbrev);
// Connect to WiFi access point
Serial.print(“Connecting to “);
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while ( WiFi.status() != WL_CONNECTED ) {
delay(500);
Serial.print(”.”);
}
// Show connection details
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: “);
Serial.println(WiFi.localIP());
Serial.println(” ");
// Create new WebThings connection handle (default port: 80)
adapter = new WebThingAdapter(mDNSHostname, WiFi.localIP());
// Associate device with connection
MultiLevelSensor.addProperty(&LevelProperty);
adapter->addDevice(&MultiLevelSensor);
OnOffSwitch.addProperty(&valveOn);
adapter->addDevice(&OnOffSwitch);
// Start mDNS and HTTP server
adapter->begin();
Serial.println(“HTTP server started”);
Serial.print(“http://”);
Serial.print(WiFi.localIP());
Serial.print(URLAbbrev);
Serial.println(MultiLevelSensor.id);
// set initial values
ThingPropertyValue initialOn = {.boolean = false};
valveOn.setValue(initialOn);
(void)valveOn.changedValueOrNull();
digitalWrite(valvePin, LOW);
}
void loop() {
recvWithEndMarker();
showNewData();
ThingPropertyValue MVal;
MVal.number = moistLevel;
LevelProperty.setValue(MVal);
adapter->update();
bool on = valveOn.getValue().boolean;
if (on) {
Serial.println(“On”);
digitalWrite(valvePin, HIGH );
} else {
Serial.println(“Off”);
digitalWrite(valvePin, LOW);
}
delay(1000);
}
void recvWithEndMarker() {
static byte ndx = 0;
char endMarker = ‘\n’;
char rc;
while (Serial.available() > 0 && newData == false) {
rc = Serial.read();
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = ‘\0’; // terminate the string
ndx = 0;
newData = true;
}
}
}
void showNewData() {
if (newData == true) {
String l = receivedChars;
moistLevel = l.toInt();
Serial.println(moistLevel);
newData = false;
}
}
I suspect that my error(s) lie in how I have defined the device, but I’m stuck. Do I need another type array for the OnOffSwitch? Or is that set up correctly? Any input will be appreciated