Page 1 of 1

Transferring a Byte via SPI

Posted: 23 Oct 2021, 22:54
by Roland_60
Hi!

I need to control a Digital Potentiometer (MCP 4151-103E/P) via the SPI interface of the nodeMCU. The value is received from the host via MQTT.
The digital potentiometer will substitute a manual potentiometer in the controller of my heating system.

I use the same nodeMCU to read some temperatures using DS18b20 and transfer those via MQTT as well.

I study already for a while the documentation. I found that SPI is somehow supported. However, I cannot find out how I could use it for this purpose. I need to transfer only one Byte via SPI to this device and the value will change only few times a day.

Could anybody give me a hint how to do?

Best regards,
Roland

Re: Transferring a Byte via SPI

Posted: 23 Oct 2021, 23:00
by TD-er
Currently the only way how SPI is used in ESPEasy is via plugins for specific hardware.
So the best way here would be to write a very basic plugin to support your potentiometer chip and accept commands for it (implemented in PLUGIN_WRITE call in the plugin)

The reason why I think it should be a plugin is that you need to select something else like a CS line too and you definately don't want to add a command that would write just anything to the SPI bus as it may cause a lot of havoc.

Re: Transferring a Byte via SPI

Posted: 23 Oct 2021, 23:25
by Roland_60
Thank you for the swift answer.

I found a documentation how to add a plugin to ESPEasy and I had a look to the plugins in the src folder. Which of those plugins would be best as a template for my project?

Re: Transferring a Byte via SPI

Posted: 23 Oct 2021, 23:31
by TD-er
I would go for the smallest, as you simply won't have to do a lot in the plugin.

P006 BMP085 is a nice small one to start with to see how quickly you can get it to compile with your own changes (e.g. start with renaming the plugin and assigning a new plugin ID to it for starters)
To see one of the simple SPI plugins (not using a lot of GPIO pins) see where DEVICE_TYPE_SPI is used, like P039

Re: Transferring a Byte via SPI

Posted: 28 Sep 2022, 19:40
by ankan
Roland_60: How did it go? Did you manage to implement your digital potentiometer with espeasy?

Re: Transferring a Byte via SPI

Posted: 15 Oct 2022, 17:56
by Roland_60
Hi!
Yes, it worked. However, I was working on this a year ago and I hardly remember how I made it and the project is not yet finished. The target was to make my domestic heating system smarter replacing the manual control with this digi-poti. Finally, I had to give other issues a priority and the project is still stuck. But winter is coming! I will resume to work on this. (Although my old oil-fired heating system is due for replacement)

What I have by now is the following:

I used, as recommended the P006 BMP085 as template. I did create a P116 and did list it in define_plugin_sets.h.

Working with the Arduino IDE I used the features provided in this environment for the SPI communication with the module.
Init module and communication:

Code: Select all

    case PLUGIN_INIT:
      {
        SPI.begin();
        pinMode(CHIPSELECT, OUTPUT);
        wiper = 0.5;
        success    = true;
        break;
      }
Writing a value if ESP Easy receives the command mcp41xx_digipoti followed by a real number between 0 and 1

Code: Select all

    case PLUGIN_WRITE:
      {
        String command = parseString(string, 1, ':');
        if (command == "mcp41xx_digipoti") {
          String cmdValue = parseString(string, 2, ':');
          char str[20];
          wiper = atof(&cmdValue[0]);
          if (wiper > 1) {
            wiper = 1;
          }
          if (wiper < 0) {
            wiper = 0;
          }
          uint8_t setCValue = (uint8_t) (255 * wiper);
          digitalWrite(CHIPSELECT, LOW);
          delay(100);
          SPI.transfer(B00000000);     //write cmd
          SPI.transfer(setCValue);  //write value
          delay(100);
          digitalWrite(CHIPSELECT, HIGH);
        } //command == "mcp41xx_digipoti"
       UserVar[event->BaseVarIndex] = wiper;
        success = true;
        break;
      } // case PLUGIN_WRITE:
  }

Sorry for the late answer.

Roland

Re: Transferring a Byte via SPI

Posted: 15 Oct 2022, 18:45
by ankan
@Roland_60 thanks for your replay.

I have it now working in ESPEasy with a MCP41010 that is controlling my indoor termostat on my heatpump.
For someone who knows ESPEasy l guess it is quite easy to develop a ESPEasy component but I just created a custom spi device.