Can't create jacks with esp32 driver.

SHARK_2

New Member
View Badges
Joined
Dec 13, 2020
Messages
14
Reaction score
10
Location
Warsaw
Rating - 0%
0   0   0
Hi @Ranjib,

First thank You for great update with support of esp32.

I've made some custom boards with esp32 :D
pH reader (ver 1.0):
download

PowerLed Lamp driver:
download

Expansion ports:
download

But I need Your help, I can't create jacks using esp32 driver.
download

download

download

In log i get:
Code:
May 10 13:14:01 home reef-pi[513758]: 2023/05/10 13:14:01 API Request:'   GET /api/me' from: 192.168.3.13:51294
May 10 13:14:09 home reef-pi[513758]: 2023/05/10 13:14:09 API Request:'   PUT /api/jacks' from: 192.168.3.13:51294
May 10 13:14:09 home reef-pi[513758]: 2023/05/10 13:14:09 ERROR: Failed to create. Error: invalid pin 0: driver rpi for jack test1 not found: driver by id rpi not available
May 10 13:14:09 home reef-pi[513758]: 2023/05/10 13:14:09 API Request:'   GET /api/jacks' from: 192.168.3.13:51294
May 10 13:14:11 home reef-pi[513758]: 2023/05/10 13:14:11 API Request:'   GET /api/me' from: 192.168.3.13:51294
May 10 13:14:21 home reef-pi[513758]: 2023/05/10 13:14:21 API Request:'   GET /api/me' from: 192.168.3.13:51294
May 10 13:14:23 home reef-pi[513758]: 2023/05/10 13:14:23 API Request:'   PUT /api/jacks' from: 192.168.3.13:51294
May 10 13:14:23 home reef-pi[513758]: 2023/05/10 13:14:23 ERROR: Failed to create. Error: invalid pin 27: driver rpi for jack test1 not found: driver by id rpi not available
May 10 13:14:23 home reef-pi[513758]: 2023/05/10 13:14:23 API Request:'   GET /api/jacks' from: 192.168.3.13:51294

download


The strange thing is that I choose esp32 as the driver, but log and gui says about rpi driver.

The esp32 itself works - I can change the brightness of individual channels:
download


Can You (or someone else) give me a hint what I am doing wrong?
 
OP
OP
S

SHARK_2

New Member
View Badges
Joined
Dec 13, 2020
Messages
14
Reaction score
10
Location
Warsaw
Rating - 0%
0   0   0
I was able to "cheat" the app and add jacks, but it didn't work for me right from the start.
 

Sral

Valuable Member
View Badges
Joined
May 2, 2022
Messages
1,006
Reaction score
976
Location
Germany
Rating - 0%
0   0   0
I was able to "cheat" the app and add jacks, but it didn't work for me right from the start.
How exactly did you cheat ?

Also: what Browser and device were you using to access ReefPi ? Might be an issue with the drop-down UI, as it seems to try and use the rpi driver even though you have selected the esp32-driver.

Have you restarted or rebooted ReefPi after establishing the driver ?
 
OP
OP
S

SHARK_2

New Member
View Badges
Joined
Dec 13, 2020
Messages
14
Reaction score
10
Location
Warsaw
Rating - 0%
0   0   0
Hi @Sral

I configured the driver to a nonexistent IP address. Thanks to this, I was able to add jacks without any problems. After this procedure, I reconfigured the Driver to the target IP and it worked.

I am able to control all channels of the lamp. Unfortunately, I noticed another problem. I don't know why, but from time to time reef-pi sets "0" PWM on some channels, even though at this particular time there should be a different value
 
OP
OP
S

SHARK_2

New Member
View Badges
Joined
Dec 13, 2020
Messages
14
Reaction score
10
Location
Warsaw
Rating - 0%
0   0   0
Log from esp32 serial (look at pin 27):
Code:
PWM Pin19 DutyCycle:89
PWM Pin27 DutyCycle:41
PWM Pin26 DutyCycle:0
PWM Pin25 DutyCycle:103
PWM Pin33 DutyCycle:109
PWM Pin17 DutyCycle:127
PWM Pin16 DutyCycle:98
PWM Pin19 DutyCycle:89
PWM Pin18 DutyCycle:89
PWM Pin27 DutyCycle:41
PWM Pin26 DutyCycle:0
PWM Pin25 DutyCycle:103
PWM Pin33 DutyCycle:109
PWM Pin17 DutyCycle:127
PWM Pin16 DutyCycle:97
PWM Pin18 DutyCycle:89
PWM Pin19 DutyCycle:89
Value:27.69
PWM Pin27 DutyCycle:40
PWM Pin26 DutyCycle:0
...
PWM Pin18 DutyCycle:89
PWM Pin27 DutyCycle:40
PWM Pin26 DutyCycle:0
...
Value:27.62
PWM Pin19 DutyCycle:89
PWM Pin27 DutyCycle:40
PWM Pin25 DutyCycle:102
...
PWM Pin27 DutyCycle:39
PWM Pin26 DutyCycle:0
PWM Pin25 DutyCycle:102
PWM Pin33 DutyCycle:108
PWM Pin17 DutyCycle:127
PWM Pin16 DutyCycle:96
PWM Pin19 DutyCycle:89
PWM Pin18 DutyCycle:89
Value:27.56
PWM Pin27 DutyCycle:39
...
PWM Pin27 DutyCycle:39
PWM Pin26 DutyCycle:0
PWM Pin25 DutyCycle:102
PWM Pin33 DutyCycle:108
PWM Pin17 DutyCycle:127
PWM Pin16 DutyCycle:96
PWM Pin27 DutyCycle:0
Value:27.56
PWM Pin19 DutyCycle:89
PWM Pin27 DutyCycle:38
...
PWM Pin27 DutyCycle:38
PWM Pin26 DutyCycle:0
PWM Pin25 DutyCycle:102
PWM Pin33 DutyCycle:108
PWM Pin17 DutyCycle:127
PWM Pin16 DutyCycle:95
PWM Pin18 DutyCycle:89
Value:27.50

But don't see (attachment) this in reef-pi log (grep L2#CH1 /var /log /syslog | less - L2#CH1 is pin nr 27).

Whether the PWM values sent in POST should not be INT??

Screenshot 2023-05-12 at 21.59.33.png
 
Last edited:

Sral

Valuable Member
View Badges
Joined
May 2, 2022
Messages
1,006
Reaction score
976
Location
Germany
Rating - 0%
0   0   0
Hi @Sral

I configured the driver to a nonexistent IP address. Thanks to this, I was able to add jacks without any problems. After this procedure, I reconfigured the Driver to the target IP and it worked.

I am able to control all channels of the lamp. Unfortunately, I noticed another problem. I don't know why, but from time to time reef-pi sets "0" PWM on some channels, even though at this particular time there should be a different value
Interesting. Might be a communication or url translation error. Have you noticed this only on pin 27 ?
If yes, is pin 27 the first in the list of jacks by any chance ?
 
OP
OP
S

SHARK_2

New Member
View Badges
Joined
Dec 13, 2020
Messages
14
Reaction score
10
Location
Warsaw
Rating - 0%
0   0   0
Interesting. Might be a communication or url translation error. Have you noticed this only on pin 27 ?
If yes, is pin 27 the first in the list of jacks by any chance ?
Yes pin 27 is the first in the list of hacks:
Code:
const int jackPins[JACK_COUNT] = { 27, 26, 25, 33, 16, 17, 18, 19 };
const int pwmChannels[JACK_COUNT] = { 0, 1, 2, 3, 4, 5, 6, 7 };
//const int analogInputPins[ANALOG_INPUT_COUNT] = { 34 };
const int oneWirePin = 4;

And I didn't analyze (yet) rest of jacks.

I also have problem with esp32 crash from time to time.
 

Sral

Valuable Member
View Badges
Joined
May 2, 2022
Messages
1,006
Reaction score
976
Location
Germany
Rating - 0%
0   0   0
Yes pin 27 is the first in the list of hacks:
Code:
const int jackPins[JACK_COUNT] = { 27, 26, 25, 33, 16, 17, 18, 19 };
const int pwmChannels[JACK_COUNT] = { 0, 1, 2, 3, 4, 5, 6, 7 };
//const int analogInputPins[ANALOG_INPUT_COUNT] = { 34 };
const int oneWirePin = 4;

And I didn't analyze (yet) rest of jacks.

I also have problem with esp32 crash from time to time.
Interesting. That suggests to me that it is a url translation error. The URL translation went wrong sometimes in my tests, likely causing a value of 0 for the 0th Jack and also causing Null-pointers that crash the ESP32.

You could try using my modified PathVariableHandler library instead of the original. If my thinking is right it might both reduce the number of crashes and also keep the jacks from erroneous values of 0.
 
OP
OP
S

SHARK_2

New Member
View Badges
Joined
Dec 13, 2020
Messages
14
Reaction score
10
Location
Warsaw
Rating - 0%
0   0   0
Interesting. That suggests to me that it is a url translation error. The URL translation went wrong sometimes in my tests, likely causing a value of 0 for the 0th Jack and also causing Null-pointers that crash the ESP32.

You could try using my modified PathVariableHandler library instead of the original. If my thinking is right it might both reduce the number of crashes and also keep the jacks from erroneous values of 0.
I just built a sketch with your library, let's see how my esp32 will behaves.
 

Sral

Valuable Member
View Badges
Joined
May 2, 2022
Messages
1,006
Reaction score
976
Location
Germany
Rating - 0%
0   0   0
Another method might be to simply insert an unused pin as the first Jack-pin. That way hopefully none of your light channels will encounter this specific error.

You can also try my modified ESP32-code, it catches a few more Null-pointer exceptions and enables the usage of a flowmeter, multiple temperature sensors and I2C sensors, specifically only Robo-Tank pH and any Atlas Scientific sensors at the moment.
 
OP
OP
S

SHARK_2

New Member
View Badges
Joined
Dec 13, 2020
Messages
14
Reaction score
10
Location
Warsaw
Rating - 0%
0   0   0
Another method might be to simply insert an unused pin as the first Jack-pin. That way hopefully none of your light channels will encounter this specific error.

You can also try my modified ESP32-code, it catches a few more Null-pointer exceptions and enables the usage of a flowmeter, multiple temperature sensors and I2C sensors, specifically only Robo-Tank pH and any Atlas Scientific sensors at the moment.
I will give it a try later.

For now I can tell that this is not only PIN "0" problem:
Code:
10:19:28.136 -> PWM Pin27 DutyCycle:82
10:19:28.136 -> PWM Pin26 DutyCycle:19
10:19:28.136 -> PWM Pin25 DutyCycle:69
10:19:28.136 -> PWM Pin33 DutyCycle:69
10:19:28.136 -> PWM Pin17 DutyCycle:92
10:19:28.136 -> PWM Pin16 DutyCycle:69
10:19:28.136 -> PWM Pin18 DutyCycle:86
10:19:28.136 -> PWM Pin19 DutyCycle:86
10:19:48.168 -> PWM Pin27 DutyCycle:82
10:19:48.168 -> PWM Pin26 DutyCycle:19
10:19:48.168 -> PWM Pin25 DutyCycle:69
10:19:48.168 -> PWM Pin33 DutyCycle:69
10:19:48.168 -> PWM Pin17 DutyCycle:93
10:19:48.168 -> PWM Pin16 DutyCycle:0
10:19:48.168 -> PWM Pin19 DutyCycle:87
10:19:48.168 -> PWM Pin18 DutyCycle:87
10:19:48.168 -> Value:26.50
10:20:28.234 -> PWM Pin27 DutyCycle:82
10:20:28.234 -> PWM Pin26 DutyCycle:20
10:20:28.234 -> PWM Pin25 DutyCycle:70
10:20:28.234 -> PWM Pin33 DutyCycle:70
10:20:28.234 -> PWM Pin17 DutyCycle:93
10:20:28.234 -> PWM Pin16 DutyCycle:70
10:20:28.234 -> PWM Pin18 DutyCycle:87
10:20:28.234 -> PWM Pin19 DutyCycle:87
10:20:48.267 -> PWM Pin27 DutyCycle:83
10:20:48.267 -> PWM Pin26 DutyCycle:20
10:20:48.267 -> PWM Pin16 DutyCycle:70
10:20:48.267 -> PWM Pin17 DutyCycle:93
10:20:48.267 -> PWM Pin25 DutyCycle:70
10:20:48.267 -> PWM Pin33 DutyCycle:70
10:20:48.267 -> PWM Pin19 DutyCycle:87
10:20:48.267 -> PWM Pin18 DutyCycle:87
10:20:48.267 -> Value:26.50
10:21:28.332 -> PWM Pin27 DutyCycle:83
10:21:28.332 -> PWM Pin26 DutyCycle:21
10:21:28.332 -> PWM Pin25 DutyCycle:70
10:21:28.332 -> PWM Pin33 DutyCycle:70
10:21:28.332 -> PWM Pin17 DutyCycle:94
10:21:28.332 -> PWM Pin16 DutyCycle:70
10:21:28.332 -> PWM Pin18 DutyCycle:87
10:21:28.332 -> PWM Pin19 DutyCycle:87
10:21:48.365 -> PWM Pin27 DutyCycle:83
10:21:48.365 -> PWM Pin26 DutyCycle:21
10:21:48.365 -> PWM Pin25 DutyCycle:70
10:21:48.365 -> PWM Pin33 DutyCycle:70
10:21:48.365 -> PWM Pin17 DutyCycle:94
10:21:48.365 -> PWM Pin16 DutyCycle:70
10:21:48.365 -> PWM Pin18 DutyCycle:87
10:21:48.365 -> PWM Pin19 DutyCycle:87
10:21:48.365 -> Value:26.50
10:22:28.431 -> PWM Pin27 DutyCycle:83
10:22:28.431 -> PWM Pin26 DutyCycle:22
10:22:28.431 -> PWM Pin25 DutyCycle:71
10:22:28.431 -> PWM Pin33 DutyCycle:71
10:22:28.431 -> PWM Pin17 DutyCycle:94
10:22:28.431 -> PWM Pin16 DutyCycle:71
10:22:28.431 -> PWM Pin19 DutyCycle:87
10:22:28.431 -> PWM Pin18 DutyCycle:87
10:22:48.463 -> PWM Pin27 DutyCycle:84
10:22:48.463 -> PWM Pin26 DutyCycle:22
10:22:48.463 -> PWM Pin25 DutyCycle:71
10:22:48.463 -> PWM Pin33 DutyCycle:71
10:22:48.463 -> PWM Pin17 DutyCycle:95
10:22:48.463 -> PWM Pin16 DutyCycle:71
10:22:48.463 -> PWM Pin18 DutyCycle:87
10:22:48.463 -> Value:26.50
10:22:48.463 -> PWM Pin19 DutyCycle:87
10:23:28.529 -> PWM Pin27 DutyCycle:84
10:23:28.529 -> PWM Pin27 DutyCycle:0
10:23:28.529 -> PWM Pin25 DutyCycle:71
10:23:28.529 -> PWM Pin33 DutyCycle:71
10:23:28.529 -> PWM Pin17 DutyCycle:95
10:23:28.529 -> PWM Pin16 DutyCycle:71
10:23:28.529 -> PWM Pin18 DutyCycle:87
10:23:28.529 -> PWM Pin19 DutyCycle:87
10:23:48.561 -> PWM Pin27 DutyCycle:84
10:23:48.561 -> PWM Pin26 DutyCycle:23
10:23:48.561 -> PWM Pin25 DutyCycle:71
10:23:48.561 -> PWM Pin33 DutyCycle:71
10:23:48.561 -> PWM Pin17 DutyCycle:95
10:23:48.561 -> PWM Pin16 DutyCycle:71
10:23:48.561 -> PWM Pin19 DutyCycle:87
10:23:48.561 -> Value:26.50
10:23:48.561 -> PWM Pin18 DutyCycle:87
10:24:28.627 -> PWM Pin27 DutyCycle:84
10:24:28.627 -> PWM Pin26 DutyCycle:24
10:24:28.627 -> PWM Pin25 DutyCycle:72
10:24:28.627 -> PWM Pin33 DutyCycle:72
10:24:28.627 -> PWM Pin17 DutyCycle:96
10:24:28.627 -> PWM Pin16 DutyCycle:72
10:24:28.627 -> PWM Pin19 DutyCycle:87
10:24:28.627 -> PWM Pin18 DutyCycle:87
10:24:48.660 -> PWM Pin27 DutyCycle:85
10:24:48.660 -> PWM Pin26 DutyCycle:24
10:24:48.660 -> PWM Pin25 DutyCycle:72
10:24:48.660 -> PWM Pin33 DutyCycle:72
10:24:48.660 -> PWM Pin17 DutyCycle:96
10:24:48.660 -> PWM Pin16 DutyCycle:72
10:24:48.660 -> PWM Pin18 DutyCycle:87
10:24:48.660 -> Value:26.50
10:24:48.660 -> PWM Pin19 DutyCycle:87
10:25:28.725 -> PWM Pin27 DutyCycle:85
10:25:28.725 -> PWM Pin26 DutyCycle:25
10:25:28.725 -> PWM Pin25 DutyCycle:72
10:25:28.725 -> PWM Pin27 DutyCycle:0
10:25:28.725 -> PWM Pin17 DutyCycle:96
10:25:28.725 -> PWM Pin16 DutyCycle:72
10:25:28.725 -> PWM Pin19 DutyCycle:87
10:25:28.725 -> PWM Pin18 DutyCycle:87
10:25:48.758 -> PWM Pin27 DutyCycle:85
10:25:48.758 -> PWM Pin25 DutyCycle:72
10:25:48.758 -> PWM Pin26 DutyCycle:25
10:25:48.758 -> PWM Pin33 DutyCycle:72
10:25:48.758 -> PWM Pin17 DutyCycle:97
10:25:48.758 -> PWM Pin16 DutyCycle:72
10:25:48.758 -> Value:26.50
10:25:48.758 -> PWM Pin18 DutyCycle:88
10:25:48.758 -> PWM Pin19 DutyCycle:88
10:26:28.824 -> PWM Pin27 DutyCycle:85
10:26:28.824 -> PWM Pin26 DutyCycle:26
10:26:28.824 -> PWM Pin25 DutyCycle:73
10:26:28.824 -> PWM Pin33 DutyCycle:73
10:26:28.824 -> PWM Pin16 DutyCycle:73
10:26:28.824 -> PWM Pin17 DutyCycle:97
10:26:28.824 -> PWM Pin18 DutyCycle:88
10:26:28.824 -> PWM Pin19 DutyCycle:88
10:26:48.856 -> PWM Pin27 DutyCycle:86
10:26:48.856 -> PWM Pin26 DutyCycle:26
10:26:48.856 -> PWM Pin25 DutyCycle:73
10:26:48.856 -> PWM Pin33 DutyCycle:73
10:26:48.856 -> PWM Pin17 DutyCycle:98
10:26:48.856 -> PWM Pin16 DutyCycle:73
10:26:48.856 -> PWM Pin18 DutyCycle:88
10:26:48.856 -> Value:26.50
10:26:48.856 -> PWM Pin19 DutyCycle:88
10:27:19.377 -> PWM Pin27 DutyCycle:86
10:27:19.377 -> PWM Pin26 DutyCycle:27
10:27:19.377 -> PWM Pin25 DutyCycle:73
10:27:19.377 -> PWM Pin17 DutyCycle:98
10:27:19.377 -> PWM Pin33 DutyCycle:73
10:27:19.377 -> PWM Pin16 DutyCycle:73
10:27:19.377 -> PWM Pin19 DutyCycle:88
10:27:19.377 -> PWM Pin18 DutyCycle:88
10:27:42.813 -> PWM Pin27 DutyCycle:86
10:27:42.846 -> PWM Pin26 DutyCycle:27
10:27:42.846 -> PWM Pin25 DutyCycle:74
10:27:42.846 -> PWM Pin33 DutyCycle:74
10:27:42.846 -> PWM Pin17 DutyCycle:98
10:27:42.846 -> PWM Pin16 DutyCycle:74
10:27:42.880 -> PWM Pin18 DutyCycle:88
10:27:42.880 -> PWM Pin19 DutyCycle:88
10:27:43.563 -> Value:26.50
10:28:12.593 -> PWM Pin27 DutyCycle:86
10:28:12.929 -> PWM Pin26 DutyCycle:28
10:28:12.929 -> PWM Pin27 DutyCycle:0
10:28:12.929 -> PWM Pin25 DutyCycle:74
10:28:12.929 -> PWM Pin16 DutyCycle:74
10:28:12.963 -> PWM Pin17 DutyCycle:99
10:28:14.139 -> PWM Pin18 DutyCycle:88
10:28:14.139 -> PWM Pin19 DutyCycle:88
10:28:42.734 -> PWM Pin27 DutyCycle:87
10:28:43.039 -> PWM Pin26 DutyCycle:28
10:28:43.039 -> PWM Pin25 DutyCycle:74
10:28:43.039 -> PWM Pin33 DutyCycle:74
10:28:43.039 -> PWM Pin16 DutyCycle:74
10:28:43.075 -> PWM Pin17 DutyCycle:99
10:28:43.950 -> PWM Pin18 DutyCycle:0
10:28:43.950 -> PWM Pin19 DutyCycle:88
10:28:44.628 -> Value:26.56
10:29:12.533 -> PWM Pin27 DutyCycle:87
10:29:12.834 -> PWM Pin25 DutyCycle:74
10:29:12.834 -> PWM Pin26 DutyCycle:29
10:29:12.834 -> PWM Pin33 DutyCycle:74
10:29:12.834 -> PWM Pin17 DutyCycle:99
10:29:12.868 -> PWM Pin16 DutyCycle:74
10:29:12.868 -> PWM Pin18 DutyCycle:88
10:29:12.868 -> PWM Pin19 DutyCycle:88
10:29:42.641 -> PWM Pin27 DutyCycle:87
10:29:42.944 -> PWM Pin26 DutyCycle:29
10:29:42.944 -> PWM Pin25 DutyCycle:75
10:29:42.944 -> PWM Pin33 DutyCycle:75
10:29:42.944 -> PWM Pin17 DutyCycle:100
10:29:42.944 -> PWM Pin16 DutyCycle:75
10:29:44.157 -> PWM Pin18 DutyCycle:0
10:29:44.865 -> Value:26.56
10:29:44.865 -> PWM Pin19 DutyCycle:88
10:30:12.720 -> PWM Pin27 DutyCycle:88
10:30:13.058 -> PWM Pin26 DutyCycle:30
10:30:13.058 -> PWM Pin25 DutyCycle:75
10:30:13.058 -> PWM Pin17 DutyCycle:100
10:30:13.058 -> PWM Pin33 DutyCycle:75
10:30:13.058 -> PWM Pin16 DutyCycle:0
10:30:13.966 -> PWM Pin19 DutyCycle:88
10:30:13.966 -> PWM Pin18 DutyCycle:88
10:30:42.537 -> PWM Pin27 DutyCycle:88
10:30:42.839 -> PWM Pin26 DutyCycle:30
10:30:42.873 -> PWM Pin25 DutyCycle:75
10:30:42.873 -> PWM Pin33 DutyCycle:75
10:30:42.873 -> PWM Pin17 DutyCycle:100
10:30:42.873 -> PWM Pin16 DutyCycle:75
10:30:42.873 -> PWM Pin19 DutyCycle:88
 
