• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

esp32 websocket with two on/off switches

Q&A Forum › Category: ESP32 › esp32 websocket with two on/off switches
0 Vote Up Vote Down
David Barker asked 3 months ago

hi
please find code here
https://github.com/davani1411/ws_on_off
I cannot get the placeholders to work correctly
can you help 
thanks

Question Tags: esp32 on /off buttons
5 Answers
0 Vote Up Vote Down
Sara Santos Staff answered 3 months ago

Hi.
Can you be a little more specific?
Please describe your exact issues and any errors that you have.
Regards,
Sara

0 Vote Up Vote Down
David Barker answered 3 months ago

hI
please find picture of buttons
https://docs.google.com/document/d/18j7YoV7R6V4rGuseqhoqQ9WPu4fZa9kV/edit
the buttons gpio27 return the correct place holder but in the  place holder for direction buttons 
I think the problem is in this part of the programme

// Replaces placeholder with LED state value
String processor(const String& var) {
  if(var == “STATE”) {
    if(digitalRead(ledPin)) {
      ledState = 1;
      return “forward”;
    }
    else {
      ledState = 0;
      return “backward”;
    }
  }
 return String();

   if(var == “STATE2”) {
    if(digitalRead(ledPin)) {
      ledState = 1;
      return “start”;
    }
    else {
      ledState = 0;
      return “stop”;
    }
  }
    return String();
}

void notifyClients2(String state) {
  ws.textAll(state);
}
void notifyClients(String state2) {
  ws.textAll(state2);
}

hope this makes it clear
regards
 
 

0 Vote Up Vote Down
Sara Santos Staff answered 3 months ago

Hi.
You have two return String() in your processor() function. Remove the first one.
You also have an issue with your javascript.
You should modify your onMessage() function to something like this:

function onMessage(event) {
  var data = event.data;
  if (data == "ON" || data =="OFF"){
    document.getElementById('state2').innerHTML = event.data;
  }
  if (data == "forward" || data =="backward"){
    document.getElementById('state').innerHTML = event.data;
  }
  console.log(event.data);
}

I hope this helps.
Regards,
Sara
 
 

0 Vote Up Vote Down
David Barker answered 3 months ago

yes yes yes it works
thanks very much
regards
david

0 Vote Up Vote Down
Sara Santos Staff answered 3 months ago

Great.
I’ll mark this issue as resolved. If you need further help, you just need to open a new question in our forum.
Regards,
Sara

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [eBook Updated] SMART HOME with RPi, ESP32, and ESP8266 (version 1.2) March 8, 2023
  • [eBook Updated] ESP32-CAM Projects (version 1.3) January 7, 2023

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2023 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.