#include <ESP8266WiFi.h>
#include "ESP8266TimerInterrupt.h"
#include "DHT.h"
#include <RH_ASK.h>
#include <SPI.h>
#define DEBUG_ENABLED false
const int DEBUG_VERBOSE = 0;
const int DEBUG_INFO = 1;
const int DEBUG_WARN = 2;
const int DEBUG_ERROR = 3;
int DEBUG_LEVEL = DEBUG_INFO;
#define DHTTYPE DHT11 // DHT 11
const int DHTPin = D5;
DHT dht(DHTPin, DHTTYPE);
const char* WIFI_SSID = "**********";
const char* WIFI_PWD = "**********";
String apiKey = "**********";
String resource = "/update?api_key=";
String server = "api.thingspeak.com";
String humidity;
String temperature;
RH_ASK transmitter(2000,16,0); //speed,RX,TX
//ESP8266Timer ITimer;
static char temperatureTemp[7];
static char humidityTemp[7];
void setup() {
if (DEBUG_ENABLED)
Serial.begin(9600);
pinMode(4,OUTPUT);
digitalWrite(4,HIGH); // Checkpoint 1: boot start
WiFi.mode(WIFI_OFF);
WiFi.forceSleepBegin();
dht.begin();
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN,HIGH);
pinMode(5,OUTPUT);
pinMode(12,OUTPUT);
pinMode(13,OUTPUT);
pinMode(15,OUTPUT);
digitalWrite(5,LOW); // boot start
digitalWrite(12,LOW); // boot start
digitalWrite(13,LOW); // boot start
digitalWrite(15,HIGH); // to power RF module
if (transmitter.init()){
digitalWrite(5,HIGH); // Checkpoint 2: transmitter ready
}
debug("Reading sensor",DEBUG_INFO) ;
readSensor();
digitalWrite(12,HIGH); // Checkpoint 3: sensor is read
debug("Sending RF",DEBUG_INFO);
send433();
digitalWrite(5,LOW); // Checkpoint 4: message sent with transmitter
digitalWrite(15,LOW);
debug("Connecting WiFi",DEBUG_INFO);
initWifi();
digitalWrite(13,HIGH); // Checkpoint 5: wifi connected
debug("Sending request",DEBUG_INFO);
makeHTTPRequest();
digitalWrite(5,HIGH); // Checkpoint 6: wifi connected
debug("Disconnecting WiFi",DEBUG_INFO);
WiFi.disconnect();
WiFi.mode(WIFI_OFF);
WiFi.mode( WIFI_OFF );
WiFi.forceSleepBegin();
digitalWrite(12,LOW); // Checkpoint 7: data sent. ready to sleep.
delay(1000); // just to see idle consumption without wifi and mpu task
debug("Turning on the LED",DEBUG_INFO);
digitalWrite(LED_BUILTIN,LOW);
delay(1000); // just to see idle consumption without wifi and mpu task but led is on
debug("Finished. Going to sleep..",DEBUG_INFO);
ESP.deepSleep(0); //Checkpoint 8: sleep
}
void loop() {
// actually, never reaches here !!
// proof: these pins will never change...
digitalWrite(4,LOW);
delay(250);
digitalWrite(4,HIGH);
}
void send433(){
const char* data = strcat(strcat("field1=", humidityTemp),strcat("&field2=",temperatureTemp));
transmitter.send((uint8_t *)data, strlen(data));
transmitter.waitPacketSent();
}
bool initWifi() {
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PWD);
int timeout = 10 * 4; // 10 seconds
while(WiFi.status() != WL_CONNECTED && (timeout-- > 0)) {
delay(250);
}
if(WiFi.status() != WL_CONNECTED) {
debug("Failed to connect, going back to sleep",DEBUG_ERROR);
return false;
}
else
{
digitalWrite(5,HIGH); // wifi connected
debug("Connected WiFi :)",DEBUG_INFO);
return true;
}
}
void readSensor()
{
float h = dht.readHumidity();
float t = dht.readTemperature();
float f = dht.readTemperature(true);
if (isnan(h) || isnan(t) || isnan(f)) {
strcpy(temperatureTemp,"Failed");
strcpy(humidityTemp, "Failed");
debug("Sensor read error",DEBUG_ERROR);
return;
}
else {
float hic = dht.computeHeatIndex(t, h, false);
dtostrf(hic, 6, 2, temperatureTemp);
float hif = dht.computeHeatIndex(f, h);
dtostrf(h, 6, 2, humidityTemp);
}
humidity = humidityTemp;
temperature = temperatureTemp;
humidity.trim();
debug("h:"+humidity,DEBUG_INFO);
temperature.trim();
debug("t:"+temperature,DEBUG_INFO);
}
bool makeHTTPRequest() {
WiFiClient client;
int retries = 5;
while(!!!client.connect(server, 80) && (retries-- > 0)) {
retries--;
}
if(!!!client.connected()) {
debug("Failed to connect, going back to sleep",DEBUG_ERROR);
return false;
}
String request = String("GET ") + resource + apiKey + "&field1=" + humidity+ "&field2=" + temperature +
" HTTP/1.1\r\n" +
"Host: " + server + "\r\n" +
"Connection: close\r\n\r\n";
client.print(request);
int timeout = 5 * 10; // 5 seconds
while(!!!client.available() && (timeout-- > 0)){
delay(100);
}
if(!!!client.available()) {
debug("No response, going back to sleep",DEBUG_ERROR);
return false;
}
while(client.available()){
client.read(); // read response
}
//... do something with the response if needed
debug("closing connection",DEBUG_INFO);
client.stop();
return true;
}
void debug(String msg, int severity){
if (DEBUG_ENABLED && severity>=DEBUG_LEVEL)
Serial.println(msg);
}