How to setup a new esp8266 for use with easyIOT

4 years 3 months ago #3122 by dragonteam
I want to send Temperature and humidity data fo easyiot raspberry pi server. for that i want to know how to setup a new esp8266 for easyiot.

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

4 years 3 months ago - 4 years 3 months ago #3128 by markusonfire
All you need to know is outlined in the Build Guides:

This guide uses an ESP with an Arduino for the DHT22 Temp & Humidity sensor: iot-playground.com/blog/2-uncategorised/...-and-humidity-sensor

This one uses only the ESP (No Arduino), but is temperature only. It would be very easy to adapt it to use the DTH22 for both Temp and Humidity, but you may need an ESP with more usable GPIO pins than the ESP-01 iot-playground.com/blog/2-uncategorised/...e-sensor-arduino-ide

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

4 years 2 months ago #3261 by NightOne
Here is my code that I use on an ESP01 the data pin for the dht is connected to GPIO2 on the ESP.

Change the settings that are relivent to your sensor settings as well as the ESP wifi and password settings
#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.

4 years 2 months ago #3262 by NightOne
If you get a base64 error when compiling follow this advise here

iot-playground.com/forum/esp8266-arduino...s-scope?start=6#3257

chocjulio33 has the best solution

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

Time to create page: 0.198 seconds

Forum latest

  • No posts to display.