Browse Source

first commit

master
lukario 6 months ago
commit
0f90ac52b7
6 changed files with 404 additions and 0 deletions
  1. 265
    0
      ESP8266HTTPClient.h
  2. 59
    0
      ESP8266Ping.h
  3. BIN
      ESP8266WiFi.h
  4. 64
    0
      ESP_Login_Ding.ino
  5. 12
    0
      README.md
  6. 4
    0
      credentials.h

+ 265
- 0
ESP8266HTTPClient.h View File

@@ -0,0 +1,265 @@
1
+/**
2
+ * ESP8266HTTPClient.h
3
+ *
4
+ * Created on: 02.11.2015
5
+ *
6
+ * Copyright (c) 2015 Markus Sattler. All rights reserved.
7
+ * This file is part of the ESP8266HTTPClient for Arduino.
8
+ *
9
+ * This library is free software; you can redistribute it and/or
10
+ * modify it under the terms of the GNU Lesser General Public
11
+ * License as published by the Free Software Foundation; either
12
+ * version 2.1 of the License, or (at your option) any later version.
13
+ *
14
+ * This library is distributed in the hope that it will be useful,
15
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17
+ * Lesser General Public License for more details.
18
+ *
19
+ * You should have received a copy of the GNU Lesser General Public
20
+ * License along with this library; if not, write to the Free Software
21
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
22
+ *
23
+ * Modified by Jeroen Döll, June 2018
24
+ */
25
+
26
+#ifndef ESP8266HTTPClient_H_
27
+#define ESP8266HTTPClient_H_
28
+
29
+#ifndef HTTPCLIENT_1_1_COMPATIBLE
30
+#define HTTPCLIENT_1_1_COMPATIBLE 1
31
+#endif
32
+
33
+#include <memory>
34
+#include <Arduino.h>
35
+
36
+#include <WiFiClient.h>
37
+
38
+#ifdef DEBUG_ESP_HTTP_CLIENT
39
+#ifdef DEBUG_ESP_PORT
40
+#define DEBUG_HTTPCLIENT(fmt, ...) DEBUG_ESP_PORT.printf_P( (PGM_P)PSTR(fmt), ## __VA_ARGS__ )
41
+#endif
42
+#endif
43
+
44
+#ifndef DEBUG_HTTPCLIENT
45
+#define DEBUG_HTTPCLIENT(...) do { (void)0; } while (0)
46
+#endif
47
+
48
+#define HTTPCLIENT_DEFAULT_TCP_TIMEOUT (5000)
49
+
50
+/// HTTP client errors
51
+#define HTTPC_ERROR_CONNECTION_REFUSED  (-1)
52
+#define HTTPC_ERROR_SEND_HEADER_FAILED  (-2)
53
+#define HTTPC_ERROR_SEND_PAYLOAD_FAILED (-3)
54
+#define HTTPC_ERROR_NOT_CONNECTED       (-4)
55
+#define HTTPC_ERROR_CONNECTION_LOST     (-5)
56
+#define HTTPC_ERROR_NO_STREAM           (-6)
57
+#define HTTPC_ERROR_NO_HTTP_SERVER      (-7)
58
+#define HTTPC_ERROR_TOO_LESS_RAM        (-8)
59
+#define HTTPC_ERROR_ENCODING            (-9)
60
+#define HTTPC_ERROR_STREAM_WRITE        (-10)
61
+#define HTTPC_ERROR_READ_TIMEOUT        (-11)
62
+
63
+/// size for the stream handling
64
+#define HTTP_TCP_BUFFER_SIZE (1460)
65
+
66
+/// HTTP codes see RFC7231
67
+typedef enum {
68
+    HTTP_CODE_CONTINUE = 100,
69
+    HTTP_CODE_SWITCHING_PROTOCOLS = 101,
70
+    HTTP_CODE_PROCESSING = 102,
71
+    HTTP_CODE_OK = 200,
72
+    HTTP_CODE_CREATED = 201,
73
+    HTTP_CODE_ACCEPTED = 202,
74
+    HTTP_CODE_NON_AUTHORITATIVE_INFORMATION = 203,
75
+    HTTP_CODE_NO_CONTENT = 204,
76
+    HTTP_CODE_RESET_CONTENT = 205,
77
+    HTTP_CODE_PARTIAL_CONTENT = 206,
78
+    HTTP_CODE_MULTI_STATUS = 207,
79
+    HTTP_CODE_ALREADY_REPORTED = 208,
80
+    HTTP_CODE_IM_USED = 226,
81
+    HTTP_CODE_MULTIPLE_CHOICES = 300,
82
+    HTTP_CODE_MOVED_PERMANENTLY = 301,
83
+    HTTP_CODE_FOUND = 302,
84
+    HTTP_CODE_SEE_OTHER = 303,
85
+    HTTP_CODE_NOT_MODIFIED = 304,
86
+    HTTP_CODE_USE_PROXY = 305,
87
+    HTTP_CODE_TEMPORARY_REDIRECT = 307,
88
+    HTTP_CODE_PERMANENT_REDIRECT = 308,
89
+    HTTP_CODE_BAD_REQUEST = 400,
90
+    HTTP_CODE_UNAUTHORIZED = 401,
91
+    HTTP_CODE_PAYMENT_REQUIRED = 402,
92
+    HTTP_CODE_FORBIDDEN = 403,
93
+    HTTP_CODE_NOT_FOUND = 404,
94
+    HTTP_CODE_METHOD_NOT_ALLOWED = 405,
95
+    HTTP_CODE_NOT_ACCEPTABLE = 406,
96
+    HTTP_CODE_PROXY_AUTHENTICATION_REQUIRED = 407,
97
+    HTTP_CODE_REQUEST_TIMEOUT = 408,
98
+    HTTP_CODE_CONFLICT = 409,
99
+    HTTP_CODE_GONE = 410,
100
+    HTTP_CODE_LENGTH_REQUIRED = 411,
101
+    HTTP_CODE_PRECONDITION_FAILED = 412,
102
+    HTTP_CODE_PAYLOAD_TOO_LARGE = 413,
103
+    HTTP_CODE_URI_TOO_LONG = 414,
104
+    HTTP_CODE_UNSUPPORTED_MEDIA_TYPE = 415,
105
+    HTTP_CODE_RANGE_NOT_SATISFIABLE = 416,
106
+    HTTP_CODE_EXPECTATION_FAILED = 417,
107
+    HTTP_CODE_MISDIRECTED_REQUEST = 421,
108
+    HTTP_CODE_UNPROCESSABLE_ENTITY = 422,
109
+    HTTP_CODE_LOCKED = 423,
110
+    HTTP_CODE_FAILED_DEPENDENCY = 424,
111
+    HTTP_CODE_UPGRADE_REQUIRED = 426,
112
+    HTTP_CODE_PRECONDITION_REQUIRED = 428,
113
+    HTTP_CODE_TOO_MANY_REQUESTS = 429,
114
+    HTTP_CODE_REQUEST_HEADER_FIELDS_TOO_LARGE = 431,
115
+    HTTP_CODE_INTERNAL_SERVER_ERROR = 500,
116
+    HTTP_CODE_NOT_IMPLEMENTED = 501,
117
+    HTTP_CODE_BAD_GATEWAY = 502,
118
+    HTTP_CODE_SERVICE_UNAVAILABLE = 503,
119
+    HTTP_CODE_GATEWAY_TIMEOUT = 504,
120
+    HTTP_CODE_HTTP_VERSION_NOT_SUPPORTED = 505,
121
+    HTTP_CODE_VARIANT_ALSO_NEGOTIATES = 506,
122
+    HTTP_CODE_INSUFFICIENT_STORAGE = 507,
123
+    HTTP_CODE_LOOP_DETECTED = 508,
124
+    HTTP_CODE_NOT_EXTENDED = 510,
125
+    HTTP_CODE_NETWORK_AUTHENTICATION_REQUIRED = 511
126
+} t_http_codes;
127
+
128
+typedef enum {
129
+    HTTPC_TE_IDENTITY,
130
+    HTTPC_TE_CHUNKED
131
+} transferEncoding_t;
132
+
133
+#if HTTPCLIENT_1_1_COMPATIBLE
134
+class TransportTraits;
135
+typedef std::unique_ptr<TransportTraits> TransportTraitsPtr;
136
+#endif
137
+
138
+class StreamString;
139
+
140
+class HTTPClient
141
+{
142
+public:
143
+    HTTPClient();
144
+    ~HTTPClient();
145
+
146
+/*
147
+ * Since both begin() functions take a reference to client as a parameter, you need to 
148
+ * ensure the client object lives the entire time of the HTTPClient
149
+ */
150
+    bool begin(WiFiClient &client, const String& url);
151
+    bool begin(WiFiClient &client, const String& host, uint16_t port, const String& uri = "/", bool https = false);
152
+
153
+#if HTTPCLIENT_1_1_COMPATIBLE
154
+    // Plain HTTP connection, unencrypted
155
+    bool begin(String url)  __attribute__ ((deprecated));
156
+    bool begin(String host, uint16_t port, String uri = "/")  __attribute__ ((deprecated));
157
+    // Use axTLS for secure HTTPS connection
158
+    bool begin(String url, String httpsFingerprint)  __attribute__ ((deprecated));
159
+    bool begin(String host, uint16_t port, String uri, String httpsFingerprint)  __attribute__ ((deprecated));
160
+    // Use BearSSL for secure HTTPS connection
161
+    bool begin(String url, const uint8_t httpsFingerprint[20])  __attribute__ ((deprecated));
162
+    bool begin(String host, uint16_t port, String uri, const uint8_t httpsFingerprint[20])  __attribute__ ((deprecated));
163
+    // deprecated, use the overload above instead
164
+    bool begin(String host, uint16_t port, String uri, bool https, String httpsFingerprint)  __attribute__ ((deprecated));
165
+#endif
166
+
167
+    void end(void);
168
+
169
+    bool connected(void);
170
+
171
+    void setReuse(bool reuse); /// keep-alive
172
+    void setUserAgent(const String& userAgent);
173
+    void setAuthorization(const char * user, const char * password);
174
+    void setAuthorization(const char * auth);
175
+    void setTimeout(uint16_t timeout);
176
+    void setFollowRedirects(bool follow);
177
+    void setRedirectLimit(uint16_t limit); // max redirects to follow for a single request
178
+    bool setURL(const String& url); // handy for handling redirects
179
+    void useHTTP10(bool usehttp10 = true);
180
+
181
+    /// request handling
182
+    int GET();
183
+    int POST(const uint8_t* payload, size_t size);
184
+    int POST(const String& payload);
185
+    int PUT(const uint8_t* payload, size_t size);
186
+    int PUT(const String& payload);
187
+    int PATCH(const uint8_t* payload, size_t size);
188
+    int PATCH(const String& payload);
189
+    int sendRequest(const char* type, const String& payload);
190
+    int sendRequest(const char* type, const uint8_t* payload = NULL, size_t size = 0);
191
+    int sendRequest(const char* type, Stream * stream, size_t size = 0);
192
+
193
+    void addHeader(const String& name, const String& value, bool first = false, bool replace = true);
194
+
195
+    /// Response handling
196
+    void collectHeaders(const char* headerKeys[], const size_t headerKeysCount);
197
+    String header(const char* name);   // get request header value by name
198
+    String header(size_t i);              // get request header value by number
199
+    String headerName(size_t i);          // get request header name by number
200
+    int headers();                     // get header count
201
+    bool hasHeader(const char* name);  // check if header exists
202
+
203
+
204
+    int getSize(void);
205
+    const String& getLocation(void); // Location header from redirect if 3XX
206
+
207
+    WiFiClient& getStream(void);
208
+    WiFiClient* getStreamPtr(void);
209
+    int writeToStream(Stream* stream);
210
+    const String& getString(void);
211
+    static String errorToString(int error);
212
+
213
+protected:
214
+    struct RequestArgument {
215
+        String key;
216
+        String value;
217
+    };
218
+
219
+    bool beginInternal(const String& url, const char* expectedProtocol);
220
+    void disconnect(bool preserveClient = false);
221
+    void clear();
222
+    int returnError(int error);
223
+    bool connect(void);
224
+    bool sendHeader(const char * type);
225
+    int handleHeaderResponse();
226
+    int writeToStreamDataBlock(Stream * stream, int len);
227
+
228
+
229
+#if HTTPCLIENT_1_1_COMPATIBLE
230
+    TransportTraitsPtr _transportTraits;
231
+    std::unique_ptr<WiFiClient> _tcpDeprecated;
232
+#endif
233
+    WiFiClient* _client;
234
+
235
+    /// request handling
236
+    String _host;
237
+    uint16_t _port = 0;
238
+    bool _reuse = true;
239
+    uint16_t _tcpTimeout = HTTPCLIENT_DEFAULT_TCP_TIMEOUT;
240
+    bool _useHTTP10 = false;
241
+
242
+    String _uri;
243
+    String _protocol;
244
+    String _headers;
245
+    String _userAgent;
246
+    String _base64Authorization;
247
+
248
+    /// Response handling
249
+    RequestArgument* _currentHeaders = nullptr;
250
+    size_t           _headerKeysCount = 0;
251
+
252
+    int _returnCode = 0;
253
+    int _size = -1;
254
+    bool _canReuse = false;
255
+    bool _followRedirects = false;
256
+    uint16_t _redirectCount = 0;
257
+    uint16_t _redirectLimit = 10;
258
+    String _location;
259
+    transferEncoding_t _transferEncoding = HTTPC_TE_IDENTITY;
260
+    std::unique_ptr<StreamString> _payload;
261
+};
262
+
263
+
264
+
265
+#endif /* ESP8266HTTPClient_H_ */

