Start z arduino i ESP8266

User avatar
lukfud
Posts: 2117
Joined: Thu Nov 23, 2017 11:33 pm
Location: Warszawa

Post

radzik_r wrote: Mon Nov 02, 2020 8:27 pm Co to jest to:

Code: Select all

#include <io.h>
https://github.com/klew/arduino/blob/ma ... supla/io.h

Zaktualizuj bibliotekę ;)
https://www.facebook.com/groups/supladiy/
elmaya
Posts: 1482
Joined: Wed Jun 27, 2018 5:48 pm
Location: El Saucejo - Sevilla

Post

Updated example.
the supla dev library has changed a lot in these months.

Code: Select all

/*
Copyright (C) AC SOFTWARE SP. Z O.O.

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

#include <SPI.h>
#include <SuplaDevice.h>
#include <supla/io.h>
#include <supla/control/relay.h>
#include <Adafruit_MCP23017.h>


// Choose proper network interface for your card:
// Arduino Mega with EthernetShield W5100:
// #include <supla/network/ethernet_shield.h>
// Ethernet MAC address
// uint8_t mac[6] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05};
// Supla::EthernetShield ethernet(mac);
//
// Arduino Mega with ENC28J60:
// #include <supla/network/ENC28J60.h>
// Supla::ENC28J60 ethernet(mac);
//
// ESP8266 based board:
#include <supla/network/esp_wifi.h>
Supla::ESPWifi wifi("your_wifi_ssid", "your_wifi_password");
//
// ESP32 based board:
// #include <supla/network/esp32_wifi.h>
// Supla::ESP32Wifi wifi("your_wifi_ssid", "your_wifi_password");

Adafruit_MCP23017 mcp;

class MyMcp23017 : public Supla::Io {
  public:
    void customDigitalWrite(int channelNumber, uint8_t pin, uint8_t val) {
      if ((pin >= 100)&& (pin <= 115)){
        mcp.digitalWrite(pin - 100, val);  
         return;
      }
      if (pin <= 99) {
        return ::digitalWrite(pin,val);  
      }
   }
   
   int customDigitalRead(int channelNumber, uint8_t pin) {
      if ((pin >= 100)&& (pin <= 115)){
        return mcp.digitalRead(pin - 100);     
      }     
      if (pin <= 99){
        return ::digitalRead(pin);  
      }
    }   
}MyMcp23017; 

void setup() {

  Serial.begin(115200);

   mcp.begin(); 
   
  mcp.pinMode(0, OUTPUT);
  mcp.pinMode(1, OUTPUT);
  mcp.pinMode(2, OUTPUT);
  mcp.pinMode(3, OUTPUT);
  mcp.pinMode(4, OUTPUT);
  mcp.pinMode(5, OUTPUT);
  mcp.pinMode(6, OUTPUT);
  mcp.pinMode(7, OUTPUT);
  mcp.pinMode(8, OUTPUT);
  mcp.pinMode(9, OUTPUT);
  mcp.pinMode(10, OUTPUT);
  mcp.pinMode(11, OUTPUT);
  mcp.pinMode(12, OUTPUT);
  mcp.pinMode(13, OUTPUT);
  mcp.pinMode(14, OUTPUT);
  mcp.pinMode(15, OUTPUT); 

  // Replace the falowing GUID with value that you can retrieve from https://www.supla.org/arduino/get-guid
  char GUID[SUPLA_GUID_SIZE] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};

  // Replace the following AUTHKEY with value that you can retrieve from: https://www.supla.org/arduino/get-authkey
  char AUTHKEY[SUPLA_AUTHKEY_SIZE] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};

  /*
   * Having your device already registered at cloud.supla.org,
   * you want to change CHANNEL sequence or remove any of them,
   * then you must also remove the device itself from cloud.supla.org.
   * Otherwise you will get "Channel conflict!" error.
   */

    new Supla::Control::Relay(100, true);     // A0 // 
    new Supla::Control::Relay(101, true);     // A1 //  
    new Supla::Control::Relay(102, true);     // A2 //  
    new Supla::Control::Relay(103, true);     // A3 //  
    new Supla::Control::Relay(104, true);     // A4 // 
    new Supla::Control::Relay(105, true);     // A5 //  
    new Supla::Control::Relay(106, true);     // A6 // 
    new Supla::Control::Relay(107, true);     // A7 // 
    new Supla::Control::Relay(108, true);     // B0 //  
    new Supla::Control::Relay(109, true);     // B1 //  
    new Supla::Control::Relay(110, true);     // B2 //  
    new Supla::Control::Relay(111, true);     // B3 //  
    new Supla::Control::Relay(112, true);     // B4 //  
    new Supla::Control::Relay(113, true);     // B5 //  
    new Supla::Control::Relay(114, true);     // B6 //  
    new Supla::Control::Relay(115, true);     // B7 //  

  /*
   * SuplaDevice Initialization.
   * Server address, LocationID and LocationPassword are available at https://cloud.supla.org 
   * If you do not have an account, you can create it at https://cloud.supla.org/account/create
   * SUPLA and SUPLA CLOUD are free of charge
   * 
   */

  SuplaDevice.begin(GUID,              // Global Unique Identifier 
                    "svr1.supla.org",  // SUPLA server address
                    "email@address",   // Email address used to login to Supla Cloud
                    AUTHKEY);          // Authorization key
    
}

