WIFI switch with the ESP8266

7 years 8 months ago #3757 by swisscore
which example are you speaking about ?

Please Log in or Create an account to join the conversation.

7 years 8 months ago #3758 by jonarotz

Please Log in or Create an account to join the conversation.

7 years 8 months ago #3759 by swisscore
Ok yes, and the goal is to have 2 relay in one plug ? or 2 differents relay in another box for another usage ?

Please Log in or Create an account to join the conversation.

7 years 8 months ago #3760 by swisscore
I think it's not the best example to add a second command, but you probably need to add a second "storage.moduleId" like "storage.moduleId2" but it's complicate to implement in this code, I haven't passed few minutes to analyze but not so easy for beginner (I am one too)

Please Log in or Create an account to join the conversation.

7 years 8 months ago #3761 by jonarotz
Yes the goal is to use one NodeMCU board to operate separate relay boards, I'd be happy with 2 relays, but the more the better.

swisscore wrote: I think it's not the best example to add a second command, but you probably need to add a second "storage.moduleId" like "storage.moduleId2" but it's complicate to implement in this code, I haven't passed few minutes to analyze but not so easy for beginner (I am one too)


That code is way to complicated for me to try to figure out. Maybe someone can chime in and help.
I appreciate your help!

Please Log in or Create an account to join the conversation.

7 years 8 months ago #3762 by swisscore
Here is a example of easier code :
#include <PubSubClient.h>
#include <ESP8266WiFi.h>

//////////////////////////Added toggling led with any topic publish "1"  /////
 
const char* ssid = "............";
const char* password = "............";
 
char* topic = "#";     //  using wildcard to monitor all traffic from mqtt server
char* server = "192.168.0.7";  // Address of my server on my network, substitute yours!
 
char message_buff[100];   // initialise storage buffer (i haven't tested to this capacity.)
 
WiFiClient wifiClient;
PubSubClient client(server, 1883, callback, wifiClient);
 
 
String macToStr(const uint8_t* mac)
{
  String result;
  for (int i = 0; i < 6; ++i) {
    result += String(mac[i], 16);
    if (i < 5)
      result += ':';
  }
  return result;
}
 
void setup() {
  Serial.begin(115200);
  delay(10);
  
    // prepare GPIO2 *********************************************************************
  pinMode(2, OUTPUT);   // i am using gpio2 as output to toggle an LED
  digitalWrite(2, 0);  //*****************************************************************
  
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  
  WiFi.begin(ssid, password);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");  
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
 
  //  connection to broker script.
  if (client.connect("arduinoClient")) {
    client.publish("outTopic","hello world");
    client.subscribe("#");   // i should probably just point this to the varialbe above.
  }
}
 
void loop() {

  client.loop();
}


void callback(char* topic, byte* payload, unsigned int length) {
  // Here is what i have been using to handle subscriptions. I took it as a snippet from elsewhere but i cannot credit author as i dont have reference!
    int i = 0;

  Serial.println("Message arrived:  topic: " + String(topic));
  Serial.println("Length: " + String(length,DEC));
  
  // create character buffer with ending null terminator (string)
  for(i=0; i<length; i++) {
    message_buff[i] = payload[i];
  }
  message_buff[i] = '\0';
  
  String msgString = String(message_buff);
  
  Serial.println("Payload: " + msgString);
  int state = digitalRead(2);  // get the current state of GPIO1 pin
  if (msgString == "1"){    // if there is a "1" published to any topic (#) on the broker then:
    digitalWrite(2, !state);     // set pin to the opposite state 
    Serial.println("Switching LED"); 
  }
}
- See more at: www.esp8266.com/viewtopic.php?f=29&t=2745#sthash.ruYbgvsU.dpuf

Please Log in or Create an account to join the conversation.

Time to create page: 0.291 seconds

Forum latest

  • No posts to display.