User Tools

Site Tools


amc2020:group_n:test

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
amc2020:group_n:test [2020/07/29 11:25] – [Back To Main Page] jonas001amc2020:group_n:test [2021/08/24 17:35] (current) – external edit 127.0.0.1
Line 178: Line 178:
  
  
-==== Back To Start of the Page ==== 
  
 +===== Links =====
  
 <html> <html>
-<span id="test">Test</span></html> +  <left> 
-<html><p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptuaAt vero eos et accusam et justo duo dolores et ea rebumStet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit ametLorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptuaAt vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.</p></html+    <a href="https://wiki.eolab.de/doku.php?id=amc2020:group_n:start"; onmouseover="style.color='green'";
-<html><p><a href="#test">Go here!</a></p>+      <span style="color:#2E71B8"; onmouseover="style.color='green'"; onmouseout="style.color='#2E71B8'";> 
 +        &#8617 Back to the main page 
 +      </span> 
 +    </a> 
 +  </left>
 </html> </html>
-===== DS3231 Code Explanation ===== 
  
 <html> <html>
-    <font style="color:black"+  <center
-    <ol+    <span
-        <li> +    <a href="javascript:self.scrollTo(0,0)"; onmouseover="style.color='green'";
-            As explained before, no external library needs to be included, only the <b><font face="Courier New"><font style="color:#CD5307">Wire</font>.h</font></b> library for I2C communication for reading and writing to the registers of the DS3231. +      <span style="color:#2E71B8"; onmouseover="style.color='green'"; onmouseout="style.color='#2E71B8'";> 
-        </li> +        Back to the top &#10548 
-        <li> +      </span
-            The I2C address of the DS3231 needs to be defined. By default (if no solder connections are used) it is hexadecimal <b><font face="Courier New">0x68</font></b>+    </a> 
-        </li> +    </span
-        <li> +  </center>
-            When setting the day of the week later1 represents Monday, and 7 represents Sunday. To make it more intuitive the days are here defined. +
-        </li> +
-        <br> +
-        <li> +
-            As explained in section 2, the alarm register mask bits define the alarm rate. For example, choosing the <b><font face="Courier New">ALARM_SECONDS_MINUTES_MATCH</font></b> option would trigger an alarm once every hour - when the seconds and the minutes in the alarm and the time register match. +
-        </li> +
-        <li> +
-            The <b><font face="Courier New"> <font style="color:#0098A3">byte</font></font></b> array <b><font face="Courier New">Alarm1MaskBits[6]</font></b> is basically an array containing table 2 with the mask bits for the alarm rates, where each array element represents a row from the table. The MSB in each element represents the bit for selecting either day or date format. Bit 6 is for A1M4, bit 5 is A1M3, and so on. The three LSBs are not needed and are just set to 0. Later on, using the previously defined (4alarm 1 settings, one element is chosen from the array to set the alarm rate accordingly. +
-        </li> +
-        <br> +
-        <li> +
-            Here the Arduino pins for the interrupt (<b><font face="Courier New">IntPin</font></b>) and for the LED (<b><font face="Courier New">LEDPin</font></b>) are chosen. The interrupt pin has to be eitehr pin 2 or 3 for the UNO. the boolean variable <b><font face="Courier New">ledstatus</font></b> is just used in the main loop to switch the LED on and off. +
-        </li> +
-        <li> +
-            When an interrupt is triggered, the MCU executes the attached interrupt service routine ISR which is defined later. The variable <b><font face="Courier New">Count</font></b> is used to count the number of interrupts triggered and is incremented by 1 in the ISR. Variables that are changed inside an ISR need to be configured as <b><font face="Courier New"><font style="color:#0098A3">volatile</font></font></b> +
-            <br> +
-            Configuring a variable as volatile tells the Compiler to load the variable from the RAM instead of the a storage register. This is necessary when the variable can be changed from somewhere else than the code that it is appearing in, for example a concurrently executed function like an ISR. +
-            <br> +
-        </li> +
-        <br> +
-        <li> +
-            In the <b><font face="Courier New"><font style="color:#5A6B0F">setup</font>()</font></b>, the method <b><font face="Courier New"><font style="color:#CD5307">Wire</font><font style="color:#000000">.</font><font style="color:#CD5307">begin</font><font style="color:#000000">()</font></font></b> starts the I2C bus connecting DS3231 and Arduino UNO. +
-        </li> +
-        <li> +
-            The interrupt pin <b><font face="Courier New">IntPin</font></b> needs to be configured as an input. Furthermore, the DS3231's SQW pin (which is activated when an alarm is triggered) is an open drain output, which means it shorts the pin to GND when active. The Arduino interrupt pin (pin 2) needs to be HIGH normally, so that the voltage drop triggered by the SQW pin can be detected. As the Arduino has internal pullup resistors, activating them using <b><font face="Courier New"><font style="color:#0098A3">INPUT_PULLUP</font></font></b> is sufficient for this. +
-        </li> +
-        <li> +
-            The pin for the LED needs to be configured as an <b><font face="Courier New"><font style="color:#0098A3">OUTPUT</font></font></b> to switch the LED on and off. +
-        </li> +
-        <li> +
-            Here the later defined function <b><font face="Courier New">setRTCTime</font></b> is called to configure the time, date and day of the week of the DS3231. As an argument it needs the seconds, minutes, hours, day of the week, day of the month, month and year. After the time is configured for the first time, this should be commented out to prevent the time from being reset every time the Arduino restarts. +
-        </li> +
-        <li> +
-            The function <b><font face="Courier New">setRTCAlarm1</font></b> configures the seconds, minutes, hours, date or day of the week, as well as the setting for the alarm rate for alarm 1 by writing to the alarm registers. Furthermore it activates alarm 1 and enables interrupts in the status register. The function is defined later on. If the alarm is already configured and working, this line of code can be commented out as well. +
-        </li> +
-        <li> +
-            To attach an interrupt on the Arduino UNO, the function <b><font face="Courier New"><font style="color:#CD5307">attachInterrupt</font>()</font></b> is used. As first argument the function <b><font face="Courier New"><font style="color:#CD5307">digitalPinToInterrupt</font>()</font></b> is given and as its argument the Arduino interrupt pin (2) is submitted. Afterwards, the function expects the name of the interrupt service routine which is called whenever an interrupt is triggeredin this sketch it is called ISRLED. The last argument is the interrupt mode. There are 5 (for the UNO 4) different modes. Here the mode <b><font face="Courier New"><font style="color:#0098A3">FALLING</font></font></b> is necessary which indicates that the interrupt is triggered whenever the interrupt pin goes from HIGH to LOW. +
-        </li> +
-        <br> +
-        <li> +
-            The main <b><font face="Courier New"><font style="color:#5A6B0F">loop</font>()</font></b> loop itself is very short. Firstly, by removing the alarm 1 flag in the DS3231 status register with the function <b><font face="Courier New">clearAlarm1()</font></b> defined below, alarm 1 is reset. In the final application this will only be done once after the MCU wakes up from deep sleep. +
-        </li> +
-        <li> +
-            The <b><font face="Courier New"><font style="color:#5A6B0F">if</font></font></b> control structure checks the variable <b><font face="Courier New">Count</font></b>. In this example, the alarm was chosen to go off every second (12), so the <b><font face="Courier New">Count</font></b> variable is incremented by 1 once a second, so the condition becomes true after 1 second. It then resets the counter variable and changes the status of the LED. So in conclusion, it switches the LED on and off in 1 second intervals. The intervals can be changed by adjusting the condition. This is just used as an indicator to check if the interrupt is working as supposed to. +
-        </li> +
-        <br> +
-        <li> +
-            The function <b><font face="Courier New">decToBCD()</font></b> returns and expects as an arguement a  <b><font face="Courier New"><font style="color:#0098A3">byte</font></font></b> type value. It is used to convert a decimal number DEC to a binary coded decimal number BCD. As explained before, the time and alarm registers of the DS3231 are written in BCD format. However, DEC format is much more intuitive for the user to set the alarm and time of the module. Therefore, this function is used to convert the decimals given as function arguments for the two functions called in the setup (11 & 12) to the format that the DS3231 understands. +
-        </li> +
-        <li> +
-            This function does the exact opposite of the previous one. It converts binary coded decimal back to decimal. This is only used for optionally checking if the time set in the DS3231 is correct. +
-        </li> +
-        <br> +
-        <li> +
-            The function <b><font face="Courier New">ISRLED()</font></b> is serving as the interrupt service routine called when an interrupt is triggered. Here it is used only to increment the counter variable and active the flag for the interrupt status. There are a few things to consider when writing an interrupt service routine. ISRs should always be as as short as possible because when they are called, the normal program stops as long as they are runningso functions that need a lot of time like print functions should be avoided. Furthermore,<b><font face="Courier New"><font style="color:#CD5307">delay</font>()</font></b>, <b><font face="Courier New"><font style="color:#CD5307">millis()</font>()</font></b> or <b><font face="Courier New"><font style="color:#CD5307">micros</font>()</font></b> are not working inside an ISR and variables used in an ISR need to be <b><font face="Courier New"><font style="color:#0098A3">volatile</font></font></b>More information can be found on the <a href="https://www.arduino.cc/reference/en/language/functions/external-interrupts/attachinterrupt/">Arduino Reference page</a>+
-        </li> +
-        <br> +
-        <li> +
-            As explained before (11), <b><font face="Courier New">setRTCTime()</font></b> is used to configure the time registers of the DS3231. +
-        </li> +
-        <li> +
-            The method <b><font face="Courier New"><font style="color:#CD5307">Wire</font>.<font style="color:#CD5307">beginTransmission</font>()</font></b> starts a transmission on the I2C bus in the slave receiver mode. As a function argument, the already defined I2C address (2) is given. Only the slave with that exact address will respond to the now following communication. +
-        </li> +
-        <li> +
-            The method <b><font face="Courier New"><font style="color:#CD5307">Wire</font>.<font style="color:#CD5307">write</font>()</font></b> transmits the byte given as an argument. The first <b><font face="Courier New"><font style="color:#CD5307">write</font>()</font></b> sets the register pointer of the DS3231. Here the pointer is set to <b><font face="Courier New">0x00</font></b>, which means that the following input is stored in the register with the address <b><font face="Courier New">0x00</font></b> which is the time register for the seconds. +
-        </li> +
-        <li> +
-            Now the value for <b><font face="Courier New">Second</font></b> given as function argument is converted into BCD format and is then sent on the I2C bus to the DS3231 which overwrites the <b><font face="Courier New">0x00</font></b> register with the new information. That means the seconds of the DS3231 are configured. The register pointer then automatically jumps to the next register which is storing the minutes. As all the time registers are grouped together, all of them can be configured in one transmission, one after the other. +
-        </li> +
-        <li> +
-            When all the registers are configured and the DS3231 has a new time set, the I2C transmission is stopped with <b><font face="Courier New"><font style="color:#CD5307">Wire</font>.<font style="color:#CD5307">endTransmission</font>()</font></b>+
-        </li> +
-        <br> +
-        <li> +
-            Here follows the definition of function <b><font face="Courier New">setRTCAlarm1()</font></b> which was used in the <b><font face="Courier New"><font style="color:#5A6B0F">setup</font>()</font></b> (12). +
-        </li> +
-        <li> +
-            To configure the four registers of alarm 1, it is necessary to convert the seconds, minutes, hours and date/day into BCD format again. However, as can be seen in table 1, the alarm registers also contain the mask bits for the alarm rate which are stored as A1M1 to A1M4 in the MSBs of the four. The Which mask bit is used for which register is determined by the element chosen in the mask bit array (5). +
-            <br> +
-            The function <b><font face="Courier New"><font style="color:#CD5307">bitRead</font>()</font></b> is used to read a single bit from a byte. The first argument is the byte to read from; here the <b><font face="Courier New">Alarm1MaskBits[]</font></b> array is used and the byte element to read is determined by the <b><font face="Courier New">Setting</font></b> (4). <b><font face="Courier New">ALARM_ONCE_PER_SECOND</font></b> thus means the first element from the array which is <b><font face="Courier New">B01111000</font></b>, is chosen to read from. The second argument is the number of the bit, where 0 is the LSB and 7 is the MSB. The bit mask for the seconds register is the 4th bit from the right, so bit number 3. To now place that bit correctly in the <b><font face="Courier New">Second</font></b> byte, it mus be written inthe A1M1 bit (MSB). The MSB has a decimal value of 128, so the mask bit is just multiplied by 128 which puts it to the front. +
-            <br> +
-            This same process is repeated for the minutes hours and days. What changes is the position of the respective mask bit in the mask bit array element; for minutes it is 4, for hours 5 and for days or date 6. +
-            <br> +
-            The hour format will automatically be 24 hours like in the time register because bit 6 stays always 0 (see table 1). However, when the alarm rate is chosen to be either once a week or once a month, i.e. days/date are matched, that has to be changed in the code. Therefore, the MSB of the array element contains a 0 for matching the date and a 1 for matching the day (table 2). This value is read from the array with another <b><font face="Courier New"><font style="color:#CD5307">bitRead</font>()</font></b> and the retrieved value is multiplied with 64 to put it to bit 6 of the <b><font face="Courier New">DayDate</font></b> byte. +
-        </li> +
-        <li> +
-            After configuring the values of the bytes for setting the alarm, another transmission to the RTC is started and the register pointer is set to register <b><font face="Courier New">0x07</font></b> containing the alarm settings for the seconds. Then the before calculated bytes are transmitted one after another to configure the alarm. and the transmission is ended. +
-        </li> +
-        <li> +
-            To activate alarm 1 and enable the interrupt output throug the SQW pin, another transmission is started and the register pointer is set to <b><font face="Courier New">0x0E</font></b> which is the DS3231's control register. +
-        </li> +
-        <li> +
-            Then the byte <b><font face="Courier New">B00011101</font></b> is transmitted through I2C to configure the control register. What it does can be found in detail in the datasheet on page 13. In short, what each bit does is: +
-            B: 0 - turn on the oscillator0 - no square wave output; 0 - no temperature conversion command; 1 - square wave frequency setting - 1 square wave frequency setting; 1 -activate interrupt output; 0 - deactivate alarm 2; 1 - activate alarm 1. +
-            <br> +
-            After that, the alarm is set and an interrupt is issued when the respective register entries match. +
-        </li> +
-        <br> +
-        <li> +
-            Whenever the alarm is activated, the alarm flag bit in the DS3231 status register is switched to a 1 and needs to be reset manually by changing the bit back to a 0. The function <b><font face="Courier New">clearAlarm1()</font></b> does exactly that. It starts a transmission, sets the register pointer to the status register and resets the value. More information can be found in the datasheet on page 14. +
-        </li> +
-        <br> +
-        <li> +
-            The last two functions are not used in the sketch but are useful to check whether the DS3231 has a correctly configured time register. The function <b><font face="Courier New">readRTCTime()</font></b> reads the values from the register and the function <b><font face="Courier New">displayTimeSerial()</font></b> prints it to the serial monitor. In the functions pointers are used to be able to only use local instead of global variables. The local variables <b><font face="Courier New">Second</font></b>, <b><font face="Courier New">Minute</font></b>, <b><font face="Courier New">Hour</font></b> and so on are declared locally in the <b><font face="Courier New">displayTimeSerial()</font></b> function. Then their addresses are given as function argument to the <b><font face="Courier New">readRTCTime()</font></b> which gives those addresses to its own locally declared pointer variables of the same name. The pointer variables are indicated by the asterisk * next to the variable name. +
-        </li> +
-        <li> +
-            Then the I2C transmission is started in slave receiver mode to set the register pointer to the seconds register and the transmission is ended again. +
-        </li> +
-        <li> +
-            Another transmission is started but in the slave transmitter mode using the <b><font face="Courier New"><font style="color:#CD5307">Wire</font>.<font style="color:#CD5307">requestFrom</font>()</font></b> method. It needs the I2C address of the slave and the number of bytes which are requested. +
-        </li+
-        <li> +
-            The dereferencing operator, the asterisk *, is used to change the value at the address of the pointer variable, in this case the value for the local variables from the <b><font face="Courier New">displayTimeSerial()</font></b> function. As value they get the time stored in the DS3231 time registers converted back to decimal. +
-        </li> +
-        <br> +
-        <li> +
-            In the <b><font face="Courier New">displayTimeSerial()</font></b> function, the local variables are created, their addresses are given to the <b><font face="Courier New">readRTCTime()</font></b> function using the referencing operator & and then their results are printed to the serial monitor +
-        </li>         +
-    </ol+
-    </font>+
 </html> </html>
 +
 +<html><span><a href="https://wiki.eolab.de/doku.php?id=amc2020:group_n:start#materials_and_methods">Main Page Sensors</a></span></html>
 +
 +
 +Default Link Color RGB: #2E71B8
 +
 +<html>
 +<div style="position: relative;">
 +  <img src="https://wiki.eolab.de/lib/exe/fetch.php?media=amc2020:group_n:pond_monitoring_station_updated.png" style="width: 100%; height: auto;">
 +  
 +  <a href="https://wiki.eolab.de/doku.php?id=amc2020:group_n:dht22" target="_blank" title="DHT-22 Temperature and Humidity Sensor"><div style="position: absolute; left: 36.72%; top: 23.59%; width: 6.72%; height: 11.8%; background-color: rgba(0,0,0, 0);"></div></a>
 +  
 +  <a href="https://wiki.eolab.de/doku.php?id=amc2020:group_n:ds18b20" target="_blank" title="DS18B20 Waterproof Digital Temperature Sensor"><div style="position: absolute; left: 38.59%; top: 56.57%; width: 4.69%; height: 8.85%; background-color: rgba(0,0,0, 0);"></div></a>
 +  
 +  <a href="https://wiki.eolab.de/doku.php?id=amc2020:group_n:ds18b20" target="_blank" title="DS18B20 Waterproof Digital Temperature Sensor"><div style="position: absolute; left: 57.50%; top: 84.99%; width: 7.97%; height: 11.53%; background-color: rgba(0,0,0, 0);"></div></a>
 +  
 +  <!--<a href="https://wiki.eolab.de/doku.php?id=amc2020:group_n:pv" target="_blank"><div style="position: absolute; left: 6.72%; top: 37.27%; width: 12.50%; height: 13.40%; background-color: rgba(0,0,0, 0);"></div></a>-->
 +  
 +  <a href="https://wiki.eolab.de/doku.php?id=amc2020:group_n:deepsleep" target="_blank" title="ESP32 DevKitC-VB"><div style="position: absolute; left: 19.84%; top: 33.51%; width: 15.31%; height: 15.55%; background-color: rgba(0,0,0, 0);"></div></a>
 +  
 +  <!--<a href="https://wiki.eolab.de/doku.php?id=amc2020:group_n:esp32cam" target="_blank"><div style="position: absolute; left: 37.50%; top: 39.68%; width: 6.56%; height: 8.31%; background-color: rgba(0,0,0, 0);"></div></a>-->
 +  
 +  <!--<a href="https://wiki.eolab.de/doku.php?id=amc2020:group_n:level" target="_blank" title="Pond"><div style="position: absolute; left: 35.47%; top: 48.26%; width: 3.59%; height: 8.31%; background-color: rgba(0,0,0, 0);"></div></a>-->
 +  
 +  <a href="https://wiki.eolab.de/lib/exe/fetch.php?media=amc2020:group_n:pond.jpg" target="_blank"><div style="position: absolute; left: 44.69%; top: 37.00%; width: 46.25%; height: 45.31%; background-color: rgba(,0,0, 0);"></div></a>
 +  
 +  <a href="https://wiki.eolab.de/doku.php?id=amc2020:group_n:sn74hc595n" target="_blank" title="SN74HC595(N) Shift Register"><div style="position: absolute; left: 11.72%; top: 54.42%; width: 9.84%; height: 9.65%; background-color: rgba(,0,0, 0);"></div></a>
 +  
 +  <a href="https://wiki.eolab.de/doku.php?id=amc2020:group_n:ds3231rtc" target="_blank" title="DS3231 Real Time Clock Module"><div style="position: absolute; left: 4.53%; top: 65.42%; width: 9.38%; height: 10.46%; background-color: rgba(,0,0, 0);"></div></a>
 +  
 +</div>
 +</html>
 +
 +===== Materials and Methods =====
 +
 +==== Sensors and modules that are used ====
 +
 +Further information on the sensors and devices used can be seen in their respective pages:
 +
 +**[[dht22|DHT-22 Air Temperature and Humidity Sensor]]**
 +
 +**[[ds18b20|DS18B20 Waterproof Temperature Sensor]]**
 +
 +**[[SN74HC595N|SN74HC595(N) Shift Register]]**
 +
 +**[[DS3231RTC|DS3231 Real Time Clock]]**
 +
 +==== Programming Deep Sleep and MQTT ====
 +
 +**[[amc2020:group_n:deepsleep|ESP32 Deep Sleep]]**
 +
 +**[[amc2020:group_n:wifi|Testing Wifi and MQTT]]**
 +
 +==== Setup of the ESP32 and the Modules ====
 +
 +^{{:amc2020:group_n:esp32_sensors_rtc_pn2222.png?direct&500|Figure 2}}^
 +|**//Figure 2//** Graphic on how to set up the ESP32 and the modules and parts.|
 +
 +^{{:amc2020:group_n:esp32_wiring.png?direct&800|Figure 3}}^
 +|**//Figure 3//** Schematic of the connections between ESP32 and the modules and parts.|
 +
 +===== Results =====
 +
 +^ {{:amc2020:group_n:pond_monitoring_station_data_logger_graphana_light_theme.png?800&direct|Figure 4}}  ^
 +| **//Figure 4//** Measurement results of the DHT-22 and DS18B20 sensors visualized in Grafana.          |
 +
 +
 +
 +{{:amc2020:group_n:pond.jpg?direct&800|}}
 +
 +
amc2020/group_n/test.1596014745.txt.gz · Last modified: 2021/08/24 17:34 (external edit)