+ 59
- 0
ESP8266Ping.h View File

@@ -0,0 +1,59 @@
1
+/*
2
+  ESP8266Ping - Ping library for ESP8266
3
+  Copyright (c) 2015 Daniele Colanardi. All rights reserved.
4
+
5
+  This library is free software; you can redistribute it and/or
6
+  modify it under the terms of the GNU Lesser General Public
7
+  License as published by the Free Software Foundation; either
8
+  version 2.1 of the License, or (at your option) any later version.
9
+
10
+  This library is distributed in the hope that it will be useful,
11
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
+  Lesser General Public License for more details.
14
+
15
+  You should have received a copy of the GNU Lesser General Public
16
+  License along with this library; if not, write to the Free Software
17
+  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
+*/
19
+
20
+#ifndef ESP8266Ping_H
21
+#define ESP8266Ping_H
22
+
23
+#include <Arduino.h>
24
+#include <ESP8266WiFi.h>
25
+
26
+extern "C" {
27
+  #include <ping.h>
28
+}
29
+
30
+#ifdef ENABLE_DEBUG_PING
31
+  #define DEBUG_PING(...) Serial.printf(__VA_ARGS__)
32
+#else
33
+  #define DEBUG_PING(...)
34
+#endif
35
+
36
+class PingClass {
37
+  public:
38
+    PingClass();
39
+
40
+    bool ping(IPAddress dest,   byte count = 5);
41
+    bool ping(const char* host, byte count = 5);
42
+
43
+    int averageTime();
44
+
45
+  protected:
46
+    static void _ping_sent_cb(void *opt, void *pdata);
47
+    static void _ping_recv_cb(void *opt, void *pdata);
48
+
49
+    IPAddress _dest;
50
+    ping_option _options;
51
+
52
+    static byte _expected_count, _errors, _success;
53
+    static int _avg_time;
54
+};
55
+
56
+#include "ESP8266Ping.impl.h"
57
+PingClass Ping;
58
+
59
+#endif

