Start z arduino i ESP8266

radzik_r
Posty: 390
Rejestracja: ndz sie 11, 2019 5:32 pm

elmaya pisze: wt lip 13, 2021 3:04 pm
radzik_r pisze: wt lip 13, 2021 2:43 pm
jaku2k pisze: wt lip 13, 2021 1:41 pm
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":

Kod: Zaznacz cały

mcp1.init(4, 5); // init(uint8_t sda, uint8_t scl, bool fast)  =  Wire.begin
Masz rację.
Faktycznie, zapomniałem o tym.
jaku2k
Posty: 830
Rejestracja: ndz maja 24, 2020 8:40 pm
Kontakt:

Super, dziękuję za odpowiedzi.

Alright, thank you for answeres.
Pozdrawiam
Jakub

PS. Czekam na Supla Offline Party 2024
rom32420
Posty: 4
Rejestracja: śr lip 19, 2023 11:17 am

Cześć,
Próbuje uruchomić moduł ESP32S3 z expanderem i2c MCP23017. Robię według powyższych przykładów, ale wygląda to jak by w ogóle nie były wykorzystywane funkcje "CustomDigitalRead/Write".
Gdy wysterowuję expander we funkcji "Setup" poleceniami "mcp.digitalWrite/read" to wszystko działa, wyjścia są wysterowywane, a wejścia sczytywane. Z poziomu kodu supli przekaźnik jest zarejestrowany, ze strony internetowej można zmienić stan przekaźnika, ale fizycznie na płycie nic się nie dzieje. Poprosił bym o sugestię bardziej doświadczonych kolegów co zrobiłem nie tak? :?

Kod: Zaznacz cały

/*
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 <SuplaDevice.h>
#include <supla/control/rgbw_leds.h>
#include <supla/control/button.h>
#include <supla/control/relay.h>
#include <Adafruit_MCP23017/Adafruit_MCP23017.h>
#include <Wire.h>
#include <supla/io.h>

#define I2C_SDA 41
#define I2C_SCL 42
Adafruit_MCP23017 mcp;

// Choose proper network interface for your card:
#ifdef ARDUINO_ARCH_AVR
  // 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);
#elif defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
  // ESP8266 and ESP32 based board:
  #include <supla/network/esp_wifi.h>
  Supla::ESPWifi wifi("wifi", "haslo"); //dane ukryte
#endif
/*
 * Youtube: https://youtu.be/FE9tqzTjmA4
 * Youtube example was done on older version of SuplaDevice library
 */

#define RED_PIN1              21
#define GREEN_PIN1            14
#define BLUE_PIN1             13
#define BRIGHTNESS_PIN1       255
#define BUTTON_PIN1           0
#define RED_PIN2              255
#define GREEN_PIN2            255
#define BLUE_PIN2             255
#define BRIGHTNESS_PIN2       12
#define BUTTON_PIN2           18
#define MAIN_RELAY            40
#define WL_DRZWI1             111 //mcp.digitalRead(11)
#define WL_DRZWI2             103 //mcp.digitalRead(3)
///#define wl_swiatlo            11
#define LED_ZIEL_WL1         109
#define LED_ZIEL_WL2         101
#define LED_CZER_WL1         110
#define LED_CZER_WL2         102
#define LED_WARNING          108

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

         return;
      }
      if (pin <= 99) {
        //pinMode(12, OUTPUT);
       // digitalWrite(12,1);
        return ::pinMode(pin,mode);   // ------------------------------ so that the other channels work normally
      }
   }
   
    int customDigitalRead(int channelNumber, uint8_t pin) {
      //pinMode(40, OUTPUT);
      //digitalWrite(40,1);
      if ((pin >= 100)&& (pin <= 115)) {
        //mcp.pinMode(pin - 100, INPUT);
        return ::mcp.digitalRead(pin - 100);     
      }     
      if (pin <= 99){
        return ::digitalRead(pin);  // ------------------------------ so that the other channels work normally
      }
    }   
}; 
MyMcp23017 instanceMyMcp23017;

