๐Ÿ“ฆ barneyman / tank-sensor

๐Ÿ“„ APmode.htm ยท 217 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio">
<TITLE>ESP8266 Provisioning</TITLE>
    <link rel="stylesheet" type="text/css" href="switch.css">
</HEAD>
<BODY onload="OnLoad()">

    <iframe src="HeaderFrame.htm" id="header" name="header" onload="headerLoaded()"></iframe>

    <h2>Wifi Details</h2>
    After successful configuration, this device will join that network and begin posting data to the nominated host<br />
    If configuration fails, this screen, on this AP, will be available again
    <hr />
    <table>
        <tr>
            <td>
                ssid (2.4Ghz)
            </td>
            <td id="wifis">
               
            </td>
        </tr>
        <tr>
            <td>
                password
            </td>
            <td>
                <input name="pwd" size="15" type="text" id="pwd"/>
            </td>
        </tr>
        <tr>
            <td>Type</td>
            <td>
                <select id="dhcp" onchange="chooseDHCP()">
                    <option value="1">DHCP</option>
                    <option value="0">Static</option>
                </select>
            </td>
        </tr>
        <tr hidden id="staticInfo"> 
            <td>
                IP Address<br />Gateway<br />Netmask<br />
            </td>
            <td>
                <input type="text" id="ip" /><br />
                <input type="text" id="gateway" /><br />
                <input type="text" id="netmask" value="255.255.255.0"><br />
            </td>
        </tr>
        <tr>
            <td>DataLog host</td>
            <td><input type="text" id="hostip"/></td>
        </tr>
        <tr>
            <td>DataLog host Port</td>
            <td><input type="number" id="hostport" value="5000" /></td>
        </tr>
        <tr>
            <td>DataLog Period (mins)</td>
            <td><input type="number" id="period" value="15"/></td>
        </tr>
    </table>        
    <button id="rescan" onclick="OnLoad()" disabled hidden>Rescan</button>
    <button id="join" onclick="JoinWifi()" disabled hidden>Join WiFi</button>
    <div id="error" class="error"></div>

    <script src="common.js"></script>

    <script>

        function chooseDHCP() {

            var select = document.getElementById("dhcp");
            var dhcp = select.options[select.selectedIndex].value

            if (dhcp == "1") {
                document.getElementById('staticInfo').hidden=true
            }
            else {
                document.getElementById('staticInfo').hidden = false
            }


        }

        function OnLoad()
        {
            let url = base+'/json/wifi'
            var wifis = document.getElementById("wifis");
            wifis.innerText = "Searching ...."

            // disable scan
            document.getElementById("rescan").disabled = true;
            document.getElementById("join").disabled = true;

            fetch(url)
                .then(function (response) {
                    if(response.ok)
                        return response.json();
                    throw new Error('network err')
                })
                .then(function (data) {

                    wifis.innerText = ""
                    select = document.createElement("select")
                    select.setAttribute('id','wifisids')
                    wifis.appendChild(select)

                    var wifiArray = data["wifi"]

                    for (var eachNode in wifiArray)
                    {
                        var wifi = document.createElement("option")
                        wifi.setAttribute("value", wifiArray[eachNode]["ssid"])
                        wifi.innerText = wifiArray[eachNode]["ssid"];
                        select.appendChild(wifi);
                    }

                    document.getElementById("hostport").value = data["hostport"];
                    document.getElementById("period").value = data["period"];

                    document.getElementById("rescan").disabled = false;
                    document.getElementById("join").disabled = false;

                    document.getElementById("rescan").hidden = false;
                    document.getElementById("join").hidden = false;

                }).catch(function (error) {
                    alert(error.message)
                    wifis.innerText = "Error"
                })


        }

        function JoinWifi() {

            // check there are proper values
            var jsonData = {};

            // get all inputs
            var pwd = document.getElementById('pwd')
            var selects = document.getElementById('wifisids')
            var ssid=selects.options[selects.selectedIndex].value;

            jsonData['pwd'] = pwd.value;
            jsonData['ssid'] = ssid;

            jsonData['loghost'] = document.getElementById('hostip').value
            jsonData['loghostport'] = document.getElementById('hostport').value
            jsonData['loghostperiod'] = document.getElementById('period').value


            var select = document.getElementById("dhcp");
            var dhcp = select.options[select.selectedIndex].value

            jsonData['dhcp'] = dhcp

            if (dhcp == "0")
            {
                jsonData['ip']=document.getElementById('ip').value;
                jsonData['gateway'] = document.getElementById('gateway').value;
                jsonData['netmask'] = document.getElementById('netmask').value;
            }

            var inputValid = true;

            if(inputValid==true) {

                let url = base + '/json/wifi'

                fetch(url, {
                    method: 'post',
                    headers: {
                        "Content-type": "text/plain"
                    },
                    body: JSON.stringify(jsonData)
                })
                .then(function (response) {
                    if (response.ok)
                        return response.json();
                    throw new Error('network err')
                })
                  .then(function (data) {
                      
                      // check the result
                      if (data["result"])
                      {
                          // woo hoo - success - reload - we'll get sent a different page
                          window.location.reload(true)
                          window.scrollTo(0, 0);
                      }
                      else
                      {
                          // failed - complain!
                          document.getElementById('error').innerHTML="failed to connect to AP"
                      }


                  })
                  .catch(function (error) {
                      console.log('Request failed', error);
                  });

            }
            else
                alert('values cannot be empty')
        }


    </script>


</BODY>
</HTML>