- Posts: 3
- Thank you received: 0
Please Log in or Create an account to join the conversation.
Please Log in or Create an account to join the conversation.
#include <ESP8266WiFi.h>
#include <SPI.h>
#include <DHT.h>
#include <Base64.h>
const char* ssid = "Your SSID here";
const char* password = "your password here";
#define EIOT_USERNAME "admin"
#define EIOT_PASSWORD "test"
#define EIOT_IP_ADDRESS "192.168.1.15"
#define EIOT_PORT 80
#define EIOT_NODE_1 "N8S0" //change this to reflect your virtual temp senor node on EasyIOT
#define EIOT_NODE_2 "N3S0" //change this to reflect your virtual humidity senor node on EasyIOT
#define REPORT_INTERVAL 60
#define USER_PWD_LEN 40
char unameenc[USER_PWD_LEN];
float oldTemp;
float oldHumid;
// DHT Sensor Setup
#define DHTPIN 2 // We have connected the DHT to Digital Pin 2
#define DHTTYPE DHT22 // This is the type of DHT Sensor (Change it to DHT11 if you're using that model)
DHT dht(DHTPIN, DHTTYPE, 15); // Initialize DHT object
void startWiFi() {
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");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(115200);
delay(10);
startWiFi();
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;
oldHumid = -1;
}
float h, t;
void loop() {
do {
delay(2000);
t = dht.readTemperature();
h = dht.readHumidity();
Serial.print("Temperature: ");
Serial.println(t);
Serial.print("Humidity: ");
Serial.println(h);
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
} while (t == 85.0 || t == (-127.0));
if (t != oldTemp)
{
sendTeperature(t);
oldTemp = t;
}
if (h != oldHumid)
{
sendHumidity(h);
oldHumid = h;
}
int cnt = REPORT_INTERVAL;
while (cnt--)
delay(5000);
}
void sendTeperature(float t) {
WiFiClient client;
while (!client.connect(EIOT_IP_ADDRESS, EIOT_PORT)) {
Serial.println("connection failed");
}
String url = "";
url += "/Api/EasyIoT/Control/Module/Virtual/" + String(EIOT_NODE_1) + "/ControlLevel/" + String(t); // generate EasIoT server node URL
Serial.print("POST Temp data to URL: ");
Serial.println(url);
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(1000);
while (client.available()) {
String line = client.readStringUntil('\r');
Serial.print(line);
}
Serial.println();
Serial.println("Connection closed");
}
void sendHumidity(float h) {
WiFiClient client;
while (!client.connect(EIOT_IP_ADDRESS, EIOT_PORT)) {
Serial.println("connection failed");
}
String url = "";
url += "/Api/EasyIoT/Control/Module/Virtual/" + String(EIOT_NODE_2) + "/ControlLevel/" + String(h); // generate EasIoT server node URL
Serial.print("POST Humidity data to URL: ");
Serial.println(url);
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(1000);
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.
Please Log in or Create an account to join the conversation.