WTA : nodeMCU DHT11/22 send to EasyIoT server

9 years 6 months ago #1509 by lesjaw

Dennis wrote: congrats. :)

You can set the Unit in the easyIot servers module settings..

regards


ooo..i see.. thanks again Dennis.. you are really help..

Please Log in or Create an account to join the conversation.

9 years 6 months ago - 9 years 6 months ago #1510 by lesjaw
to anyone who want to use this.. here is the code..
/*
  Created by Igor Jarc
 See http://iot-playground.com for details
 Please use community fourum on website do not contact author directly
 
 Code based on https://github.com/DennisSc/easyIoT-ESPduino/blob/master/sketches/ds18b20.ino
 
 External libraries:
 - https://github.com/adamvr/arduino-base64
 - I forget where do i get this DHT Library (i think i got it from ESP8266 Arduino IDE)
 
 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 version 2 as published by the Free Software Foundation.

 Add Temperature Node in EasyIoT Server (Driver - Virtual Driver)
 Add Humidity Node in EasyIoT Server (Driver - Virtual Driver)
 */
#include <ESP8266WiFi.h>
#include <DHT.h>
#include <Base64.h>

const char* ssid = "Your SSID";
const char* password = "Your Wifi Password";
const char* host = "IP Address of EasyIoT Server";
const char* nodet = "Your Node for temperature";
const char* nodeh = "Your Node for Humidity";
const char* user = "admin";
const char* userpassword = "test";

#define DHTPIN 2 // what pin we're connected to
 
DHT dht(DHTPIN, DHT11,15);

#define USER_PWD_LEN 40
char unameenc[USER_PWD_LEN];

void setup() {
  Serial.begin(115200);
  delay(10);
  dht.begin();
  
 // Connect to WiFi network
  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");
  
  Serial.println(WiFi.localIP());
  
  char uname[USER_PWD_LEN];
  String str = String(user)+":"+String(userpassword);  
  str.toCharArray(uname, USER_PWD_LEN); 
  memset(unameenc,0,sizeof(unameenc));
  base64_encode(unameenc, uname, strlen(uname));
}

void loop() {
  Serial.print("Connecting to ");
  Serial.println(host);
  
  // Use WiFiClient class to create TCP connections for Humidity
  WiFiClient client;
  const int httpPort = 80;
  if (!client.connect(host, httpPort)) {
    Serial.println("connection failed");
    return;
  }
  
  float h = dht.readHumidity();
  
  if (isnan(h)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
 
 String urlh = "";
  urlh += "/Api/EasyIoT/Control/Module/Virtual/"+ String(nodeh) + "/ControlLevel/"+String(h); // generate EasIoT server node URL

  Serial.print("POST data to URL: ");
  Serial.println(urlh);
 
  client.print(String("POST ") + urlh + " HTTP/1.1\r\n" +
               "Host: " + String(host) + "\r\n" + 
               "Connection: close\r\n" + 
               "Authorization: Basic " + unameenc + " \r\n" + 
               "Content-Length: 0\r\n" + 
               "\r\n");      
  delay(5000);
  
  // Use WiFiClient class to create TCP connections for Temperature
  WiFiClient client2;
  const int httpPort1 = 80;
  if (!client2.connect(host, httpPort1)) {
    Serial.println("connection failed");
    return;
  }
  
  float t = dht.readTemperature();
  
  if (isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  
  String urlt = "";
  urlt += "/Api/EasyIoT/Control/Module/Virtual/"+ String(nodet) + "/ControlLevel/"+String(t); // generate EasIoT server node URL

  Serial.print("POST data to URL: ");
  Serial.println(urlt);
  
  client2.print(String("POST ") + urlt + " HTTP/1.1\r\n" +
               "Host: " + String(host) + "\r\n" + 
               "Connection: close\r\n" + 
               "Authorization: Basic " + unameenc + " \r\n" + 
               "Content-Length: 0\r\n" + 
               "\r\n");
  delay(100);
  
  // Read all the lines of the reply from server and print them to Serial
    while(client.available()){
    String line = client.readStringUntil('\r');
    Serial.print(line);
  }
  
   while(client2.available()){
    String line = client2.readStringUntil('\r');
    Serial.print(line);
  }

  Serial.println();
  Serial.println("closing connection");
  
  // Repeat every 5 minutes
  delay(300000);
}





Attachments:
The following user(s) said Thank You: ArneiO

Please Log in or Create an account to join the conversation.

9 years 6 months ago #1512 by cdj
I want to use this, we can eliminate automation, but the problem is: no float value? No 23.7 degree (for ex)
This is the reason of my calculation in automation, and in bmp you must make calculation to have a common value of pressure...

Pherhaps in this way i resolve my strange graph problems...

Please Log in or Create an account to join the conversation.

9 years 6 months ago #1514 by lesjaw

cdj wrote: I want to use this, we can eliminate automation, but the problem is: no float value? No 23.7 degree (for ex)
This is the reason of my calculation in automation, and in bmp you must make calculation to have a common value of pressure...

Pherhaps in this way i resolve my strange graph problems...



Hi CDJ..

what do you mean by no float value? No. 23.7?

Please Log in or Create an account to join the conversation.

9 years 6 months ago #1516 by cdj
Take a look of your value, is 28° and 72% , rounded and not accurate...

Please Log in or Create an account to join the conversation.

9 years 6 months ago - 9 years 6 months ago #1517 by ArneiO
As far as I know, the DHT11 always reports XX.0.

Please Log in or Create an account to join the conversation.

Time to create page: 0.422 seconds

Forum latest

  • No posts to display.