void loop() {
  SuplaDevice.iterate();

  delay(15);  
}
radzik_r
Posts: 404
Joined: Sun Aug 11, 2019 5:32 pm

Post

Cool.
These are the changes that I was struggling with today, and eventually I came to it myself.
I also added a second MCP as buttons. I don't know if I made it right but it works.
of course with replacing button.h from elmaya

Code: Select all

Adafruit_MCP23017 mcp;
Adafruit_MCP23017 mcp2;

#include <supla/io.h>

class MyMcp23017 : public Supla::Io {
  public:
    void customDigitalWrite(int channelNumber, uint8_t pin, uint8_t val) {
      if ((pin >= 100) && (pin <= 115)) {
        mcp.digitalWrite(pin - 100, val);
               return;
      }
      if (pin <= 99) {
        return ::digitalWrite(pin, val);  // ------------------------------ so that the other channels work normally
      }
    }

    int customDigitalRead(int channelNumber, uint8_t pin) {
      if ((pin >= 100) && (pin <= 115)) {
        return mcp.digitalRead(pin - 100);
      }
      if (pin <= 99) {
        return ::digitalRead(pin);  // ------------------------------ so that the other channels work normally
      }
      if ((pin >= 200) && (pin <= 215)) {
        return mcp2.digitalRead(pin - 200);
      }
    }
} MyMcp23017;

Code: Select all

  auto r1 = new Supla::Control::Relay(100);
  auto b1 = new Supla::Control::Button(200);//, true, true); // przycisk na pin3, z input pullup i odwróconą logiką (zwieranie do gnd)
  b1->addAction(Supla::TOGGLE , r1, Supla::ON_PRESS);
elmaya
Posts: 1482
Joined: Wed Jun 27, 2018 5:48 pm
Location: El Saucejo - Sevilla

Post

I guess you also added the initialization of the second Mcp23017 in setup

