mirror of
				https://github.com/esp8266/Arduino.git
				synced 2025-11-03 14:33:37 +03:00 
			
		
		
		
	Add HTTPS request sample (#43)
This commit is contained in:
		
							
								
								
									
										80
									
								
								libraries/ESP8266WiFi/examples/HTTPSRequest/HTTPSRequest.ino
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										80
									
								
								libraries/ESP8266WiFi/examples/HTTPSRequest/HTTPSRequest.ino
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,80 @@
 | 
				
			|||||||
 | 
					/*
 | 
				
			||||||
 | 
					 *  HTTP over TLS (HTTPS) example sketch
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 *  This example demonstrates how to use
 | 
				
			||||||
 | 
					 *  WiFiClientSecure class to access HTTPS API.
 | 
				
			||||||
 | 
					 *  We fetch and display the status of
 | 
				
			||||||
 | 
					 *  esp8266/Arduino project continous integration
 | 
				
			||||||
 | 
					 *  build.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 *  Created by Ivan Grokhotkov, 2015.
 | 
				
			||||||
 | 
					 *  This example is in public domain.
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include <ESP8266WiFi.h>
 | 
				
			||||||
 | 
					#include <WiFiClientSecure.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const char* ssid = "........";
 | 
				
			||||||
 | 
					const char* password = "........";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const char* host = "api.github.com";
 | 
				
			||||||
 | 
					const int httpsPort = 443;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void setup() {
 | 
				
			||||||
 | 
					  Serial.begin(115200);
 | 
				
			||||||
 | 
					  Serial.println();
 | 
				
			||||||
 | 
					  Serial.print("connecting to ");
 | 
				
			||||||
 | 
					  Serial.println(ssid);
 | 
				
			||||||
 | 
					  WiFi.begin(ssid, password);
 | 
				
			||||||
 | 
					  while (WiFi.status() != WL_CONNECTED) {
 | 
				
			||||||
 | 
					    delay(500);
 | 
				
			||||||
 | 
					    Serial.print(".");
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  Serial.println("");
 | 
				
			||||||
 | 
					  Serial.println("WiFi connected");
 | 
				
			||||||
 | 
					  Serial.println("IP address: ");
 | 
				
			||||||
 | 
					  Serial.println(WiFi.localIP());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  // Use WiFiClientSecure class to create TLS connection
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  WiFiClientSecure client;
 | 
				
			||||||
 | 
					  Serial.print("connecting to ");
 | 
				
			||||||
 | 
					  Serial.println(host);
 | 
				
			||||||
 | 
					  if (!client.connect(host, httpsPort)) {
 | 
				
			||||||
 | 
					    Serial.println("connection failed");
 | 
				
			||||||
 | 
					    return;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  String url = "/repos/esp8266/Arduino/commits/esp8266/status";
 | 
				
			||||||
 | 
					  Serial.print("requesting URL: ");
 | 
				
			||||||
 | 
					  Serial.println(url);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
 | 
				
			||||||
 | 
					               "Host: " + host + "\r\n" +
 | 
				
			||||||
 | 
					               "User-Agent: BuildFailureDetectorESP8266\r\n" +
 | 
				
			||||||
 | 
					               "Connection: close\r\n\r\n");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  Serial.println("request sent");
 | 
				
			||||||
 | 
					  while (client.connected()) {
 | 
				
			||||||
 | 
					    String line = client.readStringUntil('\n');
 | 
				
			||||||
 | 
					    if (line == "\r") {
 | 
				
			||||||
 | 
					      Serial.println("headers received");
 | 
				
			||||||
 | 
					      break;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  String line = client.readStringUntil('\n');
 | 
				
			||||||
 | 
					  if (line.startsWith("{\"state\":\"success\"")) {
 | 
				
			||||||
 | 
					    Serial.println("esp8266/Arduino CI successfull!");
 | 
				
			||||||
 | 
					  } else {
 | 
				
			||||||
 | 
					    Serial.println("esp8266/Arduino CI has failed");
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  Serial.println("reply was:");
 | 
				
			||||||
 | 
					  Serial.println("==========");
 | 
				
			||||||
 | 
					  Serial.println(line);
 | 
				
			||||||
 | 
					  Serial.println("==========");
 | 
				
			||||||
 | 
					  Serial.println("closing connection");
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void loop() {
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -16,6 +16,7 @@ WiFi	KEYWORD1
 | 
				
			|||||||
WiFiClient	KEYWORD1
 | 
					WiFiClient	KEYWORD1
 | 
				
			||||||
WiFiServer	KEYWORD1
 | 
					WiFiServer	KEYWORD1
 | 
				
			||||||
WiFiUDP	KEYWORD1
 | 
					WiFiUDP	KEYWORD1
 | 
				
			||||||
 | 
					WiFiClientSecure	KEYWORD1
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#######################################
 | 
					#######################################
 | 
				
			||||||
# Methods and Functions (KEYWORD2)
 | 
					# Methods and Functions (KEYWORD2)
 | 
				
			||||||
@@ -64,4 +65,3 @@ scanNetworks	KEYWORD2
 | 
				
			|||||||
WIFI_AP	LITERAL1
 | 
					WIFI_AP	LITERAL1
 | 
				
			||||||
WIFI_STA	LITERAL1
 | 
					WIFI_STA	LITERAL1
 | 
				
			||||||
WIFI_AP_STA	LITERAL1
 | 
					WIFI_AP_STA	LITERAL1
 | 
				
			||||||
 | 
					 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user