<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>communication technique Archives - Nerd Corner</title>
	<atom:link href="https://nerd-corner.com/tag/communication-technique/feed/" rel="self" type="application/rss+xml" />
	<link>https://nerd-corner.com/tag/communication-technique/</link>
	<description>Craft your dreams!</description>
	<lastBuildDate>Wed, 06 Mar 2024 12:18:52 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.8.2</generator>

<image>
	<url>https://nerd-corner.com/wp-content/uploads/2019/10/cropped-LogoNerdCorner-2-32x32.png</url>
	<title>communication technique Archives - Nerd Corner</title>
	<link>https://nerd-corner.com/tag/communication-technique/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>WeMos D1 R2 &#8211; Host entire website with html, css &#038; js</title>
		<link>https://nerd-corner.com/wemos-d1-r2-host-entire-website-with-html-css-js/</link>
					<comments>https://nerd-corner.com/wemos-d1-r2-host-entire-website-with-html-css-js/#respond</comments>
		
		<dc:creator><![CDATA[Nerds]]></dc:creator>
		<pubDate>Wed, 06 Mar 2024 12:18:52 +0000</pubDate>
				<category><![CDATA[Arduino projects]]></category>
		<category><![CDATA[Hardware]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[Arduino]]></category>
		<category><![CDATA[Arduino with Wifi]]></category>
		<category><![CDATA[C programming]]></category>
		<category><![CDATA[communication technique]]></category>
		<category><![CDATA[control Arduino with browser]]></category>
		<category><![CDATA[data transfer]]></category>
		<category><![CDATA[entire website]]></category>
		<category><![CDATA[ESP8266]]></category>
		<category><![CDATA[ESP8266 with mDNS]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[JS]]></category>
		<category><![CDATA[led]]></category>
		<category><![CDATA[local network]]></category>
		<category><![CDATA[network]]></category>
		<category><![CDATA[Step by step guide]]></category>
		<category><![CDATA[Webserver]]></category>
		<category><![CDATA[website hosting]]></category>
		<category><![CDATA[WeMos]]></category>
		<category><![CDATA[WeMos D1]]></category>
		<category><![CDATA[WeMos D1 in Arduino IDE]]></category>
		<category><![CDATA[WeMos D1 mini]]></category>
		<category><![CDATA[WeMos D1 R2]]></category>
		<category><![CDATA[WeMOS D1 Webserver]]></category>
		<category><![CDATA[WIFI]]></category>
		<guid isPermaLink="false">https://nerd-corner.com/de/?p=1547</guid>

					<description><![CDATA[<p>The first part of the WeMos series explained the setup and Wi-Fi integration. Building on this, this article describes step by step how to host &#8230; </p>
<p>The post <a href="https://nerd-corner.com/wemos-d1-r2-host-entire-website-with-html-css-js/">WeMos D1 R2 &#8211; Host entire website with html, css &#038; js</a> appeared first on <a href="https://nerd-corner.com">Nerd Corner</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>The first part of the WeMos series explained the setup and Wi-Fi integration. Building on this, this article describes step by step how to host a complete website with HTML pages, CSS styling and JavaScript functions on a WeMos. The whole thing is pretty simple and incredibly helpful! I can&#8217;t believe how hard it is to find proper instructions for this important function anywhere on the internet.</p>
<p>I often see blog articles where the HTML code is embedded in the Arduino file. You can do this for a mini demonstration, but it&#8217;s complete rubbish for everyday use. It is far too confusing and as soon as the project grows it is no longer usable.</p>
<p>The proper alternative is to set up a folder called &#8220;data&#8221; and store the web pages in this folder as html files. In addition, the styling is saved as a CSS file and functions can even be executed via a JavaScript file. So everything is 1:1 like on a normal web server!</p>
<p><em><strong>This might also be interesting for you:</strong> <a href="https://nerd-corner.com/wemos-d1-r2-setup-and-wifi-integration/">WeMos D1 R2 first steps and Wifi integration</a></em></p>
<h2>List of components</h2>
<ul>
<li>Arduino IDE (development environment)</li>
<li>WeMos D1 R2</li>
</ul>
<h2>The setup of the file system (officially SPIFFS) needs to be done once and is very easy:</h2>
<p><a href="https://nerd-corner.com/wemos-d1-r2-setup-and-wifi-integration/">(First you should have completed the basic setup from the first part!)</a></p>
<ol>
<li>Download a copy of the file &#8220;ESP8266FS-0.2.0.zip&#8221; from GitHub and unzip it</li>
<li>Place the file esp8266fs.jar in the Arduino tool directory. The path looks like this: [home_dir]\Arduino\tools\ESP8266FS\tool\esp8266fs.jar (See picture) I had to create the path part tools\ESP8266FS\tool\ in the Arduino folder myself.<img fetchpriority="high" decoding="async" class="zoooom aligncenter wp-image-1531" src="https://nerd-corner.com/wp-content/uploads/2024/03/Screenshot-file-path-esp8266fs.jar-.png" alt="Screenshot file path esp8266fs.jar" width="1080" height="335" srcset="https://nerd-corner.com/wp-content/uploads/2024/03/Screenshot-file-path-esp8266fs.jar-.png 1190w, https://nerd-corner.com/wp-content/uploads/2024/03/Screenshot-file-path-esp8266fs.jar--300x93.png 300w, https://nerd-corner.com/wp-content/uploads/2024/03/Screenshot-file-path-esp8266fs.jar--1024x318.png 1024w, https://nerd-corner.com/wp-content/uploads/2024/03/Screenshot-file-path-esp8266fs.jar--768x238.png 768w" sizes="(max-width: 1080px) 100vw, 1080px" /></li>
<li>Restart the Arduino IDE.</li>
</ol>
<p>That&#8217;s it already! You can now see the new item &#8220;ESP8266 Sketch Data Upload&#8221; in the Arduino IDE under Tools.</p>
<h2>How can I use the new file system now?</h2>
<ol>
<li>Create an additional folder with the name &#8220;data&#8221; in your current WeMos project folder. As shown in the following image</li>
</ol>
<p><img decoding="async" class="zoooom aligncenter wp-image-1528" src="https://nerd-corner.com/wp-content/uploads/2024/03/Wemos-data-folder.png" alt="Wemos data folder" width="1040" height="324" srcset="https://nerd-corner.com/wp-content/uploads/2024/03/Wemos-data-folder.png 1146w, https://nerd-corner.com/wp-content/uploads/2024/03/Wemos-data-folder-300x93.png 300w, https://nerd-corner.com/wp-content/uploads/2024/03/Wemos-data-folder-1024x319.png 1024w, https://nerd-corner.com/wp-content/uploads/2024/03/Wemos-data-folder-768x239.png 768w" sizes="(max-width: 1040px) 100vw, 1040px" /></p>
<ol start="2">
<li>Place the files you want to upload in the &#8216;data&#8217; directory</li>
<li>In the Arduino IDE, select the WeMos in the &#8216;Tools&#8217; menu and select a size for &#8216;Flash Size&#8217;</li>
<li>Close the dialogue box for the serial monitor!</li>
<li>Select the &#8216;ESP8266 Sketch Data Upload&#8217; option from the &#8216;Tools&#8217; menu.</li>
</ol>
<p><img decoding="async" class="zoooom aligncenter wp-image-1529" src="https://nerd-corner.com/wp-content/uploads/2024/03/data-folder-upload.png" alt="Data folder upload" width="805" height="518" srcset="https://nerd-corner.com/wp-content/uploads/2024/03/data-folder-upload.png 851w, https://nerd-corner.com/wp-content/uploads/2024/03/data-folder-upload-300x193.png 300w, https://nerd-corner.com/wp-content/uploads/2024/03/data-folder-upload-768x495.png 768w" sizes="(max-width: 805px) 100vw, 805px" /></p>
<p>As soon as the upload is complete, the message window of the Arduino IDE shows 100% upload.</p>
<h2>WeMos example programme for switching the OnBoard LED on and off</h2>
<p>Similar to the first part, the web server will control the OnBoard LED. The code from the first part also serves as the basis. The revised code looks like this:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="atomic" data-enlighter-title="D1_webserver_filesystem">#include &lt;ESP8266WiFi.h&gt;
#include &lt;ESP8266WebServer.h&gt;
#include &lt;ESP8266mDNS.h&gt;

ESP8266WebServer server(80);

void setup() {
  Serial.begin(115200); //Baudrate
  Serial.println("ESP starts");

  WiFi.begin("NerdCornerWiFi","NerdCornerPassword");


  Serial.print("Connecting...");

  while(WiFi.status()!=WL_CONNECTED){ //Loop which makes a point every 500ms until the connection process has finished

    delay(500);
    Serial.print(".");
  }
  Serial.println();

  Serial.print("Connected! IP-Address: ");
  Serial.println(WiFi.localIP()); //Displaying the IP Address

  if (MDNS.begin("nerd-corner")) {
    Serial.println("DNS started, available with: ");
    Serial.println("http://nerd-corner.local/");
  }

  server.serveStatic("/", SPIFFS, "/", "max-age=86400");
  SPIFFS.begin();

  server.onNotFound([](){ 
    server.send(404, "text/plain", "Landing page not found! Don't forget to name your landing page 'index.html'!");  
  });
 
  server.on("/led", HTTP_POST, []() {    
     
    const String ledState = server.arg("ledstate");
    if(ledState=="on"){
      switchLedOn();
    }
    else if(ledState=="off"){
      switchLedOff();
    }
    server.send(200, "text/json", "{\"result\":\"ok\"}");
  });

  server.begin();
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
  server.handleClient();
  MDNS.update();

}

void switchLedOff(){ 
  digitalWrite(LED_BUILTIN, HIGH);   // turn the D1 LED off 
}

void switchLedOn(){ 
  digitalWrite(LED_BUILTIN, LOW);    // turn the LED on 
}</pre>
<p>I would like to point out a few special features. For example, we have added the following:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="atomic">server.serveStatic("/", SPIFFS, "/", "max-age=86400"); 
SPIFFS.begin();</pre>
<p>Without these two lines, access to the files in the &#8220;data&#8221; folder would not be possible. Please note that the name &#8220;index.html&#8221; is set as the default for the landing page. However, you can change this if you really want to.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="atomic">server.on("/led", HTTP_POST, []() {    
     
    const String ledState = server.arg("ledstate");
    if(ledState=="on"){
      switchLedOn();
    }
    else if(ledState=="off"){
      switchLedOff();
      }
      server.send(200, "text/json", "{\"result\":\"ok\"}");
  });</pre>
