Differences

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

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
en:iot-open:remotelab:sut:generalpurpose2:u1 [2019/07/25 10:36] pczekalskien:iot-open:remotelab:sut:generalpurpose2:u1 [2020/07/20 09:00] (current) – external edit 127.0.0.1
Line 1: Line 1:
-==== Connecting to the network in STA mode ==== +==== U1: Connecting to the network in STA mode ==== 
-//Give few words about this scenarioProvide some objectives and impact on the students.//+Most IoT (if not all of them) require your device to communicate via a networkHere we connect to the existing WiFi network, 2.4GHz. All laboratory nodes can access common access point and require credentials to connect to it (see laboratory description section for the credentials and latest updates)ESP 8266 has a built-in WiFi interface, so you're using it to connect to the network. Every ESP has an individual MAC address. An IP address is assigned to the ESP via DHCP server, automatically, so you will present it on the LCD screen
 === Target group === === Target group ===
-//This hands-on lab guide is intended for the Beginners/Undergraduates/Masters/Professionals. Choose appropriate.//+Undergraduate Bachelor Engineering Students
  
 === Prerequisites === === Prerequisites ===
-//Provide prerequisite readings/hardware/software/software libraries/skills if other than stated in the Laboratory DescriptionIf noneremove along with the header. Please note some of those may be common for all scenarios (i.emqtt library), so provide it in the laboratory description level, not in the scenario level.//+You will need to know the credentials of the network - see node description for detailsMind, those may be different than in the code chunks presented belowYou need to know how to handle 4x20 characters LCD screenIn case of doubt re-work on scenarios B1 and B2.
  
 === Scenario === === Scenario ===
