Skip to content

Commit e62288f

Browse files
committed
added config and set dns
Former-commit-id: 9d668a6
1 parent 559bdba commit e62288f

File tree

3 files changed

+64
-16
lines changed

3 files changed

+64
-16
lines changed

libraries/WiFiS3/src/Modem.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ bool ModemClass::write(const string &prompt, string &data_res, char * fmt, ...){
3939
vsprintf ((char *)tx_buff, fmt, va);
4040
va_end (va);
4141
#ifdef MODEM_DEBUG
42-
Serial.print("Write Call, command sent: ");
42+
Serial.print(" Write Call, command sent: ");
4343
Serial.write(tx_buff,strlen((char *)tx_buff));
4444
Serial.println();
4545
#endif
@@ -78,14 +78,14 @@ bool ModemClass::buf_read(const string &prompt, string &data_res) {
7878
}
7979
trim(data_res);
8080
#ifdef MODEM_DEBUG
81-
Serial.print("Write Call, response rx |>>");
81+
Serial.print(" Write Call, response rx |>>");
8282
Serial.print(data_res.c_str());
8383
Serial.println("<<|");
8484
if(res) {
85-
Serial.println("Result: OK");
85+
Serial.println(" Result: OK");
8686
}
8787
else {
88-
Serial.println("Result: FAILED");
88+
Serial.println(" Result: FAILED");
8989
}
9090
#endif
9191

libraries/WiFiS3/src/WiFi.cpp

Lines changed: 56 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,12 @@ void CWifi::config(IPAddress local_ip) {
7474
IPAddress _gw(local_ip[0],local_ip[1], local_ip[2], 1);
7575
Serial.println(_gw);
7676
IPAddress _sm(255,255,255,0);
77-
return _config(local_ip, _gw, _sm);
77+
IPAddress dns(0,0,0,0);
78+
return _config(local_ip, _gw, _sm,dns,dns);
7879
}
7980

8081
/* -------------------------------------------------------------------------- */
81-
void CWifi::_config(IPAddress local_ip, IPAddress gateway, IPAddress subnet) {
82+
void CWifi::_config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1, IPAddress dns2) {
8283
/* -------------------------------------------------------------------------- */
8384
string res = "";
8485
modem.begin();
@@ -97,32 +98,55 @@ void CWifi::_config(IPAddress local_ip, IPAddress gateway, IPAddress subnet) {
9798
nm += to_string(subnet[2]) + ".";
9899
nm += to_string(subnet[3]);
99100

100-
modem.write(string(PROMPT(_SETIP)),res, "%s%s,%s,%s\r\n" , CMD_WRITE(_SETIP), ip.c_str(), gw.c_str(), nm.c_str());
101+
string _dns1 = to_string(dns1[0]) + ".";
102+
_dns1 += to_string(dns1[1]) + ".";
103+
_dns1 += to_string(dns1[2]) + ".";
104+
_dns1 += to_string(dns1[3]);
105+
106+
string _dns2 = to_string(dns2[0]) + ".";
107+
_dns2 += to_string(dns2[1]) + ".";
108+
_dns2 += to_string(dns2[2]) + ".";
109+
_dns2 += to_string(dns2[3]);
110+
111+
modem.write(string(PROMPT(_SETIP)),res, "%s%s,%s,%s,%s,%s\r\n" , CMD_WRITE(_SETIP), ip.c_str(), gw.c_str(), nm.c_str(),_dns1.c_str(),_dns2.c_str());
101112
}
102113

103114
/* -------------------------------------------------------------------------- */
104115
void CWifi::config(IPAddress local_ip, IPAddress dns_server) {
105-
/* -------------------------------------------------------------------------- */
116+
/* -------------------------------------------------------------------------- */
117+
IPAddress _gw(local_ip[0],local_ip[1], local_ip[2], 1);
118+
Serial.println(_gw);
119+
IPAddress _sm(255,255,255,0);
120+
IPAddress dns(0,0,0,0);
121+
return _config(local_ip, _gw, _sm,dns_server,dns);
106122
}
107123

108124
/* -------------------------------------------------------------------------- */
109125
void CWifi::config(IPAddress local_ip, IPAddress dns_server, IPAddress gateway) {
110-
/* -------------------------------------------------------------------------- */
126+
/* -------------------------------------------------------------------------- */
127+
IPAddress _sm(255,255,255,0);
128+
IPAddress dns(0,0,0,0);
129+
return _config(local_ip, gateway, _sm,dns_server,dns);
111130
}
112131

113132
/* -------------------------------------------------------------------------- */
114133
void CWifi::config(IPAddress local_ip, IPAddress dns_server, IPAddress gateway, IPAddress subnet) {
115134
/* -------------------------------------------------------------------------- */
135+
IPAddress dns(0,0,0,0);
136+
return _config(local_ip, gateway, subnet,dns_server,dns);
116137
}
117138

118139
/* -------------------------------------------------------------------------- */
119140
void CWifi::setDNS(IPAddress dns_server1) {
120-
/* -------------------------------------------------------------------------- */
141+
/* -------------------------------------------------------------------------- */
142+
IPAddress dns(0,0,0,0);
143+
return _config(localIP(), gatewayIP(), subnetMask(),dns_server1,dns);
121144
}
122145

123146
/* -------------------------------------------------------------------------- */
124147
void CWifi::setDNS(IPAddress dns_server1, IPAddress dns_server2) {
125148
/* -------------------------------------------------------------------------- */
149+
return _config(localIP(), gatewayIP(), subnetMask(),dns_server1,dns_server2);
126150
}
127151

128152
/* -------------------------------------------------------------------------- */
@@ -203,9 +227,33 @@ int8_t CWifi::scanNetworks() {
203227
return (int8_t)access_points.size();
204228
}
205229

230+
/* -------------------------------------------------------------------------- */
231+
IPAddress CWifi::dnsIP(int n) {
232+
/* -------------------------------------------------------------------------- */
233+
modem.begin();
234+
string res;
235+
if(n == 0) {
236+
if(modem.write(string(PROMPT(_IPSTA)),res, "%s%d\r\n" , CMD_WRITE(_IPSTA), DNS1_ADDR)) {
237+
IPAddress dns_IP;
238+
dns_IP.fromString(res.c_str());
239+
return dns_IP;
240+
}
241+
}
242+
else if(n == 1) {
243+
if(modem.write(string(PROMPT(_IPSTA)),res, "%s%d\r\n" , CMD_WRITE(_IPSTA), DNS2_ADDR)) {
244+
IPAddress dns_IP;
245+
dns_IP.fromString(res.c_str());
246+
return dns_IP;
247+
}
248+
}
249+
return IPAddress(0,0,0,0);
250+
}
251+
252+
206253
/* -------------------------------------------------------------------------- */
207254
IPAddress CWifi::localIP() {
208255
/* -------------------------------------------------------------------------- */
256+
modem.begin();
209257
string res = "";
210258
if(modem.write(string(PROMPT(_IPSTA)),res, "%s%d\r\n" , CMD_WRITE(_IPSTA), IP_ADDR)) {
211259
IPAddress local_IP;
@@ -218,6 +266,7 @@ IPAddress CWifi::localIP() {
218266
/* -------------------------------------------------------------------------- */
219267
IPAddress CWifi::subnetMask() {
220268
/* -------------------------------------------------------------------------- */
269+
modem.begin();
221270
string res = "";
222271
if(modem.write(string(PROMPT(_IPSTA)),res, "%s%d\r\n" , CMD_WRITE(_IPSTA), NETMASK_ADDR)) {
223272
IPAddress subnetMask;
@@ -230,6 +279,7 @@ IPAddress CWifi::subnetMask() {
230279
/* -------------------------------------------------------------------------- */
231280
IPAddress CWifi::gatewayIP() {
232281
/* -------------------------------------------------------------------------- */
282+
modem.begin();
233283
string res = "";
234284
if(modem.write(string(PROMPT(_IPSTA)),res, "%s%d\r\n" , CMD_WRITE(_IPSTA), GATEWAY_ADDR)) {
235285
IPAddress gateway_IP;

libraries/WiFiS3/src/WiFi.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,12 @@ class CAccessPoint {
2323
string encryption_mode;
2424
};
2525

26-
enum{
27-
IP_ADDR = 0,
28-
GATEWAY_ADDR,
29-
NETMASK_ADDR
30-
} ip_type;
26+
3127

3228

3329
class CWifi {
3430
private:
35-
void _config(IPAddress local_ip, IPAddress gateway, IPAddress subnet);
31+
void _config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1, IPAddress dns2);
3632
unsigned long _timeout;
3733

3834
vector<CAccessPoint> access_points;
@@ -163,6 +159,8 @@ class CWifi {
163159
*/
164160
IPAddress gatewayIP();
165161

162+
IPAddress dnsIP(int n = 0);
163+
166164
/*
167165
* Return the current SSID associated with the network
168166
*

0 commit comments

Comments
 (0)