<p>The &#8220;/led&#8221; endpoint receives the commands from the web server. If the command is &#8220;on&#8221;, the LED is switched on and if &#8220;off&#8221;, the LED is switched off.</p>
<h2>Wemos website for switching the WeMos OnBoard LED on and off</h2>
<p>The example website has a very simple structure. It primarily consists of 2 buttons for switching the LED on and off.</p>
<p><img loading="lazy" decoding="async" class="zoooom aligncenter wp-image-1536" src="https://nerd-corner.com/wp-content/uploads/2024/03/Webseite-Wemos-Control.jpeg" alt="Website Wemos control LED" width="875" height="1946" srcset="https://nerd-corner.com/wp-content/uploads/2024/03/Webseite-Wemos-Control.jpeg 921w, https://nerd-corner.com/wp-content/uploads/2024/03/Webseite-Wemos-Control-135x300.jpeg 135w, https://nerd-corner.com/wp-content/uploads/2024/03/Webseite-Wemos-Control-461x1024.jpeg 461w, https://nerd-corner.com/wp-content/uploads/2024/03/Webseite-Wemos-Control-768x1708.jpeg 768w, https://nerd-corner.com/wp-content/uploads/2024/03/Webseite-Wemos-Control-691x1536.jpeg 691w" sizes="auto, (max-width: 875px) 100vw, 875px" /></p>
<p>The folder structure of the website is very clearly organised. There is a main page with the name &#8220;index.html&#8221;. This name is common worldwide for the main pages and is also automatically recognised by WeMos. There is also a &#8220;CSS&#8221; folder for styling and a &#8220;JS&#8221; folder for functions.</p>
<p><img loading="lazy" decoding="async" class="zoooom aligncenter wp-image-1537" src="https://nerd-corner.com/wp-content/uploads/2024/03/Screenshot-data-file-system.png" alt="Screenshot esp8266 data file system" width="1045" height="347" srcset="https://nerd-corner.com/wp-content/uploads/2024/03/Screenshot-data-file-system.png 1152w, https://nerd-corner.com/wp-content/uploads/2024/03/Screenshot-data-file-system-300x100.png 300w, https://nerd-corner.com/wp-content/uploads/2024/03/Screenshot-data-file-system-1024x340.png 1024w, https://nerd-corner.com/wp-content/uploads/2024/03/Screenshot-data-file-system-768x255.png 768w" sizes="auto, (max-width: 1045px) 100vw, 1045px" /></p>
<p>We link the styles in the header area of the website. There is a standard bootstrap, which automatically makes everything a bit nicer, and a custom styles file with my own customisations. The functions of the website are also linked in the header area. I use the jQuery standard to send requests from the website to the WeMos. My own custom functions are in the &#8220;index.js&#8221;.</p>
<p>Please note that the jQuery file must be included BEFORE your own file, otherwise no jQuery commands can be used in your own code! The custom functions are then used by the buttons. The HTML code of the page looks as follows:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="html" data-enlighter-theme="atomic">&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;&lt;/html&gt;
&lt;html&gt;
  &lt;head&gt;
    &lt;meta charset="utf-8" /&gt;
    &lt;meta
      name="viewport"
      content="width=device-width, initial-scale=1, shrink-to-fit=no"
    /&gt;
    &lt;script type="text/javascript" src="js/jquery-3.5.1.min.js"&gt;&lt;/script&gt;
    &lt;script type="text/javascript" src="./js/index.js"&gt;&lt;/script&gt;
    &lt;link rel="stylesheet" href="css/bootstrap.min.css" /&gt;
    &lt;link rel="stylesheet" href="css/custom-style.css" /&gt;
    &lt;title&gt;D1 Webserver&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;h1&gt;D1 Webserver with filesystem&lt;/h1&gt;
    &lt;p&gt;
      This is an example for a WeMos Webserver with a filesystem. You can easily
      create webpages with html, css and js!
    &lt;/p&gt;

    &lt;h3&gt;Example to turn on and off the built in LED&lt;/h3&gt;
    &lt;button class="button-style" onclick="changeLEDState('on')"&gt;Turn on&lt;/button&gt;
    &lt;button class="button-style" onclick="changeLEDState('off')"&gt;
      Turn off
    &lt;/button&gt;

    &lt;h3&gt;Example to demo a JS function&lt;/h3&gt;
    &lt;button class="button-style" onclick="showAlert()"&gt;Show alert&lt;/button&gt;
  &lt;/body&gt;
