-->
Page 1 of 1

4 channels input + 4 channel output

PostPosted: Sat Apr 22, 2017 12:05 pm
by davidcgu
Hello guys

Can someone support me on this project? I'm trying to make 4 digital inputs + 4 digital outputs at once on module ESP12E but I'm not able to make it work.

If I disable part of the code for inputs then outputs are working or oposite too however all at once looks to make module bloacket.

Thanks in advance for your help

Code: Select all//ItKindaWorks - Creative Commons 2016
//github.com/ItKindaWorks
//
//Requires PubSubClient found here: https://github.com/knolleary/pubsubclient
//
//ESP8266 Simple MQTT light controller


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




//EDIT THESE LINES TO MATCH YOUR SETUP
#define MQTT_SERVER "192.168.1.200"
const char* ssid = "JAZZTEL_david";
const char* password = "0102030405060";



// Assign Arduino Friendly Names to GPIO pins

   
  #define D0 16  //
  #define D1 5 // I2C Bus SCL (clock)
  #define D2 4 // I2C Bus SDA (data)
  #define D3 0
 // #define D4 2 // Same as "LED_BUILTIN", but inverted logic
  #define D5 14 // SPI Bus SCK (clock)
  #define D6 12 // SPI Bus MISO
  #define D7 13 // SPI Bus MOSI
  #define D8 15 // SPI Bus SS (CS)
  #define D9 3 // RX0 (Serial console)
  #define D10 1 // TX0 (Serial console)
    const int internalled = 2; //GPIO 2 internal led

int D0State = 0;
int D1State = 0;
int D2State = 0;
int D3State = 0;

int D5State = 0;
int D6State = 0;
int D7State = 0;
int D8State = 0;

char* Topic = "/test/light";



WiFiClient wifiClient;
// Callback function header
void callback(char* topic, byte* payload, unsigned int length);
PubSubClient client(MQTT_SERVER, 1883,callback, wifiClient);
long lastMsg = 0;
char msg[50];


void setup() {


  //initialize the light as an output and set to  (off)
   pinMode(D0, INPUT);
   pinMode(D1,INPUT);
   pinMode(D2,INPUT);
   pinMode(D3, INPUT);
 
   pinMode (D5, OUTPUT);
   pinMode (D6, OUTPUT);
   pinMode (D7, OUTPUT);
   pinMode (D8, OUTPUT);
   
 
   EEPROM.begin(512);              // Begin eeprom to store on/off state
  D5State = EEPROM.read(0);
  digitalWrite(D5,D5State);
  D6State = EEPROM.read(1);
  digitalWrite(D6,D6State);
  D7State = EEPROM.read(2);
  digitalWrite(D7,D7State);
  D8State = EEPROM.read(3);
  digitalWrite(D8,D8State);

 


    pinMode(internalled, OUTPUT);
  digitalWrite(internalled, HIGH);

  //start the serial line for debugging
  Serial.begin(115200);
  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);
}



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(10);
  digitalWrite(internalled, LOW);
 
 
}

 

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);
 D0State = digitalRead(D0);
 D1State = digitalRead(D1);
 D2State = digitalRead(D2);
 D3State = digitalRead(D3);


  //turn the light on if the payload is '1' and publish to the MQTT server a confirmation message
   if  (payload[0] == '0'){
    digitalWrite(D5, HIGH);
    client.publish("/test/light1", "out1 On");
    D5State = HIGH;
    EEPROM.write(0, D5State);    // Write state to EEPROM
    EEPROM.commit();

 }

  //turn the light off if the payload is '0' and publish to the MQTT server a confirmation message
 
else if (payload[0] == '1'){
    digitalWrite(D5, LOW);
    client.publish("/test/light1", "out1 Off");
    D5State = LOW;
    EEPROM.write(0, D5State);    // Write state to EEPROM
    EEPROM.commit();
  }

   //turn the light2 on if the payload is '1' and publish to the MQTT server a confirmation message
