Build preconfigured ESP with MQTT and sensors/actuators

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
flo17
New user
Posts: 6
Joined: 26 Sep 2019, 17:39

Build preconfigured ESP with MQTT and sensors/actuators

#1 Post by flo17 » 13 Oct 2019, 21:35

Hello Everyone,

We have a project at school (bachelor degrees) to develop a IoT system for external application (greenhouse, chicken coop,...).
We want to use an architecture with 3 layers :

- Cloud App with user frontend
- Central Unit (Rpi connected to the Internet) - Bridge between cloud and edge devices + administration
- Edge devices (ESP) with differents sensors/actuators

I am currently working on the esp side and looking to find the best way to preconfigure the ESP from the RPI. The idea is that the user have to connect the new ESP to the Rpi (by USB or Serial) to add it to the system. A new image will be compiled with correct Wifi and MQTT settings and flashed to the board.
I have already used EspEasy for previous projects and I would like to know if is it possible to configure the "devices" (sensors/actuators) from source files or by terminal ?

Best regards

Flo17
P.S. Sorry for spelling mystakes, english is not my mother tongue

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

Re: Build preconfigured ESP with MQTT and sensors/actuators

#2 Post by TD-er » 13 Oct 2019, 22:00

I've made a very basic attempt a while ago to add rules and some tasks depending on the chosen factory default setting.
But it is quite a lot of work to have it setup like that for all plugins.
Another approach is to let the files download from another host.
See the "settings archive": https://espeasy.readthedocs.io/en/lates ... gs-archive
It is not finished yet, but may help you think of possible options.

flo17
New user
Posts: 6
Joined: 26 Sep 2019, 17:39

Re: Build preconfigured ESP with MQTT and sensors/actuators

#3 Post by flo17 » 13 Oct 2019, 22:47

Thank you TD-er for your quick answer.
I can understand the complexity (specially with devices using multiples GPIO).

I don't think that the "settings archives" is a viable option, I would like to avoid the ("dummy") user to connect to the ESP.

In that case, I will certainly go for a homemade software.

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

Re: Build preconfigured ESP with MQTT and sensors/actuators

#4 Post by TD-er » 14 Oct 2019, 09:00

Well, we could also extend the layers of authentication.
Now there is:
- No authentication
- Admin

When a password is set, you cannot change anything, but also a lot of view-only pages are inaccessible.
Maybe we also need some "operator" level, which could at most view values.
But it also needs some added layer for the commands then.

So instead of re-inventing the wheel, you could maybe also help in deciding what is needed to make it "classroom proof" ?

flo17
New user
Posts: 6
Joined: 26 Sep 2019, 17:39

Re: Build preconfigured ESP with MQTT and sensors/actuators

#5 Post by flo17 » 14 Oct 2019, 09:54

From my view, it would be interesting having a "device" command with some parameters :
  • device : id of the module to use (P001,P002,...)
    name : name of the device
    GPIO : array of GPIO needed by the device
which will call the PLUGIN_DEVICE_ADD: method of the plugin.

flo17
New user
Posts: 6
Joined: 26 Sep 2019, 17:39

Re: Build preconfigured ESP with MQTT and sensors/actuators

#6 Post by flo17 » 14 Oct 2019, 15:25

I take a look of the R120 source code. It seems that it should be possible to add a sensor by running a method with this line :

Code: Select all

Settings.TaskDeviceNumber[0] = 5;
tmpString = 'TestSensor'
strcpy(ExtraTaskSettings.TaskDeviceName, tmpString);
Settings.TaskDeviceTimer[0] = 0;
Settings.TaskDeviceID[0] = 0;
Settings.TaskDevicePin1[0] = 2;
Settings.TaskDevicePluginConfig[event->TaskIndex][0] = 22;
Settings.TaskDeviceSendData = 1;
ExtraTaskSettings.TaskDeviceValueDecimals[1] = 2;
tmpString = ''
strcpy(ExtraTaskSettings.TaskDeviceFormula[1], tmpString);
ExtraTaskSettings.TaskDeviceValueDecimals[2] = 2;
tmpString = ''
strcpy(ExtraTaskSettings.TaskDeviceFormula[2], tmpString);
tmpString = 'Temperature'
strcpy(ExtraTaskSettings.TaskDeviceValueNames[1], tmpString);
tmpString = 'Humidity'
strcpy(ExtraTaskSettings.TaskDeviceValueNames[2], tmpString);
I took the parameters from the POST method Image
.

