0

I am working on a project to run some (relays or LED's) on an inputs (Pins) Arduino by esp8266-01s . now while searching on internet I found a video divided it two step that talks about the issue using a ESP8266-01. The important thing in the topic is that the person uses two codes for project :

1) The first code for esp8266.

2) The second code for arduino uno.

The most important part of the topic I want to modify the code for esp8266 . but the owner of the video uses node red website and I want to use the cayenne website . I want to specify basic commands to modify in order to work on cayenne website .

I play with esp and arduino in order to project offline.

First video , Second video

// Code to upload onto ESP8266

 #include <PubSubClient.h>
 #include <ESP8266WiFi.h>


 //EDIT THESE LINES TO MATCH YOUR SETUP
 #define MQTT_SERVER "192.168.1.117"  ///YourMQTTBroker'sIP
 const char* ssid = "Weems_2GHz";
 const char* password = "1029384756";

 char* Topic = "inTopic"; //subscribe to topic to be notified about

 WiFiClient wifiClient;

 void setup() {
 //initialize the light as an output and set to LOW (off)

 //start the serial line for debugging
 Serial.begin(9600);
 delay(100);


//start wifi subsystem
WiFi.begin(ssid, password);
//attempt to connect to the WIFI network and then connect to the MQTT server
reconnect();

//wait a bit before starting the main loop
  delay(2000);
}

PubSubClient client(MQTT_SERVER, 1883, callback, wifiClient);

void loop(){

//reconnect if connection is lost
if (!client.connected() && WiFi.status() == 3) {reconnect();}

//maintain MQTT connection
client.loop();

//MUST delay to allow ESP8266 WIFI functions to run
delay(20); 

}


void callback(char* topic, byte* payload, unsigned int length) {

//convert topic to string to make it easier to work with
String topicStr = topic; 

//Print out some debugging info
////Serial.println("Callback update.");
//Serial.print("Topic: ");
//Serial.println(topicStr);

//turn the light off if the payload is '0' and publish to the MQTT server a confirmation message
if (payload[0] == '0'){
Serial.print("0");
respond();}
if(payload[0] == '1'){
Serial.print("1");
respond();}
if (payload[0] == '2'){
Serial.print("2");
respond();}
if(payload[0] == '3'){
Serial.print("3");
respond();}
if (payload[0] == '4'){
Serial.print("4");
respond();}
if(payload[0] == '5'){
Serial.print("5");
respond();}
if (payload[0] == '6'){
Serial.print("6");
respond();}
if(payload[0] == '7'){
Serial.print("7");
respond(); 
}

}
void respond(){
delay(5);
if (Serial.available()) {
//read serial as a character
char ser = Serial.read();
switch (ser) {
  case 'a':
client.publish("outTopic", "Relay 1 is ON");
break;
  case 'b':
client.publish("outTopic", "Relay 1 is OFF");
break;
  case 'c':
client.publish("outTopic", "Relay 2 is ON");
break;
  case 'd':
client.publish("outTopic", "Relay 2 is OFF");
break;
  case 'e':
client.publish("outTopic", "Relay 3 is ON");
break;
  case 'f':
client.publish("outTopic", "Relay 3 is OFF");
break;
  case 'g':
client.publish("outTopic", "Relay 4 is ON");
break;
  case 'h':
client.publish("outTopic", "Relay 4 is OFF");
break;
}//switch 
}//if
}//void
void reconnect() {

//attempt to connect to the wifi if connection is lost
if(WiFi.status() != WL_CONNECTED){

while (WiFi.status() != WL_CONNECTED) {
  delay(500);
}
}

//make sure we are connected to WIFI before attemping to reconnect to MQTT
if(WiFi.status() == WL_CONNECTED){
// Loop until we're reconnected to the MQTT server
while (!client.connected()) {
  //Serial.print("Attempting MQTT connection...");

  // Generate client name based on MAC address and last 8 bits of microsecond counter
  String clientName;
  clientName += "esp8266-";
  uint8_t mac[6];
  WiFi.macAddress(mac);
  clientName += macToStr(mac);

  //if connected, subscribe to the topic(s) we want to be notified about
  if (client.connect((char*) clientName.c_str())) {
    Serial.print("\tMTQQ Connected");
    client.subscribe(Topic);
  }

  //otherwise print failed for debugging
  else{Serial.println("\tFailed."); abort();}
  }
  }
  }

  //generate unique name from MAC addr
  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;
  }

/* control Relays via serial with numbers*/

/// Upload to Arduino uno

 void setup(){
 Serial.begin(9600);
 //Set all the pins we need to output pins
 pinMode(12, OUTPUT);
 pinMode(11, OUTPUT);
 pinMode(10, OUTPUT);
 pinMode(9, OUTPUT);
 digitalWrite(12, HIGH);
 digitalWrite(11, HIGH);
 digitalWrite(10, HIGH);
 digitalWrite(9, HIGH);
 }

 void loop (){
 if (Serial.available()) {
 //read serial as a character
 char ser = Serial.read();

 //NOTE because the serial is read as "char" and not "int", the read value must be compared to character numbers
 //hence the quotes around the numbers in the case statement
 switch (ser) {
  case '0':
    digitalWrite(12, LOW);
    Serial.print("a");
    break;
  case '1':
    digitalWrite(12, HIGH);
    Serial.print("b");
    break;
  case '2':
    digitalWrite(11, LOW);
    Serial.print("c");
    break;
  case '3':
    digitalWrite(11, HIGH);
    Serial.print("d");
    break;
  case '4':
    digitalWrite(10, LOW);
    Serial.print("e");
    break;
  case '5':
    digitalWrite(10, HIGH);
    Serial.print("f");
    break;
  case '6':
    digitalWrite(9, LOW);
    Serial.print("g");
    break;
  case '7':
    digitalWrite(9, HIGH);
    Serial.print("h");
    break;

   }  
  }
 }
  • sadly, neither "backend" has anything to do with arduino code, so this is off-topic. that said, there's not really much in your code that needs changing to use either one. I do wonder what the nano is for if your running code on the ESP; is it needed? – dandavis Feb 10 at 18:28
  • @dandavis I did not understand anything from what you say. What do you mean by the backend. And what do you mean by being outside the topic? – Khalid Abu-Khadija Feb 10 at 19:00
  • Just change the MQTT details to those you need for the cayenne website. – Majenko Feb 10 at 19:13
  • @Majenko the edit will do on code for esp yes . And i want specify line important in order to work change this code . Plese edit code or specify line change for code – Khalid Abu-Khadija Feb 10 at 20:38
  • I suggest you look at some of the example code that should exist for working with cayenne, then compare that with your existing code. And yes, on the esp. – Majenko Feb 10 at 22:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.