- Posts: 1
- Thank you received: 1
Please Log in or Create an account to join the conversation.
#include <DHT.h>
#include <ESP8266WiFi.h>
#include <Base64.h>
const char* ssid = "xxxxxxxxxxxx";
const char* password = "xxxxxxxxxxxxx";
// EasyIoT server definitions
#define EIOT_USERNAME "admin"
#define EIOT_PASSWORD "test"
#define EIOT_IP_ADDRESS "192.168.0.52"
#define EIOT_PORT 80
#define EIOT_NODE "N5S0"
#define DHTPIN 2 // what pin we're connected to
#define USER_PWD_LEN 40
char unameenc[USER_PWD_LEN];
float oldTemp;
DHT dht(DHTPIN, DHT22,15);
WiFiClient client;
void setup() {
Serial.begin(57600);
delay(10);
dht.begin();
WiFi.begin(ssid, password);
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
char uname[USER_PWD_LEN];
String str = String(EIOT_USERNAME)+":"+String(EIOT_PASSWORD);
str.toCharArray(uname, USER_PWD_LEN);
memset(unameenc,0,sizeof(unameenc));
base64_encode(unameenc, uname, strlen(uname));
oldTemp = -1;
}
void loop() {
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
sendTeperature(t);
delay(1000*60*10); //millis * 60seconds * 10mins
//delay(1000*20); //millis * 60seconds * 10mins
}
void sendTeperature(float temp)
{
WiFiClient client;
while(!client.connect(EIOT_IP_ADDRESS, EIOT_PORT)) {
Serial.println("connection failed");
//wifiConnect();
}
String url = "";
url += "/Api/EasyIoT/Control/Module/Virtual/"+ String(EIOT_NODE) + "/ControlLevel/"+String(temp); // generate EasIoT server node URL
Serial.print("POST data to URL: ");
Serial.println(String("POST ") + url + " HTTP/1.1\r\n" +
"Host: " + String(EIOT_IP_ADDRESS) + "\r\n" +
"Connection: close\r\n" +
"Authorization: Basic " + unameenc + " \r\n" +
"Content-Length: 0\r\n" +
"\r\n");
client.print(String("POST ") + url + " HTTP/1.1\r\n" +
"Host: " + String(EIOT_IP_ADDRESS) + "\r\n" +
"Connection: close\r\n" +
"Authorization: Basic " + unameenc + " \r\n" +
"Content-Length: 0\r\n" +
"\r\n");
delay(100);
while(client.available()){
String line = client.readStringUntil('\r');
Serial.print(line);
}
Serial.println();
Serial.println("Connection closed");
}
Please Log in or Create an account to join the conversation.