mirror of
https://bitbucket.org/myhomie/mycorerepository.git
synced 2025-12-06 09:41:19 +00:00
84 lines
1.5 KiB
C++
84 lines
1.5 KiB
C++
#include <ESP8266WiFi.h>
|
|
#include <WiFiClient.h>
|
|
#include <ESP8266WebServer.h>
|
|
#include <ESP8266mDNS.h>
|
|
|
|
|
|
const char* ssid = "ESP8266";
|
|
const char* password = "password";
|
|
|
|
const char* host = "192.168.4.2";
|
|
|
|
ESP8266WebServer server(80);
|
|
|
|
const int Led = 14;
|
|
|
|
|
|
void setup() {
|
|
pinMode(Led, OUTPUT);
|
|
Serial.begin(115200);
|
|
WiFi.begin(ssid, password);
|
|
Serial.println("");
|
|
digitalWrite(Led, LOW);
|
|
|
|
// Wait for connection
|
|
while (WiFi.status() != WL_CONNECTED) {
|
|
delay(500);
|
|
Serial.print(".");
|
|
}
|
|
Serial.println("");
|
|
Serial.print("Connected to ");
|
|
Serial.println(ssid);
|
|
Serial.print("IP address: ");
|
|
Serial.println(WiFi.localIP());
|
|
digitalWrite(Led, HIGH);
|
|
|
|
if (MDNS.begin("esp8266")) {
|
|
Serial.println("MDNS responder started");
|
|
}
|
|
}
|
|
|
|
|
|
void loop() {
|
|
|
|
if(WiFi.status() != WL_CONNECTED){
|
|
digitalWrite(Led, LOW);
|
|
Serial.println("Trying to reconnect !");
|
|
while (WiFi.status() != WL_CONNECTED) {
|
|
delay(500);
|
|
Serial.print(".");
|
|
}
|
|
Serial.println("");
|
|
Serial.println("Connected !");
|
|
}else{
|
|
if(digitalRead(Led)==LOW){
|
|
digitalWrite(Led, HIGH);
|
|
}
|
|
|
|
|
|
|
|
WiFiClient client;
|
|
const int httpPort = 80;
|
|
if (!client.connect(host, httpPort)) {
|
|
Serial.println("connection failed");
|
|
digitalWrite(Led, LOW);
|
|
return;
|
|
}
|
|
|
|
|
|
String urlTemp = "/temp";
|
|
Serial.print("Requesting URL: ");
|
|
|
|
client.print(String("GET ") + urlTemp + " HTTP/1.1\r\n" +
|
|
"Host: " + host + "\r\n" +
|
|
"Connection: close\r\n\r\n");
|
|
|
|
|
|
|
|
|
|
|
|
delay(20000);
|
|
|
|
}
|
|
}
|