Controlling a motor with potentiometer - arduino

I am trying to use my arduino and a potentiometer to make a motor spin one way when the potentiometer is past 0 and spin the other when the potentiometer is past 0 the other direction. The code is working on the SensorValue < 512 side but not on the >507 side.
const int analogInPin = A1; // Analog input pin that the potentiometer is attached to
const int analogOutPin = 9; //
const int analogOutPin_2 = 11; //
int sensorValue = 0; // value read from the pot
int outputValue = 0; // value output to the PWM (analog out)
void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
}
void loop() {
sensorValue = analogRead(analogInPin);
if (sensorValue < 507) {
analogWrite(analogOutPin, LOW);
outputValue = map(sensorValue, 0, 512, 0, 255);
analogWrite(analogOutPin_2, outputValue);
}
if (512 > sensorValue) {
analogWrite(analogOutPin_2, LOW);
outputValue = map(sensorValue, 512, 1023, 0, 255);
analogWrite(analogOutPin, outputValue);
}
else {
}
delay(2);
}

For solving issues such as this. I recommend a few strategic prints and a for loop to simulate the data range, reveals the following code with a few tweaks works better
const int analogInPin = A1; // Analog input pin that the potentiometer is attached to
const int analogOutPin = 9; //
const int analogOutPin_2 = 11; //
int sensorValue = 0; // value read from the pot
int outputValue = 0; // value output to the PWM (analog out)
void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
delay(1000);
Serial.println("Begin1");
}
void loop() {
// sensorValue = analogRead(analogInPin);
for (sensorValue = 0; sensorValue < 1024; sensorValue++) {
Serial.print("sensorValue = ");
Serial.print(sensorValue);
if (sensorValue < 507) {
outputValue = map(sensorValue, 0, 512, 255, 0);
Serial.print(" Forward : ");
Serial.print(outputValue);
analogWrite(analogOutPin, LOW);
analogWrite(analogOutPin_2, outputValue);
} else if (512 < sensorValue) {
outputValue = map(sensorValue, 512, 1023, 0, 255);
Serial.print(" Reverse : ");
Serial.print(outputValue);
analogWrite(analogOutPin_2, LOW);
analogWrite(analogOutPin, outputValue);
}
Serial.println();
}
while(1);
delay(1000);
}

Related

Why is my if statement in my program not working?

I'm trying to make a program so when 2 switches are flipped they light up two 4 pin LEDs, but only when both switches are flipped. I'm trying to make a cool BattleBots arena and thought something like that would be cool to have as a starting sequence! If you spot any extra errors feel free to correct me.
int buttonPin = 13;
int buttonPin2 = 3;
int counter = 0;
int redpin = 11; //select the pin for the red LED
int bluepin =10; // select the pin for the blue LED
int greenpin =9;// select the pin for the green LED
int redpin2 = 7; //select the pin for the 2 red LED
int bluepin2 =6; // select the pin for the 2 blue LED
int greenpin2 =5;// select the pin for the 2 green LED
int pinButton = 13; //the pin where we connect the button
int pinButton2 = 3; //the pin where we connect the button
int val;
void setup() {
pinMode(buttonPin, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(redpin, OUTPUT);
pinMode(bluepin, OUTPUT);
pinMode(greenpin, OUTPUT);
pinMode(redpin2, OUTPUT);
pinMode(bluepin2, OUTPUT);
pinMode(greenpin2, OUTPUT);
Serial.begin(9600);
}
void loop()
{ int buttonState;
buttonState = digitalRead(buttonPin);
int buttonState2;
buttonState2 = digitalRead(buttonPin2);
}
int main()
if (buttonState == LOW && buttonState2 == LOW);
analogWrite(11, 0);
analogWrite(10, 225);
analogWrite(9, 0);
delay(5);
analogWrite(7, 0);
analogWrite(6, 225);
analogWrite(5, 0);
delay(5);
}else{
analogWrite(11, 225);
analogWrite(10, 0);
analogWrite(9, 0);
delay(5);
analogWrite(7, 0);
analogWrite(6, 0);
analogWrite(5, 225);
delay(5);
}
Your code seem to be a cut and paste from several sources, here's what it should look like, I added notes in the code:
const int buttonPin = 13;
const int buttonPin2 = 3;
const int redpin = 11; //select the pin for the red LED
const int bluepin = 10; // select the pin for the blue LED
const int greenpin = 9; // select the pin for the green LED
const int redpin2 = 7; //select the pin for the 2 red LED
const int bluepin2 = 6; // select the pin for the 2 blue LED
const int greenpin2 = 5; // select the pin for the 2 green LED
/*
Unused variables removed:
int counter = 0;
int pinButton = 13;
int pinButton2 = 3;
int val;
*/
void setup() {
pinMode(buttonPin, INPUT);
pinMode(redpin, OUTPUT);
/*
Depending on your hardware you may want to pull-up the button pins:
pinMode(buttonPin, INPUT_PULLUP);
pinMode(buttonPin2, INPUT_PULLUP);
*/
pinMode(bluepin, OUTPUT);
pinMode(greenpin, OUTPUT);
pinMode(redpin2, OUTPUT);
pinMode(bluepin2, OUTPUT);
pinMode(greenpin2, OUTPUT);
Serial.begin(9600);
}
void loop() {
int buttonState;
buttonState = digitalRead(buttonPin);
int buttonState2;
buttonState2 = digitalRead(buttonPin2);
/*
main() function had several issues, it was a function returning an integer without
any return statement and it is never called by your code. Looks like it should be
part of loop() like so:
*/
if (buttonState == LOW && buttonState2 == LOW) {
analogWrite(11, 0);
analogWrite(10, 225);
analogWrite(9, 0);
delay(5);
analogWrite(7, 0);
analogWrite(6, 225);
analogWrite(5, 0);
delay(5);
} else {
analogWrite(11, 225);
analogWrite(10, 0);
analogWrite(9, 0);
delay(5);
analogWrite(7, 0);
analogWrite(6, 0);
analogWrite(5, 225);
delay(5);
}
}