else if (payload[0] == '2'){
    digitalWrite(D6, HIGH);
    client.publish("/test/light1", "out2 On");
    D6State = HIGH;
    EEPROM.write(1, D6State);    // Write state to EEPROM
    EEPROM.commit();
   

  }

  //turn the light2 off if the payload is '0' and publish to the MQTT server a confirmation message
else if (payload[0] == '3'){
    digitalWrite(D6, LOW);
    client.publish("/test/light1", "out2 Off");
    D6State = LOW;
    EEPROM.write(1, D6State);    // Write state to EEPROM
    EEPROM.commit();
  }

   //turn the light3 on if the payload is '1' and publish to the MQTT server a confirmation message
 else if (payload[0] == '4'){
    digitalWrite(D7, HIGH);
    client.publish("/test/light1", "out3 On");
    D7State = HIGH;
    EEPROM.write(2, D7State);    // Write state to EEPROM
    EEPROM.commit();

  }

  //turn the light3 off if the payload is '0' and publish to the MQTT server a confirmation message
 else if (payload[0] == '5'){
    digitalWrite(D7, LOW);
    client.publish("/test/light1", "out3 Off");
    D7State = LOW;
    EEPROM.write(2, D7State);    // Write state to EEPROM
    EEPROM.commit();
  }

    //turn the light4 on if the payload is '1' and publish to the MQTT server a confirmation message
 else if (payload[0] == '6'){
    digitalWrite(D8, HIGH);
    client.publish("/test/light1", "out4 On");
     D8State = HIGH;
    EEPROM.write(3, D8State);    // Write state to EEPROM
    EEPROM.commit();

  }

  //turn the light4 off if the payload is '0' and publish to the MQTT server a confirmation message
 
 
else if (payload[0] == '7'){
    digitalWrite(D8, LOW);
    client.publish("/test/light1", "out4 Off");
     D8State = LOW;
    EEPROM.write(3, D8State);    // Write state to EEPROM
    EEPROM.commit();
 
 
}
  //turn the lights off if the payload is '8' and publish to the MQTT server a confirmation message
 
 //else if (payload[0] == '8'){
   // digitalWrite(D0, LOW);
    //digitalWrite(D1, LOW);
    //digitalWrite(D2, LOW);
    //digitalWrite(D3, LOW);
    //client.publish("/test/light1", "all lights Off");
    //client.publish("/test/light1", "out1 Off");
    //client.publish("/test/light1", "out2 Off");
    //client.publish("/test/light1", "out3 Off");
    //client.publish("/test/light1", "out4 Off");
  //}

   //turn aa the lights on if the payload is '9' and publish to the MQTT server a confirmation message
 
 //else if (payload[0] == '9'){
   // digitalWrite(D0, HIGH);
   // digitalWrite(D1, HIGH);
    //digitalWrite(D2, HIGH);
    //digitalWrite(D3, HIGH);
    //client.publish("/test/light1", "all lights on");
    //client.publish("/test/light1", "out1 On");
    //client.publish("/test/light1", "out2 On");
    //client.publish("/test/light1", "out3 On");
    //client.publish("/test/light1", "out4 On");
  //}


 if  (D0State == HIGH);{
client.publish("/test/light1", "input1");}

if  (D1State == HIGH);{
client.publish("/test/light1", "input2");}


if  (D2State == HIGH);{
client.publish("/test/light1", "input3");}


if  (D3State == HIGH);{
client.publish("/test/light1", "input4");}


}







void reconnect() {

  //attempt to connect to the wifi if connection is lost
  if(WiFi.status() != WL_CONNECTED){
    //debug printing
    Serial.print("Connecting to ");
    Serial.println(ssid);
    digitalWrite(internalled, HIGH);
   

    //loop while we wait for connection
    while (WiFi.status() != WL_CONNECTED) {
      delay(500);
      Serial.print(".");
    }

    //print out some more debug once connected
    Serial.println("");
    Serial.println("WiFi connected"); 
    Serial.println("IP address: ");
    Serial.println(WiFi.localIP());
    //-digitalWrite(internalledpin, LOW);
   
  }

  //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("/test/light1");
      }

      //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;
}



//------------------------------------------------------------------------------------------------------------------------------