void setup() {
  Wire.begin(I2C_SDA, I2C_SCL, 100000);
  mcp.begin();
  Serial.begin(115200);

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

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

  /*
   * 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.
   */

  // CHANNEL0 - RGB controller and dimmer (RGBW)
  auto rgbw1 = new Supla::Control::RGBWLeds(
      RED_PIN1, GREEN_PIN1, BLUE_PIN1, BRIGHTNESS_PIN1);

  auto rgbw2 = new Supla::Control::RGBWLeds(
      RED_PIN2, GREEN_PIN2, BLUE_PIN2, BRIGHTNESS_PIN2);

  auto button1 = new Supla::Control::Button(BUTTON_PIN1, false, true);
  auto button2 = new Supla::Control::Button(BUTTON_PIN2, false, true); //pin, podciaganie, inwersja stanu
  auto button3 = new Supla::Control::Button(WL_DRZWI1, false, true);

  
  auto relay1 = new Supla::Control::Relay(LED_WARNING, 0);
  auto relay2 = new Supla::Control::Relay(MAIN_RELAY, 1);

  button1->setMulticlickTime(200);
  button1->setHoldTime(400);
  button1->repeatOnHoldEvery(35);
  button2->setMulticlickTime(200);
  button2->setHoldTime(400);
  button2->repeatOnHoldEvery(35);
  button3->setMulticlickTime(200);
  button3->setHoldTime(400);
  button3->repeatOnHoldEvery(35);
  rgbw1->setStep(1);
  rgbw2->setStep(1);
// rgbw->setMinMaxIterationDelay(750);  // delay between dimming direction
                                        // change, 750 ms (default)
//  rgbw->setMinIterationBrightness(1);  // 1 is default value

  button1->addAction(Supla::ITERATE_DIM_ALL, rgbw1, Supla::ON_HOLD);
  button1->addAction(Supla::TOGGLE, rgbw1, Supla::ON_CLICK_1);
  button2->addAction(Supla::TOGGLE, rgbw2, Supla::ON_CLICK_1);
  button3->addAction(Supla::TOGGLE, relay2, Supla::ON_CLICK_1);
  //button2->addAction(Supla::ITERATE_DIM_ALL, rgbw2, Supla::ON_HOLD);
//-----------------------------------------------------------------------------------------------


mcp.pinMode(2, OUTPUT); // pin LED_CZERWONA2 wyjscie

mcp.digitalWrite(2, LOW);






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


  /*
   * SuplaDevice Initialization.
   * Server address is 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
      "svrxx.supla.org",  // SUPLA server address (dane ukryte)
      "adres@email",   // Email address used to login to Supla Cloud (Dane ukryte)
      AUTHKEY);          // Authorization key
}

void loop() {
  SuplaDevice.iterate();
}
Gdyby tak nagle zniknął prąd i internet... :mrgreen:
Awatar użytkownika
lukfud
Posty: 2091
Rejestracja: czw lis 23, 2017 11:33 pm
Lokalizacja: Warszawa

rom32420 pisze: ndz sie 27, 2023 6:59 am
Ostatnio zmieniło się podejście do ekspanderów
viewtopic.php?p=145479#p145479

Wrzuć do src/supa/control jako EXT_MCP23017.h

Kod: Zaznacz cały

#ifndef SRC_SUPLA_CONTROL_EXT_MCP23017_H_
#define SRC_SUPLA_CONTROL_EXT_MCP23017_H_

/*
Dependency: https://github.com/RobTillaart/MCP23017_RT
use library manager to install it
*/

#include <MCP23017_RT.h>
#include <supla/io.h>

namespace Supla {
namespace Control {
	
class ExtMCP23017 : public Supla::Io {
 public:
  ExtMCP23017(uint8_t address = 0x20)
      : ioExpander(address), address(address), Supla::Io(false) {
    if (!ioExpander.begin()) {
      SUPLA_LOG_DEBUG("Unable to find MCP23017");
    } else {
      Serial.print("MCP23017 is connected at address: 0x");
      Serial.println(address, HEX);
      isConnected = true;
    }
  }

  void customPinMode(int channelNumber, uint8_t pin, uint8_t mode) override {
    if (isConnected) {
	  ioExpander.pinMode(pin, mode);
    }
  }
  void customDigitalWrite(int channelNumber, uint8_t pin,
                                                        uint8_t val) override {
    if (isConnected) {
	  ioExpander.digitalWrite(pin, val);
    }
  }
  int customDigitalRead(int channelNumber, uint8_t pin) override {
      return isConnected ? ioExpander.digitalRead(pin) : 0;
  }
  unsigned int customPulseIn(int channelNumber, uint8_t pin, uint8_t value,
                                              uint64_t timeoutMicro) override {
    // add implementation or leave empty
    return 0;
  }
  void customAnalogWrite(int channelNumber, uint8_t pin, int val) override {
    // add implementation
  }

  int customAnalogRead(int channelNumber, uint8_t pin) override {
    // add implementation
    return 0;
  }

 protected:
  ::MCP23017_RT ioExpander;
  uint8_t address;
  bool isConnected = false;
};

};  // namespace Control
};  // namespace Supla

#endif  // SRC_SUPLA_CONTROL_EXT_MCP23017_H_
i dołącz bibliotekę https://github.com/RobTillaart/MCP23017_RT w załączniku jest lekko zmodyfikowana
Zmieniłem nazwę klasy z MCP23017 na MCP23017_RT
https://github.com/RobTillaart/MCP23017 ... 3017.h#L30
i wyłączyłem domyślny pullup (używam przekaźników sterowanych stanem wysokim)
https://github.com/RobTillaart/MCP23017 ... 17.cpp#L73
0xFF -> 0x00

W szkicu dodajesz odpowiednie linie:

Kod: Zaznacz cały

#include <supla/control/EXT_MCP23017.h>
Supla::Control::ExtMCP23017 *extmcp = nullptr;

setup() {
  Wire.begin(sda,scl);
  Wire.setClock(400000); // opcjonalnie
  
  extmcp = new Supla::Control::ExtMCP23017(0x20);
  
  auto sensor = new Supla::Sensor::Binary(extmcp, 0, true); // 0 - 15
  
  auto relay = new Supla::Control::Relay(extmcp, 1, true);
  
  auto button = new Supla::Control::Button(extmcp, 2, true, true);
  
  // itd
}
Załączniki
MCP23017_RT.zip
(12.38 KiB) Pobrany 20 razy
https://www.facebook.com/groups/supladiy/
rom32420
Posty: 4
Rejestracja: śr lip 19, 2023 11:17 am

Cześć, dzięki za super szybką pomoc.
Wszystko zadziałało, musiałem tylko wykomentować:

"SUPLA_LOG_DEBUG("Unable to find MCP23017");"

z pliku EXT_MCP23017.h gdyż kompilator pluł błędami i nie rozpoznawał polecenia. Pewnie chodzi o brak podłączenia odpowiedniej biblioteki do debugowania. Na razie działa bez tego.

Pozdrawiam i hylę czoła. :ugeek:
Gdyby tak nagle zniknął prąd i internet... :mrgreen:
Awatar użytkownika
lukfud
Posty: 2091
Rejestracja: czw lis 23, 2017 11:33 pm
Lokalizacja: Warszawa

rom32420 pisze: ndz sie 27, 2023 8:55 pm Wszystko zadziałało, musiałem tylko wykomentować:
"SUPLA_LOG_DEBUG("Unable to find MCP23017");"
z pliku EXT_MCP23017.h gdyż kompilator pluł błędami i nie rozpoznawał polecenia. Pewnie chodzi o brak podłączenia odpowiedniej biblioteki do debugowania. Na razie działa bez tego.
Dodaj do pliku poniższą linię

Kod: Zaznacz cały

#include <supla/log_wrapper.h>
https://www.facebook.com/groups/supladiy/
ODPOWIEDZ

Wróć do „Arduino IDE”