Is there, and if so a way to use the data packets you receive from the LoRa Transmitter in specific commands like an if statement?

I am finishing up a project for school, and I am at the last step for getting my device 100% working. For a little information on the project. I am developing a wireless speedometer that tracks rpm and converts it to MPH through a hall effects sensor. The modules I am using Heltec LoRa esp32 in addition to the LoRa library through Arduino. The issue I am having is that I am trying to use the data packets received and use the sensor values sent out to be used in an if statement to activate a vibration motor to indicate if they reached a certain top speed. My code is below, and any input on how to use the packets correctly would be much appreciated. Thank you.
Receiver:
//lora stuff
#include <SPI.h>
#include <LoRa.h>
#include "SSD1306.h"
SSD1306 display(0x3c, 4, 15);
#define SS 18
#define RST 14
#define DI0 26
#define BAND 433E6
//neopixel
#include <Adafruit_NeoPixel.h>
#define LED_PIN 32
#define LED_COUNT 1
Adafruit_NeoPixel strip = Adafruit_NeoPixel(1, 32, NEO_GRB + NEO_KHZ800);
const int buttonPin = 17;// button
const int neo = 32; //
const int neoCount = 1; //number of pixels
int counter = 0; //button state counter
int buttonState = 0;
//Vibration motor
int motorPin = 25;
//level of speed in mph to trigger motor
int beginner = 10;
int intermediate = 15;
int expert = 25;
int mphVal = 0;
String data;
unsigned int totalMPH;
void setup() {
//lora
pinMode(16, OUTPUT);
digitalWrite(16, LOW); // set GPIO16 low to reset OLED
delay(50);
digitalWrite(16, HIGH);
display.init();
display.flipScreenVertically();
display.setFont(ArialMT_Plain_10);
display.setTextAlignment(TEXT_ALIGN_LEFT);
// button
pinMode(buttonPin, INPUT);
//motor
pinMode(motorPin, OUTPUT);
//neopixel
strip.begin();
strip.show();
strip.setBrightness(50);
Serial.begin(115200);
//lora initial
while (!Serial); //if just the the basic function, must connect to a computer
delay(100);
Serial.println("Speed Receiver");
display.drawString(5, 5, "Speed Receiver");
display.display();
SPI.begin(5, 19, 27, 18);
LoRa.setPins(SS, RST, DI0);
if (!LoRa.begin(BAND)) {
display.drawString(5, 25, "Starting LoRa failed!");
while (1);
}
Serial.println("LoRa Initial OK!");
display.drawString(5, 25, "LoRa Initializing OK!");
display.display();
}
void loop() {
// try to parse packet
int packetSize = LoRa.parsePacket();
if (packetSize) {
// received a packets
//Serial.print("Received packet. ");
display.clear();
display.setFont(ArialMT_Plain_16);
display.drawString(3, 0, "Received Speed ");
display.display();
// read packet
while (LoRa.available()) {
String data = LoRa.readString();
Serial.print(data);
display.drawString(20, 22, data);
display.display();
// if speed reaches x then motor will buzz
if (data.equals("MPH: 12.5")) {
digitalWrite(motorPin, HIGH);
delay(10);
digitalWrite(motorPin, LOW);
delay(10);
}
}
// print RSSI of packet
//Serial.print(" with RSSI ");
//Serial.println(LoRa.packetRssi());
// display.drawString(20, 45, "RSSI: ");
// display.drawString(70, 45, (String)LoRa.packetRssi());
// display.display();
}
//taking the packet and turning it into an int from string
//String data = data.toInt();
//int data = totalMPH;
//float totalMPH = data.toFloat();
// char data;
// float totalMPH;
//totalMPH = atof(data);
//Serial.println(totalMPH);
//button
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
counter++;
delay(150);
}
//button states with three colors
//beginner
else if (counter == 0) {
//Serial.println("pushed");
strip.setPixelColor(0, 255, 0, 0);
strip.show();
//Serial.println(0);
display.drawString(10, 45, "REC: 10 MPH");
display.display();
//vibration motor will trigger if the speed recieved is less then or equal to 12mph
// digitalWrite(motorPin, HIGH);
// delay(4000);
// digitalWrite(motorPin, LOW);
// delay(120000);
}
//intermediate
else if (counter == 1) {
strip.setPixelColor(0, 0, 255, 0);
strip.show();
Serial.println(1);
display.drawString(10, 45, "REC: 15 MPH");
display.display();
//vibration motor will trigger if the speed recieved is less then or equal to 17mph
// digitalWrite(motorPin, HIGH);
// delay(4000);
// digitalWrite(motorPin, LOW);
// delay(240000);
//digitalWrite(motorPin, LOW);
}
//expert
else if (counter == 2) {
strip.setPixelColor(0, 0, 0, 255);
strip.show();
Serial.print(2);
display.drawString(10, 45, "REC: 20 MPH");
display.display();
//digitalWrite(motorPin, LOW);
}
//reset settings
else {
counter = 0;
}
}
Transmitter:
//lora
#include <SPI.h>
#include <LoRa.h>
#include "SSD1306.h"
#include <Arduino.h>
#include <Bounce2.h>
Bounce hall = Bounce();
SSD1306 display(0x3c, 4, 15);
#define SS 18
#define RST 14
#define DI0 26
#define BAND 433E6 //915E6
int counter = 0;
int rpmTimer = 0;
int rpmInterval = 1000;
void setup() {
//board and lcd
pinMode(25, OUTPUT); //Send success, LED will bright 1 second
pinMode(16, OUTPUT);
digitalWrite(16, LOW); // set GPIO16 low to reset OLED
delay(50);
digitalWrite(16, HIGH);
// sensor and led
hall.attach(37, INPUT);
hall.interval(1);
//pinMode(led, OUTPUT);
//pinMode(hallSens, INPUT);
Serial.begin(115200);
//lcd
while (!Serial); //If just the the basic function, must connect to a computer
// Initialising the UI will init the display too.
display.init();
display.flipScreenVertically();
display.setFont(ArialMT_Plain_10);
display.setTextAlignment(TEXT_ALIGN_LEFT);
display.drawString(5, 5, "LoRa Sender");
display.display();
//lora
SPI.begin(5, 19, 27, 18);
LoRa.setPins(SS, RST, DI0);
Serial.println("LoRa Sender");
if (!LoRa.begin(BAND)) {
Serial.println("Starting LoRa failed!");
while (1);
}
Serial.println("LoRa Initial OK!");
display.drawString(5, 20, "LoRa Initializing OK!");
display.display();
delay(10);
}
void loop() {
hall.update();
if(hall.rose()){
counter++;
}
if(millis() - rpmTimer > rpmInterval){
float rpm = counter * 60;
float rph = rpm * 60;
float dia = 0.00004349;
float cir = dia * 3.14;
float totalMPH = rph * cir;
Serial.println(totalMPH);
rpmTimer = millis();
counter = 0;
//sending packet
LoRa.beginPacket();
LoRa.print("MPH: " + String(totalMPH));
LoRa.endPacket();
//LoRa.sleep(); //puts lora receiver to sleep probably dont need
digitalWrite(25, HIGH); // turn the LED on (HIGH is the voltage level)
delay(10); // wait for a second
digitalWrite(25, LOW); // turn the LED off by making the voltage LOW
delay(100); // wait for a second
//delay(300);
}
}
In terms of wire-based API design:
// packet is the packet from the transmitter
String packet = "S12.5"
String measurement = packet.substring(0, 1); // "S"
if (measurement == "S") {
// at this point we know that the rest of the message is the MPH
String data = packet.substring(1, strlen(packet)-1); // "12.5"
float mph = data.toFloat();
if (mph >= TOP_SPEED) {
doSomething();
}
else {
doSomethingElse();
}
}
you could use charAt(0) to get the measurement instead of substring() if you know there are less than 25 sensors on the network.
each measurement would have its own char at the start of message and the data that related to that measurement would immediately follow. The sender and receiver are therefore interacting like a client/server in an API exchange. There is an implicit contract between them such that, when the transmitter says "S12.5", the receiver knows that means 12.5MPH. It's your system so you can design the communication packets any way you like.

