Staram się dokonać odczytu na płytce Wemos D1 mini. W cloud supla dostaje odczyty -1°C oraz -1% wilgotności, której nawet nie dodałem do kodu.. Do odczytu danych z DHT używam biblioteki DHT.h w wersji 1.3.2, ponieważ z innymi miałem problemy.
Code: Select all
/**
* Supla.org NodeMCU WiFi minimal example
* Author: Programistyk - Kamil Kaminski <[email protected]>
*
* This example shows how to configure SuplaDevice for building for NodeMCU within Arduino IDE
*/
#include <srpc.h>
#include <log.h>
#include <eh.h>
#include <proto.h>
#include <IEEE754tools.h>
// We define our own ethernet layer
#define SUPLADEVICE_CPP
#include <SuplaDevice.h>
#include <lck.h>
#include <WiFiClient.h>
#include <ESP8266WiFiType.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiScan.h>
#include <ESP8266WiFiMulti.h>
#include <WiFiServer.h>
#include <ESP8266WiFiGeneric.h>
#include <WiFiClientSecure.h>
#include <ESP8266WiFiAP.h>
#include <ESP8266WiFiSTA.h>
#include <WiFiUdp.h>
#include <DHT.h>
int button1 = 0; //wartosc początkowa dla przycisku 1
WiFiClient client;
#define DHTPIN 2
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
// Setup Supla connection
const char* ssid = "ZHN";
const char* password = "*********";
// DHT11 odczytywanie wartosci
double get_temperature(int channelNumber, double last_val) { //ta część kodu odpowiada za odczyt temperatury
// Read temperature as Celsius (the default)
double t = dht.readTemperature();
if (isnan(t)) {
Serial.println(F("Failed to read from DHT sensor!"));
return t;
}
Serial.println(t);
}
void setup() {
Serial.begin(115200);
pinMode (16, INPUT_PULLUP); //ustawiamy Pin 16 (D0) na przycisk
// Init DHT11 library
dht.begin();
// Set temperature callback
SuplaDevice.setTemperatureCallback(&get_temperature);
delay(10);
// Replace the falowing GUID
char GUID[SUPLA_GUID_SIZE] = {0x51,0xAE,0xAA,0x11,0xF0,0x8D,0x86,0xC5,0x77,0x07,0xAF,0x60,0xF5,0x98,0xBB,0xD0};
// pobieramy identyfikator urządzenia ze strony https://www.supla.org/arduino/get-guid i wprowadzamy wiersz wyżej
// Ethernet MAC address
uint8_t mac[6] = {0x00, 0x05, 0x04, 0x03, 0x02, 0x01}; // adres mac wpisujemy dowolny
/*
* Having your device already registered at cloud.supla.org,
* you want to change CHANNEL sequence or remove any of them,
* then you must also remove the device itself from cloud.supla.org.
* Otherwise you will get "Channel conflict!" error.
*/
// CHANNEL0 - RELAY
SuplaDevice.addRelay(D5, true); //definiujemy co chcemy widzieć pod danym kanałem. Tu mamy przekaźnik. true oznacza, ze
//aktywujemy go masą. D5 to GPIO14. mozna wpisywać naprzemiennie
// CHANNEL1 - RELAY
SuplaDevice.addRelay(D6, true);
// CHANNEL2 - RELAY
SuplaDevice.addRelay(D7, true);
// CHANNEL3 - RELAY
SuplaDevice.addRelay(D8, true);
// CHANNEL4 - Thermometer dht11
SuplaDevice.addDHT11();; //kanał 4 to temomemetr
// CHANNEL - Opening sensor (Normal Open)
//SuplaDevice.addSensorNO(A0); // A0 - sensor otwarcia drzwi. na razie go nie używam. podłączamy go przez rezystor 10k
// jezeli nie wiesz o czym mowa to zajrzyj tu https://forbot.pl/blog/microswitche-proste-czujniki-przeszkod-id1870
// CHANNEL5 - Opening sensor (Normal Open)
//SuplaDevice.addSensorNO(A1); // A1 - Pin number where the sensor is connected
// CHANNEL6 - DHT22 Sensor // pozostałe możliwe czujniki do wykorzystania
// SuplaDevice.addDHT11();
// SuplaDevice.addAM2302();
// SuplaDevice.addDHT22();
SuplaDevice.begin(GUID, // Global Unique Identifier
mac, // Ethernet MAC address
"svr14.supla.org", // SUPLA server address // w miejsca xxxx wprowadzamy dane z clouda
, // Location ID
""); // Location Password
}
void loop() {
SuplaDevice.iterate();
// ponizej kod umozliwiający włączać i wyłączać przekaźnik 1 z dodatkowego fizycznego przycisku dzwonkowego. W moim przypadku
// otwieranie/zamykanie bramy
TSD_SuplaChannelNewValue przycisk1; //ustaw nazwe dla przycisku
przycisk1.SenderID = 0; // Powiadom clouda, że załączasz recznie. W przypadku siłowników ma być 0
przycisk1.ChannelNumber = 1; // nr kanału przekaźnika
przycisk1.DurationMS = 0; //czas wlaczenia
button1 = digitalRead(16);
if(digitalRead(16)==LOW){ // tu dodajemy jeszcze raz zeby nie pstrykalo samo czyli przerwa i ponowne zapytanie
delay(100);
if(digitalRead(16)==LOW){ //sprawdzam 2 razy stan na PINie 16 w odstępach 100ms . bez tego było dużo zakłuceń
przycisk1.value[0] = !przycisk1.value[0];
SuplaDevice.channelSetValue(&przycisk1);
while(digitalRead(16)==LOW);
delay(20);
}
}}
// Supla.org ethernet layer
int supla_arduino_tcp_read(void *buf, int count) {
_supla_int_t size = client.available();
if ( size > 0 ) {
if ( size > count ) size = count;
return client.read((uint8_t *)buf, size);
};
return -1;
};
int supla_arduino_tcp_write(void *buf, int count) {
return client.write((const uint8_t *)buf, count);
};
bool supla_arduino_svr_connect(const char *server, int port) {
return client.connect(server, 2015);
}
bool supla_arduino_svr_connected(void) {
return client.connected();
}
void supla_arduino_svr_disconnect(void) {
client.stop();
}
void supla_arduino_eth_setup(uint8_t mac[6], IPAddress *ip) {
// Serial.println("WiFi init");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
// Serial.print(".");
}
//Serial.print("\nlocalIP: ");
//Serial.println(WiFi.localIP());
//Serial.print("subnetMask: ");
//Serial.println(WiFi.subnetMask());
//Serial.print("gatewayIP: ");
//Serial.println(WiFi.gatewayIP());
}
SuplaDeviceCallbacks supla_arduino_get_callbacks(void) {
SuplaDeviceCallbacks cb;
cb.tcp_read = &supla_arduino_tcp_read;
cb.tcp_write = &supla_arduino_tcp_write;
cb.eth_setup = &supla_arduino_eth_setup;
cb.svr_connected = &supla_arduino_svr_connected;
cb.svr_connect = &supla_arduino_svr_connect;
cb.svr_disconnect = &supla_arduino_svr_disconnect;
cb.get_temperature = NULL; //&get_temperature;
cb.get_temperature_and_humidity = NULL;
cb.get_rgbw_value = NULL;
cb.set_rgbw_value = NULL;
return cb;
}