Slider Problem on dashboard

#1

Hello,

I working on a project about using a stepper motor to open a greenhouse window, but i can’t add a digital slider on dashboard.

Hardware : ARDUINO MEGA 2560 REV3 with W5100 ethernet shield

Code it’s from a post of cayenne community:

//#define CAYENNE_DEBUG // Uncomment to show debug messages
#define CAYENNE_PRINT Serial // Comment this out to disable prints and save space

#include <CayenneDefines.h>
#include <BlynkSimpleEsp8266.h>
#include <CayenneWiFiClient.h>
#include <ESP8266WiFi.h>

//define stepper pins
#define DIR_1 12
#define DIR_2 13
#define STEP_1 14
#define STEP_2 16

// Cayenne authentication token. This should be obtained from the Cayenne Dashboard.
char token = “token”; //your token

// Your network name and password.
char ssid = “ssid”; //your ssid
char password = “pass”; //your pass

//step direction globals
int step_direction_1 = 0;
int step_direction_2 = 0;

void setup() {

//setup your stepper pins as outputs
pinMode(DIR_1, OUTPUT);
pinMode(STEP_1, OUTPUT);
pinMode(DIR_2, OUTPUT);
pinMode(STEP_2, OUTPUT);

Serial.begin(9600);
Cayenne.begin(token, ssid, password);
}

void step_one(int dir, int steps) {
step(step_direction_1, STEP_1, dir, steps);
}

void step_two(int dir, int steps) {
step(step_direction_2, STEP_2, dir, steps);
}

void step(int dir_pin, int step_pin, int dir, int steps){

digitalWrite(dir_pin,dir);

delay(50);

for(int i=0;i<steps;i++){
digitalWrite(step_pin, HIGH);
delayMicroseconds(100);
digitalWrite(step_pin, LOW);
delayMicroseconds(100);
}
}

void loop()
{
Cayenne.run();
}

//get step direction from dashboard (stepper 1)
CAYENNE_IN(V1)
{
step_direction_1 = getValue.asInt();
}

//step number of steps in slider on stepper 1
CAYENNE_IN(V2)
{
step_one(step_direction_1,getValue.asInt());
}

//get step direction from dashboard (stepper 2)
CAYENNE_IN(V3)
{
step_direction_1 = getValue.asInt();
}

//step number of steps in slider on stepper 2
CAYENNE_IN(V4)
{
step_two(step_direction_2,getValue.asInt());
}

Problem is when i want to add digital widget as slider i only can add analog :slight_smile:
image

I did something wrong?

Thanks

#2

this code is of old cayenne. you need to use new MQTT cayenne library. https://github.com/myDevicesIoT/Cayenne-MQTT-Arduino

#3

Yes sir,

The code is just one example, it has been modified for the new system, the full code is this:

//Conectare pini

// Pin D2 - Senzor debit YF-S401
// Pin D3 - Senzor temperatura apa DS18B20.
// Pin D4 - Releu pornire pompa.
// Pin D5 - Releu lumina sera.
// Pin D6 - Senzor temperatura si umiditate sera AM2301.
// Pin D7 - Senzor temperatura si umiditate automatizare DHT11
// Pin D8 - Senzor distanta HC-SR04
// Pin D9 - Senzor distanta HC-SR04
// Pin SDA - Senzor temperatura si presiune atmosferica BMP180
// Pin SCL - Senzor temperatura si presiune atmosferica BMP180

// Librariile necesare pentru Automatizare
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP085_U.h>
#include <CayenneMQTTEthernet.h>
#include <Ultrasonic.h>
#include <DHT.h>

// Autorizarea catre siteul MyDevices.Com
char username = “”;
char password = “”;
char clientID = “”;

// Pinul virtual pentru senzorul de temperatura exterior/presiunea atmosferica (BMP180)
#define VIRTUAL_PIN2 V2
#define VIRTUAL_PIN3 V3

// Pinurile digitale DHT21/DHT11
#define VIRTUAL_PIN6 V6
#define VIRTUAL_PIN7 V7
#define DHT1PIN 6 // Pinul digital pentru senzorul DHT21
#define DHT2PIN 7 // Pinul digital pentru senzorul DHT11
#define DHT1TYPE DHT21 // DHT 21
#define DHT2TYPE DHT11 // DHT 11
DHT dht1(DHT1PIN, DHT1TYPE);
DHT dht2(DHT2PIN, DHT2TYPE);
unsigned long lastMillis = 0;