DISABLING VOIDS IN ARDUINO

Ok, basically I'm trying to enable and disable voids in the code. Here is the code:
what I'm trying to create here is a dimmer. I want to enable and disable some of the voids to create that. Also when the value of "brightness" changes in one of the "for" loops, I can't transfer that value to other loops. Please help
int sensorPin = A0;
int sensorValue = 0;
int LED = 10;
void setup() {
Serial.begin(9600);
pinMode(LED, OUTPUT);
}
void loop() {
sensorValue = analogRead(sensorPin);
Serial.println(sensorValue);
if ( analogRead < 30){
Down() = false;
}
if ( analogRead > 31) {
Up() = false;
}
}
void Up () {
for ( int brightness = 0; sensorValue < 30; brightness = brightness + 5 ) {
analogWrite(LED, brightness);
delay(100);
}
}
void Down () {
for ( int brightness = brightness; sensorValue > 31; brightness = brightness - 5 ) {
analogWrite(LED, brightness);
delay(100);
}
}
Still does not know what you want do.
But if I undestand it right (from the comments), the code should be:
int sensorPin = A0;
int sensorValue = 0;
uint8_t lightBrightness = 0;
int LED = 10;
void setup() {
Serial.begin(9600);
pinMode(LED, OUTPUT);
}
void loop() {
sensorValue = analogRead(sensorPin);
// Serial.println(sensorValue);
if ( sensorValue < 30){
if(lightBrightness < 255) lightBrightness++;
analogWrite(LED, lightBrightness );
}
if ( sensorValue > 31) {
if(lightBrightness > 0) lightBrightness--;
analogWrite(LED, lightBrightness );
}
delay(100); // << just to see fading the light
}