Code: Select all

 
  mcp2.begin(1);  
  mcp2.pinMode(0, INPUT);  mcp2.pullUp(0, HIGH);  // turn on a 100K pullup internally
  mcp2.pinMode(1, INPUT);  mcp2.pullUp(1, HIGH);  // turn on a 100K pullup internally
  mcp2.pinMode(2, INPUT);  mcp2.pullUp(2, HIGH);  // turn on a 100K pullup internally
  mcp2.pinMode(3, INPUT);  mcp2.pullUp(3, HIGH);  // turn on a 100K pullup internally
  mcp2.pinMode(4, INPUT);  mcp2.pullUp(4, HIGH);  // turn on a 100K pullup internally
  mcp2.pinMode(5, INPUT);  mcp2.pullUp(5, HIGH);  // turn on a 100K pullup internally
  mcp2.pinMode(6, INPUT);  mcp2.pullUp(6, HIGH);  // turn on a 100K pullup internally
  mcp2.pinMode(7, INPUT);  mcp2.pullUp(7, HIGH);  // turn on a 100K pullup internally
  mcp2.pinMode(8, INPUT);  mcp2.pullUp(8, HIGH);  // turn on a 100K pullup internally
  mcp2.pinMode(9, INPUT);  mcp2.pullUp(9, HIGH);  // turn on a 100K pullup internally
  mcp2.pinMode(10, INPUT);  mcp2.pullUp(10, HIGH);  // turn on a 100K pullup internally
  mcp2.pinMode(11, INPUT);  mcp2.pullUp(11, HIGH);  // turn on a 100K pullup internally
  mcp2.pinMode(12, INPUT);  mcp2.pullUp(12, HIGH);  // turn on a 100K pullup internally
  mcp2.pinMode(13, INPUT);  mcp2.pullUp(13, HIGH);  // turn on a 100K pullup internally
  mcp2.pinMode(14, INPUT);  mcp2.pullUp(14, HIGH);  // turn on a 100K pullup internally
  mcp2.pinMode(15, INPUT);  mcp2.pullUp(15, HIGH);  // turn on a 100K pullup internally
radzik_r
Posts: 404
Joined: Sun Aug 11, 2019 5:32 pm

Post

Yes of course

my design ideas have too little gpio in esp so mcp is a lifesaver

I used for

Code: Select all

for (int i = 0; i <= 15; i++) {
    mcp2.pinMode(i, INPUT);  mcp2.pullUp(i, HIGH);  // turn on a 100K pullup internally
  }
Moezzz
Posts: 1
Joined: Tue Dec 15, 2020 3:43 pm

Post

radzik_r wrote: Tue Nov 03, 2020 6:36 pm Cool.
These are the changes that I was struggling with today, and eventually I came to it myself.
I also added a second MCP as buttons. I don't know if I made it right but it works.
of course with replacing button.h from elmaya

Code: Select all

Adafruit_MCP23017 mcp;
Adafruit_MCP23017 mcp2;

#include <supla/io.h>

class MyMcp23017 : public Supla::Io {
  public:
    void customDigitalWrite(int channelNumber, uint8_t pin, uint8_t val) {
      if ((pin >= 100) && (pin <= 115)) {
        mcp.digitalWrite(pin - 100, val);
               return;
      }
      if (pin <= 99) {
        return ::digitalWrite(pin, val);  // ------------------------------ so that the other channels work normally
      }
    }

    int customDigitalRead(int channelNumber, uint8_t pin) {
      if ((pin >= 100) && (pin <= 115)) {
        return mcp.digitalRead(pin - 100);
      }
      if (pin <= 99) {
        return ::digitalRead(pin);  // ------------------------------ so that the other channels work normally
      }
      if ((pin >= 200) && (pin <= 215)) {
        return mcp2.digitalRead(pin - 200);
      }
    }
} MyMcp23017;

Code: Select all

  auto r1 = new Supla::Control::Relay(100);
  auto b1 = new Supla::Control::Button(200);//, true, true); // przycisk na pin3, z input pullup i odwróconą logiką (zwieranie do gnd)
  b1->addAction(Supla::TOGGLE , r1, Supla::ON_PRESS);
Dobry wieczór
Nie rozumiem, jak wskazałeś adresy I2C dwóch mcps. proszę wyjaśnić tę kwestię
michcio667
Posts: 10
Joined: Fri Jul 17, 2020 8:27 pm

Post

Moezzz wrote: Thu Jan 21, 2021 7:48 pm
radzik_r wrote: Tue Nov 03, 2020 6:36 pm Cool.
These are the changes that I was struggling with today, and eventually I came to it myself.
I also added a second MCP as buttons. I don't know if I made it right but it works.
of course with replacing button.h from elmaya

Code: Select all

Adafruit_MCP23017 mcp;
Adafruit_MCP23017 mcp2;

#include <supla/io.h>