// Pinurile digitale pentru relee
#define VIRTUAL_PIN4 V4
#define VIRTUAL_PIN5 V5
#define RELAY_DIGITAL_PIN1 V4
#define RELAY_DIGITAL_PIN2 V5

// Pinurile digitale pentru senzorul de masurare a cantitatii apei din bazin (HC-SR04)
#define pin_trigger 9
#define pin_echo 8
#define VIRTUAL_PIN8 V8
Ultrasonic ultrasonic(pin_trigger, pin_echo);

//API-ul pentru senzorul de temperatura exterior/presiune atmosferica (BMP180)
Adafruit_BMP085_Unified bmp = Adafruit_BMP085_Unified(10180);
bool bmpSensorDetected = true;

// Pinul virtual pentru senzorul de temperatura apei (DS18B20).
#define VIRTUAL_PIN1 V1
// Pinul digital pentru senzorul de temperatura apei (DS18B20).
const int tmpPin = 3;
//API-ul pentru senzorul de temperatura apei (DS18B20)
OneWire oneWire(tmpPin);
DallasTemperature sensors(&oneWire);

// Variabile senzori umiditate sol
int dataread1, dataread2, dataread3, dataread4, dataread5;
int moisturepercent1, moisturepercent2, moisturepercent3, moisturepercent4, moisturepercent5;
#define VIRTUAL_PIN10 V10
#define VIRTUAL_PIN11 V11
#define VIRTUAL_PIN12 V12
#define VIRTUAL_PIN13 V13
#define VIRTUAL_PIN14 V14

#define sensorpin1 A1
#define sensorpin2 A2
#define sensorpin3 A3
#define sensorpin4 A4
#define sensorpin5 A5

#define sensorpowersupply1 22
#define sensorpowersupply2 24
#define sensorpowersupply3 26
#define sensorpowersupply4 28
#define sensorpowersupply5 30

// Variabilele pentru senzorul debit

#define VIRTUAL_PIN15 V15

byte sensorInterrupt = 0;
byte sensorPin = 2;

float calibrationFactor = 27.3;

volatile byte pulseCount;

float flowRate;
unsigned int flowMilliLitres;
unsigned long totalMilliLitres;

unsigned long oldTime;

unsigned long read1, read2, read3, read4, read5;
unsigned long pause = 300000;

unsigned long startpump = 0;
unsigned long irrigationtime = 30000;

//Variabile stepper motor

#define DIR_1 10
#define STEP_1 12

#define VIRTUAL_PIN20 V20
#define VIRTUAL_PIN21 V21

int step_direction_1 = 0;

