<?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>data transfer Archives - Nerd Corner</title>
	<atom:link href="https://nerd-corner.com/tag/data-transfer/feed/" rel="self" type="application/rss+xml" />
	<link>https://nerd-corner.com/tag/data-transfer/</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>data transfer Archives - Nerd Corner</title>
	<link>https://nerd-corner.com/tag/data-transfer/</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=1756248572" 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>WeMos D1 R2 setup and wifi integration</title>
		<link>https://nerd-corner.com/wemos-d1-r2-setup-and-wifi-integration/</link>
					<comments>https://nerd-corner.com/wemos-d1-r2-setup-and-wifi-integration/#comments</comments>
		
		<dc:creator><![CDATA[Nerds]]></dc:creator>
		<pubDate>Sun, 06 Mar 2022 10:36:53 +0000</pubDate>
				<category><![CDATA[Arduino projects]]></category>
		<category><![CDATA[Hardware]]></category>
		<category><![CDATA[Arduino]]></category>
		<category><![CDATA[Arduino with Wifi]]></category>
		<category><![CDATA[C programming]]></category>
		<category><![CDATA[control Arduino with browser]]></category>
		<category><![CDATA[controller]]></category>
		<category><![CDATA[data transfer]]></category>
		<category><![CDATA[ESP 8266]]></category>
		<category><![CDATA[ESP8266]]></category>
		<category><![CDATA[ESP8266 with mDNS]]></category>
		<category><![CDATA[mDNS]]></category>
		<category><![CDATA[Step by step guide]]></category>
		<category><![CDATA[Webserver]]></category>
		<category><![CDATA[WeMos]]></category>
		<category><![CDATA[WeMos D1]]></category>
		<category><![CDATA[WeMos D1 in Arduino IDE]]></category>
		<category><![CDATA[WeMos D1 mDNS]]></category>
		<category><![CDATA[WeMos D1 mini]]></category>
		<category><![CDATA[WeMos D1 Pinout]]></category>
		<category><![CDATA[WeMos D1 R2]]></category>
		<category><![CDATA[WeMos D1 setup]]></category>
		<category><![CDATA[WeMOS D1 Webserver]]></category>
		<category><![CDATA[WIFI]]></category>
		<category><![CDATA[Wifi Modul]]></category>
		<category><![CDATA[WLAN]]></category>
		<guid isPermaLink="false">https://nerd-corner.com/de/?p=1184</guid>

					<description><![CDATA[<p>I have often thought that it would be extremely convenient to connect my Arduino projects with the Wi-Fi. At some point I randomly came across &#8230; </p>
<p>The post <a href="https://nerd-corner.com/wemos-d1-r2-setup-and-wifi-integration/">WeMos D1 R2 setup and wifi integration</a> appeared first on <a href="https://nerd-corner.com">Nerd Corner</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>I have often thought that it would be extremely convenient to connect my Arduino projects with the Wi-Fi. At some point I randomly came across the WeMos D1 R2 and I am thrilled! In principle, every Arduino project can be realized with the D1 and you always have the possibility to use Wi-Fi. Probably the D1 will even displace the Arduino from the market in the future. In the following I explain the differences between an Arduino UNO R3 and a WeMos D1. Afterwards I describe the setup of the D1 with a simple example. In the example, a browser is used to control an LED, which is built-in on the D1.</p>
<p><em><strong>This might also be interesting for you: </strong><a href="https://nerd-corner.com/arduino-fan-controller/">How to control a fan with an Arduino!</a></em></p>
<h2>List of components</h2>
<ul>
<li>Arduino IDE (development environment)</li>
<li>WeMos D1 R2</li>
</ul>
<h2>Differences between the WeMos D1 R2 and the Arduino UNO R3</h2>
<p>An Arduino UNO R3 looks very similar to the WeMos D1. The main difference is that the D1 has an ESP2866 WIFI module integrated on its board. This gives you the possibility to connect the D1 to the internet. Roughly speaking, the D1 is a wifi-enabled Arduino!</p>
<p><img loading="lazy" decoding="async" class="aligncenter wp-image-1175 zoooom" src="https://nerd-corner.com/wp-content/uploads/2022/01/Wemos-D1-und-Arduino-Uno-scaled.jpg" alt="Wemos D1 and Arduino Uno" width="2000" height="900" srcset="https://nerd-corner.com/wp-content/uploads/2022/01/Wemos-D1-und-Arduino-Uno-scaled.jpg 2560w, https://nerd-corner.com/wp-content/uploads/2022/01/Wemos-D1-und-Arduino-Uno-300x135.jpg 300w, https://nerd-corner.com/wp-content/uploads/2022/01/Wemos-D1-und-Arduino-Uno-1024x461.jpg 1024w, https://nerd-corner.com/wp-content/uploads/2022/01/Wemos-D1-und-Arduino-Uno-768x346.jpg 768w, https://nerd-corner.com/wp-content/uploads/2022/01/Wemos-D1-und-Arduino-Uno-1536x691.jpg 1536w, https://nerd-corner.com/wp-content/uploads/2022/01/Wemos-D1-und-Arduino-Uno-2048x922.jpg 2048w" sizes="auto, (max-width: 2000px) 100vw, 2000px" /></p>
<p>However, you should note that the clock frequencies of the two processors are different. The Arduino has an 8 bit Atmel processor and the D1 a Wi-Fi capable ESP8266 processor with 32 bit clock frequency. The pins of the D1 are arranged similarly to an Arduino UNO, but differ in the operating voltage. On an Arduino UNO the voltage of the pins is 5V and on the D1 it is 3.3V. However, there is also a 5V output on the D1. Because of the voltage differences on the pins you should not blindly plug an Arduino Shield on a D1. It is recommended to check the compatibility first!</p>
<p><img loading="lazy" decoding="async" class="zoooom aligncenter wp-image-1166" src="https://nerd-corner.com/wp-content/uploads/2022/01/Pinout_D1_R2.png" alt="Pinout WeMos D1 R2 Pinbelegung" width="770" height="888" srcset="https://nerd-corner.com/wp-content/uploads/2022/01/Pinout_D1_R2.png 783w, https://nerd-corner.com/wp-content/uploads/2022/01/Pinout_D1_R2-260x300.png 260w, https://nerd-corner.com/wp-content/uploads/2022/01/Pinout_D1_R2-768x886.png 768w" sizes="auto, (max-width: 770px) 100vw, 770px" /></p>
<p>In addition, it should be noted that the D1, unlike the Arduino, has only one analog pin and additionally the number of digital pins is different. The Arduino has 20 digital pins and the D1 only 16 digital pins. However, the digital pins of the D1 have more useful additional functions.</p>
<p>Please note: The designation of the D1 digital pins is also different from those of the Arduino and therefore must be adjusted in software.</p>
<p>Last but not least, the USB connector is also different. In contrast to the Arduino, the D1 has the much more common Micro USB standard.</p>
<p><img loading="lazy" decoding="async" class="aligncenter wp-image-1176 zoooom" src="https://nerd-corner.com/wp-content/uploads/2022/01/Wemos-D1-vs-Arduino-Uno-scaled.jpg" alt="Wemos D1 R2 vs Arduino Uno" width="2000" height="1500" srcset="https://nerd-corner.com/wp-content/uploads/2022/01/Wemos-D1-vs-Arduino-Uno-scaled.jpg 2560w, https://nerd-corner.com/wp-content/uploads/2022/01/Wemos-D1-vs-Arduino-Uno-300x225.jpg 300w, https://nerd-corner.com/wp-content/uploads/2022/01/Wemos-D1-vs-Arduino-Uno-1024x768.jpg 1024w, https://nerd-corner.com/wp-content/uploads/2022/01/Wemos-D1-vs-Arduino-Uno-768x576.jpg 768w, https://nerd-corner.com/wp-content/uploads/2022/01/Wemos-D1-vs-Arduino-Uno-1536x1152.jpg 1536w, https://nerd-corner.com/wp-content/uploads/2022/01/Wemos-D1-vs-Arduino-Uno-2048x1536.jpg 2048w" sizes="auto, (max-width: 2000px) 100vw, 2000px" /></p>
<h2>Setup of the D1</h2>
<p>Open the Arduino IDE and connect the D1 to the computer. Select the appropriate COM port of the D1 under Tools &gt; Port. If the D1 is not recognized here, this is probably due to a missing driver.</p>
<p>If you now select the appropriate COM port and upload an example sketch to the D1 you will probably get the following error message: &#8220;An error occurred while uploading the sketch avrdude: stk500_getsync() attempt 1 of 10: not in sync: resp=0x2e&#8221;. The reason for this error is that the wrong board was selected. The name of the selected board appears in the lower right corner of the IDE:</p>
<p><img loading="lazy" decoding="async" class="aligncenter wp-image-1167 zoooom" src="https://nerd-corner.com/wp-content/uploads/2022/01/WEMOS-choosen-in-IDE.png" alt="WeMos D1 com Port IDE" width="1300" height="55" srcset="https://nerd-corner.com/wp-content/uploads/2022/01/WEMOS-choosen-in-IDE.png 1308w, https://nerd-corner.com/wp-content/uploads/2022/01/WEMOS-choosen-in-IDE-300x13.png 300w, https://nerd-corner.com/wp-content/uploads/2022/01/WEMOS-choosen-in-IDE-1024x43.png 1024w, https://nerd-corner.com/wp-content/uploads/2022/01/WEMOS-choosen-in-IDE-768x32.png 768w" sizes="auto, (max-width: 1300px) 100vw, 1300px" /></p>
<p>To be able to select the D1 the board manager for the ESP8266 must be added. Insert the following link in Preferences &gt; Board Manager: http://arduino.esp8266.com/stable/package_esp8266com_index.json</p>
<p><img loading="lazy" decoding="async" class="aligncenter wp-image-1168 zoooom" src="https://nerd-corner.com/wp-content/uploads/2022/01/preferences-board-manager.png" alt="Preferences boardmananger WeMos D1 R2" width="910" height="703" srcset="https://nerd-corner.com/wp-content/uploads/2022/01/preferences-board-manager.png 921w, https://nerd-corner.com/wp-content/uploads/2022/01/preferences-board-manager-300x232.png 300w, https://nerd-corner.com/wp-content/uploads/2022/01/preferences-board-manager-768x593.png 768w" sizes="auto, (max-width: 910px) 100vw, 910px" /></p>
<p>Then search for ESP8266 in Tools &gt; Board &gt; Board Manager and install the latest version:</p>
<p><img loading="lazy" decoding="async" class="aligncenter wp-image-1169 zoooom" src="https://nerd-corner.com/wp-content/uploads/2022/01/Esp8266-in-boardsmanager.png" alt="WeMos D1 R2 ESP8266 boardmananger in Arduino IDE" width="1200" height="692" srcset="https://nerd-corner.com/wp-content/uploads/2022/01/Esp8266-in-boardsmanager.png 1213w, https://nerd-corner.com/wp-content/uploads/2022/01/Esp8266-in-boardsmanager-300x173.png 300w, https://nerd-corner.com/wp-content/uploads/2022/01/Esp8266-in-boardsmanager-1024x590.png 1024w, https://nerd-corner.com/wp-content/uploads/2022/01/Esp8266-in-boardsmanager-768x443.png 768w" sizes="auto, (max-width: 1200px) 100vw, 1200px" /></p>
<p>Now select in Tools &gt; Board &gt; ESP8266 Boards &gt; LOLIN (WEMOS D1 R2 &amp;mini):</p>
<p><img loading="lazy" decoding="async" class="aligncenter wp-image-1170 zoooom" src="https://nerd-corner.com/wp-content/uploads/2022/01/Choose-ESP8266-board.png" alt="choose WeMos D1 board in Arduino IDE" width="1900" height="999" srcset="https://nerd-corner.com/wp-content/uploads/2022/01/Choose-ESP8266-board.png 1920w, https://nerd-corner.com/wp-content/uploads/2022/01/Choose-ESP8266-board-300x158.png 300w, https://nerd-corner.com/wp-content/uploads/2022/01/Choose-ESP8266-board-1024x539.png 1024w, https://nerd-corner.com/wp-content/uploads/2022/01/Choose-ESP8266-board-768x404.png 768w, https://nerd-corner.com/wp-content/uploads/2022/01/Choose-ESP8266-board-1536x808.png 1536w" sizes="auto, (max-width: 1900px) 100vw, 1900px" /></p>
<p>Now you can select an example project under File &gt; Examples &gt; 01.Basics &gt; Blink and with a click on Upload the whole thing works. The example project now lets the blue LED on the ESP8266 module blink.</p>
<p>Attention: This setup was unique for the first time usage and does not need to be repeated again!</p>
<h2>Connecting WeMos D1 to the Internet</h2>
<p>The program structure corresponds to that of a regular microcontroller as known from the Arduino. There is a setup() function, which is called only once directly at the start of the program and a loop() function, which runs in a continuous loop after the setup().</p>
<p>First the ESP WiFi library is needed. This is included with #include &lt;ESP8266WiFi.h&gt;. As soon as the ESP is powered, a Wi-Fi connection should be established. Therefore the code is written into the setup() function.</p>
<p>We need the serial monitor to display the current state of the connection. The baudrate can be set by the user. For the D1 I use 115200. The serial monitor can be found under Tools &gt; Serial Monitor. In the window of the serial monitor you can set the baudrate. Please select 115200 as well. If you don&#8217;t choose the same baudrate as defined in setup(), you will only see unreadable special characters.</p>
<p>To connect to the Wi-Fi the simple command WiFi.beginn(&#8220;WlanName&#8221;, &#8220;WlanPasswort&#8221;) is sufficient. It can be very helpful to display the IP address of the D1. For this purpose use the command WiFi.localIP()</p>
<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="atomic">#include &lt;ESP8266WiFi.h&gt;

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

}

void loop() {
  // put your main code here, to run repeatedly:

}</pre>
<h2>The D1 as web server and DNS server</h2>
<p>After the previous steps the D1 is connected to the WiFi, but not yet accessible via a browser. So that the D1 can be controlled comfortably over the Browser one needs the #include &lt;ESP8266WebServer.h&gt; library. Then create a web server object with the command ESP8266WebServer server(80) and set the port 80. The server can now be started with the command server.begin(). So that the D1 can check at any time whether requests are to be processed, the server.handleClient() is written into the loop() function.</p>
<p>If you now enter the IP address of the D1 in a browser, from a device that is in the same WiFi, the D1 is already accessible. It is just not specified yet what should happen when the D1 is called.</p>
<p>You can also display predefined functions here. Initially, for example, a sentence if the link has not been defined, or a text for a custom link. It is also possible to execute own functions for certain links.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="atomic">server.onNotFound([](){ 
  server.send(404, "text/plain", "Link was not found!");  
});
 
server.on("/", []() {
  server.send(200, "text/plain", "Landing page!");
});
 
server.on("/custom", []() {
  server.send(200, "text/plain", "Just a custom route!");
  ownFunction();
});</pre>
<p>The web server setup is now complete. Since it is usually very annoying to type the IP address of the D1 into the browser or it can change, it makes sense to assign a name to the DNS of the D1. For this again a library is used, which is included by #include &lt;ESP8266mDNS.h&gt;. The DNS is defined once and therefore written into the setup() function. To define the DNS name, use the method MDNS.begin(DNS Name), for example MDNS.begin(nerd-corner). Additionally MDNS.update() must be written into the loop function.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="atomic">#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.onNotFound([](){ 
    server.send(404, "text/plain", "Link was not found!");  
  });
 
  server.on("/", []() {
    server.send(200, "text/plain", "Landing page!");
  });
 
  server.on("/custom", []() {
    server.send(200, "text/plain", "Just a custom route!");
    ownFunction();
  });

  server.begin();

}