class MyMcp23017 : public Supla::Io {
  public:
    void customDigitalWrite(int channelNumber, uint8_t pin, uint8_t val) {
      if ((pin >= 100) && (pin <= 115)) {
        mcp.digitalWrite(pin - 100, val);
               return;
      }
      if (pin <= 99) {
        return ::digitalWrite(pin, val);  // ------------------------------ so that the other channels work normally
      }
    }

    int customDigitalRead(int channelNumber, uint8_t pin) {
      if ((pin >= 100) && (pin <= 115)) {
        return mcp.digitalRead(pin - 100);
      }
      if (pin <= 99) {
        return ::digitalRead(pin);  // ------------------------------ so that the other channels work normally
      }
      if ((pin >= 200) && (pin <= 215)) {
        return mcp2.digitalRead(pin - 200);
      }
    }
} MyMcp23017;

Code: Select all

  auto r1 = new Supla::Control::Relay(100);
  auto b1 = new Supla::Control::Button(200);//, true, true); // przycisk na pin3, z input pullup i odwróconą logiką (zwieranie do gnd)
  b1->addAction(Supla::TOGGLE , r1, Supla::ON_PRESS);
Dobry wieczór
Nie rozumiem, jak wskazałeś adresy I2C dwóch mcps. proszę wyjaśnić tę kwestię
Cześć.
W funkcji mcp.begin(); użyty jest adres 0x20. A w deklaracji mcp.begin(1); użyty jest adres 0x21. A fizycznie o ile się nie mylę to wejścia w układzie MCP a0,a1,a2 powinny być połączone do gnd do expandera pierwszego. A dla drugiego expandera a0 podajemy logiczną 1(+5v) i a1 oraz a2 do gnd. I tak kolejny expander jak deklarujemy to musimy zmienić adres fizyczny za pomocą wejść w układzie a0-a2. Oraz deklaracje w programie.
jaku2k
Posts: 861
Joined: Sun May 24, 2020 8:40 pm

Post

radzik_r wrote: Tue Nov 03, 2020 6:36 pm Cool.
These are the changes that I was struggling with today, and eventually I came to it myself.
I also added a second MCP as buttons. I don't know if I made it right but it works.
of course with replacing button.h from elmaya
Witam,
gdzie ustawia się piny, do których podpięty jest MCP?
Pozdrawiam
Jakub
radzik_r
Posts: 404
Joined: Sun Aug 11, 2019 5:32 pm

Post

jaku2k wrote: Tue Jul 13, 2021 1:41 pm
radzik_r wrote: Tue Nov 03, 2020 6:36 pm Cool.
These are the changes that I was struggling with today, and eventually I came to it myself.
I also added a second MCP as buttons. I don't know if I made it right but it works.
of course with replacing button.h from elmaya
Witam,
gdzie ustawia się piny, do których podpięty jest MCP?
nigdzie się nie ustawia.
MCP23017 podłącza się do wemosa poprzez I2C.
D1(gpio5) SCL
D2(gpio4) SDA
elmaya
Posts: 1482
Joined: Wed Jun 27, 2018 5:48 pm
Location: El Saucejo - Sevilla

Post

radzik_r wrote: Tue Jul 13, 2021 2:43 pm
jaku2k wrote: Tue Jul 13, 2021 1:41 pm
radzik_r wrote: Tue Nov 03, 2020 6:36 pm Cool.
These are the changes that I was struggling with today, and eventually I came to it myself.
I also added a second MCP as buttons. I don't know if I made it right but it works.
of course with replacing button.h from elmaya
Witam,
gdzie ustawia się piny, do których podpięty jest MCP?
nigdzie się nie ustawia.
MCP23017 podłącza się do wemosa poprzez I2C.
D1(gpio5) SCL
D2(gpio4) SDA
@radzik_r are you sure about that?
in your 12 blind code you have this in "setup" before "mcp1.begin":

Code: Select all

mcp1.init(4, 5); // init(uint8_t sda, uint8_t scl, bool fast)  =  Wire.begin

Return to “Arduino IDE”