void setup()
{

Serial.begin(9600);
Serial.println(“Automatizare Sera - 2019”);
delay(5000);
Serial.println(“Conectare catre serviciul MyDevices.Com”);
pinMode(sensorpowersupply1, OUTPUT);
pinMode(sensorpowersupply2, OUTPUT);
pinMode(sensorpowersupply3, OUTPUT);
pinMode(sensorpowersupply4, OUTPUT);
pinMode(sensorpowersupply5, OUTPUT);
pinMode(RELAY_DIGITAL_PIN1, OUTPUT);
pinMode(RELAY_DIGITAL_PIN2, OUTPUT);
digitalWrite(RELAY_DIGITAL_PIN1, HIGH);
digitalWrite(RELAY_DIGITAL_PIN2, HIGH);
pinMode(sensorPin, INPUT);
digitalWrite(sensorPin, HIGH);
pinMode(DIR_1, OUTPUT);
pinMode(STEP_1, OUTPUT);
dht1.begin();
dht2.begin();

pulseCount = 0;
flowRate = 0.0;
flowMilliLitres = 0;
totalMilliLitres = 0;
oldTime = 0;

attachInterrupt(sensorInterrupt, pulseCounter, FALLING);

float h1 = dht1.readHumidity();
float t1 = dht1.readTemperature();
float h2 = dht2.readHumidity();
float t2 = dht2.readTemperature();
sensors.begin();

Cayenne.begin(username, password, clientID);
if (!bmp.begin())
{
bmpSensorDetected = false;
}

digitalWrite(sensorpowersupply1, HIGH);
delay(1000);
dataread1 = analogRead(sensorpin1);
moisturepercent1 = map(dataread1, 1023,0,0,100);
digitalWrite(sensorpowersupply1, LOW);
read1 = millis();

digitalWrite(sensorpowersupply2, HIGH);
delay(1000);
dataread2 = analogRead(sensorpin2);
moisturepercent2 = map(dataread2, 1023,0,0,100);
digitalWrite(sensorpowersupply2, LOW);
read2 = millis();

digitalWrite(sensorpowersupply3, HIGH);
delay(1000);
dataread3 = analogRead(sensorpin3);
moisturepercent3 = map(dataread3, 1023,0,0,100);
digitalWrite(sensorpowersupply3, LOW);
read3 = millis();

digitalWrite(sensorpowersupply4, HIGH);
delay(1000);
dataread4 = analogRead(sensorpin4);
moisturepercent4 = map(dataread4, 1023,0,0,100);
digitalWrite(sensorpowersupply4, LOW);
read4 = millis();

digitalWrite(sensorpowersupply5, HIGH);
delay(1000);
dataread5 = analogRead(sensorpin5);
moisturepercent5 = map(dataread5, 1023,0,0,100);
digitalWrite(sensorpowersupply5, LOW);
read5 = millis();
}

void step_one(int dir, int steps) {
step(step_direction_1, STEP_1, dir, steps);
}

void step(int dir_pin, int step_pin, int dir, int steps){

digitalWrite(dir_pin,dir);

delay(50);

for(int i=0;i<steps;i++){
digitalWrite(step_pin, HIGH);
delayMicroseconds(100);
digitalWrite(step_pin, LOW);
delayMicroseconds(100);
}
}

void loop()
{
Cayenne.loop();

if((millis() - oldTime) > 1000)
{

detachInterrupt(sensorInterrupt);   
flowRate = ((1000.0 / (millis() - oldTime)) * pulseCount) / calibrationFactor;
oldTime = millis();
flowMilliLitres = (flowRate / 60) * 1000;
totalMilliLitres += flowMilliLitres;
unsigned int frac;
frac = (flowRate - int(flowRate)) * 10;   
pulseCount = 0; 
attachInterrupt(sensorInterrupt, pulseCounter, FALLING);

}
if (millis() - lastMillis > 10000) {
float h1 = dht1.readHumidity();
float t1 = dht1.readTemperature();
float h2 = dht2.readHumidity();
float t2 = dht2.readTemperature();
if (isnan(h1) || isnan(t1)) {
return;

}
if (isnan(h2) || isnan(t2)) {
return;

}
Cayenne.celsiusWrite(16, t1);
Cayenne.virtualWrite(17, h1);
lastMillis = millis();
Cayenne.celsiusWrite(18, t2);
Cayenne.virtualWrite(19, h2);
lastMillis = millis();

}

}
void pulseCounter()
{
pulseCount++;
}

// Senzorul DS18B20
CAYENNE_OUT(VIRTUAL_PIN1)
{
sensors.requestTemperatures();
Cayenne.celsiusWrite(VIRTUAL_PIN1, sensors.getTempCByIndex(0));
delay(500);
}

// Releu Pornire Pompa
CAYENNE_IN(V4)
{
int currentValue1 = getValue.asInt();
int currentState1 = digitalRead(RELAY_DIGITAL_PIN1);

if ((currentValue1 == 0) and (currentState1 == 0))
{
digitalWrite(RELAY_DIGITAL_PIN1, HIGH);
startpump = millis();
}
if ((currentState1 == 1) and (millis() - startpump > irrigationtime))
{
digitalWrite(RELAY_DIGITAL_PIN1, LOW);
}
}

// Releu Aprindere Lumina
CAYENNE_IN(V5)
{
int currentValue = getValue.asInt();
if (currentValue == 0) {
digitalWrite(RELAY_DIGITAL_PIN2, HIGH);
} else {
digitalWrite(RELAY_DIGITAL_PIN2, LOW);
}
}