&lt;/html&gt;
</pre>
<p>Special attention is paid to the JavaScript function &#8220;changeLEDState(value)&#8221;</p>
<pre class="EnlighterJSRAW" data-enlighter-language="js" data-enlighter-theme="atomic">function changeLEDState(value) {
  $.post("/led", { ledstate: value });
}</pre>
<p>Because jQuery is used for communication with the WeMos, a simple dollar sign with the corresponding request command is sufficient. A value is also sent with this POST request, which is either &#8220;on&#8221; or &#8220;off&#8221; to switch the LED on and off.</p>
<p>The web page can be downloaded as a zip file below.</p>
<h2>Files to download</h2>
<ul>
<li><a  data-e-Disable-Page-Transition="true" class="download-link" title="" href="https://nerd-corner.com/download/1540/?tmstv=1756264922" rel="nofollow" id="download-link-1540" data-redirect="false" >
	Wemos example webserver to control OnBoard LED</a>
</li>
</ul>
<p>The post <a href="https://nerd-corner.com/wemos-d1-r2-host-entire-website-with-html-css-js/">WeMos D1 R2 &#8211; Host entire website with html, css &#038; js</a> appeared first on <a href="https://nerd-corner.com">Nerd Corner</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://nerd-corner.com/wemos-d1-r2-host-entire-website-with-html-css-js/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Cyclic Redundancy Check in C code</title>
		<link>https://nerd-corner.com/cyclic-redundancy-check-in-c-code/</link>
					<comments>https://nerd-corner.com/cyclic-redundancy-check-in-c-code/#comments</comments>
		
		<dc:creator><![CDATA[Nerds]]></dc:creator>
		<pubDate>Tue, 15 Jun 2021 12:08:30 +0000</pubDate>
				<category><![CDATA[Software]]></category>
		<category><![CDATA[communication technique]]></category>
		<category><![CDATA[CRC]]></category>
		<category><![CDATA[CRC Wert]]></category>
		<category><![CDATA[cyclic redundancy check]]></category>
		<category><![CDATA[error]]></category>
		<category><![CDATA[error free transmission]]></category>
		<category><![CDATA[frequency]]></category>
		<category><![CDATA[LiFi]]></category>
		<category><![CDATA[visible Light communication]]></category>
		<category><![CDATA[VLC]]></category>
		<guid isPermaLink="false">https://nerd-corner.com/de/?p=930</guid>

					<description><![CDATA[<p>I plan to build a transmitter and receiver station which is able to transmit data with the help of light signals (&#8220;Visible Light Communication&#8221; &#8211; &#8230; </p>
<p>The post <a href="https://nerd-corner.com/cyclic-redundancy-check-in-c-code/">Cyclic Redundancy Check in C code</a> appeared first on <a href="https://nerd-corner.com">Nerd Corner</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>I plan to build a transmitter and receiver station which is able to transmit data with the help of light signals (&#8220;Visible Light Communication&#8221; &#8211; VLC). In order for the data transmission to be possible, the clock frequency of the transmitter and the receiver must be precisely synchronized. I have already explained in previous posts how to set a precise clock frequency for the Arduino and the Raspberry Pi, to which I link below.</p>
<p>A cyclic redundancy check is a simple and elegant way to verify that the data arriving at a receiving station is correct. For example, if bits have been swapped in a received data packet or a 1 has been received instead of a 0, this can be detected by the cyclic redundancy check (CRC for short). The CRC identifies that a data packet has an error and the receiving station will drop the packet.</p>
<p><em><strong>This might also be interesting for you:</strong> <a href="https://nerd-corner.com/arduino-timer-interrupts-how-to-program-arduino-registers/" target="_blank" rel="noopener">Setting a precise clock frequency for an Arduino</a></em></p>
<p><em><strong>Also closely related to this article:</strong> <a href="https://nerd-corner.com/how-to-program-a-highly-precise-timer-in-c-for-linux/" target="_blank" rel="noopener">Setting a precise clock frequency for the Raspberry Pi</a></em></p>
<h2>Cyclic redundancy check &#8211; How it works:</h2>
<p>The CRC is a mathematical trick. In principle, the calculation is a polynomial division. For example, the data packet 10010101 corresponds to the following polynomial:</p>
<p><img loading="lazy" decoding="async" class="zoooom aligncenter wp-image-918" src="https://nerd-corner.com/wp-content/uploads/2021/06/Datenpaket.jpg" alt="zyklische redundazprüfung Datenpaket polynom cyclic redundancy check" width="1852" height="123" srcset="https://nerd-corner.com/wp-content/uploads/2021/06/Datenpaket.jpg 1853w, https://nerd-corner.com/wp-content/uploads/2021/06/Datenpaket-300x20.jpg 300w, https://nerd-corner.com/wp-content/uploads/2021/06/Datenpaket-1024x68.jpg 1024w, https://nerd-corner.com/wp-content/uploads/2021/06/Datenpaket-768x51.jpg 768w, https://nerd-corner.com/wp-content/uploads/2021/06/Datenpaket-1536x102.jpg 1536w" sizes="auto, (max-width: 1852px) 100vw, 1852px" /></p>
<p>A so-called generator polynomial is required for the calculation of the CRC. The generator polynomial can be chosen freely, but some polynomials have proven to be particularly suitable. It is important that the <a href="https://users.ece.cmu.edu/~koopman/crc/" target="_blank" rel="noopener">generator polynomial</a> for sending the data and for receiving the data is identical. I have chosen the polynomial 1011 for the sake of simplicity:</p>
<p><img loading="lazy" decoding="async" class="zoooom aligncenter wp-image-919" src="https://nerd-corner.com/wp-content/uploads/2021/06/Generatorpolynom.jpg" alt="Generatorpolynom CRC cyclic redundancy check" width="873" height="115" srcset="https://nerd-corner.com/wp-content/uploads/2021/06/Generatorpolynom.jpg 874w, https://nerd-corner.com/wp-content/uploads/2021/06/Generatorpolynom-300x39.jpg 300w, https://nerd-corner.com/wp-content/uploads/2021/06/Generatorpolynom-768x101.jpg 768w" sizes="auto, (max-width: 873px) 100vw, 873px" /></p>
<p>The procedure is the same for each data packet. An additional CRC value is appended to each data packet that is to be sent. This CRC value is one digit smaller than the generator polynomial. In the current case, the generator polynomial 1011 has a total of 4 digits, which means that the CRC value has 3 digits.  At the beginning of the calculation the CRC value is still unknown, therefore simply 3 times (since the CRC value has here 3 digits) the 0 is appended to the data package. Thus the data packet 10010101 now becomes 10010101000.</p>
<p>Then the polynomial division starts. 10010101000 is divided by 1011. I have summarized the step-by-step process of the division in a graphic. At the end of the polynomial division the remainder 110 remains on the transmitter. This remainder is the desired CRC value. The CRC value is appended to the actual data packet 10010101 and transmitted to the receiver. If the data transmission is correct, the receiver receives the packet 10010101110.</p>
<p><img loading="lazy" decoding="async" class="zoooom aligncenter wp-image-920" src="https://nerd-corner.com/wp-content/uploads/2021/06/CRC_example.png" alt="zyklische Redundanzprüfung Beispiel cyclic redundancy check" width="1044" height="638" srcset="https://nerd-corner.com/wp-content/uploads/2021/06/CRC_example.png 1045w, https://nerd-corner.com/wp-content/uploads/2021/06/CRC_example-300x183.png 300w, https://nerd-corner.com/wp-content/uploads/2021/06/CRC_example-1024x626.png 1024w, https://nerd-corner.com/wp-content/uploads/2021/06/CRC_example-768x470.png 768w" sizes="auto, (max-width: 1044px) 100vw, 1044px" /></p>
<p>At this point the mathematical trick happens. The calculated CRC was appended to the data packet and transmitted to the receiver. The receiver can now also perform a polynomial division with the generator polynomial 1011, but since this time the CRC value 110 was appended to the data packet 10010101 instead of 3 times 0, no remainder is left when the polynomial division is repeated.</p>
<p>No remainder on the receiver side means that the data packet was transmitted correctly. However, if a remainder is left, then an error has crept in during the transmission. The faulty data packet can be discarded and the receiver can ask for a retransmission of the data packet.</p>
<p><iframe loading="lazy" hcb-fetch-image-from="https://www.youtube.com/watch?v=6gbkoFciryA" title="Error Detection and Correction 2: Cyclic Redundancy Check" width="1200" height="675" src="https://www.youtube.com/embed/6gbkoFciryA?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe></p>
<h2>Download files:</h2>
<ul>
<li><a  data-e-Disable-Page-Transition="true" class="download-link" title="" href="https://nerd-corner.com/download/923/?tmstv=1756264922" rel="nofollow" id="download-link-923" data-redirect="false" >
	Cyclic Redundancy Check in C</a>
</li>
</ul>
<p>The post <a href="https://nerd-corner.com/cyclic-redundancy-check-in-c-code/">Cyclic Redundancy Check in C code</a> appeared first on <a href="https://nerd-corner.com">Nerd Corner</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://nerd-corner.com/cyclic-redundancy-check-in-c-code/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
		<item>
		<title>How to program a highly precise timer in C for Linux</title>
		<link>https://nerd-corner.com/how-to-program-a-highly-precise-timer-in-c-for-linux/</link>
					<comments>https://nerd-corner.com/how-to-program-a-highly-precise-timer-in-c-for-linux/#comments</comments>
		
		<dc:creator><![CDATA[Nerds]]></dc:creator>
		<pubDate>Sat, 27 Feb 2021 13:42:40 +0000</pubDate>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[C programming]]></category>
		<category><![CDATA[C Timer]]></category>
		<category><![CDATA[communication technique]]></category>
		<category><![CDATA[data transfer]]></category>
		<category><![CDATA[precise]]></category>
		<category><![CDATA[Raspberry Pi]]></category>
		<category><![CDATA[Timer]]></category>
		<category><![CDATA[Timer in C]]></category>
		<guid isPermaLink="false">https://nerd-corner.com/?p=831</guid>

					<description><![CDATA[<p>I wanted to program a timer in C for my Raspberry Pi, which executes precise work instructions for 1 ms as well as for 0.1 &#8230; </p>
<p>The post <a href="https://nerd-corner.com/how-to-program-a-highly-precise-timer-in-c-for-linux/">How to program a highly precise timer in C for Linux</a> appeared first on <a href="https://nerd-corner.com">Nerd Corner</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>I wanted to program a timer in C for my Raspberry Pi, which executes precise work instructions for 1 ms as well as for 0.1 ms. Unfortunately, since Python is compiled at runtime, Python was way too slow for this purpose. For this reason, the program was written in C. The 1 ms and 0.1 ms were confirmed after programming with the help of an oscilloscope.</p>
<p>In the following is an explanation why the library &#8220;sys/time.h&#8221; was used, as well as a code example with subsequent explanation. This code example is very well suited for bit-wise data transmission in communication technology.</p>
<p><em><strong>This might also be interesting for you:</strong> <a href="https://nerd-corner.com/arduino-timer-interrupts-how-to-program-arduino-registers/" target="_blank" rel="noopener">How to program a highly precise Arduino Timer</a></em></p>
<h2>List of components</h2>
<ul>
<li>Linux operating system (for example Raspberry Pi)</li>
<li>Editor for C &#8211; Programming</li>
</ul>
<p><img loading="lazy" decoding="async" class="aligncenter wp-image-848 zoooom" src="https://nerd-corner.com/wp-content/uploads/2021/02/proxy-image-3-1.jpg" alt="Timer in C" width="484" height="392" srcset="https://nerd-corner.com/wp-content/uploads/2021/02/proxy-image-3-1.jpg 1084w, https://nerd-corner.com/wp-content/uploads/2021/02/proxy-image-3-1-300x243.jpg 300w, https://nerd-corner.com/wp-content/uploads/2021/02/proxy-image-3-1-1024x830.jpg 1024w, https://nerd-corner.com/wp-content/uploads/2021/02/proxy-image-3-1-768x623.jpg 768w" sizes="auto, (max-width: 484px) 100vw, 484px" /></p>
<p>&nbsp;</p>
<h2>The library „sys/time.h“</h2>
<p>Functions like &#8220;usleep()&#8221; or &#8220;nanosleep()&#8221; would stop the complete program. For simple applications this might be sufficient, but for my purposes this was too imprecise. I wanted a timer that really works exactly in a 1 ms or 0.1 ms cycle. So instead of &#8220;usleep()&#8221; or &#8220;nanosleep()&#8221; another solution was chosen. The library &#8220;sys/time.h&#8221;. This library is able to read and compare the current &#8220;System Clock Time&#8221;.</p>
<h2>Code example for a precise timer in C</h2>
<pre class="EnlighterJSRAW" data-enlighter-language="c">#include &lt;sys/time.h&gt;

int main()
{
    struct timeval tval_before, tval_after, tval_result;
    int counter=0;
    bool stop=false;
   
    gettimeofday(&amp;tval_before, NULL);
    while(stop!=true)
    {
        gettimeofday(&amp;tval_after, NULL);
        timersub(&amp;tval_after, &amp;tval_before, &amp;tval_result);
        double time_elapsed = (double)tval_result.tv_sec + ((double)tval_result.tv_usec/1000000.0f);
        
        while(time_elapsed &lt; 0.001)  //1ms; you can change your desired time interval here
        {
            gettimeofday(&amp;tval_after, NULL);
            timersub(&amp;tval_after, &amp;tval_before, &amp;tval_result);
            time_elapsed = (double)tval_result.tv_sec + ((double)tval_result.tv_usec/1000000.0f);
        }
        gettimeofday(&amp;tval_before, NULL);
        
        if (counter==10000)
        {
            stop=true;
        }
            
        else 
        {
            counter++;
        }
    }
    return 0;
}
</pre>
<h2>Explanation of the code example:</h2>
<p>A function &#8220;gettimeofday&#8221; writes the current system time into the variable &#8220;tval_before&#8221;. A While loop will then be executed until the actual task is completed.</p>
<p>Within the While loop the system time is stored again into a variable &#8220;tval_after&#8221;. Then the time difference between &#8220;tval_after&#8221; and &#8220;tval_before&#8221; is measured and stored in &#8220;tval_result&#8221;.</p>
<p>The next step of the timer in C is not immediately obvious: &#8220;tv_result&#8221; consists by definition of 2 parts. On the one hand a seconds part &#8220;.tv_sec&#8221; and on the other hand a microseconds part &#8220;.tv_usec&#8221;. This microsecond part has to be divided by one million to get the value in seconds. Afterwards, the microsecond part can be added to the second part.</p>
<p>The added value is called &#8220;time_elapsed&#8221;. If this value is less than one millisecond, another inner While loop is opened, which recalculates the value for &#8220;time_elapsed&#8221; until exactly 1 ms has passed. Afterwards, the value for &#8220;tval_before&#8221; is redefined using the &#8220;gettimeofday&#8221; function.</p>
<p>Since exactly 1 ms has passed at this point, the timer can now perform its actual operation. In this simple code example the variable &#8220;counter&#8221; is incremented by 1. That means for each interval step (in the code example 1 ms) the counter increases by 1. As soon as a fixed value for counter was reached the program stops. In this case the defined value is 10000. Then the While loop will be terminated. But this part of the code can be easily changed for your own purposes.</p>
<p>My measurements with an oscilloscope detected an exact frequency of 1 ms, even 0.1 ms was measured exactly. This code example is therefore also very suitable for an exact <a href="https://en.wikipedia.org/wiki/Data_transmission" target="_blank" rel="noopener">data transmission</a> in the communication technology.</p>
<h2>Download files</h2>
<ul>
<li><a  data-e-Disable-Page-Transition="true" class="download-link" title="" href="https://nerd-corner.com/download/835/?tmstv=1756264922" rel="nofollow" id="download-link-835" data-redirect="false" >
	Downloadfile Timer in C</a>
</li>
</ul>
<p>The post <a href="https://nerd-corner.com/how-to-program-a-highly-precise-timer-in-c-for-linux/">How to program a highly precise timer in C for Linux</a> appeared first on <a href="https://nerd-corner.com">Nerd Corner</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://nerd-corner.com/how-to-program-a-highly-precise-timer-in-c-for-linux/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
	</channel>
</rss>