OP
OP
S

SHARK_2

New Member
View Badges
Joined
Dec 13, 2020
Messages
14
Reaction score
10
Location
Warsaw
Rating - 0%
0   0   0
Reboots and "0-s" were very annoying so i changed esp32 code to subscribe to mqtt topics. After this change, everything works as it should, and the esp doesn't have to match the links all the time.
 

Sral

Valuable Member
View Badges
Joined
May 2, 2022
Messages
1,006
Reaction score
976
Location
Germany
Rating - 0%
0   0   0
Reboots and "0-s" were very annoying so i changed esp32 code to subscribe to mqtt topics. After this change, everything works as it should, and the esp doesn't have to match the links all the time.
Nice, can you share the code ? Would be very interesting indeed.
 
OP
OP
S

SHARK_2

New Member
View Badges
Joined
Dec 13, 2020
Messages
14
Reaction score
10
Location
Warsaw
Rating - 0%
0   0   0
Yes sure. This code probably needs optimization, unfortunately I'm not a programmer, and this is my combination of various tutorials found on the net like:
https://randomnerdtutorials.com/esp32-mqtt-publish-ds18b20-temperature-arduino/
https://randomnerdtutorials.com/esp32-ota-over-the-air-arduino/
https://randomnerdtutorials.com/esp32-webserial-library/

Code:
#include <WiFi.h>
#include <AsyncMqttClient.h>
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <AsyncElegantOTA.h>
#include <TokenIterator.h>
#include <UrlTokenBindings.h>
#include <NTPClient.h>
#include <OneWire.h>
#include <DallasTemperature.h>

/*
 * Hostname
 */
String hostname = "Lamp1";

/*
 * WiFi
 */
#define WIFI_SSID "YOUR_WIFI_SSID"
#define WIFI_PASSWORD "YOUR_WIFI_PASSWORD"

/*
 * MQTT Brocker
 */
#define MQTT_HOST "YOUR_MQTT_BROCKER_DOMAIN"
#define MQTT_PORT 1883

/*
 * AsyncWebServer
 */
AsyncWebServer server(80);

/*
 * Time
 */
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "time.nask.pl", 3600, 60000);

/*
 * Temperature
 */
const int oneWireBus = 4;      
OneWire oneWire(oneWireBus);
DallasTemperature sensors(&oneWire);

/*
 * Leds & PWM
 */
#define JACK_COUNT 8
#define PWM_FREQ 100
#define PWM_RESOLUTION 8
const int jackPins[JACK_COUNT] = { 27, 26, 25, 33, 16, 17, 18, 19 };
const int pwmChannels[JACK_COUNT] = { 0, 1, 2, 3, 4, 5, 6, 7 };
#define ANALOG_INPUT_COUNT 0
int led_power[101];
int ch1State = 0;
int ch2State = 0;
int ch3State = 0;
int ch4State = 0;
int ch5State = 0;
int ch6State = 0;
int fan1State = 0;
int fan2State = 0;

/*
 * MQTT
 */
AsyncMqttClient mqttClient;
TimerHandle_t mqttReconnectTimer;
TimerHandle_t wifiReconnectTimer;

void connectToMqtt() {
  Serial.println("Connecting to MQTT...");
  mqttClient.connect();
}

void onMqttConnect(bool sessionPresent) {
  Serial.println("Connected to MQTT.");
  Serial.print("Session present: ");
  Serial.println(sessionPresent);
  uint16_t packetIdSub1 = mqttClient.subscribe("reef-pi/l1_ch1", 1);
  uint16_t packetIdSub2 = mqttClient.subscribe("reef-pi/l1_ch2", 1);
  uint16_t packetIdSub3 = mqttClient.subscribe("reef-pi/l1_ch3", 1);
  uint16_t packetIdSub4 = mqttClient.subscribe("reef-pi/l1_ch4", 1);
  uint16_t packetIdSub5 = mqttClient.subscribe("reef-pi/l1_ch5", 1);
  uint16_t packetIdSub6 = mqttClient.subscribe("reef-pi/l1_ch6", 1);
  uint16_t packetIdSub7 = mqttClient.subscribe("reef-pi/l1_fan1", 1);
  uint16_t packetIdSub8 = mqttClient.subscribe("reef-pi/l1_fan2", 1);
}

void onMqttDisconnect(AsyncMqttClientDisconnectReason reason) {
  if (WiFi.isConnected()) {
    xTimerStart(mqttReconnectTimer, 0);
  }
}

void onMqttSubscribe(uint16_t packetId, uint8_t qos) {
  Serial.println("Subscribe acknowledged.");
}

void onMqttUnsubscribe(uint16_t packetId) {
  Serial.println("Unsubscribe acknowledged.");
}

void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) {
  if (String(topic) == "reef-pi/l1_ch1") {
    const int value = atoi(payload);
    if (value >= 0 && value < (sizeof(led_power) / sizeof(int))) {
      ch1State = led_power[value];
      ledcWrite(0, ch1State);
    }
  }
  if (String(topic) == "reef-pi/l1_ch2") {
    const int value = atoi(payload);
    if (value >= 0 && value < (sizeof(led_power) / sizeof(int))) {
      ch2State = led_power[value];
      ledcWrite(1, ch2State);
    }
  }
  if (String(topic) == "reef-pi/l1_ch3") {
    const int value = atoi(payload);
    if (value >= 0 && value < (sizeof(led_power) / sizeof(int))) {
      ch3State = led_power[value];
      ledcWrite(2, ch3State);
    }
  }
  if (String(topic) == "reef-pi/l1_ch4") {
    const int value = atoi(payload);
    if (value >= 0 && value < (sizeof(led_power) / sizeof(int))) {
      ch4State = led_power[value];
      ledcWrite(3, ch4State);
    }
  }
  if (String(topic) == "reef-pi/l1_ch5") {
    const int value = atoi(payload);
    if (value >= 0 && value < (sizeof(led_power) / sizeof(int))) {
      ch5State = led_power[value];
      ledcWrite(4, ch5State);
    }
  }
  if (String(topic) == "reef-pi/l1_ch6") {
    const int value = atoi(payload);
    if (value >= 0 && value < (sizeof(led_power) / sizeof(int))) {
      ch6State = led_power[value];
      ledcWrite(5, ch6State);
    }
  }
  if (String(topic) == "reef-pi/l1_fan1") {
    const int value = atoi(payload);
    if (value >= 0 && value < (sizeof(led_power) / sizeof(int))) {
      fan1State = led_power[value];
      ledcWrite(6, fan1State);
    }
  }
  if (String(topic) == "reef-pi/l1_fan2") {
    const int value = atoi(payload);
    if (value >= 0 && value < (sizeof(led_power) / sizeof(int))) {
      fan2State = led_power[value];
      ledcWrite(7, fan2State);
    }
  }
}

void onMqttPublish(uint16_t packetId) {
}

/*
 * WiFi Connection & Events
 */
void connectToWifi() {
  WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE, INADDR_NONE);
  WiFi.setHostname(hostname.c_str()); //define hostname
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  while (WiFi.status() != WL_CONNECTED) {
    Serial.println("Connecting to WiFi..");
  }
}

void WiFiEvent(WiFiEvent_t event) {
  switch(event) {
    case SYSTEM_EVENT_STA_GOT_IP:
      delay(5000);
      connectToMqtt();
      break;
    case SYSTEM_EVENT_STA_DISCONNECTED:
      xTimerStop(mqttReconnectTimer, 0); // ensure we don't reconnect to MQTT while reconnecting to Wi-Fi
      xTimerStart(wifiReconnectTimer, 0);
      break;
  }
}

/*
 * Setup
 */
void setup(void) {
  Serial.begin(115200);

  mqttReconnectTimer = xTimerCreate("mqttTimer", pdMS_TO_TICKS(5000), pdFALSE, (void*)0, reinterpret_cast<TimerCallbackFunction_t>(connectToMqtt));
  wifiReconnectTimer = xTimerCreate("wifiTimer", pdMS_TO_TICKS(5000), pdFALSE, (void*)0, reinterpret_cast<TimerCallbackFunction_t>(connectToWifi));

  WiFi.onEvent(WiFiEvent);

  // MQTT
  mqttClient.onConnect(onMqttConnect);
  mqttClient.onDisconnect(onMqttDisconnect);
  mqttClient.onSubscribe(onMqttSubscribe);
  mqttClient.onUnsubscribe(onMqttUnsubscribe);
  mqttClient.onMessage(onMqttMessage);
  mqttClient.onPublish(onMqttPublish);
  mqttClient.setServer(MQTT_HOST, MQTT_PORT);
  mqttClient.setCredentials("mqtt_user", "mqtt_password");

  connectToWifi();

  server.on("/analog_inputs/*", HTTP_GET, readAnalogInput);
  server.on("/restart", HTTP_GET, [] (AsyncWebServerRequest *request) {
    ESP.restart();
  });

  sensors.begin();

  for (int i = 0; i < JACK_COUNT; i++) {
    ledcSetup(pwmChannels[i], PWM_FREQ, PWM_RESOLUTION);
    ledcAttachPin(jackPins[i], pwmChannels[i]);
  }

  timeClient.begin();
  timeClient.setTimeOffset(2 * 3600); // add 3600s to NTP time
  while (!timeClient.update()) {
    timeClient.forceUpdate();
  }

  AsyncElegantOTA.begin(&server);

  for (int j = 0; j < 101; j++) {
    led_power[j] = map(j, 0, 100, 0, 255);
    Serial.println(led_power[j]);
  }
 
  server.begin();
}

/*
 * Loop
 */
void loop(void) {
}

UrlTokenBindings parseURL(AsyncWebServerRequest *request, char templatePath[]) {
  char urlBuffer[30];
  request->url().toCharArray(urlBuffer, 30);
  int urlLength = request->url().length();
  TokenIterator templateIterator(templatePath, strlen(templatePath), '/');
  TokenIterator pathIterator(urlBuffer, urlLength, '/');
  UrlTokenBindings bindings(templateIterator, pathIterator);
  return bindings;
}

void readAnalogInput(AsyncWebServerRequest *request) {
  char path[] = "/analog_inputs/:id";
  UrlTokenBindings bindings = parseURL(request, path);
  int id = String(bindings.get("id")).toInt();
  if (id < 0 || id > ANALOG_INPUT_COUNT) {
    request->send(409, "text/plain", "invalid analog input pin id");
    return;
  }
  float value;
  if (id == 0) {
    sensors.requestTemperatures();
    value = sensors.getTempCByIndex(0);
  }
  Serial.print("Temperature: ");
  Serial.print(value);
  Serial.println(" °C");
  request->send(200, "text/plain", String(value));
}

I am planning to add an option to save the "schedule" on flash, so that the lamp will work, even if it will not be connected to the Internet (but stilo connected to wifi) or will not be able to access the mqtt broker.
 
Last edited:

Sral

Valuable Member
View Badges
Joined
May 2, 2022
Messages
1,006
Reaction score
976
Location
Germany
Rating - 0%
0   0   0
Yes sure. This code probably needs optimization, unfortunately I'm not a programmer, and this is my combination of various tutorials found on the net like:
https://randomnerdtutorials.com/esp32-mqtt-publish-ds18b20-temperature-arduino/
https://randomnerdtutorials.com/esp32-ota-over-the-air-arduino/
https://randomnerdtutorials.com/esp32-webserial-library/

Code:
#include <WiFi.h>
#include <AsyncMqttClient.h>
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <AsyncElegantOTA.h>
#include <TokenIterator.h>
#include <UrlTokenBindings.h>
#include <NTPClient.h>
#include <OneWire.h>
#include <DallasTemperature.h>

/*
 * Hostname
 */
String hostname = "Lamp1";

/*
 * WiFi
 */
#define WIFI_SSID "YOUR_WIFI_SSID"
#define WIFI_PASSWORD "YOUR_WIFI_PASSWORD"

/*
 * MQTT Brocker
 */
#define MQTT_HOST "YOUR_MQTT_BROCKER_DOMAIN"
#define MQTT_PORT 1883

/*
 * AsyncWebServer
 */
AsyncWebServer server(80);

/*
 * Time
 */
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "time.nask.pl", 3600, 60000);

/*
 * Temperature
 */
const int oneWireBus = 4;     
OneWire oneWire(oneWireBus);
DallasTemperature sensors(&oneWire);

/*
 * Leds & PWM
 */
#define JACK_COUNT 8
#define PWM_FREQ 100
#define PWM_RESOLUTION 8
const int jackPins[JACK_COUNT] = { 27, 26, 25, 33, 16, 17, 18, 19 };
const int pwmChannels[JACK_COUNT] = { 0, 1, 2, 3, 4, 5, 6, 7 };
#define ANALOG_INPUT_COUNT 0
int led_power[101];
int ch1State = 0;
int ch2State = 0;
int ch3State = 0;
int ch4State = 0;
int ch5State = 0;
int ch6State = 0;
int fan1State = 0;
int fan2State = 0;

/*
 * MQTT
 */
AsyncMqttClient mqttClient;
TimerHandle_t mqttReconnectTimer;
TimerHandle_t wifiReconnectTimer;

void connectToMqtt() {
  Serial.println("Connecting to MQTT...");
  mqttClient.connect();
}

void onMqttConnect(bool sessionPresent) {
  Serial.println("Connected to MQTT.");
  Serial.print("Session present: ");
  Serial.println(sessionPresent);
  uint16_t packetIdSub1 = mqttClient.subscribe("reef-pi/l1_ch1", 1);
  uint16_t packetIdSub2 = mqttClient.subscribe("reef-pi/l1_ch2", 1);
  uint16_t packetIdSub3 = mqttClient.subscribe("reef-pi/l1_ch3", 1);
  uint16_t packetIdSub4 = mqttClient.subscribe("reef-pi/l1_ch4", 1);
  uint16_t packetIdSub5 = mqttClient.subscribe("reef-pi/l1_ch5", 1);
  uint16_t packetIdSub6 = mqttClient.subscribe("reef-pi/l1_ch6", 1);
  uint16_t packetIdSub7 = mqttClient.subscribe("reef-pi/l1_fan1", 1);
  uint16_t packetIdSub8 = mqttClient.subscribe("reef-pi/l1_fan2", 1);
}

void onMqttDisconnect(AsyncMqttClientDisconnectReason reason) {
  if (WiFi.isConnected()) {
    xTimerStart(mqttReconnectTimer, 0);
  }
}

void onMqttSubscribe(uint16_t packetId, uint8_t qos) {
  Serial.println("Subscribe acknowledged.");
}

void onMqttUnsubscribe(uint16_t packetId) {
  Serial.println("Unsubscribe acknowledged.");
}

void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) {
  if (String(topic) == "reef-pi/l1_ch1") {
    const int value = atoi(payload);
    if (value >= 0 && value < (sizeof(led_power) / sizeof(int))) {
      ch1State = led_power[value];
      ledcWrite(0, ch1State);
    }
  }
  if (String(topic) == "reef-pi/l1_ch2") {
    const int value = atoi(payload);
    if (value >= 0 && value < (sizeof(led_power) / sizeof(int))) {
      ch2State = led_power[value];
      ledcWrite(1, ch2State);
    }
  }
  if (String(topic) == "reef-pi/l1_ch3") {
    const int value = atoi(payload);
    if (value >= 0 && value < (sizeof(led_power) / sizeof(int))) {
      ch3State = led_power[value];
      ledcWrite(2, ch3State);
    }
  }
  if (String(topic) == "reef-pi/l1_ch4") {
    const int value = atoi(payload);
    if (value >= 0 && value < (sizeof(led_power) / sizeof(int))) {
      ch4State = led_power[value];
      ledcWrite(3, ch4State);
    }
  }
  if (String(topic) == "reef-pi/l1_ch5") {
    const int value = atoi(payload);
    if (value >= 0 && value < (sizeof(led_power) / sizeof(int))) {
      ch5State = led_power[value];
      ledcWrite(4, ch5State);
    }
  }
  if (String(topic) == "reef-pi/l1_ch6") {
    const int value = atoi(payload);
    if (value >= 0 && value < (sizeof(led_power) / sizeof(int))) {
      ch6State = led_power[value];
      ledcWrite(5, ch6State);
    }
  }
  if (String(topic) == "reef-pi/l1_fan1") {
    const int value = atoi(payload);
    if (value >= 0 && value < (sizeof(led_power) / sizeof(int))) {
      fan1State = led_power[value];
      ledcWrite(6, fan1State);
    }
  }
  if (String(topic) == "reef-pi/l1_fan2") {
    const int value = atoi(payload);
    if (value >= 0 && value < (sizeof(led_power) / sizeof(int))) {
      fan2State = led_power[value];
      ledcWrite(7, fan2State);
    }
  }
}