void loop() {
  server.handleClient();
  MDNS.update();

}

void ownFunction(){ //go to "IP-Adress/custom" to call this function
  Serial.println("Own function was called");

}</pre>
<p>Afterwards you can enter http://nerd-corner.local in the browser and you will reach the D1. Please note that the ending must always be &#8220;.local&#8221;! Some Android devices do not support mDNS, then the actual IP address must still be specified. Now you can use it for your own projects!</p>
<p><img loading="lazy" decoding="async" class="aligncenter wp-image-1171 zoooom" src="https://nerd-corner.com/wp-content/uploads/2022/01/custom-route-D1.png" alt="WeMos D1 R2 testing custom route functions" width="1240" height="737" srcset="https://nerd-corner.com/wp-content/uploads/2022/01/custom-route-D1.png 1249w, https://nerd-corner.com/wp-content/uploads/2022/01/custom-route-D1-300x178.png 300w, https://nerd-corner.com/wp-content/uploads/2022/01/custom-route-D1-1024x608.png 1024w, https://nerd-corner.com/wp-content/uploads/2022/01/custom-route-D1-768x456.png 768w, https://nerd-corner.com/wp-content/uploads/2022/01/custom-route-D1-890x530.png 890w, https://nerd-corner.com/wp-content/uploads/2022/01/custom-route-D1-445x265.png 445w" sizes="auto, (max-width: 1240px) 100vw, 1240px" /></p>
<h2>Example program to switch the OnBoard LED on and off</h2>
<p>At the end of this blogpost we have a simple example program where we apply what we have learned so far. We write two additional functions. One to switch on the built-in LED and one to switch it off. A small hint, in contrast to the Arduino the LED is switched on with digitalWrite(LED_BUILTIN, LOW) instead of switched off, so exactly inverted!</p>
<p><img loading="lazy" decoding="async" class="aligncenter wp-image-1177 zoooom" src="https://nerd-corner.com/wp-content/uploads/2022/01/Wemos-D1-R2-build-in-LED-scaled.jpg" alt="Wemos D1 R2 blinking LED" width="2000" height="900" srcset="https://nerd-corner.com/wp-content/uploads/2022/01/Wemos-D1-R2-build-in-LED-scaled.jpg 2560w, https://nerd-corner.com/wp-content/uploads/2022/01/Wemos-D1-R2-build-in-LED-300x135.jpg 300w, https://nerd-corner.com/wp-content/uploads/2022/01/Wemos-D1-R2-build-in-LED-1024x461.jpg 1024w, https://nerd-corner.com/wp-content/uploads/2022/01/Wemos-D1-R2-build-in-LED-768x346.jpg 768w, https://nerd-corner.com/wp-content/uploads/2022/01/Wemos-D1-R2-build-in-LED-1536x691.jpg 1536w, https://nerd-corner.com/wp-content/uploads/2022/01/Wemos-D1-R2-build-in-LED-2048x922.jpg 2048w" sizes="auto, (max-width: 2000px) 100vw, 2000px" /></p>
<p>The functions can be called via http://nerd-corner.local/on and http://nerd-corner.local/off. Here is the complete program code:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="atomic">#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.onNotFound([](){ 
    server.send(404, "text/plain", "Link was not found!");  
  });
 
  server.on("/", []() {
    server.send(200, "text/plain", "Landing page!");
  });
 
  server.on("/on", []() {
    server.send(200, "text/plain", "Switching LED on!");
    switchLedOn();
  });

  server.on("/off", []() {
    server.send(200, "text/plain", "Switching LED off!");
    switchLedOff();
  });

  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>&nbsp;</p>
<p>&nbsp;</p>
<p>The post <a href="https://nerd-corner.com/wemos-d1-r2-setup-and-wifi-integration/">WeMos D1 R2 setup and wifi integration</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-setup-and-wifi-integration/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
		<item>
		<title>Upgrade: Data transfer via VLC and LiFi &#8211; Pi to Pi transfer</title>
		<link>https://nerd-corner.com/upgrade-data-transfer-via-vlc-and-lifi-pi-to-pi-transfer/</link>
					<comments>https://nerd-corner.com/upgrade-data-transfer-via-vlc-and-lifi-pi-to-pi-transfer/#comments</comments>
		
		<dc:creator><![CDATA[Nerds]]></dc:creator>
		<pubDate>Thu, 08 Jul 2021 14:27:07 +0000</pubDate>
				<category><![CDATA[Handcrafted]]></category>
		<category><![CDATA[Hardware]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[3D print]]></category>
		<category><![CDATA[CAD]]></category>
		<category><![CDATA[CAD design]]></category>
		<category><![CDATA[cyclic redundancy check]]></category>
		<category><![CDATA[data transfer]]></category>
		<category><![CDATA[fan]]></category>
		<category><![CDATA[LiFi]]></category>
		<category><![CDATA[LiFI Router]]></category>
		<category><![CDATA[Light Fidelity]]></category>
		<category><![CDATA[LM393]]></category>
		<category><![CDATA[pi]]></category>
		<category><![CDATA[precise]]></category>
		<category><![CDATA[Raspberry Pi]]></category>
		<category><![CDATA[Timer]]></category>
		<category><![CDATA[visible Light communication]]></category>
		<category><![CDATA[visible light communication project]]></category>
		<category><![CDATA[VLC Raspberry Pi project]]></category>
		<guid isPermaLink="false">https://nerd-corner.com/de/?p=985</guid>

					<description><![CDATA[<p>After I already transferred simple text messages via light signals from a Raspberry Pi to an Arduino Uno ( Link: https://nerd-corner.com/sending-text-messages-with-visible-light-communication/ ), I wanted to &#8230; </p>
<p>The post <a href="https://nerd-corner.com/upgrade-data-transfer-via-vlc-and-lifi-pi-to-pi-transfer/">Upgrade: Data transfer via VLC and LiFi &#8211; Pi to Pi transfer</a> appeared first on <a href="https://nerd-corner.com">Nerd Corner</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>After I already transferred simple text messages via light signals from a Raspberry Pi to an Arduino Uno ( Link: <a href="https://nerd-corner.com/sending-text-messages-with-visible-light-communication/" target="_blank" rel="noopener">https://nerd-corner.com/sending-text-messages-with-visible-light-communication/</a> ), I wanted to improve this system to be able to transfer any file formats in both directions. Since a Raspberry Pi uses Linux, this system can be transferred to any Linux device for data transfer via VLC (visual light communication). The software code and STL files can be downloaded at the end of the blog entry.</p>
<p><em><strong>This might also be interesting for you:</strong> <a href="https://nerd-corner.com/sending-text-messages-with-visible-light-communication/" target="_blank" rel="noopener">Sending text messages with light signals</a></em></p>
<p><em><strong>Closely related to this article:</strong> <a href="https://nerd-corner.com/cyclic-redundancy-check-in-c-code/" target="_blank" rel="noopener">Error detection during data transmission</a></em></p>
<p><em><strong>Also relevant:</strong> <a href="https://nerd-corner.com/how-to-program-a-highly-precise-timer-in-c-for-linux/" target="_blank" rel="noopener">Precise Timer in C for data transfer</a></em></p>
<h2>List of components</h2>
<ul>
<li>2x Raspberry Pi 4</li>
<li>2x 5V Solar cell</li>
<li>2x 5V Laser diode</li>
<li>Jumper cables</li>
<li>3D printer</li>
<li>Filament</li>
<li>2x 5V Fan</li>
<li>NPN Transistor</li>
<li>2x ADC Board with a LM393 dual comparator</li>
</ul>
<p><img loading="lazy" decoding="async" class="zoooom aligncenter wp-image-966" src="https://nerd-corner.com/wp-content/uploads/2021/07/Assembly-parts-scaled.jpg" alt="Datenübertragung mit Licht Einzelteile Data transfer via VLC" width="2500" height="1681" srcset="https://nerd-corner.com/wp-content/uploads/2021/07/Assembly-parts-scaled.jpg 2560w, https://nerd-corner.com/wp-content/uploads/2021/07/Assembly-parts-300x202.jpg 300w, https://nerd-corner.com/wp-content/uploads/2021/07/Assembly-parts-1024x689.jpg 1024w, https://nerd-corner.com/wp-content/uploads/2021/07/Assembly-parts-768x516.jpg 768w, https://nerd-corner.com/wp-content/uploads/2021/07/Assembly-parts-1536x1033.jpg 1536w, https://nerd-corner.com/wp-content/uploads/2021/07/Assembly-parts-2048x1377.jpg 2048w" sizes="auto, (max-width: 2500px) 100vw, 2500px" /></p>
<h2>Wiring</h2>
<p>Unlike the wiring in the last article, where text messages were sent from a Pi to an Arduino via light signals ( Link: <a href="https://nerd-corner.com/sending-text-messages-with-visible-light-communication/" target="_blank" rel="noopener">https://nerd-corner.com/sending-text-messages-with-visible-light-communication/</a> ), there is now no defined receiver and no defined transmitter. Instead, two identical stations are set up that can both send and receive data.</p>
<p><img loading="lazy" decoding="async" class="aligncenter wp-image-965 zoooom" src="https://nerd-corner.com/wp-content/uploads/2021/07/FritzingPiTransceiver_Steckplatine.png" alt="Fritzing Pi VLC LiFI" width="2250" height="1459" srcset="https://nerd-corner.com/wp-content/uploads/2021/07/FritzingPiTransceiver_Steckplatine.png 2270w, https://nerd-corner.com/wp-content/uploads/2021/07/FritzingPiTransceiver_Steckplatine-300x195.png 300w, https://nerd-corner.com/wp-content/uploads/2021/07/FritzingPiTransceiver_Steckplatine-1024x664.png 1024w, https://nerd-corner.com/wp-content/uploads/2021/07/FritzingPiTransceiver_Steckplatine-768x498.png 768w, https://nerd-corner.com/wp-content/uploads/2021/07/FritzingPiTransceiver_Steckplatine-1536x996.png 1536w, https://nerd-corner.com/wp-content/uploads/2021/07/FritzingPiTransceiver_Steckplatine-2048x1328.png 2048w" sizes="auto, (max-width: 2250px) 100vw, 2250px" /></p>
<p>For this reason, the Raspberry Pi&#8217;s are connected to both a 5V laser and a 5V solar cell. For the laser the GPIO18 pin was chosen, which corresponds to pin 1 in the &#8220;wiringPi&#8221; library. The &#8220;wiringPi&#8221; library is used in the program code. Directly below GPIO18 is a ground, which is connected to the negative pole of the laser.</p>
<p><img loading="lazy" decoding="async" class="aligncenter wp-image-967 zoooom" src="https://nerd-corner.com/wp-content/uploads/2021/07/GPIO.png" alt="Datenübertragung mittels Licht Pi" width="1500" height="1125" srcset="https://nerd-corner.com/wp-content/uploads/2021/07/GPIO.png 1600w, https://nerd-corner.com/wp-content/uploads/2021/07/GPIO-300x225.png 300w, https://nerd-corner.com/wp-content/uploads/2021/07/GPIO-1024x768.png 1024w, https://nerd-corner.com/wp-content/uploads/2021/07/GPIO-768x576.png 768w, https://nerd-corner.com/wp-content/uploads/2021/07/GPIO-1536x1152.png 1536w" sizes="auto, (max-width: 1500px) 100vw, 1500px" /></p>
<p>The solar cell provides a corresponding voltage value depending on the light intensity. But since the digital pins of the Raspberry Pi can only recognize 1 and 0, the analog value of the solar cell must be converted into a digital value with the help of a comparator. The process is explained in more detail in the section &#8220;ADC Board with an LM393 Comparator&#8221;. For the wiring, the plus and minus pole of the solar cell is connected to the plus and minus contacts of the ADC board. Then the ground of the ADC board is connected to a Raspberry Pi ground and for the power supply the plus pole of the ADC board is connected to 5V of the Pi. The D0 pin of the ADC board provides the digital value 0 or 1, depending on the light intensity of the solar cell. I connected this pin to GPIO17, which corresponds to pin 0 in the &#8220;wiringPi&#8221; library.</p>
<p>Since I noticed that the Pi gets very hot during operation, I connected a fan. So that the fan is not permanently in operation, which would have a negative effect on the performance of the laser, the fan can be switched on and off by a NPN transistor. Connect the positive pole of the fan directly to a 5V pin of the Raspberry Pi and the negative pole of the fan to the emitter of the NPN transistor. The collector of the transistor is connected to a ground of the Pi. To switch the fan on and off via the transistor, the transistor base is connected to a GPIO pin. For example I chose GPIO27 (corresponds to pin 2 in the &#8220;wiringPi&#8221; library). In the following table the pins of the Pi are compared to the numbering of the &#8220;wiringPi&#8221; library.</p>
<p><img loading="lazy" decoding="async" class="aligncenter wp-image-940 zoooom" src="https://nerd-corner.com/wp-content/uploads/2021/06/pinbelegung-Pi.png" alt="VLC Textnachrichten visuelle Lichtkommunikation wiringPi Datenübertragung mit Licht" width="599" height="295" srcset="https://nerd-corner.com/wp-content/uploads/2021/06/pinbelegung-Pi.png 600w, https://nerd-corner.com/wp-content/uploads/2021/06/pinbelegung-Pi-300x148.png 300w" sizes="auto, (max-width: 599px) 100vw, 599px" /></p>
<h2>ADC Board with a LM393 camparator</h2>
<p><img loading="lazy" decoding="async" class="aligncenter wp-image-968 zoooom" src="https://nerd-corner.com/wp-content/uploads/2021/07/ADC-LM393.jpg" alt="ADC Board mit LM393 Komparator" width="450" height="319" srcset="https://nerd-corner.com/wp-content/uploads/2021/07/ADC-LM393.jpg 771w, https://nerd-corner.com/wp-content/uploads/2021/07/ADC-LM393-300x213.jpg 300w, https://nerd-corner.com/wp-content/uploads/2021/07/ADC-LM393-768x545.jpg 768w, https://nerd-corner.com/wp-content/uploads/2021/07/ADC-LM393-120x85.jpg 120w" sizes="auto, (max-width: 450px) 100vw, 450px" /></p>
<p>The solar cell returns a voltage value depending on the light intensity. Unfortunately the Raspberry Pi has no analog pins to read this voltage value. Therefore the analog signal has to be converted into a digital signal. This is possible with the help of the LM393 comparator. The comparator is often used on ADC boards. Here I simply replaced the original sensor (it was a photoresistor) through a solar cell. With the help of a potentiometer the comparator can be adjusted. That means, as soon as the voltage value of the solar cell, which depends on the light intensity, exceeds the adjusted value of the potentiometer, a digital 1 is measured, otherwise a digital 0.</p>
<h2>Structure of the data frame</h2>
<p><img loading="lazy" decoding="async" class="zoooom aligncenter wp-image-969" src="https://nerd-corner.com/wp-content/uploads/2021/07/MultipleFrames-1.jpg" alt="Aufbau Datenframe Lichtübertragung Daten Datentransfer mittels VLC" width="1300" height="469" srcset="https://nerd-corner.com/wp-content/uploads/2021/07/MultipleFrames-1.jpg 1409w, https://nerd-corner.com/wp-content/uploads/2021/07/MultipleFrames-1-300x108.jpg 300w, https://nerd-corner.com/wp-content/uploads/2021/07/MultipleFrames-1-1024x369.jpg 1024w, https://nerd-corner.com/wp-content/uploads/2021/07/MultipleFrames-1-768x277.jpg 768w" sizes="auto, (max-width: 1300px) 100vw, 1300px" /></p>
<p>The data frame for sending the text messages using visual light communication (Link: <a href="https://nerd-corner.com/sending-text-messages-with-visible-light-communication/" target="_blank" rel="noopener">https://nerd-corner.com/sending-text-messages-with-visible-light-communication/ </a>) consisted of a preamble, the length of the text message, the text content and the cyclic redundancy check. However, to be able to send all kinds of data instead of text messages, the file name, the file extension, the total number of packets and the number of the current packet must be specified instead of the length of the text message. Then the data content and the code of the cyclic redundancy check can be added.</p>
<h2>Software code for data transfer via VLC</h2>
<p>Basically, the receiver and sender scripts from the previous project on sending text messages using visual light communication (link: https://nerd-corner.com/de/textnachrichten-mittels-lichtsignale-senden-pi-zu-arduino/ ) were further developed and combined into one single script. Which is applied on both Raspberry Pi&#8217;s. For example, a &#8220;ReadFile&#8221; and &#8220;WriteFile&#8221; function was added, which can read files and combine and save received data packets to a file. The program was written in C again, because a high speed of data transfer should be achieved. Details about precise programming in C for fast data transfer are given in this article: <a href="https://nerd-corner.com/how-to-program-a-highly-precise-timer-in-c-for-linux/" target="_blank" rel="noopener">https://nerd-corner.com/how-to-program-a-highly-precise-timer-in-c-for-linux/</a> .</p>
<p>The complete software code for data transfer using VLC can be downloaded at the end of the article. The core of the program is again a state machine with the help of which can be selected whether data should be sent or received. In addition, the program automatically turns on the fan when no data transfer is taking place. Important: When compiling please do not forget the &#8220;wiringPi&#8221; library and the &#8220;math.h&#8221; library! The command is: &#8220;gcc -o transceiver transceiver.c -lwiringPi -lm&#8221;.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c" data-enlighter-group="transceiver" data-enlighter-title="transceiver.c">while(1)
    {
        digitalWrite (2, HIGH);
        printf("Press the R button for Receiver Mode or any other key for Sender Moden");
        scanf(" %c",&amp;mode);
        
        if (mode=='R'||mode=='r')
        {
            digitalWrite(2,LOW);
            modeReceiver=true;
        }
        
        if (mode!='R'&amp;&amp;mode!='r')
        {
            digitalWrite(2,LOW);
            modeReceiver=false;
            
            char dataName[NAME_MAX];
            char dataExtension[NAME_MAX];
            
               
            printf("n Name of file WITHOUT extension: ");
            scanf("%s",dataName);

            printf("n Extension: ");
            scanf("%s",dataExtension);

            if (read_file(dataName, dataExtension, file_content) != OK)
            {
                printf("File read error, size exceeds array sizen");
                return -1;
            }
            BuildDataFrame(dataName, dataExtension, file_content);
        }
        
        
        
        while(modeReceiver)
        {
            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)
            {
                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);
            
            int data = digitalRead(0);
            
            
            switch (state)
            {
                case 0:
                    //looking for preamble pattern
                    synchro_Done=false;
                    LookForSynchro(data);
                    
                    if (synchro_Done==true)
                    {
                        state=1;
                    }
                    break;
                    
                case 1:
                    //receive the actual data
                    receiveData_Done=false;
                    senderState=false;
                    ReceiveData(data);
                    
                    if(receiveData_Done&amp;&amp;senderState==false)
                    {
                        state=0;
                    }
                    if(senderState==true){
                        senderState=false;
                        state=0;
                        modeReceiver=false;
                        }
                    break;
                  
            }
            
        }
    }</pre>
<h2>Housing</h2>
<p>In order to be able to hold the components in place, a housing was constructed in CAD. This also has the advantage that no complicated alignment of the lasers and the solar cells is necessary for the data transmission. For beginners, <a href="https://www.tinkercad.com/" target="_blank" rel="noopener">TinkerCAD</a> is suitable for housing design. TinkerCAD is free and can be used directly in the browser. Alternatively, the STL files for the 3D printer can also be downloaded here.</p>
<p><img loading="lazy" decoding="async" class="zoooom aligncenter wp-image-970" src="https://nerd-corner.com/wp-content/uploads/2021/07/gehaeuse-CAD.jpg" alt="Datenübertragung per Licht Gehäuse Datentransfer mittels VLC" width="600" height="530" srcset="https://nerd-corner.com/wp-content/uploads/2021/07/gehaeuse-CAD.jpg 682w, https://nerd-corner.com/wp-content/uploads/2021/07/gehaeuse-CAD-300x265.jpg 300w" sizes="auto, (max-width: 600px) 100vw, 600px" /></p>
<p>The housing for data transfer via VLC has an opening for the solar cell and the laser. In addition, an exhaust vent for the fan was constructed and space was also left free for a Raspberry Pi housing. The following picture shows the installation of the components.</p>
<p><img loading="lazy" decoding="async" class="aligncenter wp-image-971 zoooom" src="https://nerd-corner.com/wp-content/uploads/2021/07/assembly-end-scaled.jpg" alt="Datenübertragung per Licht Lifi VLC" width="2500" height="1875" srcset="https://nerd-corner.com/wp-content/uploads/2021/07/assembly-end-scaled.jpg 2560w, https://nerd-corner.com/wp-content/uploads/2021/07/assembly-end-300x225.jpg 300w, https://nerd-corner.com/wp-content/uploads/2021/07/assembly-end-1024x768.jpg 1024w, https://nerd-corner.com/wp-content/uploads/2021/07/assembly-end-768x576.jpg 768w, https://nerd-corner.com/wp-content/uploads/2021/07/assembly-end-1536x1152.jpg 1536w, https://nerd-corner.com/wp-content/uploads/2021/07/assembly-end-2048x1536.jpg 2048w" sizes="auto, (max-width: 2500px) 100vw, 2500px" /></p>
<h2>Conclusion about data transfer via VLC</h2>
<p>The existing system for sending text messages by means of visual light signals was further developed so that all types of data can now be sent and received. The system works amazingly successfully. It is very robust and achieves a data rate of 1 kbps to 10 kbps. All incoming data packets can be directly assigned due to the intelligent structure of the data frame. Only an acknowledgement signal would be a useful addition. Such a signal would be a feedback from the receiver to the sender to inform the sender that all packets have arrived, or possibly a certain packet was faulty and must be sent again.</p>
<p>Also interesting for the future would be to investigate other types of modulation. In particular, I would like to explore color shift keying, which is specifically designed for visual light communication, and compare the resulting data rates.</p>
<p><img loading="lazy" decoding="async" class="zoooom aligncenter wp-image-972" src="https://nerd-corner.com/wp-content/uploads/2021/07/Assembly-finish-scaled.jpg" alt="Datenübertragung per Licht Gehäuse Lifi VLC Datentransfer mittels VLC" width="2500" height="1125" srcset="https://nerd-corner.com/wp-content/uploads/2021/07/Assembly-finish-scaled.jpg 2560w, https://nerd-corner.com/wp-content/uploads/2021/07/Assembly-finish-300x135.jpg 300w, https://nerd-corner.com/wp-content/uploads/2021/07/Assembly-finish-1024x461.jpg 1024w, https://nerd-corner.com/wp-content/uploads/2021/07/Assembly-finish-768x346.jpg 768w, https://nerd-corner.com/wp-content/uploads/2021/07/Assembly-finish-1536x691.jpg 1536w, https://nerd-corner.com/wp-content/uploads/2021/07/Assembly-finish-2048x922.jpg 2048w" sizes="auto, (max-width: 2500px) 100vw, 2500px" /></p>
<h2>Download files:</h2>
<ul>
<li><a  data-e-Disable-Page-Transition="true" class="download-link" title="" href="https://nerd-corner.com/download/977/?tmstv=1756248572" rel="nofollow" id="download-link-977" data-redirect="false" >
	Softwarecode Transceiver</a>
</li>
<li><a href="https://cults3d.com/en/3d-model/various/visible-light-communication-pi-housing" target="_blank" rel="noopener">STL files housing</a></li>
<li><a  data-e-Disable-Page-Transition="true" class="download-link" title="" href="https://nerd-corner.com/download/991/?tmstv=1756248572" rel="nofollow" id="download-link-991" data-redirect="false" >
	Pi Case with free pins (Creative Common License from Thingiverse)</a>
</li>
</ul>
<p>&nbsp;</p>
<p>The post <a href="https://nerd-corner.com/upgrade-data-transfer-via-vlc-and-lifi-pi-to-pi-transfer/">Upgrade: Data transfer via VLC and LiFi &#8211; Pi to Pi transfer</a> appeared first on <a href="https://nerd-corner.com">Nerd Corner</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://nerd-corner.com/upgrade-data-transfer-via-vlc-and-lifi-pi-to-pi-transfer/feed/</wfw:commentRss>
			<slash:comments>8</slash:comments>
		
		
			</item>
		<item>
		<title>Sending text messages with visible light communication or LiFi &#8211; Pi to Arduino</title>
		<link>https://nerd-corner.com/sending-text-messages-with-visible-light-communication/</link>
					<comments>https://nerd-corner.com/sending-text-messages-with-visible-light-communication/#comments</comments>
		
		<dc:creator><![CDATA[Nerds]]></dc:creator>
		<pubDate>Sun, 20 Jun 2021 23:31:03 +0000</pubDate>
				<category><![CDATA[Arduino projects]]></category>
		<category><![CDATA[Hardware]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[cyclic redundancy check]]></category>
		<category><![CDATA[data transfer]]></category>
		<category><![CDATA[data transfer with light]]></category>
		<category><![CDATA[LiFi]]></category>
		<category><![CDATA[LiFi Arduino project]]></category>
		<category><![CDATA[LiFI Router]]></category>
		<category><![CDATA[Light Fidelity]]></category>
		<category><![CDATA[text messages]]></category>
		<category><![CDATA[text messages from Pi to Arduino]]></category>
		<category><![CDATA[visible Light communication]]></category>
		<category><![CDATA[visible light communication project]]></category>
		<category><![CDATA[VLC]]></category>
		<category><![CDATA[VLC Arduino project]]></category>
		<category><![CDATA[VLC Raspberry Pi project]]></category>
		<guid isPermaLink="false">https://nerd-corner.com/de/?p=962</guid>

					<description><![CDATA[<p>I like the idea of sending data via visible light. In general, Li-Fi (Light Fidelity) and VLC (Visible Light Communication) are heavily discussed at the &#8230; </p>
<p>The post <a href="https://nerd-corner.com/sending-text-messages-with-visible-light-communication/">Sending text messages with visible light communication or LiFi &#8211; Pi to Arduino</a> appeared first on <a href="https://nerd-corner.com">Nerd Corner</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>I like the idea of sending data via visible light. In general, Li-Fi (Light Fidelity) and VLC (Visible Light Communication) are heavily discussed at the moment. VLC is a visible light communication, which means it is primarily a cable replacement, while LiFi is a similarity to WiFi. A LiFi device would be a kind of Internet-enabled router that transmits data via light signals instead of radio frequency signals. Currently, however, no company has developed or is selling a Li-Fi router. VLC technology is used prominently at the moment by Elon Musk&#8217;s StarLink project. This allows satellites to exchange data using laser beams. I have replicated that in a simplified way in this blog article.</p>
<p>At first, I only want to transmit individual text messages in one direction. In a next step this can then be extended to transmit whole files bidirectionally. To receive the messages, I chose an Ardunio, because unlike the Raspberry Pi, it has analog inputs and can thus be easily connected to analog sensors. For sending, a Raspberry Pi is suitable, since only digital pins are needed here and a Pi can generally transmit data more efficiently and faster. This has already been tested in the previous posts about defining a clock frequency for the Arduino and the Raspberry Pi. All scripts to run the VLC communication can be downloaded at the end of the article. The following three blogposts are directly related to this project:</p>
<p><span style="color: #ff0000;"><strong>===&gt; UPGRADE: </strong></span><em><a href="https://nerd-corner.com/upgrade-data-transfer-via-vlc-and-lifi-pi-to-pi-transfer/" target="_blank" rel="noopener">SENDING ALL KINDS OF DATA WITH VISIBLE LIGHT COMMUNICATION</a></em></p>
<p><em><strong>How to program the clock frequency of the Arduino:</strong> <a href="https://nerd-corner.com/arduino-timer-interrupts-how-to-program-arduino-registers/">Use timer interrupts as clock frequency.</a></em></p>
<p><em><strong>How to program the clock frequency of the Pi:</strong> <a href="https://nerd-corner.com/how-to-program-a-highly-precise-timer-in-c-for-linux/">Precise timer function in C for the Pi.</a></em></p>
<p><em><strong>How to prevent errors in data transmission:</strong> <a href="https://nerd-corner.com/cyclic-redundancy-check-in-c-code/">Using CRC to detect corrupted packets.</a></em></p>
<h2>List of components</h2>
<ul>
<li>Arduino Uno</li>
<li>Raspberry Pi</li>
<li>5V Solarcell</li>
<li>5V Laserdiode</li>
<li>Jumper cables</li>
</ul>
<h2>Wiring for the visible light communication</h2>
<p><img loading="lazy" decoding="async" class="zoooom aligncenter wp-image-939" src="https://nerd-corner.com/wp-content/uploads/2021/06/FritzingPi_Arduino.png" alt="Textnachrichten VLC Fritzing Verkabelung für Visuelle Lichtkommunikation" width="2770" height="1130" srcset="https://nerd-corner.com/wp-content/uploads/2021/06/FritzingPi_Arduino.png 2772w, https://nerd-corner.com/wp-content/uploads/2021/06/FritzingPi_Arduino-300x122.png 300w, https://nerd-corner.com/wp-content/uploads/2021/06/FritzingPi_Arduino-1024x418.png 1024w, https://nerd-corner.com/wp-content/uploads/2021/06/FritzingPi_Arduino-768x313.png 768w, https://nerd-corner.com/wp-content/uploads/2021/06/FritzingPi_Arduino-1536x627.png 1536w, https://nerd-corner.com/wp-content/uploads/2021/06/FritzingPi_Arduino-2048x836.png 2048w" sizes="auto, (max-width: 2770px) 100vw, 2770px" /></p>
<p>The wiring is rather simple. The 5V solar cell is very well suited to detect light signals. Therefore the solar cell is connected to the receiver, which is the Arduino. The ground of the solar cell is connected to a ground of the Arduino. The positive line of the solar cell is connected to an analog pin of the Arduino. Here, for example, A0 is suitable.</p>
<p>From the Raspberry Pi the messages are transmitted to the Arduino via light signals of the laser. For this, the ground of a 5V laser diode is connected to a ground pin of the Pi (see graphic). Then the positive line of the laser diode is connected to one of the digital pins of the Raspberry Pi. Here I chose the GPIO17 pin, which corresponds to pin 0 in the &#8220;wiringPi&#8221; library (see graphic).</p>
<p><img loading="lazy" decoding="async" class="aligncenter wp-image-940 zoooom" src="https://nerd-corner.com/wp-content/uploads/2021/06/pinbelegung-Pi.png" alt="VLC Textnachrichten visuelle Lichtkommunikation wiringPi" width="598" height="294" srcset="https://nerd-corner.com/wp-content/uploads/2021/06/pinbelegung-Pi.png 600w, https://nerd-corner.com/wp-content/uploads/2021/06/pinbelegung-Pi-300x148.png 300w" sizes="auto, (max-width: 598px) 100vw, 598px" /></p>
<p>As an alternative to the laser and the solar cell combination, an LED and a photoresistor can also be used. However, a laser is better suited for faster and more precise data transmission. In addition, a solar cell offers a large area to detect the laser beams.</p>
<h2>Modulationtechnique On-Off-Keying</h2>
<p>The text messages are transmitted in binary code as &#8220;1&#8221; or &#8220;0&#8221;. There are different ways to modulate this data. One of the simplest possibilities is to set an identical clock frequency for the transmitter and receiver. Then either a &#8220;1&#8221; or a &#8220;0&#8221; is transmitted in each clock pulse.</p>
<p>The procedure is called aplitude shift keying. If the laser diode illuminates the solar plate particularly strongly, this is detected by the receiver as a binary &#8220;1&#8221;. If the laser diode shines only weakly, it will be detected as binary &#8220;0&#8221;.</p>
<p>In fact it makes sense not to let the laser diode shine at all for a binary &#8220;0&#8221;. This is called On-Off keying. This is a simplification of amplitude shift keying. The following graphic illustrates the difference between amplitude shift keying and on-off keying.</p>
<p><img loading="lazy" decoding="async" class="aligncenter wp-image-942 zoooom" src="https://nerd-corner.com/wp-content/uploads/2021/06/OOK-ASK-1.jpg" alt="Modulationstechnik Visuelle Lichtkommunikation" width="848" height="587" srcset="https://nerd-corner.com/wp-content/uploads/2021/06/OOK-ASK-1.jpg 850w, https://nerd-corner.com/wp-content/uploads/2021/06/OOK-ASK-1-300x208.jpg 300w, https://nerd-corner.com/wp-content/uploads/2021/06/OOK-ASK-1-768x531.jpg 768w" sizes="auto, (max-width: 848px) 100vw, 848px" /></p>
<p>That means, whenever the solar cell detects light, a corresponding voltage value is passed to the analog pin of the Arduino. If this voltage value exceeds a predefined value, the Arduino registers it as a binary &#8220;1&#8221;, otherwise as a binary &#8220;0&#8221;. It makes sense to adjust the predefined value to the daylight. Possibly with the help of an additional light sensor.</p>
<h2>Arduino code to receive messages</h2>
<pre class="EnlighterJSRAW" data-enlighter-language="c" data-enlighter-group="Receiver" data-enlighter-title="Receiver.ino" data-enlighter-theme="beyond">//This is the "real" loop function
  switch (state)
  {
    case 0:
      //looking for synchronization sequence
      synchro_Done=false;
      lookForSynchro(data);

      if (synchro_Done== true)
      {
        state=1;
      }
      break;
    case 1:
      //receive Data
      receiveData_Done =false;
      receiveData(data);

      if (receiveData_Done==true)
      {
        state=0; 
      }
      break;
  }</pre>
<p>The software of the Arduino is basically built as a state machine. There are two states. One state for synchronization and one for reading the text message. In the state synchronization the receiver waits for a fixed bit sequence (preamble) for example &#8220;101010101111111111&#8221;. This sequence means that the receiver must listen now because a text message follows. The complete Arduino code can be downloaded at the end of the article. The following graphic shows the exact structure of the data packets.</p>
<p><img loading="lazy" decoding="async" class="aligncenter wp-image-943 zoooom" src="https://nerd-corner.com/wp-content/uploads/2021/06/FrameVariableDataLength.jpg" alt="Aufbau Textnachrichten VLC" width="953" height="554" srcset="https://nerd-corner.com/wp-content/uploads/2021/06/FrameVariableDataLength.jpg 955w, https://nerd-corner.com/wp-content/uploads/2021/06/FrameVariableDataLength-300x174.jpg 300w, https://nerd-corner.com/wp-content/uploads/2021/06/FrameVariableDataLength-768x446.jpg 768w" sizes="auto, (max-width: 953px) 100vw, 953px" /></p>
<p>As soon as the preamble is recognized, the receiver automatically switches to the second state and receives the actual message. However, the first 16 bits of the text message correspond to a decimal number, which tells the receiver how many characters the incoming text message contains. When this number is reached, the text is printed in the Serial Monitor of the Arduino and the state changes back again. Now the synchronization sequence is awaited again. The complete software code can be downloaded at the end of the blog article. To improve the transmission quality, a cyclic redundancy check (CRC) can be performed as shown in the following graphic.</p>
<p><img loading="lazy" decoding="async" class="aligncenter wp-image-944 zoooom" src="https://nerd-corner.com/wp-content/uploads/2021/06/FrameVariableDataLengthAndCRC.jpg" alt="CRC Datenpakete Aufbau VLC" width="1264" height="555" srcset="https://nerd-corner.com/wp-content/uploads/2021/06/FrameVariableDataLengthAndCRC.jpg 1266w, https://nerd-corner.com/wp-content/uploads/2021/06/FrameVariableDataLengthAndCRC-300x132.jpg 300w, https://nerd-corner.com/wp-content/uploads/2021/06/FrameVariableDataLengthAndCRC-1024x450.jpg 1024w, https://nerd-corner.com/wp-content/uploads/2021/06/FrameVariableDataLengthAndCRC-768x337.jpg 768w" sizes="auto, (max-width: 1264px) 100vw, 1264px" /></p>
<h2>Raspberry Pi code to send messages</h2>
<pre class="EnlighterJSRAW" data-enlighter-language="c" data-enlighter-group="simpleLaser" data-enlighter-title="simpleLaser.c">//Read message
        char msg[3000]; 
        int len, k, length;
       
        printf("n Enter the Message: ");
        scanf("%[^'n']",msg);
        
        len=strlen(msg);
        
        
        int2bin(len*8, 16); //len*8, because 8 bits are one byte
        
        for(k=0;k&lt;len;k++)
        {
                chartobin(msg[k]);            
        }</pre>
<p>The program code of the Raspberry Pi must be written in C. Python would be too slow and would not achieve a stable clock frequency for sending the data. The complete program code can be downloaded at the end of the article. In order for the Pi to control the laser diode the &#8220;wiringPi.h&#8221; library is needed. With &#8220;digitalWrite(0, HIGH)&#8221; the diode can be switched on and with &#8220;digitalWrite(0, LOW)&#8221; it can be switched off. Important: Please don&#8217;t forget the &#8220;<a href="http://wiringpi.com/" target="_blank" rel="noopener">wiringPi</a>&#8221; library when compiling! The command is: &#8220;gcc -o simpleLaser simpleLaser.c -lwiringPi&#8221;.</p>
<p>At the beginning the program asks for a text message using the printf function. This is read in and stored by a scanf function. Then each letter is converted to binary code and stored in an array. The conversion is shown in the following graphic. At the end, the binary code is transmitted from the array in a fixed clock pulse. The laser diode is switched on for a binary &#8220;1&#8221; and off again for a binary &#8220;0&#8221;.</p>
<p><img loading="lazy" decoding="async" class="aligncenter wp-image-945 zoooom" src="https://nerd-corner.com/wp-content/uploads/2021/06/BinToDec.jpg" alt="Character to binary conversion" width="750" height="389" srcset="https://nerd-corner.com/wp-content/uploads/2021/06/BinToDec.jpg 758w, https://nerd-corner.com/wp-content/uploads/2021/06/BinToDec-300x156.jpg 300w" sizes="auto, (max-width: 750px) 100vw, 750px" /></p>
<h2>Conclusion on the Visible Light Communication for Arduino</h2>
<p>The transmission of text messages by means of visual light communication works excellently. Thanks to the cyclic redundancy check, it is also absolutely error-free. However, the benefit of one-dimensional message communication is rather low. It would be better if any kind of files could be transmitted and that in both directions. An update will probably follow soon.</p>
<p><img loading="lazy" decoding="async" class="zoooom aligncenter wp-image-953" src="https://nerd-corner.com/wp-content/uploads/2021/06/SetUp-Arduino-Pi-scaled.jpg" alt="Set Up VLC Arduino Pi Visuelle Lichtkommunikation" width="2550" height="1913" srcset="https://nerd-corner.com/wp-content/uploads/2021/06/SetUp-Arduino-Pi-scaled.jpg 2560w, https://nerd-corner.com/wp-content/uploads/2021/06/SetUp-Arduino-Pi-300x225.jpg 300w, https://nerd-corner.com/wp-content/uploads/2021/06/SetUp-Arduino-Pi-1024x768.jpg 1024w, https://nerd-corner.com/wp-content/uploads/2021/06/SetUp-Arduino-Pi-768x576.jpg 768w, https://nerd-corner.com/wp-content/uploads/2021/06/SetUp-Arduino-Pi-1536x1152.jpg 1536w, https://nerd-corner.com/wp-content/uploads/2021/06/SetUp-Arduino-Pi-2048x1536.jpg 2048w" sizes="auto, (max-width: 2550px) 100vw, 2550px" /></p>
<h2>Download files</h2>
<ul>
<li><a  data-e-Disable-Page-Transition="true" class="download-link" title="" href="https://nerd-corner.com/download/946/?tmstv=1756248572" rel="nofollow" id="download-link-946" data-redirect="false" >
	Arduino Code VLC to receive text messages</a>
</li>
<li><a  data-e-Disable-Page-Transition="true" class="download-link" title="" href="https://nerd-corner.com/download/949/?tmstv=1756248572" rel="nofollow" id="download-link-949" data-redirect="false" >
	Raspberry Pi code to send text messages with VLC</a>
</li>
</ul>
<p>&nbsp;</p>
<p>The post <a href="https://nerd-corner.com/sending-text-messages-with-visible-light-communication/">Sending text messages with visible light communication or LiFi &#8211; Pi to Arduino</a> appeared first on <a href="https://nerd-corner.com">Nerd Corner</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://nerd-corner.com/sending-text-messages-with-visible-light-communication/feed/</wfw:commentRss>
			<slash:comments>10</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=1756248572" 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>
