How can I help?
Here is the code:
D5, D6, D7 identify the WeMos pins.
Only D5 is presently used.
D6, D7 relate to the encoder software.
`
// This example shows how to connect to Cayenne using an ESP8266 and send/receive sample data.
// Make sure you install the ESP8266 Board Package via the Arduino IDE Board Manager and select the correct ESP8266 board before compiling.
//#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial
#include <CayenneMQTTESP8266.h>
// #include <Encoder.h>
// WiFi network info.
char ssid[] = "xxxxxx";
char wifiPassword[] = "xxxxx;
// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = "";
char password[] = "";
char clientID[] = "";
unsigned long lastMillis = 0;
#define TIME_CHANNEL 3
#define CONTROL_CHANNEL 2
Encoder myEnc(D6, D7);
#define MotorPin D5 // Turn motor on/off
int MotorTime;
void setup() {
Serial.begin(9600);
pinMode(MotorPin, INPUT);
digitalWrite(MotorPin, HIGH);
Cayenne.begin(username, password, clientID, ssid, wifiPassword);
}
void loop() {
Cayenne.loop();
}
// Default function for sending sensor data at intervals to Cayenne.
// You can also use functions for specific channels, e.g CAYENNE_OUT(1) for sending channel 1 data.
CAYENNE_OUT_DEFAULT()
{
// Write data to Cayenne here. This example just sends the current uptime in milliseconds on virtual channel 0.
Cayenne.virtualWrite(0, millis());
// Some examples of other functions you can use to send data.
//Cayenne.celsiusWrite(1, 22.0);
//Cayenne.luxWrite(2, 700);
//Cayenne.virtualWrite(3, 50, TYPE_PROXIMITY, UNIT_CENTIMETER);
}
// This function is called when data is sent from Cayenne.
CAYENNE_IN(TIME_CHANNEL)
{
MotorTime = getValue.asInt(); // 0 to 255
CAYENNE_LOG("Channel %d, pin %d, value %d", TIME_CHANNEL, MotorPin, MotorTime);
// Write the value received to the PWM pin. analogWrite accepts a value from 0 to 255.
if (MotorTime > 0) {
Serial.print("Running for ");
Serial.print(MotorTime);
Serial.print(" seconds - ");
pinMode(MotorPin, OUTPUT);
digitalWrite(MotorPin, LOW);
delay(MotorTime*1000);
Serial.println("Stopped");
pinMode(MotorPin, INPUT);
digitalWrite(MotorPin, HIGH);
}
else {
Serial.println("Stopped");
pinMode(MotorPin, INPUT);
digitalWrite(MotorPin, HIGH);
}
}
// This function is called when data is sent from Cayenne.
CAYENNE_IN(CONTROL_CHANNEL)
{
CAYENNE_LOG("Channel %d, pin %d, value %d", CONTROL_CHANNEL, MotorPin, getValue.asInt());
// Write the value received to the PWM pin. analogWrite accepts a value from 0 to 255.
if (getValue.asInt() > 0) {
Serial.println("Running...");
pinMode(MotorPin, OUTPUT);
digitalWrite(MotorPin, LOW);
}
else if (getValue.asInt() == 0) {
Serial.println("Stopped...");
pinMode(MotorPin, INPUT);
digitalWrite(MotorPin, HIGH);
}
}
// Default function for processing actuator commands from the Cayenne Dashboard.
// You can also use functions for specific channels, e.g CAYENNE_IN(1) for channel 1 commands.
CAYENNE_IN_DEFAULT()
{
CAYENNE_LOG("Channel %u, value %s", request.channel, getValue.asString());
//Process message here. If there is an error set an error message using getValue.setError(), e.g getValue.setError("Error message");
}
`
Here is the screen shot on Mac: