One of the first IoT use cases that comes in ones mind ist measuring the temperature of your environment. An easy and cost-efficient way is to use an 1-Wire bus sensor like the DS18S20. You can get this sensor for around 2,15€ and it is really easy to implement in your µC as there are a bunch of libraries for the 1-Wire protocol. So the first thing to do is put the sensor in a bread board and connect it to your WiPy2 breakout board. The pin assignment is simple. Connect Pin1 of the sensor to GND, Pin2 to G10 and Pin3 to 3.3V. That' s it.
Now comes the tougher part. There is no 1-wire library included in the official firmware so I had to go and find a library that was fitting which took me some time. After a while I found out that there exists an 1-Wire example on the official Pycom Documentation for ESP32 based Boards. It is a bit confusing that there are actually two online documentations supplied from Pycom concerning the WiPy2. One is the WiPy2 Micropython documenation which holds some examples about basic functions like WiFi connection, board control, I2C, SPI and IO. The other one is the Pycom documentation for ESP32 boards which holds some more advanced tutorials like SSL, 1-Wire, MQTT.
The 1-Wire driver tutorial contains the code of a library file that we can copy and save on our WiPy2 as /lib/onewire.py
. Now we can read the temperature by some simple code lines:
import time from machine import Pin from onewire import DS18X20, OneWire #DS18B20 data line connected to pin G10 ow = OneWire(Pin('G10')) temp = DS18X20(ow) while True: val = float(temp.read_temp_async() / 100.0 print('T = {:.2f}C'.format(val)) time.sleep(1) temp.start_convertion() time.sleep(1)