How to fix digitalRead

So my problem is that after like two readings of digital pin 6,7 the pin stays on forever for some reason here's my code. I can't seem to find why does it happen I tried to add some delay(x); but it didn't help .. never happend for pin 5 which is even more strange to me... any help will be appreciated
#include <Stepper.h>
int obstaclePin = A0;
int obstaclePin2 = A1;
int obstaclePin3 = A2;
const int button1Pin = 5;
const int button2Pin = 6;
const int button3Pin = 7;
const int stepsPerRevolution = 80;
Stepper myStepper(stepsPerRevolution, 50, 51, 52, 53);
Stepper myStepper2(stepsPerRevolution, 46, 47, 48, 49);
Stepper myStepper3(stepsPerRevolution, 42, 43, 44, 45);
void setup() {
myStepper.setSpeed(100);
myStepper2.setSpeed(100);
myStepper3.setSpeed(100);
pinMode(2,OUTPUT);
pinMode(3,OUTPUT);
pinMode(4,OUTPUT);
pinMode(obstaclePin, INPUT); // sets the sensor pin
pinMode(obstaclePin2, INPUT); // sets the sensor pin
pinMode(obstaclePin3, INPUT); // sets the sensor pin
pinMode(button1Pin, INPUT_PULLUP); // declare pushbutton as input
pinMode(button2Pin, INPUT_PULLUP);
pinMode(button3Pin, INPUT_PULLUP);
Serial.begin(9600); // initialize the serial port:
}
int counter = 0;
void loop() {
while(true){
if(digitalRead(button1Pin)==0){
counter = 1;
break;
}
else if(digitalRead(button2Pin)==0){
counter = 2;
break;
}
else if(digitalRead(button3Pin)==0){
counter = 3;
break;
}
}
while(counter!=0){
digitalWrite(2,HIGH);
digitalWrite(3,HIGH);
digitalWrite(4,HIGH);
//*******************************************************************************************************************************************************
if(counter==1){
if(analogRead(obstaclePin)<=400){
counter=0;
delay(100);
break;
}
Serial.println("Motor 1");
myStepper.step(1);
}
//*******************************************************************************************************************************************************
else if(counter==2){
if(analogRead(obstaclePin2)<=400){
counter=0;
delay(100);
break;
}
Serial.println("Motor 2");
myStepper2.step(1);
}
//*******************************************************************************************************************************************************
else if(counter==3){
if(digitalRead(obstaclePin3)==0){
counter=0;
delay(100);
break;
}
Serial.println("Motor 3");
myStepper3.step(1);
}
}
}
Debouncing the inputs is the solution here.

