Trying to set up an Arduino/ESP8266 irrigation system. An Arduino takes soil moisture readings and sends them to the ESP8266 serially which communicates with a Mozilla IoT Gateway. I'd like to be able to have the Arduino control the valve solenoid, but to do that, I need to send an integer back to Arduino. Seems like a simple idea, but I haven't been able to work it out .
The Arduino code:
#include <SoftwareSerial.h>
SoftwareSerial nodemcu(2,3);
const int sensorPin1 = A0;
const int sensorPin2 = A1;
const int valvePin = 13;
boolean Init = true;
int valueHist[10][2];
int moistLevel = 0;
String cdata;
String indata;
void setup() {
Serial.begin(115200);
nodemcu.begin(115200);
pinMode(sensorPin1, INPUT);
pinMode(sensorPin2, INPUT);
pinMode(valvePin, OUTPUT);
digitalWrite(valvePin, LOW);
}
void loop() {
moistLevel = sensorRead();
cdata = cdata + moistLevel;
if(nodemcu.availableForWrite() == 0 )
{
nodemcu.println(cdata);
//Serial.println(cdata);
cdata = "";
delay(1000);
}
if ( nodemcu.available() > 0 )
{
indata = nodemcu.read();
delay(100);
if ( indata == 10 )
{
digitalWrite(valvePin, HIGH);
Serial.println("ON");
}
if ( indata == 11 )
{
digitalWrite(valvePin, LOW);
Serial.println("OFF");
}
}
delay(1000);
}
The ESP8266 code:
#define LARGE_JSON_BUFFERS 1
#include <Arduino.h>
#include "Thing.h"
#include "WebThingAdapter.h"
const char* ssid = "xxxxx";
const char* password = "xxxxxx";
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 *sensorDeviceTypes[] = {"MultiLevelSensor", nullptr};
ThingDevice MultiLevelSensor("device:esp8266:moisture", "Soil Moisture Sensor", sensorDeviceTypes);
ThingProperty moistureLevel("moistureLevel", "Sensor", NUMBER, "LevelProperty");
const char *switchDeviceTypes[] = {"OnOffSwitch", nullptr};
ThingDevice OnOffSwitch("device:esp8266:valve", "Valve", switchDeviceTypes);
ThingProperty valveOn("valveOn", "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/", "/WLP/", "/WHP/", "/EHP/", "/ELP/"};
void setup() {
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());
// Set up property details
moistureLevel.title = "Moisture Level";
moistureLevel.minimum = 200;
moistureLevel.maximum = 700;
moistureLevel.unit = " ";
valveOn.title = "Water On";
// Associate device with connection
MultiLevelSensor.addProperty(&moistureLevel);
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();
}
void loop() {
recvWithEndMarker();
showNewData();
ThingPropertyValue MVal;
MVal.number = moistLevel;
moistureLevel.setValue(MVal);
adapter->update();
bool on = valveOn.getValue().boolean;
if ( Serial.availableForWrite() == 0 )
{
if (on) {
Serial.println("On");
Serial.write(10);
} else {
Serial.println("Off");
Serial.write(11);
}
}
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;
}
}
Any assistance would be greatly appreciated.
