#include <ESP8266WiFi.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 4 // tempsensor på GPIO4
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature DS18B20(&oneWire);
const char* server = "api.thingspeak.com";
String apiKey ="**********";
const char* ssid = "**********";
const char* passord = "**********";
const float kalibrering = 0.0; // kalibrering av tempsensor
const int venteTid = 300; // dvale i sekunder
ADC_MODE(ADC_VCC);
void setup() {
// kobler til wifi, går videre etter 10 sekunder hvis mislykket
WiFi.begin(ssid, passord);
while ((millis() < 10000) && (WiFi.status() != WL_CONNECTED)) {
delay(500);
}
// leser temperatur og spenning
DS18B20.requestTemperatures();
float temp = (DS18B20.getTempCByIndex(0)) + kalibrering;
float volt = ESP.getVcc() / 1000.0;
// sender data til Thingspeak
WiFiClient client;
if (client.connect(server, 80)) {
String postStr = apiKey;
postStr += "&field1=";
postStr += String(temp);
postStr +="&field2=";
postStr += String(volt);
postStr += "\r\n\r\n";
client.print("POST /update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: " + apiKey + "\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(postStr.length());
client.print("\n\n");
client.print(postStr);
delay(100);
client.stop();
}
// setter ESP i dvale
ESP.deepSleep(venteTid * 1000000);
delay(100);
}
void loop() {}