BIN
ESP8266WiFi.h View File


+ 64
- 0
ESP_Login_Ding.ino View File

@@ -0,0 +1,64 @@
1
+
2
+#include <ESP8266WiFi.h>
3
+#include <ESP8266HTTPClient.h>
4
+#include <ESP8266Ping.h>
5
+#include "credentials.h"
6
+
7
+
8
+
9
+void wifi_connect (){
10
+
11
+  WiFi.begin(WLAN_ID, WLAN_PASSWORD);
12
+  WiFi.mode(WIFI_STA);
13
+
14
+  while(WiFi.status() != WL_CONNECTED) {
15
+
16
+    delay(1000);
17
+    Serial.println("Connecting ...");
18
+  }
19
+
20
+  Serial.println(WiFi.localIP());
21
+  
22
+}
23
+  
24
+  
25
+  
26
+
27
+void setup (){
28
+
29
+  Serial.begin(115200);
30
+  wifi_connect();
31
+}
32
+
33
+void login (){
34
+
35
+  HTTPClient http;
36
+  
37
+  http.begin("http://login.rz.ruhr-uni-bochum.de/cgi-bin/laklogin");
38
+  Serial.println("Attempt Login") ;
39
+
40
+  int httpCode = http.POST("code=1&loginid=" LOGIN_ID "&password=" LOGIN_PASSWORD "&action=Login");
41
+  
42
+
43
+  http.end();
44
+  
45
+ 
46
+}
47
+
48
+
49
+
50
+void loop (){
51
+  if(WiFi.status() != WL_CONNECTED){
52
+    wifi_connect();
53
+  }
54
+  IPAddress ip = (1, 1, 1, 1);
55
+  bool ret = Ping.ping(ip);
56
+  Serial.println(String("Connection state : ") + (ret ? "connected" : "not connected"));
57
+  if (ret == false){
58
+    Serial.println("Connection check failed");
59
+    login(); 
60
+    }
61
+   else Serial.println("Still connected");
62
+  delay(10000);
63
+ 
64
+}

+ 12
- 0
README.md View File

@@ -0,0 +1,12 @@
1
+# Lies mich! :)
2
+
3
+Autologinscript für die Anmeldung in Wohnheimen, welche über eduroam ins Internet gehen.
4
+Achtung aktuell KEIN https, sondern nur http!!!
5
+
6
+Autoren: kilobyte22, bambi
7
+
8
+# Notwendige Hardware
9
+- ESP8266 (gehen natürlich auch Andere, dann müssen nur die jeweiligen Header nur angepasst werden
10
+
11
+# Einrichten
12
+- Über die Arduino IDE die Datei credentials.h mit Logindaten für WLAN und Login füllen

+ 4
- 0
credentials.h View File

@@ -0,0 +1,4 @@
1
+#define WLAN_ID ""
2
+#define WLAN_PASSWORD ""
3
+#define LOGIN_ID ""
4
+#define LOGIN_PASSWORD ""

Loading…
Cancel
Save