Tak przepraszam Przesyłając go na forum zapomniałem umieścić Ht.toFloat (). I to samo, zapomniałem zmienić H_t na Ht.
Podczas drukowania szeregowego temperatura jest ustawiona na zero, gdy drukuje się „value changed”.
Drukuję *temp, *humidity it t samych funkcjach. Jeśli chodzi o zmienne lokalne i globalne, rozumiem, że w „void loop” każda zmienna jest typowa dla iteracji, ale jeśli „Supla” jest w tej samej iteracji, nie powinno być problemu.
Code: Select all
ESP
#include <Wire.h>
#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>
WiFiClient client;
// Setup Supla connection
const char* ssid = "...";
const char* password = "...";
String T = "";
String H = "";
String Ht = "";
String NA = "";
String string1 = "";
String string2 = "";
int n=1;
float A1;
int l_r=1, l_a=1, ven=1, rad=1, hum=1;
String l_r_s, l_a_s, vent_s, rad_s, hum_s, s_arduino;
void get_temperature_and_humidity(int channelNumber, double *temp, double *humidity) { //1
*temp=T.toFloat();
*humidity=H.toFloat();
Serial.println(*temp);
Serial.println(*humidity);
}
double get_temperature(int channelNumber, double last_val) { //1
double t=Ht.toFloat;
Serial.println(t);
return t;
}
void setup() {
Serial.begin(9600);
Wire.begin(5, 4); /* join i2c bus with SDA=D1 and SCL=D2 of NodeMCU */
delay(100);
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
pinMode(10, OUTPUT);
pinMode(9, OUTPUT);
pinMode(4, OUTPUT);
SuplaDevice.setTemperatureHumidityCallback(&get_temperature_and_humidity); //2
SuplaDevice.setTemperatureCallback(&get_temperature);
// Replace the falowing GUID
char GUID[SUPLA_GUID_SIZE] = {...};
// with GUID that you can retrieve from https://www.supla.org/arduino/get-guid
// Ethernet MAC address
uint8_t mac[6] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05};
/*
* 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.
*/
SuplaDevice.addDHT22(); //3
SuplaDevice.addDS18B20Thermometer();
//CHANNEL2 - L_r
SuplaDevice.addRelay(13); //Normally return 0
// CHANNEL3 - L-a
SuplaDevice.addRelay(12);
//CHANNEL4 - Vent
SuplaDevice.addRelay(10);
//CHANNEL5 - Rad
SuplaDevice.addRelay(9);
//CHANNEL6 - Hum
SuplaDevice.addRelay(2);
// CHANNEL8 - NA (Normal Open)
SuplaDevice.addSensorNO(NA.toFloat()); // 1.00->Full, 0.00->Empty
SuplaDevice.begin(GUID, // Global Unique Identifier
mac, // Ethernet MAC address
"...", // SUPLA server address
..., // Location ID
"..."); // Location Password
}
void loop() {
SuplaDevice.iterate();
//esp->Arduino
Wire.beginTransmission(8);
Wire.write(string_arduino.c_str());
Wire.endTransmission();
//Arduino->esp
Wire.requestFrom(8, 21);
string1=comunication(10);
string2=comunication(10);
if (string1.endsWith("&")==true){
H=string1.substring(1,8);
}
else if (string1.endsWith("&")==false){
NA=string1;
}
if (string2.endsWith("&")==true){
T=string2.substring(1,8);
}
else if (string2.endsWith("&")==false){
Ht=string2;
}
//Relay
/*
if (digitalRead(13)==true){
l_r=1;
Serial.println("Turn on");
}
else{
l_r=0;
Serial.println("Turn off");
}
if (digitalRead(12)==true){
l_a=1;
Serial.println("Turn on");
}
else{
l_a=0;
Serial.println("Turn off");
}
if (digitalRead(10)==true){
vent=1;
Serial.println("Turn on");
}
else{
ventr=0;
Serial.println("Turn off");
}
if (digitalRead(9)==true){
rad=1;
Serial.println("Turn on");
}
else{
rad=0;
Serial.println("Turn off");
}
if (digitalRead(2)==true){
hum=1;
Serial.println("Turn on");
}
else{
hum=0;
Serial.println("Turn off");
}
*/
l_r_s=String(l_r);
l_a_s=String(l_a);
vent_s=String(vent);
rad_s=String(rad);
hum_s=String(hum);
string_arduino=concat_info( l_r_s, l_a_s, vent_s, radr_s, hum_s);
}
String comunication(int size){
char difference='/';
int i=0;
String variable ="";
while(i<size){
i++;
char c=Wire.read();
if (c != difference){
variable.concat(c);
}
}
return variable;
}
String concat_info( String string1, String string2, String string3, String string4, String string5){
string1.concat(string2);
string1.concat(string3);
string1.concat(string4);
string1.concat(string5);
return string1;
}
// 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 = &get_temperature;
cb.get_temperature_and_humidity = &get_temperature_and_humidity; //4
cb.get_rgbw_value = NULL;
cb.set_rgbw_value = NULL;
return cb;
}
ARDUINO MEGA
#include <Wire.h>
String T_I2C, H_I2C,NA_I2C,H_t_I2C,string_1, string_2;
int n=1;
char c,d,e,f,g;
void setup() {
Wire.begin(8); // join i2c bus with address #8
Wire.onReceive(receiveEvent); // register receive event
Wire.onRequest(requestEvent); // register request event
Serial.begin(9600); // start serial for debug
}
void loop() {
float t=1.0;
float h=2.0;
float NA=1.0;
float h_t=3.3;
T_I2C = floatToString_I2C_1(t, 8, 2, true);
H_I2C = floatToString_I2C_1(h, 8, 2, true);
string_1=concat_info(H_I2C,T_I2C);
H_t_I2C=floatToString_I2C_2(h_t, 9, 2, true);
NA_I2C=floatToString_I2C_2(NA, 9, 2, true);
string_2=concat_info(NA_I2C,h_t_I2C);
delay(100);
}
// function that executes whenever data is received from master
void receiveEvent(int howMany) {
int i=0;
c = Wire.read();
test=c;
d = Wire.read();
e = Wire.read();
f = Wire.read();
g = Wire.read();
}
//In two times because of the 32bytes buffer limit
void requestEvent() {
if(n==1){
Wire.write(string_1.c_str());
n++;
}
else{
Wire.write(string_2.c_str());
n--;
}
}
// n -> number to convert.
// l -> lenght, default 8.
// d -> decimals, default 2.
// z ->to fill with 0 in the left , default true.
String floatToString_I2C_1( float n, int l, int d, boolean z){
char c[l+1];
char s[6];
String f;
dtostrf(n,l,d,c);
f=String(c);
if(z){
f.replace(" ","0");
}
f.concat("&"); //To differenciate from floatToString_I2C_2
f.concat("/"); //To separate the variables
return f;
}
String floatToString_I2C_2( float n, int l, int d, boolean z){
char c[l+1];
char s[6];
String f;
dtostrf(n,l,d,c);
f=String(c);
if(z){
f.replace(" ","0");
}
f.concat("/"); //To separate the variables
return f;
}
String concat_info( String string1, String string2){
string1.concat(string2);
return string1;
}