I will try to build this image on a ESP.

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

Re: Build preconfigured ESP with MQTT and sensors/actuators

#7 Post by TD-er » 14 Oct 2019, 16:07

R120 source code is about 2 years old now.
Maybe you could have a look at a bit more recent version?
I don't think the old code will compile with the current core versions.

flo17
New user
Posts: 6
Joined: 26 Sep 2019, 17:39

Re: Build preconfigured ESP with MQTT and sensors/actuators

#8 Post by flo17 » 14 Oct 2019, 16:20

Yeah you right, I get the source from the last release and will adapt it.

flo17
New user
Posts: 6
Joined: 26 Sep 2019, 17:39

Re: Build preconfigured ESP with MQTT and sensors/actuators

#9 Post by flo17 » 15 Oct 2019, 09:07

I tried something like this,but I am getting a reboot when trying to access /devices

Code: Select all

void SetSensors()
{
  byte taskIndex = 0;
  char deviceName[] = "TestSensor";
  unsigned long taskdevicetimer = 60;
  bool enabled = true;
  int pin1 = 2;
  int pin2 = -1;
  int pin3 = -1;

  char deviceValue0[] = "Temperature";
  char deviceFormula0[] = "";
  int deviceValueDecimal0 = 2;

  char deviceValue1[] = "Humidity";
  int deviceValueDecimal1 = 2;
  char deviceFormula1[] = "";

  //LoadTaskSettings(taskIndex);

  Settings.TaskDevicePluginConfig[taskIndex][0] = 22;

  Settings.TaskDeviceNumber[taskIndex] = 5;
  safe_strncpy(ExtraTaskSettings.TaskDeviceName, deviceName, sizeof(ExtraTaskSettings.TaskDeviceName));

  Settings.TaskDeviceEnabled[taskIndex] = enabled;

  if (taskdevicetimer > 0) {
    Settings.TaskDeviceTimer[taskIndex] = taskdevicetimer;
  } else {
    Settings.TaskDeviceTimer[taskIndex] = 0;
  }

  if (pin1 >= 0) { Settings.TaskDevicePin1[taskIndex] = pin1; }

  if (pin2 >= 0) { Settings.TaskDevicePin2[taskIndex] = pin2; }

  if (pin3 >= 0) { Settings.TaskDevicePin3[taskIndex] = pin3; }


  Settings.TaskDeviceSendData[0][0] = 1;

  strcpy(ExtraTaskSettings.TaskDeviceValueNames[0], deviceValue0);
  strcpy(ExtraTaskSettings.TaskDeviceFormula[0], deviceFormula0);
  ExtraTaskSettings.TaskDeviceValueDecimals[1] = deviceValueDecimal0;

  strcpy(ExtraTaskSettings.TaskDeviceValueNames[1], deviceValue1);
  strcpy(ExtraTaskSettings.TaskDeviceFormula[1], deviceFormula1);
  ExtraTaskSettings.TaskDeviceValueDecimals[1] = deviceValueDecimal1;

}
I think the isuee should be related to the ExtraTaskSettings (cannot call LoadTaskSettings method)

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

Re: Build preconfigured ESP with MQTT and sensors/actuators

#10 Post by TD-er » 15 Oct 2019, 11:02

Without calling LoadTaskSettings(taskIndex);
You will probably have the wrong extratasksettings active.
So changing it will corrupt the settings of another task (when saved)

Also I would not use char arrays like that.
Just to be sure, you can store them in a String object and call c_str() on the object.
You may also want to have a look at the F() macro, since having plain strings in the code will very quickly let you run out of memory.


You also made a typo here:
ExtraTaskSettings.TaskDeviceValueDecimals[1] = deviceValueDecimal0;

This is a list of controllers for the taskindex (2nd parameter) so you should iterate over it, or just call clearTask(taskindex) first.
Settings.TaskDeviceSendData[0][0] = 1;

Post Reply

Who is online

Users browsing this forum: No registered users and 23 guests