Transferring a Byte via SPI

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
Roland_60
New user
Posts: 3
Joined: 23 Oct 2021, 22:26

Transferring a Byte via SPI

#1 Post by Roland_60 » 23 Oct 2021, 22:54

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

TD-er
Core team member
Posts: 8643
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Transferring a Byte via SPI

#2 Post by TD-er » 23 Oct 2021, 23:00

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.

Roland_60
New user
Posts: 3
Joined: 23 Oct 2021, 22:26

Re: Transferring a Byte via SPI

#3 Post by Roland_60 » 23 Oct 2021, 23:25

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?

TD-er
Core team member
Posts: 8643
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Transferring a Byte via SPI

#4 Post by TD-er » 23 Oct 2021, 23:31

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

ankan
New user
Posts: 5
Joined: 02 Nov 2017, 08:45
Location: Stockholm, Sweden

Re: Transferring a Byte via SPI

#5 Post by ankan » 28 Sep 2022, 19:40

Roland_60: How did it go? Did you manage to implement your digital potentiometer with espeasy?

Roland_60
New user
Posts: 3
Joined: 23 Oct 2021, 22:26

Re: Transferring a Byte via SPI

#6 Post by Roland_60 » 15 Oct 2022, 17:56

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

ankan
New user
Posts: 5
Joined: 02 Nov 2017, 08:45
Location: Stockholm, Sweden

Re: Transferring a Byte via SPI

#7 Post by ankan » 15 Oct 2022, 18:45

@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.

Post Reply

Who is online

Users browsing this forum: No registered users and 20 guests