Da var jeg i mål!
Her er koden jeg brukter, den har da både en IR-mottaker og en DHT22 temp/fukt tiloblet.
// http://platformio.org/lib/show/721/TaskScheduler/examples
#include <TaskScheduler.h>
void checkIR();
void checkDHT();
void checkWifi();
Task tskCheckIR(1000, TASK_FOREVER, &checkIR);
Task tskCheckDHT(30000, TASK_FOREVER, &checkDHT);
Task tskCheckWifi(10000, TASK_FOREVER, &checkWifi);
Scheduler runner;
// Including the ESP8266 WiFi library
#include <ESP8266WiFi.h>
#include <IRremoteESP8266.h>
#include <DHT.h>
#include <WiFiClientSecure.h>
// Use WiFiClientSecure class to create TLS connection
// https://github.com/esp8266/Arduino/blob/master/libraries/ESP8266WiFi/examples/HTTPSRequest/HTTPSRequest.ino
WiFiClientSecure client;
// Uncomment one of the lines below for whatever DHT sensor type you're using!
//#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
// Replace with your network details
const char* ssid = "....";
const char* password = ".....";
IPAddress ip(192, 168, 0, 10);
IPAddress subnet(255, 255, 255, 0);
IPAddress gateway(192, 168, 0, 1);
IPAddress dns(192, 168, 0, 1);
const char* host = "homeseer.local";
const int httpsPort = 443;
String lastTemperature;
String lastHumidity;
// DHT Sensor
const int DHTPin = D6;
// Initialize DHT sensor.
DHT dht(DHTPin, DHTTYPE);
// IR-sensor
int IRPin = D1;
IRrecv irrecv(IRPin);
decode_results IRresults;
void checkIR() {
Serial.println("Sjekker IR");
if (irrecv.decode(&IRresults)) {
irrecv.resume(); // Receive the next value
Serial.println(IRresults.value);
sendDataToHS(525, String(IRresults.value));
}
Serial.println("END: Sjekker IR");
}
void checkDHT() {
Serial.println("Sjekker DHT");
String strHumidity = String((int)(dht.readHumidity() + 0.5));
String strTemperature = String(dht.readTemperature());
if (strTemperature != lastTemperature) {
Serial.println("Temp er:" + strTemperature + " - Forrige var:" + lastTemperature);
sendDataToHS(520, strTemperature);
lastTemperature = strTemperature;
}
if (strHumidity != lastHumidity) {
Serial.println("Fukt er:" + strHumidity + " - Forrige var:" + lastHumidity);
sendDataToHS(521, strHumidity);
lastHumidity = strHumidity;
}
Serial.println("END: Sjekker DHT");
}
void checkWifi() {
Serial.println("Sjekker wifi");
if (WiFi.status() != WL_CONNECTED) {
Serial.println("Mistet wifi, reconnect...");
WiFi.begin(ssid, password);
}
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("END: Sjekker wifi");
}
// only runs once on boot
void setup() {
// Initializing serial port for debugging purposes
Serial.begin(115200);
delay(10);
// Connecting to WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.config(ip, gateway, subnet, dns);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println(WiFi.macAddress());
// Printing the ESP IP address
Serial.println(WiFi.localIP());
configTime(3 * 3600, 3600, "pool.ntp.org", "time.nist.gov");
dht.begin();
irrecv.enableIRIn(); // Start the receiver
runner.init();
runner.addTask(tskCheckDHT);
runner.addTask(tskCheckIR);
runner.addTask(tskCheckWifi);
tskCheckDHT.enable();
tskCheckIR.enable();
tskCheckWifi.enable();
}
// runs over and over again
void loop() {
runner.execute();
}
bool sendDataToHS(int dvRef, String data) {
if (!client.connect(host, httpsPort)) {
Serial.println("Connection failed");
return false;
}
String url = "/JSON?request=controldevicebyvalue&ref=" + String(dvRef) + "&value=" + String(data);
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Authorization: Basic BASE64-STRENG-HER\r\n" +
"User-Agent: ESP8266\r\n" +
"Connection: close\r\n\r\n");
Serial.println("Request sent (device " + String(dvRef) + ", data " + String(data) + ")");
while (client.connected()) {
String line = client.readStringUntil('\n');
if (line == "\r") {
Serial.println("headers received");
return true;
break;
}
}
return false;
}