// Senzor presiune atmosferica BMP180
CAYENNE_OUT(VIRTUAL_PIN2)
{
if (bmpSensorDetected)
{
sensors_event_t event;
bmp.getEvent(&event);

if (event.pressure)
{
  Cayenne.hectoPascalWrite(VIRTUAL_PIN2, event.pressure);
}

}
}

// Senzor Temperatura Exterior BMP180
CAYENNE_OUT(VIRTUAL_PIN3)
{
if (bmpSensorDetected)
{
float temperature;
bmp.getTemperature(&temperature);
Cayenne.celsiusWrite(VIRTUAL_PIN3, temperature);
}
}

// Senzor Nivel Apa In Bazin HC-SR04
CAYENNE_OUT(VIRTUAL_PIN8)
{
float cmMsec;
long microsec = ultrasonic.timing();
cmMsec = ultrasonic.convert(microsec, Ultrasonic::CM);
Cayenne.virtualWrite(V8, cmMsec, “prox”,“cm”);
}

// Senzor Umiditate Sol 1
CAYENNE_OUT(VIRTUAL_PIN10)
{
if((millis() - read1) > pause)
{
digitalWrite(sensorpowersupply1, HIGH);
delay(1000);
dataread1 = analogRead(sensorpin1);
moisturepercent1 = map(dataread1, 1023,0,0,100);
digitalWrite(sensorpowersupply1, LOW);
read1 = millis();
}
Cayenne.virtualWrite(V10, moisturepercent1, “soil_moist”, “p”);
}

// Senzor Umiditate Sol 2
CAYENNE_OUT(VIRTUAL_PIN11)
{
if((millis() - read2) > pause)
{
digitalWrite(sensorpowersupply2, HIGH);
delay(1000);
dataread2 = analogRead(sensorpin2);
moisturepercent2 = map(dataread2, 1023,0,0,100);
digitalWrite(sensorpowersupply2, LOW);
read2 = millis();
}
Cayenne.virtualWrite(V11, moisturepercent2, “soil_moist”, “p”);
}

// Senzor Umiditate Sol 3
CAYENNE_OUT(VIRTUAL_PIN12)
{
if((millis() - read3) > pause)
{
digitalWrite(sensorpowersupply3, HIGH);
delay(1000);
dataread3 = analogRead(sensorpin3);
moisturepercent3 = map(dataread3, 1023,0,0,100);
digitalWrite(sensorpowersupply3, LOW);
read3 = millis();
}
Cayenne.virtualWrite(V12, moisturepercent3, “soil_moist”, “p”);
}

// Senzor Umiditate Sol 4
CAYENNE_OUT(VIRTUAL_PIN13)
{
if((millis() - read4) > pause)
{
digitalWrite(sensorpowersupply4, HIGH);
delay(1000);
dataread4 = analogRead(sensorpin4);
moisturepercent4 = map(dataread4, 1023,0,0,100);
digitalWrite(sensorpowersupply4, LOW);
read4 = millis();
}
Cayenne.virtualWrite(V13, moisturepercent4, “soil_moist”, “p”);
}

// Senzor Umiditate Sol 5
CAYENNE_OUT(VIRTUAL_PIN14)
{
if((millis() - read5) > pause)
{
digitalWrite(sensorpowersupply5, HIGH);
delay(1000);
dataread5 = analogRead(sensorpin5);
moisturepercent5 = map(dataread5, 1023,0,0,100);
digitalWrite(sensorpowersupply5, LOW);
read5 = millis();
}
Cayenne.virtualWrite(V14, moisturepercent5, “soil_moist”, “p”);
}

// Senzor debit YF-S401
CAYENNE_OUT(VIRTUAL_PIN15)
{
Cayenne.virtualWrite(V15, totalMilliLitres, “liquid”, “l” );
}

// Senzor Temperatura & Umiditate In Sera AM2301
CAYENNE_OUT(VIRTUAL_PIN6)
{
float h1 = dht1.readHumidity();
float t1 = dht1.readTemperature();
Cayenne.celsiusWrite(16, t1);
Cayenne.virtualWrite(17, h1, “rel_hum”, “p”);
}

// Senzor Temperatura & Umiditate Automatizare DHT11
CAYENNE_OUT(VIRTUAL_PIN7)
{

float  h2 = dht2.readHumidity();
float  t2 = dht2.readTemperature();

Cayenne.celsiusWrite(18, t2);
Cayenne.virtualWrite(19, h2, “rel_hum”, “p”);
}

CAYENNE_IN(V20)
{
step_direction_1 = getValue.asInt();
}

CAYENNE_IN(V21)
{
step_one(step_direction_1,getValue.asInt());
}

#4

you want to add a digital button or a slider?

#5

I want to use a slider for stepper direction

#6

then it will be a analog actuator. the slider position will control the position of stepper motor.

#7

So, any support for add a stepper motor in future? I use that sketch found on community forum but don’t work for me :slightly_smiling_face:

#8

it is already supported. you need to code your Arduino accordingly to so that it works with cayenne.
what is that you are trying to do with stepper motor and cayenne?

#9

Going to use Cayenne for my hobby greenhouse, have a lot of sensors up and running at this moment.

1 x YF-S401 ( to have status on how much water need for last irrigation )
2 x relay ( pump & light )
1 x DS18B20 ( for water temperature )
1 x AM2301 ( greenhouse temp & hum )
1 x DHT11 ( outside temp & hum )
1x BMP180 ( temp & pa )
1 x HC-SR04 ( water level in basin )

I need a code for a NEMA 17 with axe for greenhouse window, never used a stepper motor. If i can’t find a proper code maybe i will buy a servomotor instead.
I draw something in paint :smirk:

image

#10

first get the code working for your stepper motor without cayenne and then share here.

#11

I try that code

// defines pins numbers
const int stepPin = 10;
const int dirPin = 11;

void setup() {
// Sets the two pins as Outputs
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);
}
void loop() {
digitalWrite(dirPin,HIGH); // Enables the motor to move in a particular direction
// Makes 200 pulses for making one full cycle rotation
for(int x = 0; x < 5000; x++) {
digitalWrite(stepPin,HIGH);
delayMicroseconds(500);
digitalWrite(stepPin,LOW);
delayMicroseconds(500);
}
delay(10000); // One second delay

digitalWrite(dirPin,LOW); //Changes the rotations direction
// Makes 400 pulses for making two full cycle rotation
for(int x = 0; x < 5000; x++) {
digitalWrite(stepPin,HIGH);
delayMicroseconds(500);
digitalWrite(stepPin,LOW);
delayMicroseconds(500);
}
delay(10000);
}

But it’s with delay and automated, i need to control by myself and use a trigger with.

#12

Just a simple code like this should work. Add a button on dashboard with channel 1.

CAYENNE_IN(1)
{
int x = getValue.asInt();
if (x == 1){
digitalWrite(dirPin,HIGH); // Enables the motor to move in a particular direction
// Makes 200 pulses for making one full cycle rotation
for(int x = 0; x < 5000; x++) {
digitalWrite(stepPin,HIGH);
delayMicroseconds(500);
digitalWrite(stepPin,LOW);
delayMicroseconds(500);
}
delay(10000); // One second delay

digitalWrite(dirPin,LOW); //Changes the rotations direction
// Makes 400 pulses for making two full cycle rotation
for(int x = 0; x < 5000; x++) {
digitalWrite(stepPin,HIGH);
delayMicroseconds(500);
digitalWrite(stepPin,LOW);
delayMicroseconds(500);
}
}
}
#13

I need to use a slider for direction, to open/close the window when trigger temperature sensor.

#14

any specific reason to use a slider in your case?

#15

No really a reason, i can use 2 button for each direction… tested that code but it don’t work:upside_down_face:

#16

Ok it seems that i have a hardware issue, it’s working now but from any reason he get hot, what can be?

#17

read this https://forum.arduino.cc/index.php?topic=296590.0

#18

Ok, i made 2 buttons Open/Close, now i see something strange about motor, when is idle make some tiny rotation, that can be a code problem?

#19

follow this tutorial and see if it works http://www.mrhobbytronics.com/arduino-stepper-motor-control/.
if it works without any issue then add the cayenne code.

#20

also go through this important post http://forum.arduino.cc/index.php?topic=284828.0 and http://forum.arduino.cc/index.php?topic=277692.0