Hello everyone, I’m making a project about automatic irrigation system, and I’m making use of the tutorials in ds18b20, adc inputs on the gpio pins, aon outputs on gpio pins, and I haven’t really make a progress on transferring this onto a web server, I can’t make the web server read multiple ds18b20 sensor, multiple humidity sensors (they work like an Adc would work) and multiple outputs, maybe someone who already made multiple ds18b20 sensors work on a web server could help me.
Hi Fernando.
Can you provide more details? What’s exactly your issue?
What errors do you get? You need to provide more details so that I can help you.
Regards,
Sara
Hello Sara, the problem that I seem to have is that I don’t know how to use multiple ds18b20 sensors on a web server, the tutorial that you posted on the RNT page was very helpful but it only works for one ds18b20 I get all the readings on the terminal but trying to post them on the web page seems to be the problem.
Thanks
Have you checked this tutorial: https://randomnerdtutorials.com/esp32-plot-readings-charts-multiple/ ?
I haven’t read it yet but it seems to use Arduino IDE and arduino related things, which I’m not allowed to use because it’s a school related project, I’m currently using this tutorial as an example: https://randomnerdtutorials.com/micropython-ds18b20-esp32-esp8266/
And I can’t make the web server work for multiple sensors.
Hi.
The first thing you need to do is to be able to get data from multiple sensors and save each reading on a different variable.
Can you do that? Check this discussion.https://rntlab.com/question/multi-ds18x20/
Then, you need to edit the HTML to be able to display more sensor readings.
For example, you would need to multiply the following lines:
<p><i class="fas fa-thermometer-half" style="color:#059e8a;"></i>
<span class="ds-labels">Temperature</span>
<span id="temperature">""" + str(temp) + """</span>
<sup class="units">°C</sup>
</p>
<p><i class="fas fa-thermometer-half" style="color:#059e8a;"></i>
<span class="ds-labels">Temperature</span>
<span id="temperature">""" + str(round(temp * (9/5) + 32.0, 2)) + """</span>
<sup class="units">°F</sup>
</p>
Make sure you use a different variable name for each sensor value (highlighted in bold in previous lines) and that you use a different id for each reading.
I hope this helps.
Regards,
Sara
Hello Sara, i’ve been trying to make the changes that you recomended and i’m getting some errors maybe you could look at the code and see what’s wrong with my code:
boot.py
try:
import usocket as socket
except:
import socket
from time import sleep
from machine import Pin
import onewire, ds18x20
import network
import esp
esp.osdebug(None)
import gc
gc.collect()
ds_pin = Pin(4)
ds_sensor = ds18x20.DS18X20(onewire.OneWire(ds_pin))
ssid = 'Prueba'
password = '123456789'
station = network.WLAN(network.STA_IF)
station.active(True)
station.connect(ssid, password)
while station.isconnected() == False:
pass
print('Connection successful')
print(station.ifconfig())
main.py
def read_ds_sensor():
roms = ds_sensor.scan()
print('Found DS devices: ', roms)
print('Temperatures: ')
temperatures = []
ds_sensor.convert_temp()
time.sleep_ms(750)
for rom in roms:
print(rom)
print(ds_sensor.read_temp(rom))
temperatures.append(ds_sensor.read_temp(rom))
time.sleep(5)
temperature1 = temperatures[0]
temperature2 = temperatures[1]
temperature3 = temperatures[2]
temperature4 = temperatures[3]
return b'0.0'
def web_page():
temp = read_ds_sensor()
html = """
html { font-family: Arial; display: inline-block; margin: 0px auto; text-align: center; }
h2 { font-size: 3.0rem; } p { font-size: 3.0rem; } .units { font-size: 1.2rem; }
.ds-labels{ font-size: 1.5rem; vertical-align:middle; padding-bottom: 15px; }
Sensores de temperatura
Temperatura
""" + str(temperature1) + """
°C
Temperatura
""" + str(temperature2) + """
°C
Temperatura
""" + str(temperature3) + """
°C
Temperatura
""" + str(temperature4) + """
°C
"""
return html
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('', 80))
s.listen(5)
while True:
try:
if gc.mem_free() < 102000:
gc.collect()
conn, addr = s.accept()
conn.settimeout(3.0)
print('Got a connection from %s' % str(addr))
request = conn.recv(1024)
conn.settimeout(None)
request = str(request)
print('Content = %s' % request)
response = web_page()
conn.send('HTTP/1.1 200 OK\n')
conn.send('Content-Type: text/html\n')
conn.send('Connection: close\n\n')
conn.sendall(response)
conn.close()
except OSError as e:
conn.close()
print('Connection closed')
Thanks Sara.
Hello sara, im getting these errors:
Traceback (most recent call last):
File “main.py”, line 64, in <module>
File “main.py”, line 18, in web_page
File “main.py”, line 5, in read_ds_sensor
File “ds18x20.py”, line 20, in convert_temp
File “onewire.py”, line 23, in reset
OneWireError:
Helo Sara, sorry for not answering i was working on some personal issues and wasn’t able to respond here is the code:
boot.py: https://pastebin.com/UBxxXdLJ
main.py: https://pastebin.com/KfsDkG8s
and i’m getting this error and getting the message that the site can’t be reached:
Traceback (most recent call last):
File “main.py”, line 13
IndentationError: unexpected indent
MicroPython v1.19.1 on 2022-06-18; ESP32S3 module with ESP32S3
Type “help()” for more information.
but i’m not sure if it’s the only error, i can’t move beyond this, thanks Sara.
Hi.
In micropython/python, indentation is very important.
That line 13 is not indented properly. You need to add two spaces before that line.
Learn more about indentation: https://www.w3schools.com/python/gloss_python_indentation.asp
Anotther issue… why are you using print() inside the HTML page?
<span id="temperature">""" + print(planta1_4) +"%" + """</span>
You should only use the variable value as you did for temp1 and temp2.
The function you created read_ds_sensor() doesn’t return any variables. So, there’s no benefit in calling it like this (line 72):
temp = read_ds_sensor()
It should be like this:
read_ds_sensor()
Learn more about return:
- https://realpython.com/python-return-statement/#:~:text=The%20Python%20return%20statement%20is%20a%20special%20statement%20that%20you,can%20be%20any%20Python%20object.
- https://realpython.com/python-return-statement/#implicit-return-statements
I didn’t test the code, but I could identify those issues.
I hope this helps.
Regards,
Sara
Hello Sara, i didn’t understood very well how to get the temperature value as a variable the code i made using your references doesn’t seem to be working and i don’t really get what’s wrong with it, maybe you could tell me if something is off, and thank you for the help in the return statements, indentations and the print on html i changed the things you commented:
boot.py (no changes): https://pastebin.com/UBxxXdLJ
main.py: https://pastebin.com/cm1qqziK
And the errors it get’s me are:
Traceback (most recent call last):
File “main.py”, line 253, in <module>
File “main.py”, line 135, in web_page
NameError: name ‘temp1’ isn’t defined
Thanks for the help Sara
You need to define your variables as global:
global temperature1 = temperatures[0]
global temperature2 = temperatures[1]
global temperature3 = temperatures[2]
global temperature4 = temperatures[3]
So that they can be used throughout the code and not only inside the function.
Another alternative is to modify the function to return the temperature values.
Learn more about variable scope: https://www.w3schools.com/python/python_scope.asp
I hope this helps.
Regards,
Sara
Hello Sara, i’ve been trying to modulate the programs by separating them into 3 programs and see whats working and what’s not, until now i made the humidity web server work, then i tried to recreate one of your tutorials on the micropython ebook (web server slider switch) and i can’t seem to make the 4 sliders work.
Maybe you can help me:
boot: https://pastebin.com/Mng6iQkF
main: https://pastebin.com/aYXuXu3F
Traceback (most recent call last):
File “main.py”, line 88, in <module>
File “main.py”, line 18, in web_page
TypeError: format string needs more arguments
This is the multiple ds18b20 web server:
boot: https://pastebin.com/WT4aySbP
main: https://pastebin.com/164cwX02
Traceback (most recent call last):
File “main.py”, line 73, in <module>
File “main.py”, line 25, in web_page
File “main.py”, line 8, in read_ds_sensor
File “ds18x20.py”, line 20, in convert_temp
File “onewire.py”, line 23, in reset
OneWireError:
Thanks Sara