- Posts: 10
- Thank you received: 0
#include <UIPEthernet.h>
#include <SPI.h>
#include <Base64.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#define EIOT_USERNAME "admin"
#define EIOT_PASSWORD "test"
#define EIOT_IP_ADDRESS "192.168.1.100"
#define EIOT_PORT 8080
#define EIOT_NODE "N6S0"
#define REPORT_INTERVAL 30 // in sec
#define ONE_WIRE_BUS 3 // DS18B20 pin
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature DS18B20(&oneWire);
#define USER_PWD_LEN 40
char unameenc[USER_PWD_LEN];
float oldTemp;
byte mac[] = { 0x74, 0x69, 0x69, 0x2D, 0x30, 0x31 };
IPAddress ip(192, 168, 1, 170);
IPAddress myDns(192, 168, 1, 1);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);
void setup() {
Serial.begin(115200);
lanConnect();
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 temp;
do {
DS18B20.requestTemperatures();
temp = DS18B20.getTempCByIndex(0);
Serial.print("Temperature: ");
Serial.println(temp);
} while (temp == 85.0 || temp == (-127.0));
if (temp != oldTemp)
{
sendTeperature(temp);
oldTemp = temp;
}
int cnt = REPORT_INTERVAL;
while (cnt--)
delay(1000);
}
void lanConnect()
{
EthernetClient client;
Serial.print("Connecting");
Ethernet.begin(mac, ip, myDns, gateway, subnet); {
delay(1000);
Serial.print(".");
}
Serial.println("");
Serial.println("Lan connected");
}
void sendTeperature(float temp)
{
EthernetClient client;
while (!client.connect(EIOT_IP_ADDRESS, EIOT_PORT)) {
Serial.println("connection failed");
lanConnect();
}
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(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(2500);
while (client.available()) {
String line = client.readStringUntil('\r');
Serial.print(line);
}
Serial.println();
Serial.println("Connection closed");
}
#include <UIPEthernet.h>
#include <SPI.h>
#include <DHT.h>
#include <Base64.h>
#define EIOT_USERNAME "admin"
#define EIOT_PASSWORD "test"
#define EIOT_IP_ADDRESS "192.168.1.100"
#define EIOT_PORT 8080
#define EIOT_NODE_1 "N7S0"
#define EIOT_NODE_2 "N20S0"
#define REPORT_INTERVAL 15
#define USER_PWD_LEN 40
char unameenc[USER_PWD_LEN];
float oldTemp;
float oldHumid;
// DHT Sensor Setup
#define DHTPIN 3 // 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); // Initialize DHT object
byte mac[] = { 0x74, 0x69, 0x69, 0x2D, 0x30, 0x31 };
IPAddress ip(192, 168, 1, 170);
IPAddress myDns(192, 168, 1, 1);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);
void lanConnect()
{
EthernetClient client;
Serial.print("Connecting");
Ethernet.begin(mac, ip, myDns, gateway, subnet); {
delay(1000);
Serial.print(".");
}
Serial.println("");
Serial.println("Lan connected");
}
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(115200);
dht.begin();
delay(10);
lanConnect();
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 {
t = dht.readTemperature();
h = dht.readHumidity();
Serial.print("Temperature: ");
Serial.println(t);
Serial.print("Humidity: ");
Serial.println(h);
} while (t == 85.0 || t == (-127.0));
if (t != oldTemp)
{
sendTemp(t);
oldTemp = t;
}
if (h != oldHumid)
{
sendHumidity(h);
oldHumid = h;
}
int cnt = REPORT_INTERVAL;
while(cnt--)
delay(1000);
}
void sendTemp(float t) {
EthernetClient client;
while(!client.connect(EIOT_IP_ADDRESS, EIOT_PORT)) {
Serial.println("connection failed");
lanConnect();
}
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(2500);
while(client.available()){
String line = client.readStringUntil('\r');
Serial.print(line);
}
Serial.println();
Serial.println("Connection closed");
}
void sendHumidity(float h) {
EthernetClient client;
while(!client.connect(EIOT_IP_ADDRESS, EIOT_PORT)) {
Serial.println("connection failed");
lanConnect();
}
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(2500);
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.
Please Log in or Create an account to join the conversation.
Please Log in or Create an account to join the conversation.
Please Log in or Create an account to join the conversation.
Please Log in or Create an account to join the conversation.