MQTT Communication
Smart Factory
Network.cpp
Go to the documentation of this file.
1 
14 #include "Network.h"
15 //=====PUBLIC====================================================================================
16 
17 Network::Network(String ssid, String password, int WIFI_CS, int WIFI_IRQ, int WIFI_RST, int WIFI_EN) : pSsid(ssid),
18  pPassword(password),
19  pWifi_CS(WIFI_CS),
20  pWiFi_IRQ(WIFI_IRQ),
21  pWifi_RST(WIFI_RST),
22  pWifi_EN(WIFI_EN) {
23 }
24 
25 void Network::init() {
26  DBFUNCCALLln("Network::init()");
27  WiFi.setPins(pWifi_CS, pWiFi_IRQ, pWifi_RST, pWifi_EN);
28  if (WiFi.status() == WL_NO_SHIELD) { // check if the shield is presence
29  DBERROR("NO WiFi shield present");
30  DBERROR("WiFi Library could not find WiFi shield. " + decodeWiFistate(WiFi.status()));
31  DBINFO3ln("programm is not continuing");
32  while (true) {
33  // don't continue
34  }
35  }
36  DBINFO3ln(String("WiFi Firmware Version = ") + String(WiFi.firmwareVersion()));
37  connectToWiFi();
38 }
39 
41  DBFUNCCALLln("Network::connectToWiFi()");
42  while (WiFi.status() != WL_CONNECTED) { // connect to Wifi network
43  DBSTATUS("Status: " + decodeWiFistate(WiFi.status()));
44  DBINFO3ln("Attempting WLAN connection (WEP)...");
45  DBINFO3ln("SSID: " + pSsid);
46  // DBINFO1ln("PW: " + pPassword);
47  if (WiFi.begin(pSsid, pPassword) != WL_CONNECTED) {
48  DBERROR("WLAN connection failed");
49  DBINFO3ln("trying again in 3 seconds");
50  delay(3000);
51  } else {
52  pIPLocal = WiFi.localIP();
53  WiFi.macAddress(pMac);
54  pSsid = WiFi.SSID();
55  WiFi.macAddress(pMac);
56  WiFi.BSSID(pMacRouter);
57  pRssi = WiFi.RSSI();
58  pEncryption = WiFi.encryptionType();
59  // hostname(hostname.c_str());
60  }
61  }
62 }
63 
65  DBFUNCCALLln("Network::printNetworkInfo()");
66  printWiFiData();
68 }
69 
70 //=====PRIVATE====================================================================================
71 
73  DBFUNCCALLln("Network::printWiFiData()");
74  // print your WiFi shield's IP address:
75  pIPLocal = WiFi.localIP();
76  Serial.print("IP Address: ");
77  Serial.println(pIPLocal);
78  Serial.println(pIPLocal);
79  // print your MAC address:
80  WiFi.macAddress(pMac);
81  Serial.print("MAC address: ");
83 }
84 
86  DBFUNCCALLln("Network::printCurrentNet()");
87  // print the SSID of the network you're attached to:
88  Serial.print("SSID: ");
89  Serial.println(WiFi.SSID());
90  // print the MAC address of the router you're attached to:
91  WiFi.BSSID(pMacRouter);
92  Serial.print("BSSID: ");
94  // print the received signal strength:
95  pRssi = WiFi.RSSI();
96  Serial.print("signal strength (RSSI):");
97  Serial.println(pRssi);
98  // print the encryption type:
99  pEncryption = WiFi.encryptionType();
100  Serial.print("Encryption Type: ");
101  Serial.println(decodeEncryptionType(pEncryption));
102  Serial.println();
103 }
104 
105 void Network::printMacAddress(byte mac[]) {
106  for (int i = 5; i >= 0; i--) {
107  if (mac[i] < 16) {
108  Serial.print("0");
109  }
110  Serial.print(mac[i], HEX);
111  if (i > 0) {
112  Serial.print(":");
113  }
114  }
115  Serial.println();
116 }
117 
118 String Network::decodeWiFistate(int errorcode) {
119  DBFUNCCALLln("Network::decodeWiFistate(int errorcode)");
120  switch (errorcode) {
121  case WL_NO_SHIELD:
122  return "WL_NO_SHIELD";
123  case WL_IDLE_STATUS:
124  return "WL_IDLE_STATUS";
125  case WL_NO_SSID_AVAIL:
126  return "WL_NO_SSID_AVAIL";
127  case WL_SCAN_COMPLETED:
128  return "WL_SCAN_COMPLETED";
129  case WL_CONNECTED:
130  return "WL_CONNECTED";
131  case WL_CONNECT_FAILED:
132  return "WL_CONNECT_FAILED";
133  case WL_CONNECTION_LOST:
134  return "WL_CONNECTION_LOST";
135  case WL_DISCONNECTED:
136  return "WL_DISCONNECTED";
137  case WL_AP_LISTENING:
138  return "WL_AP_LISTENING";
139  case WL_AP_CONNECTED:
140  return "WL_AP_CONNECTED";
141  case WL_AP_FAILED:
142  return "WL_AP_FAILED";
143  case WL_PROVISIONING:
144  return "WL_PROVISIONING";
145  case WL_PROVISIONING_FAILED:
146  return "WL_PROVISIONING_FAILED";
147  default:
148  return "Error";
149  }
150 }
151 
152 String Network::decodeEncryptionType(int errorcode) {
153  DBFUNCCALLln("Network::decodeEncryptionType(int errorcode)");
154  switch (errorcode) {
155  case 2:
156  return "TKIP (WPA)";
157  case 5:
158  return "WEP";
159  case 4:
160  return "CCMP (WPA)";
161  case 7:
162  return "NONE";
163  case 8:
164  return "AUTO ";
165  default:
166  return "Error";
167  }
168 }
The Network class establishes a WLAN-Connection.
void printMacAddress(byte mac[])
Pritns Mac-address.
Definition: Network.cpp:105
void connectToWiFi()
Connects to a WiFi with the given Credential.
Definition: Network.cpp:40
void printNetworkInfo()
Prints all relevant Network-Information of the connected network to serial.
Definition: Network.cpp:64
byte pEncryption
value represents the type of encryption
Definition: Network.h:132
byte pMacRouter[6]
Contains MAC Adress of the Router BSSID.
Definition: Network.h:130
Network(String ssid, String password, int WIFI_CS, int WIFI_IRQ, int WIFI_RST, int WIFI_EN)
Construct a new Network object.
Definition: Network.cpp:17
IPAddress pIPLocal
Contains own IP-Adress.
Definition: Network.h:129
long pRssi
The current RSSI /Received Signal Strength in dBm.
Definition: Network.h:146
int pWifi_CS
Definition: Network.h:125
String decodeWiFistate(int errorcode)
Decodes the Error Values from Wifi status() and returns a description.
Definition: Network.cpp:118
int pWiFi_IRQ
Definition: Network.h:126
void printCurrentNet()
Prints all relevant Information of the current Network.
Definition: Network.cpp:85
void init()
Intitialize WLan-Hardware.
Definition: Network.cpp:25
void printWiFiData()
Prints all relevant WiFi-Information of the connected network to serial.
Definition: Network.cpp:72
int pWifi_RST
Definition: Network.h:127
String decodeEncryptionType(int errorcode)
Construct a new decode Encryption Type object.
Definition: Network.cpp:152
String pPassword
Contains WiFi Password.
Definition: Network.h:124
String pSsid
Contains the SSID the WiFi shield is currently connected to.
Definition: Network.h:123
byte pMac[6]
Contains own MAC Adress.
Definition: Network.h:131
int pWifi_EN
Definition: Network.h:128