/*
* Simple_DHT11_Json
*
* Description: Simple Dweet Json sample code for publishing (POST metod)
* Arduino sensors to dweet.io using ESP8266
*
* Author: Andre Santana
* [AndreSantana.NET]
*
* Version: 0.9: 2020-08-14
* Initial test release
*
* Sketch: SimpleDHTjson.ino [pastebin.com/xxx]
* [pastebin.com/xxx]
*
* Hardware: Use NodeMCU ESP8266 or compatible and DHT11 / DHT22 sensor
*
* Interface:
* /---------------------\ DHT - 11 / 22 Pinout:
* |*A0 _ _ __ D0*| ---------------------
* |*RSV | | | | | D1*| 1 ==> VCC
* |*RSV | |_| |_| ~D2*| 2 ==> Data
* |*SD3 D3*| 3 ==> N.C.
* |*SD2 +---------+ D4*|--> to DHT pin 2 4 ==> GND
* |*SD1 | NodeMCU | 3V3*|
* |*CMD | ESP8266 | GND*| /-----------\
* |*SD0 +---------+ ~D5*| | # # # # # |
* |*CLK ~D6*| | # # # # # |
* |*GND ~ PWM D7*| | # # # # # |
* |*3V3 ~D8*| | # # # # # |
* |*EN RX*| | DHTxx |
* |*RST TX*| \-----------/
* |*GND +-----+ GND*|--> to DHT pin 4 | | | |
* |*Vin | USB | 3V3*|--> to DHT pin 1 1 2 3 4
* \-------+-----+-------/
*
* Tec. Details:
* DHT11 DHT22
* ------------------------------------------
* Operating Voltage......: 3 to 5V 3 to 5V
* Max Operating Current..: 2.5mA max 2.5mA max
* Humidity Range.........: 20-80% / 5% 0-100% / 2-5%
* Temperature Range......: 0-50°C / ± 2°C -40 to 80°C / ± 0.5°C
* Sampling Rate..........: 1 Hz 0.5 Hz
* Body size..............: 15.5 x 12 x 5.5mm 15.1 x 25 x 7.7mm
* Advantage..............: Low cost More Accurate
*
* License: GPL-3.0-or-later
*
* This is free software. Please donate for support our coffe or beer ;-)
* @ [AndreSantana.NET/donate] - Thank you!
*
* SimpleDweetJson is distributed under the GNU General Public License
* Version 3.0 as described below. This is free to use, as long as its
* source and author are kept.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* The Software is provided "AS IS" and "WITH ALL FAULTS,"
* without warranty of any kind, including without limitation
* the warranties of merchantability, fitness for a particular
* purpose and non-infringement.
*
* NOT FOR USE TO CONTROL PRODUCTION EQUIPMENT OR HEAVY MACHINES.
* SERIOUS INJURY COULD RESULT. Make good use of it.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* Copyright (C) 2020 by Andre Santana
*
*****************************************************************************************/
/* ----------------------- Library Imports --------------------- */
#include <WiFi.h> // ESP8266 WiFi library
#include <ArduinoJson.h> // Json library
#include <DHT.h> // DHT sensor library
/* -----------------------------------------------------------------------
Equivalence of Digital outputs between NodeMCU ESP8266 GPIOs
in Arduino IDE (uncomment the pins if you want to use).
Arduino IDE :: GPIO :: Description
-------------------------------------------------------------------------- */
// #define A0 17 // ADC0 / TOUT
// #define D0 16 // User / Wake
// #define D1 5 // I2C Bus SCL (clock)
// #define D2 4 // I2C Bus SDA (data)
// #define D3 0 // Flash
// #define D4 2 // Same as "LED_BUILTIN", but inverted logic
// #define D5 14 // SPI Bus SCK (clock)
// #define D6 12 // SPI Bus MISO
// #define D7 13 // SPI Bus MOSI
// #define D8 15 // SPI Bus SS (CS)
// #define D9 3 // RX0 (Serial console)
// #define D10 1 // TX0 (Serial console)
/* --------------------- Define Declarations -------------------- */
#define WiFiSSID "Wokwi-GUEST" // WiFi SSID name
#define WiFiPasswd "" // wireless Password
#define SerialBaudRate 115200L // set UART @ 115.200 BPS
#define DHTPIN 4 // DHT sensor pin
#define DHTTYPE DHT22 // DHT sensor type
#define httpServ "dweet.io" // dweet.io server addr
#define httpPort 80 // dweet.io server port
#define thingName "Wokwi-Device-Teste1" // dweet.io thing name
/* -------------------- Variable Declarations ------------------- */
String httpHeader; // httpHeader declaration
int t, h, i; // temp, humidity, index var
/* -------------------- Initialize Parameters ------------------- */
DHT dht(DHTPIN, DHTTYPE, 15); // setup DHT sensor
StaticJsonDocument<80> jsonDoc; // allocate the JSON document
/* ---------------------- Initialize Setup ---------------------- */
void setup() {
Serial.begin(SerialBaudRate); // initialize UART port
dht.begin(); // initialize DHT sensor
Serial.print(F("\n\nConnecting to ")); // Show connecting to
Serial.print(WiFiSSID); Serial.print(F("...")); // wireless SSID...
WiFi.begin(WiFiSSID, WiFiPasswd); // Connect WiFi network
while ((WiFi.status() != WL_CONNECTED)) { // Check for connection
delay(500); Serial.print("."); // Dot printing while connecting
}
if (WiFi.status() == WL_CONNECTED){
Serial.print(F("\n\nWiFi connected!\n\nIP address: ")); // WiFi connected
Serial.println(WiFi.localIP()); // IP address
Serial.print(F("Dweet addr.: ")); Serial.println(httpServ); // Http server
Serial.print(F("Server port: ")); Serial.println(httpPort); // Server port
Serial.print(F("Thing name: ")); Serial.println(thingName); // Thing name
Serial.print(F("\r\nHttp headers:\r\n")); // Show headers on serial (for debug)
httpHeader = "POST /dweet/for/" + String(thingName) + " HTTP/1.1\r\n"; // HTTP 1.1 header
httpHeader += "Host: " + String(httpServ) + "\r\n"; // Host identifier
httpHeader += "User-Agent: Arduino/ESP\r\n"; // User-Agent
httpHeader += "Content-Type: application/json\r\n"; // Content-Type
httpHeader += "Accept: application/json\r\n"; // Accept application
httpHeader += "Content-Length: "; // Content-Lenght
Serial.print(httpHeader + F("(depends payload data size)\r\n\r\n")); // Print headers
}
}
/* --------------- Main (Infinite) Loop Function ---------------- */
void loop() {
WiFiClient client; // Open TCP connection
if (!client.connect(httpServ, httpPort)) { // Connection check
return;
}
// multiplying by 10 for decimal precision without using float var
t = 10 * dht.readTemperature(); // read temperature in ºC
h = 10 * dht.readHumidity(); // read humidity
i = 10 * dht.computeHeatIndex(t/10, h/10, false); // heat index (isFahreheit = false)
if(t == 2147483647 || h == 2147483647){
Serial.println("Failed to read from DHT sensor!"); // check for DHT read error
delay(10E3);
return;
}
String jsonBuffer; // Initialize Json variables
jsonDoc["Temperature"] = (float)t/10; // Add temperature to jsonDoc
jsonDoc["Humidity"] = (float)h/10; // Add humidity to jsonDoc
jsonDoc["Heat_Index"] = (float)i/10; // Add heat index to jsonDoc
serializeJson(jsonDoc, jsonBuffer); // Generate the JSON buffer payload
client.print(httpHeader + String(jsonBuffer.length()) + "\r\n\r\n"); // Publish http headers
client.print(jsonBuffer); Serial.println(jsonBuffer); // Print and publish payload
delay(30E3); // Repeat every 30 seconds
}
//////////////////////////////////////////////////////////////////////////////
// This is free software. Please donate for support our coffe or beer ;-) //
// @ [AndreSantana.NET/donate] - Thank you! //
//////////////////////////////////////////////////////////////////////////////