-//Describe scenario to let the user understand its idea and processWrite information about the approachalgorithmetc. (depends on the lab)Provide useful information about protocolsassumptionsaddresses (i.e. communication servers)credentialsetc.//+In this scenario, you will create a WiFi client to connect to the AP then present connection status (eventually the failure, with attempts) on the LCD screen. Then you will show on the LCD given IP address and MAC address of your ESP8266.\\ 
 +While attempting to connect, the first line of the LCD should present information "Connecting Wifi" and the second line should present the attempt number. 
 +When connectedthe first line of the LCD should present information "WiFi connected"following one your given IP address including "IP: XXX.XXX.XXX.XXX". As MAC address is pretty long (17 chars including separators, here we will use the fourth line to present it, while "MAC: " header should be present in the third line of the LCDNotethere is a tickershowing that the device is aliveimplemented the same way as in scenario B1for beginners.
  
 +We suggest putting connection and information code into the separate function that you will call from the ''setup()'' one, just for cleaner code.
 === Result === === Result ===
-//Describe expected result when scenario is finished.//+The LCD screen should present the current situation and all the necessary information. Once connected, observe the IP address assigned by the DHCP server and device's MAC.
  
 === Start === === Start ===
-//Write starting conditionsi.e. what to do at the beginningwhat to pay attention before beginninghow the mechanical part should look like etc.//+Define some identifiers to separate and update AP's SSID and passphrase easily. To format lines for the LCDwe suggest using a ''char'' buffer of 20 characters (one full line) and some 2-3 integers for iteratorsRemember to declare the LCD control class in your code. You do not need to instantiate WiFi communication class - as you have only one interface here, it is singleton class you can refer directly using ''WiFi''. Note - in this scenario, you connect only once. If your connection breaks, you will have no information about it here. To handle such situation, there are multiple solutions: you can check in the ''loop()'' if the connection is OK and in case it is downyou can call your re-connecting functionor you can use one of the asynchronous handlerstriggered automatically via the ''WiFi'' manager. To use the latter approach, refer to the ESP8266 WiFi implementation for Arduino documentation.
  
 === Steps === === Steps ===
-// Write some extra information if i.e. some steps are optional otherwise cancel this paragraph (but do not remove header).// +Following steps do not present full code - you need to supply missing parts on your own!
-== Step 1 == +
-//Describe activities done in Step 1.//+
  
 +== Step 1 ==
 +Include all necessary libraries. The minimum set here is:
 +<code c>
 +#include <Arduino.h>
 +#include <ESP8266WiFi.h>
 +#include <LiquidCrystal_I2C.h>
 ... ...
 +</code>
 +== Step 2 ==
 +Give it some definitions as identifiers for cleaner code:
 +<code c>
 +#define wifi_ssid "internal.IOT"
 +#define wifi_password "IoTlab32768"
 +</code>
 +<note important>Always refer to the node documentation to ensure you know current SSID and passphrase. They may differ to those in the code above!</note>
 +
 +== Step 3 ==
 +Print some information about starting connecting to WiFi, configure network interface as a WiFi client and give it a try to connect to the AP:
 +<code c>
 +  delay(10);
 +  WiFi.mode(WIFI_STA);
 +  WiFi.begin(wifi_ssid, wifi_password);
 +  n=0;
 +</code>
 +<note important>''delay(10)'' is necessary to give it a breath before you ask it to connect to the web - wifi interface itself boots slower than rest of the ESP8266 because of radio physics.</note>
 +<note tip>''n=0'' is an iterator here - you will use it to show a number of attempts - you should usually succeed in one or couple. If attempts start ticking up, look for the code mistakes and check your SSID and passphrase.</note>
 +<note warning>Please, explicitly use interface mode setting: ''WiFi.mode(WIFI_STA);''. See FAQ section for details.</note>
 +
 +== Step 4 ==
 +Check if connected, if not, give it next attempt:
 +<code c>
 +while (WiFi.status() != WL_CONNECTED) {
 +    lcd.setCursor(0,1);
 +    n++;
 +    sprintf(buffer,"Attempt %d",n);
 +    lcd.print(buffer);
 +    delay(1000);
 +</code>
  
-== Step == +== Step == 
-//Describe activities done in Step n.//+When connected, show details to the camera: 
 +<code c> 
 +  lcd.clear(); 
 +  lcd.home(); 
 +  lcd.print("WiFi connected!"); 
 +  //Print IP 
 +  String s = WiFi.localIP().toString(); 
 +  sprintf(buffer,"IP: %s",s.c_str()); 
 +  lcd.setCursor(0,1); 
 +  lcd.print(buffer); 
 +  //Print MAC 
 +  lcd.setCursor(0,2); 
 +  lcd.print("MAC:"); 
 +  s = WiFi.macAddress(); 
 +  lcd.setCursor(0,3); 
 +  lcd.print(s.c_str()); 
 +</code> 
 +<note tip>IP address returned by the WiFi.localIP() function of the WiFi manager returns ''IPAddress'' structure so you need to convert it to String. Supprisingly, MAC address is an already pre-formatted String object.</note> 
 +== Step 6 == 
 +In the loop() function present ticker in the 3rd line, right side of the LCD. The 4th line is all occupied by the MAC address, if you followed 1:1 the guide. In case your UI looks different, handle coordinates appropriatelly. 
  
 === Result validation === === Result validation ===
-//Provide some result validation methodsfor self assesment.//+Run your code and observe LCD presenting information on connection progressIP and MAC addresses.
  
 === FAQ === === FAQ ===
-This section is to be extended as new questions appear. \\ +**Does IP address change over time?**: Yes. First of all, IP is given automatically by the DHCP server. There is no strict rule saying, your IP is always going to be the same. Second, IP address reservation is done for some period of time and then DHCP server may assign you other IP so it may change over runtime, not necessarily between restarts only.\\ 
-When using the printed version of this manual please refer to the latest online version of this document to obtain the valid and up-to-date list of the FAQ. +**Does MAC address change**: No. It is a factory given one and should be unique worldwide. Anyway, you can programmatically change it this technique is commonly used by hackers to mimic other devices.\\ 
-//Provide some FAQs in the following form:\\ +**Do I need to use ''WiFi.mode(WIFI_STA);''?**: Yes, please doTheoretically, if consecutive compilations use STA (client to AP) mode of the ESP8266 wifi interface, your code may work without it. However, you never know if the previous user used STA or AP (or both). Your code may fail then if not explicitly stated!
-**Question?**: Answer. +
-//+
  
en/iot-open/remotelab/sut/generalpurpose2/u1.1564051002.txt.gz · Last modified: 2020/07/20 09:00 (external edit)
CC Attribution-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0