Skip to content

Commit

Permalink
thermostat with thingspeak
Browse files Browse the repository at this point in the history
  • Loading branch information
Yurik72 committed Apr 25, 2020
1 parent 4052f6e commit 5a41d3a
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 15 deletions.
Binary file modified .vs/slnx.sqlite
Binary file not shown.
52 changes: 46 additions & 6 deletions examples/EspHap_Thermostat/EspHap_Thermostat.ino
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
#define SENSOR_TYPE_DHT
//#define SENSOR_TYPE_BME280
//#define SENSOR_TYPE_DALLAS
//#define SEND_DATA_TO_THINGSPEAK //To send data please specify your api_key to Thingspeak

const char* ssid = "ssid";
const char* password = "pwd";

#include <Arduino.h>

Expand Down Expand Up @@ -59,13 +63,14 @@ OneWire OW(DALLAS_PIN);
DallasTemperature DALLAS(&OW);
#endif

const char* ssid = "ssid";
const char* password = "pwd";


const int identity_led=2;

#ifdef SEND_DATA_TO_THINGSPEAK
//validate compiltion for issue #14
///#include "HTTPSimpleClient.h"
#include "HTTPSimpleClient.h"
#endif
///HTTPSimpleClient http;
extern "C"{
#include "homeintegration.h"
Expand All @@ -82,12 +87,14 @@ homekit_service_t* humidity=NULL;


#define SENSOR_READ_PERIOD_MS 5000
#define SEND_THINGSPEAK_PERIOD_MS 500000

struct device_data_t{
float temp=20.0;
float hum=50.0;
float pressure=1000.0;
unsigned long next_read_dht_ms=0;
unsigned long next_read_sensor_ms=0;
unsigned long next_send_thingspeak_ms=0;
};

device_data_t DeviceData;
Expand Down Expand Up @@ -211,11 +218,20 @@ void handleSetVal(){

}
void loop() {
if(DeviceData.next_read_dht_ms<=millis()){
if(DeviceData.next_read_sensor_ms<=millis()){
readSensor();
notify_hap();
DeviceData.next_read_dht_ms=millis()+SENSOR_READ_PERIOD_MS;
DeviceData.next_read_sensor_ms=millis()+SENSOR_READ_PERIOD_MS;
}

#ifdef SEND_DATA_TO_THINGSPEAK
if(DeviceData.next_send_thingspeak_ms<=millis()){
sendToThingspeak();

DeviceData.next_send_thingspeak_ms=millis()+SEND_THINGSPEAK_PERIOD_MS;
}
#endif

#ifdef ESP8266
hap_homekit_loop();
#endif
Expand Down Expand Up @@ -311,3 +327,27 @@ DALLAS.requestTemperatures(); // Send the command to get temperatures
}

}


#ifdef SEND_DATA_TO_THINGSPEAK
const char* thing_api_key="YOUR KEY";
void sendToThingspeak(){
Serial.println("sendToThingspeak start");
String url="http://api.thingspeak.com";
url=" https://api.thingspeak.com/update?api_key="+String(thing_api_key);
HTTPSimpleClient http;


url+="&field1="+String(DeviceData.temp);
url+="&field2="+String(DeviceData.hum);
url+="&field3="+String(DeviceData.pressure);
Serial.println(url);
//nt httpcode=http.POST(poststr);
if(!http.begin(url)){
Serial.println("Failed to connect to"+url );
}
int httpCode = http.GET();
Serial.println("http code returns"+String(httpCode) );

}
#endif
11 changes: 3 additions & 8 deletions examples/EspHap_Thermostat/HTTPSimpleClient.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <HardwareSerial.h>

//#define DEBUG_HTTPCLIENT

#include <Arduino.h>

Expand All @@ -15,12 +15,7 @@
#endif

#include "HTTPSimpleClient.h"
#if defined(ESPHOMECONTROLLER)
#include "config.h"
#endif
#if !defined(DBG_OUTPUT_PORT)
#define DBG_OUTPUT_PORT Serial
#endif


//#define DEBUG_HTTPCLIENT
/**
Expand Down Expand Up @@ -803,4 +798,4 @@ bool HTTPSimpleClient::downloadfile(String baseurl, String filename)
}
this->end();
return outcome;
}
}
6 changes: 5 additions & 1 deletion examples/EspHap_Thermostat/HTTPSimpleClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
#include <WiFiClientSecure.h>


#if !defined(DBG_OUTPUT_PORT)
#define DBG_OUTPUT_PORT Serial
#endif

#define HTTPCLIENT_DEFAULT_TCP_TIMEOUT (5000)

/// HTTP client errors
Expand Down Expand Up @@ -192,4 +196,4 @@ class HTTPSimpleClient



#endif /* HTTPClient_H_ */
#endif /* HTTPClient_H_ */

0 comments on commit 5a41d3a

Please sign in to comment.