void onMqttPublish(uint16_t packetId) {
}

/*
 * WiFi Connection & Events
 */
void connectToWifi() {
  WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE, INADDR_NONE);
  WiFi.setHostname(hostname.c_str()); //define hostname
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  while (WiFi.status() != WL_CONNECTED) {
    Serial.println("Connecting to WiFi..");
  }
}

void WiFiEvent(WiFiEvent_t event) {
  switch(event) {
    case SYSTEM_EVENT_STA_GOT_IP:
      delay(5000);
      connectToMqtt();
      break;
    case SYSTEM_EVENT_STA_DISCONNECTED:
      xTimerStop(mqttReconnectTimer, 0); // ensure we don't reconnect to MQTT while reconnecting to Wi-Fi
      xTimerStart(wifiReconnectTimer, 0);
      break;
  }
}

/*
 * Setup
 */
void setup(void) {
  Serial.begin(115200);

  mqttReconnectTimer = xTimerCreate("mqttTimer", pdMS_TO_TICKS(5000), pdFALSE, (void*)0, reinterpret_cast<TimerCallbackFunction_t>(connectToMqtt));
  wifiReconnectTimer = xTimerCreate("wifiTimer", pdMS_TO_TICKS(5000), pdFALSE, (void*)0, reinterpret_cast<TimerCallbackFunction_t>(connectToWifi));

  WiFi.onEvent(WiFiEvent);

  // MQTT
  mqttClient.onConnect(onMqttConnect);
  mqttClient.onDisconnect(onMqttDisconnect);
  mqttClient.onSubscribe(onMqttSubscribe);
  mqttClient.onUnsubscribe(onMqttUnsubscribe);
  mqttClient.onMessage(onMqttMessage);
  mqttClient.onPublish(onMqttPublish);
  mqttClient.setServer(MQTT_HOST, MQTT_PORT);
  mqttClient.setCredentials("mqtt_user", "mqtt_password");

  connectToWifi();

  server.on("/analog_inputs/*", HTTP_GET, readAnalogInput);
  server.on("/restart", HTTP_GET, [] (AsyncWebServerRequest *request) {
    ESP.restart();
  });

  sensors.begin();

  for (int i = 0; i < JACK_COUNT; i++) {
    ledcSetup(pwmChannels[i], PWM_FREQ, PWM_RESOLUTION);
    ledcAttachPin(jackPins[i], pwmChannels[i]);
  }

  timeClient.begin();
  timeClient.setTimeOffset(2 * 3600); // add 3600s to NTP time
  while (!timeClient.update()) {
    timeClient.forceUpdate();
  }

  AsyncElegantOTA.begin(&server);

  for (int j = 0; j < 101; j++) {
    led_power[j] = map(j, 0, 100, 0, 255);
    Serial.println(led_power[j]);
  }
 
  server.begin();
}

/*
 * Loop
 */
void loop(void) {
}

UrlTokenBindings parseURL(AsyncWebServerRequest *request, char templatePath[]) {
  char urlBuffer[30];
  request->url().toCharArray(urlBuffer, 30);
  int urlLength = request->url().length();
  TokenIterator templateIterator(templatePath, strlen(templatePath), '/');
  TokenIterator pathIterator(urlBuffer, urlLength, '/');
  UrlTokenBindings bindings(templateIterator, pathIterator);
  return bindings;
}

void readAnalogInput(AsyncWebServerRequest *request) {
  char path[] = "/analog_inputs/:id";
  UrlTokenBindings bindings = parseURL(request, path);
  int id = String(bindings.get("id")).toInt();
  if (id < 0 || id > ANALOG_INPUT_COUNT) {
    request->send(409, "text/plain", "invalid analog input pin id");
    return;
  }
  float value;
  if (id == 0) {
    sensors.requestTemperatures();
    value = sensors.getTempCByIndex(0);
  }
  Serial.print("Temperature: ");
  Serial.print(value);
  Serial.println(" °C");
  request->send(200, "text/plain", String(value));
}

I am planning to add an option to save the "schedule" on flash, so that the lamp will work, even if it will not be connected to the Internet (but stilo connected to wifi) or will not be able to access the mqtt broker.
Very nice, maybe that’s a better version than handling the URLs.

Did the hostname setting work for you ? I had trouble getting that to work and my ESP32 never showed up with that name in my ping commands.
 
OP
OP
S

SHARK_2

New Member
View Badges
Joined
Dec 13, 2020
Messages
14
Reaction score
10
Location
Warsaw
Rating - 0%
0   0   0
No
Very nice, maybe that’s a better version than handling the URLs.

Did the hostname setting work for you ? I had trouble getting that to work and my ESP32 never showed up with that name in my ping commands.
Thx. About hostname - it doesn't work.
 

Reefing threads: Do you wear gear from reef brands?

  • I wear reef gear everywhere.

    Votes: 24 14.6%
  • I wear reef gear primarily at fish events and my LFS.

    Votes: 11 6.7%
  • I wear reef gear primarily for water changes and tank maintenance.

    Votes: 1 0.6%
  • I wear reef gear primarily to relax where I live.

    Votes: 23 14.0%
  • I don’t wear gear from reef brands.

    Votes: 94 57.3%
  • Other.

    Votes: 11 6.7%
Back
Top