Let’s see…when we last left this build log we were having “trouble” getting results back from the Grove CO2 sensor using a Wemos D1 as the µCU…what will happen next? Well, obviously a well written narrative is beyond my grasp so…the next step is to hook up the RX/TX pins to the logic analyzer and check out the traffic.
I set up two async channels:
and hooked stuff up using the Arduino Uno for a reference to the pin outs:
The image doesn’t show this well:
- grey (GND) to GND
- black (Channel 0) to RX (pin 0)
- brown (Channel 1) to TX (pin 1)
I expect to see the “get a CO2 sensor reading” to appear on channel 1. And results to appear on channel 0.
Here’s what I got:
A bit rough to read. The results met my expectations. I set channel 1 data to display in hex and channel 0 data to display in decimal.
Channel 1 starts off with:
As noted in the MH-Z16 data sheet (and I talked about yesterday),
this is the command to get a reading from the CO2 sensor. The traffic is on Channel 1 -> the TX pin of UART0.
There are more bytes coming from TX. But I’m looking at RX at this point since immediately after 0x79, the bytes:
looking at the data sheet for the 9 return bytes from a read gas concentration request:
high level = 2 and low level = 152. So the gas concentration = 2*256+152 = 664ppm. This is a reasonable number.
Conclusion: The hardware connection is working…YIPPEE!!! HOWEVER, the WebREPL UART object isn’t holding onto the RX data. Another indication of this is the results that show up on the WebREPL ui. After a write, garbage characters are shown.
Perhaps it is because WebREPL talks to the D1 over wireless. Perhaps it is the way the UART class implements the (lack) of buffer holding RX data…perhaps….but ultimately, the code won’t be line by line as I enter it. The interactive playset micro python enables will indeed be useful for getting out the kinks, but ultimately I’ll be using .py files.
What I’d end up doing is have the D1 boot up a main.py that has the MicroPython code in it. Adafruit has a great post on MicroPython’s use of main.py. Here’s the python script I put into a main.py file:
from machine import UART
if __name__ == ‘__main__’:
uart = UART(0,9600)
gasdata = bytearray(9)
for value in gasdata:
I copied main.py onto the D1 using WebREPL.
Here’s one of the results from the logic analyzer and on WebREPL. A YIPPEE moment! It worked…
I can read the sensors. Let’s see for the 9 return bytes highlighted, the CO2 reading = 1*256 + 225 = 481ppm…um…reads “too good to be true?”…so I say we’re reading the CO2 sensor…wait for it….YIPPEE!!!!….Now – can I interrupt the program to take a reading after some amount of time? Also – what about sending readings to Adafruit.io? And where the heck are the relays I ordered? Stay tuned…as we find out…