Welcome, Guest
Username: Password: Remember me

TOPIC: WIFI switch with the ESP8266

WIFI switch with the ESP8266 6 months 2 weeks ago #3757

  • swisscore
  • swisscore's Avatar
  • Offline
  • New Member
  • Posts: 16
  • Karma: 0
which example are you speaking about ?
The administrator has disabled public write access.

WIFI switch with the ESP8266 6 months 2 weeks ago #3758

  • jonarotz
  • jonarotz's Avatar
  • Offline
  • New Member
  • Posts: 18
  • Karma: 0
This is the tutorial/example I am using:

iot-playground.com/blog/2-uncategorised/...mqtt-api-v1-improved
The administrator has disabled public write access.

WIFI switch with the ESP8266 6 months 2 weeks ago #3759

  • swisscore
  • swisscore's Avatar
  • Offline
  • New Member
  • Posts: 16
  • Karma: 0
Ok yes, and the goal is to have 2 relay in one plug ? or 2 differents relay in another box for another usage ?
The administrator has disabled public write access.

WIFI switch with the ESP8266 6 months 2 weeks ago #3760

  • swisscore
  • swisscore's Avatar
  • Offline
  • New Member
  • Posts: 16
  • Karma: 0
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)
The administrator has disabled public write access.

WIFI switch with the ESP8266 6 months 2 weeks ago #3761

  • jonarotz
  • jonarotz's Avatar
  • Offline
  • New Member
  • Posts: 18
  • Karma: 0
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!
The administrator has disabled public write access.

WIFI switch with the ESP8266 6 months 2 weeks ago #3762

  • swisscore
  • swisscore's Avatar
  • Offline
  • New Member
  • Posts: 16
  • Karma: 0
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
The administrator has disabled public write access.
Time to create page: 0.145 seconds
Powered by Kunena Forum