Openhab doesn't change the status of switch from manual overide

I have just made an account because of this particular problem I'm having with OpenHAB. I was following a tutorial from this https://openhardwarecoza.wordpress.com/2015/03/29/openhab-mqtt-arduino-and-esp8266-part-3-hardware-arduino-with-ethernet-shield/ site but since the reply there didn't help me. I decided to go to this site.
I have successfully installed OpenHAB and use it. When I turn the switch off and on from both the HTTP page and android device, It works just fine. But when I tried to manual override using a push button on an Arduino. It didn't update the state of the switch in both of them. I'm using windows with OpenHAB version 1.71
The Openhab server notices that there is an update of the state from the push button, but the button in the HTTP page and android device didn't change at all.
I have tested the connection using MQTTlens and it works just fine.
Below is my code
items configuration
Group All
Switch mqttsw1 "Switch 1" (all) {mqtt=">[mymosquitto:/arduino/l1/com:command:off:0],>[mymosquitto:/arduino/l1/com:command:on:1],<[mymosquitto:/arduino/l1/state:state:default]"}
Switch mqttsw2 "Switch 2" (all) {mqtt=">[mymosquitto:/arduino/l2/com:command:off:0],>[mymosquitto:/arduino/l2/com:command:on:1],<[mymosquitto:/arduino/l2/state:state:default]"}
Number temp "Temperature [%.1f °C]" <temperature> {mqtt="<[mymosquitto:/arduino/temp/state:state:default]"}
Number hum "Humidity [%.1f &#37]" <temperature> {mqtt="<[mymosquitto:/arduino/hum/state:state:default]"}
Sitemap configuration
sitemap dolphin label="Main Menu"
{
Frame label="Switch" {
Switch item=mqttsw1 label="Switch 1"
Switch item=mqttsw2 label="Switch 2"
}
Frame label="Weather" {
Text item=temp
Text item=hum
}
The Arduino Code
#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>
#include <DHT.h>
const int butt1 = 3;// the pin that the pushbutton is attached to
const int butt2 = 2;
const int ledPin1 = 5;
const int ledPin2 = 4;
int ledState1 = HIGH;
int buttonState1;
int lastButtonState1 = LOW;
int ledState2 = HIGH;
int buttonState2;
int lastButtonState2 = LOW;
long previousMillis = 0;
unsigned long currentMillis = 0;
long interval = 60000; // READING INTERVAL
int t = 0; // TEMPERATURE VAR
int h = 0; // HUMIDITY VAR
#define DHTPIN 24 // SENSOR PIN
#define DHTTYPE DHT11 // SENSOR TYPE - THE ADAFRUIT LIBRARY OFFERS SUPPORT FOR MORE MODELS
DHT dht(DHTPIN, DHTTYPE);
// Update these with values suitable for your network.
byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xEF };
IPAddress ip(192, 168, 1, 103);
IPAddress server(192, 168, 1, 100);
void callback(char* topic, byte* payload, unsigned int length);
EthernetClient ethClient;
PubSubClient client(server, 1883, callback, ethClient);
void callback(char* topic, byte* payload, unsigned int length) {
Serial.println();
Serial.println("Callback");
Serial.print("Topic = ");
Serial.println(topic);
Serial.print("Payload = ");
for (int i=0;i<length;i++){
Serial.print((char)payload[i]);
}
Serial.println();
if (strcmp(topic,"/esp1/l1/com")==0) {
if (payload[0] == '0')
{
digitalWrite(ledPin1, LOW);
delay(100);
client.publish("/esp1/l1/state","0");
}
else if (payload[0] == '1')
{
digitalWrite(ledPin1, HIGH);
delay(100);
client.publish("/esp1/l1/state","1");
}
}
if (strcmp(topic,"/esp1/l2/com")==0) {
if (payload[0] == '0')
{
digitalWrite(ledPin2, LOW);
delay(100);
client.publish("/esp1/l2/state","0");
}
else if (payload[0] == '1')
{
digitalWrite(ledPin2, HIGH);
delay(100);
client.publish("/esp1/l2/state","1");
}
}
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("arduinoClient")) {
Serial.println("connected");
client.subscribe("/esp1/#");
client.publish("conn","Connected");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void push1() {
int reading1 = digitalRead(butt1);
buttonState1 = reading1;
if (buttonState1 == HIGH) {
ledState1 = !ledState1;
if (ledState1 < 1)
{
digitalWrite(ledPin1, LOW);
delay(100);
client.publish("/esp1/l1/com","0");
client.publish("/esp1/l1/state","0");
}
else
{
digitalWrite(ledPin1, HIGH);
delay(100);
client.publish("/esp1/l1/com","1");
client.publish("/esp1/l1/state","1");
}
}
}
void push2() {
int reading2 = digitalRead(butt2);
buttonState2 = reading2;
if (buttonState2 == HIGH) {
ledState2 = !ledState2;
if (ledState2 < 1)
{
digitalWrite(ledPin2, LOW);
delay(100);
client.publish("/esp1/l2/com","0");
client.publish("/esp1/l2/state","0");
}
else
{
digitalWrite(ledPin2, HIGH);
delay(100);
client.publish("/esp1/l2/com","1");
client.publish("/esp1/l2/state","1");
}
}
}
void temp() {
h = (int)dht.readHumidity();
t = (int)dht.readTemperature();
char temp[2];
char hum[3];
sprintf(hum, "%d", h);
sprintf(temp, "%d", t);
client.publish("/esp1/temp/state", temp);
client.publish("/esp1/hum/state", hum);
}
void setup() {
// put your setup code here, to run once:
pinMode(butt1, INPUT);
pinMode(butt2, INPUT);
// initialize the LED as an output:
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
Serial.begin(9600);
Ethernet.begin(mac, ip);
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
pinMode(26, OUTPUT); // sets the digital pin as output
pinMode(22, OUTPUT); // sets the digital pin as output
digitalWrite(26, HIGH); // sets +5v for the sensor
digitalWrite(22, LOW); // sets gnd for the sensor
h = (int)dht.readHumidity();
t = (int) dht.readTemperature();
if (client.connect("arduinoClient")) {
client.publish("conn","Connected");
client.subscribe("/esp1/#");
}
}
void loop() {
// put your main code here, to run repeatedly:
if (!client.connected()) {
reconnect();
}
currentMillis = millis();
if (currentMillis - previousMillis > interval) { // READ ONLY ONCE PER INTERVAL
previousMillis = currentMillis;
temp();
}
int reading1 = digitalRead(butt1);
int reading2 = digitalRead(butt2);
if (reading1 != buttonState1) {
push1();
}
if (reading2 != buttonState2){
push2();
}
client.loop();
}
If there are anybody who can help me I would be very grateful. Thank you for your attention !
Best Regards,
Aldi
If I remember correctly you should post back a status of ON or OFF instead of 1 or 0.
Could you change your arduino code to publish back ON and OFF and test that?
e.g.
client.publish("/esp1/l1/state","ON");

Resources