Last active
January 23, 2019 17:35
-
-
Save erijpkema/15eea51e733de34e584d9971eb846b0a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
I use this sketch on an esp8266 with a DHT humidity sensor. | |
*/ | |
#include "DHT.h" | |
#include <ESP8266WiFi.h> | |
#include <PubSubClient.h> | |
#include <WiFiClientSecure.h> | |
#define DHTPIN 2 // what digital pin we're connected to | |
#define DHTTYPE DHT11 // DHT 11 | |
#define CLIENT_ID "" // MQTT client id | |
// create MQTT object | |
DHT dht(DHTPIN, DHTTYPE); | |
const char* ssid = "KPN"; | |
const char* password = ""; | |
const char* KPNHost = "login.wifi.kpn.com"; // Hostname of login portal | |
const int KPNPort = 443; // Port of login portal | |
const char* mqtt_user = ""; | |
const char* mqtt_pass = ""; | |
const char* MQTTtopic = ""; | |
IPAddress server(1,2,3,4); // mqqt server | |
WiFiClient espClient; | |
PubSubClient client(server, 1883, espClient); | |
long lastMsg = 0; | |
int value = 0; | |
int attempt = 0; //th attempt at sending. | |
void setup_wifi() { | |
delay(10); | |
// We start by connecting to a WiFi network | |
Serial.println(); | |
Serial.print("Connecting to "); | |
Serial.println(ssid); | |
WiFi.begin(ssid, password); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
randomSeed(micros()); | |
Serial.println(""); | |
Serial.println("WiFi connected"); | |
Serial.println("IP address: "); | |
Serial.println(WiFi.localIP()); | |
} | |
void reconnect() { | |
// Loop until we're reconnected | |
while (!client.connected()) { | |
Serial.print("Attempting MQTT connection..."); | |
// Create a random client ID | |
String clientId = "sereno"; | |
// Attempt to connect | |
if (client.connect(clientId.c_str()), mqtt_user, mqtt_pass) { | |
Serial.println("connected"); | |
// Once connected, publish an announcement... | |
// client.publish("outTopic", "Starting..."); | |
} else { | |
Serial.print("failed, rc="); | |
Serial.print(client.state()); | |
Serial.println(" try again in 5 seconds"); | |
// Wait 5 seconds before retrying | |
delay(5000); | |
} | |
} | |
} | |
void logonKPN() { | |
WiFiClientSecure client; | |
Serial.print("*** Connecting to "); | |
Serial.print(KPNHost); | |
int retries = 5; | |
while (!!!client.connect(KPNHost, KPNPort) && (retries-- > 0)) { | |
Serial.print("."); | |
} | |
Serial.println(); | |
if (!!!client.connected()) { | |
Serial.println("*** Failed to connect to KPN"); | |
ESP.deepSleep(3600e6); // Just sleep an hour and try again. | |
} | |
// curl 'https://login.wifi.kpn.com/Home/StartSessionForFree' -H 'Host: login.wifi.kpn.com' -H 'User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/60.0' -H 'Accept: */*' -H 'Accept-Language: en-US,en;q=0.5' --compressed -H 'Referer: https://login.wifi.kpn.com/' -H 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8' -H 'X-Requested-With: XMLHttpRequest' -H 'DNT: 1' -H 'Connection: keep-alive' --data '' | |
String url = "/Home/StartSessionForFree"; | |
String postData = ""; | |
String request = "POST " + url + " HTTP/1.1\r\n" | |
"Host: login.wifi.kpn.com" + "\r\n" | |
"User-Agent: curl/7.30.0\r\n" + | |
"Accept: */*\r\n" + | |
"Accept-Language: en-US,en;q=0.5\r\n" | |
"Referer: https://login.wifi.kpn.com/\r\n" + | |
"Content-Type: application/x-www-form-urlencoded\r\n" + | |
"X-Requested-With: XMLHttpReq\r\n" + | |
"Connection: keep-alive\r\n" + | |
"DNT: 1\r\n" + | |
"Content-Length: " + postData.length() + "\r\n" + | |
"\r\n" + postData; | |
client.print(request); | |
Serial.print(request); | |
Serial.print("\n Request sent, receiving response"); | |
retries = 50 * 20 * 6; // 6 seconds | |
while (!!!client.available() && (retries-- > 0)) { | |
delay(50); | |
Serial.print("."); | |
} | |
Serial.println(); | |
while (client.available()) { | |
Serial.write(client.read()); | |
} | |
Serial.println("*** KPN logon done, closing connection"); | |
client.stop(); | |
} | |
void setup() { | |
Serial.begin(115200); | |
dht.begin(); | |
setup_wifi(); | |
logonKPN(); | |
client.setServer(server, 1883); | |
} | |
void loop() { | |
delay(2000); | |
// Reading temperature or humidity takes about 250 milliseconds! | |
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor) | |
float h = dht.readHumidity(); | |
// Read temperature as Celsius (the default) | |
float t = dht.readTemperature(); | |
// Check if any reads failed and exit early (to try again). | |
if (isnan(h) || isnan(t)) { | |
Serial.println("Failed to read from DHT sensor!"); | |
return; | |
} | |
// Compute heat index in Celsius (isFahreheit = false) | |
float hic = dht.computeHeatIndex(t, h, false); | |
Serial.print("Humidity: "); | |
Serial.print(h); | |
Serial.print(" %\t"); | |
Serial.print("Temperature: "); | |
Serial.print(t); | |
Serial.print(" *C "); | |
Serial.print("Heat index: "); | |
Serial.print(hic); | |
Serial.print(" *C \n"); | |
// Wait a few seconds between measurements. | |
pinMode(A0, INPUT); | |
unsigned int raw = analogRead(A0); | |
// lead acid battery connected with 1.1 MOHM resistor to A0 | |
float volt = raw * 14.2 / 1023.0; | |
if (!client.connected()) { | |
reconnect(); | |
} | |
client.loop(); | |
String payload = "{\"Humidity\":" + String(h) + | |
",\"Temp\":" + String(t) + | |
",\"Hic\":" + String(hic) + | |
",\"Volt\":" + String(volt) + | |
"}"; | |
delay(2000); | |
Serial.println(payload); | |
int succ = false; | |
int attempts = 10; | |
while (succ == false && attempts-- > 0) { | |
succ = client.publish(MQTTtopic, payload.c_str()); | |
Serial.print('Sending: '); | |
Serial.println(succ); | |
delay(500); | |
} | |
//Sleep for one hour. | |
ESP.deepSleep(3600e6); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment