У меня есть мега Arduino, который контролирует 18 датчиков ds18b20, а также контролирует 18 реле.
Мега подключен к сети и отправляет / получает на сервер mqtt.
По неизвестной причине меня это зависает примерно через 18-20 часов. Я вижу, что, глядя на базу данных, куда узел сервера красный вставляет данные, полученные от мега.
Я приведу здесь код, может быть, некоторые из вас сталкивались с проблемой кодирования ... Я не вижу другой причины для этого, так что я думаю, может быть, способ, которым я написал код, нехорошо ...
#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>
#include <OneWire.h>
#include <DallasTemperature.h>
byte mac[] = { 0xD2, 0x33, 0xE1, 0x7E, 0xCA, 0x8D };
IPAddress ip(192, 168, 1, 25);
IPAddress server(192, 168, 1, 10);
// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 2
//Relays
#define pinC01 22
#define pinC02 23
#define pinC03 24
#define pinC04 25
#define pinC05 26
#define pinC06 27
#define pinC07 28
#define pinC08 29
#define pinC09 30
#define pinC10 31
#define pinC11 32
#define pinC12 33
#define pinC13 34
#define pinC14 35
#define pinC15 36
#define pinC16 37
#define pinRETUR 38
#define pinPOMPA 39
// Addresses of DS18B20s
uint8_t t_tur_intrare[8] = { 0x28, 0xFF, 0xE3, 0x2A, 0x33, 0x17, 0x03, 0xED };
uint8_t t_tur[8] = { 0x28, 0xFF, 0xA9, 0x06, 0x52, 0x17, 0x04, 0x56 };
uint8_t t_retur[8] = { 0x28, 0xFF, 0xA9, 0x39, 0x21, 0x17, 0x04, 0x8F };
uint8_t t_c01[8] = { 0x28, 0xFF, 0x91, 0xAD, 0x53, 0x17, 0x04, 0x7A };
uint8_t t_c02[8] = { 0x28, 0xFF, 0x4F, 0xD1, 0x52, 0x17, 0x04, 0x97 };
uint8_t t_c03[8] = { 0x28, 0xFF, 0x56, 0x07, 0x53, 0x17, 0x04, 0xBB };
uint8_t t_c04[8] = { 0x28, 0xFF, 0xE8, 0x08, 0x52, 0x17, 0x04, 0x67 };
uint8_t t_c05[8] = { 0x28, 0xFF, 0x05, 0xB0, 0x51, 0x17, 0x04, 0x38 };
uint8_t t_c06[8] = { 0x28, 0xFF, 0x12, 0xB6, 0x51, 0x17, 0x04, 0x1C };
uint8_t t_c07[8] = { 0x28, 0xFF, 0x68, 0x5D, 0x40, 0x17, 0x03, 0x73 };
uint8_t t_c08[8] = { 0x28, 0xFF, 0xB9, 0x0B, 0x33, 0x17, 0x03, 0x3A };
uint8_t t_c09[8] = { 0x28, 0xFF, 0x0B, 0x37, 0x21, 0x17, 0x03, 0xA9 };
uint8_t t_c10[8] = { 0x28, 0xFF, 0x54, 0xE8, 0x51, 0x17, 0x04, 0xA4 };
uint8_t t_c11[8] = { 0x28, 0xFF, 0x29, 0x0D, 0x33, 0x17, 0x03, 0x84 };
uint8_t t_c12[8] = { 0x28, 0xFF, 0x4D, 0xFC, 0x20, 0x17, 0x03, 0xDA };
uint8_t t_c13[8] = { 0x28, 0xFF, 0x66, 0xB8, 0x51, 0x17, 0x04, 0x7B };
uint8_t t_c14[8] = { 0x28, 0xFF, 0x10, 0x41, 0x52, 0x17, 0x04, 0x8C };
uint8_t t_c15[8] = { 0x28, 0xFF, 0x92, 0xC3, 0x52, 0x17, 0x04, 0x1A };
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
unsigned long previousMillis = 0;
const long interval = 90000;
//String distr_etaj_temp ;
void callback(char* topic, byte* payload, unsigned int len) {
String messageTemp;;
for (int i = 0; i < len; i++) {
messageTemp += (char)payload[i];
}
//Serial.println(messageTemp);
if (strcmp(topic,"etaj_distr_command") == 0){
if (messageTemp == "retur_off") {
relay_off(pinRETUR);
}
else if (messageTemp == "retur_on") {
relay_on(pinRETUR);
}
else if (messageTemp == "pompa_off") {
relay_off(pinPOMPA);
}
else if (messageTemp == "pompa_on") {
relay_on(pinPOMPA);
}
else if (messageTemp == "c01_off") {
relay_off(pinC01);
}
else if (messageTemp == "c01_on") {
relay_on(pinC01);
}
else if (messageTemp == "c02_off") {
relay_off(pinC02);
}
else if (messageTemp == "c02_on") {
relay_on(pinC02);
}
else if (messageTemp == "c03_off") {
relay_off(pinC03);
}
else if (messageTemp == "c03_on") {
relay_on(pinC03);
}
else if (messageTemp == "c04_off") {
relay_off(pinC04);
}
else if (messageTemp == "c04_on") {
relay_on(pinC04);
}
else if (messageTemp == "c05_off") {
relay_off(pinC05);
}
else if (messageTemp == "c05_on") {
relay_on(pinC05);
}
else if (messageTemp == "c06_off") {
relay_off(pinC06);
}
else if (messageTemp == "c06_on") {
relay_on(pinC06);
}
else if (messageTemp == "c07_off") {
relay_off(pinC07);
}
else if (messageTemp == "c07_on") {
relay_on(pinC07);
}
else if (messageTemp == "c08_off") {
relay_off(pinC08);
}
else if (messageTemp == "c08_on") {
relay_on(pinC08);
}
else if (messageTemp == "c09_off") {
relay_off(pinC09);
}
else if (messageTemp == "c09_on") {
relay_on(pinC09);
}
else if (messageTemp == "c10_off") {
relay_off(pinC10);
}
else if (messageTemp == "c10_on") {
relay_on(pinC10);
}
else if (messageTemp == "c11_off") {
relay_off(pinC11);
}
else if (messageTemp == "c11_on") {
relay_on(pinC11);
}
else if (messageTemp == "c12_off") {
relay_off(pinC12);
}
else if (messageTemp == "c12_on") {
relay_on(pinC12);
}
else if (messageTemp == "c13_off") {
relay_off(pinC13);
}
else if (messageTemp == "c13_on") {
relay_on(pinC13);
}
else if (messageTemp == "c14_off") {
relay_off(pinC14);
}
else if (messageTemp == "c14_on") {
relay_on(pinC14);
}
else if (messageTemp == "c15_off") {
relay_off(pinC15);
}
else if (messageTemp == "c15_on") {
relay_on(pinC15);
}
}
}
EthernetClient ethClient;
PubSubClient client(ethClient);
void reconnect() {
while (!client.connected()) {
if (client.connect("arduinoClient")) {
client.subscribe("etaj_distr_command");
} else {
delay(5000);
}
}
}
void setup(void)
{
Serial.begin(9600);
Serial.println("start");
sensors.begin();
pinMode(13, OUTPUT);
pinMode(pinC01, OUTPUT);
pinMode(pinC02, OUTPUT);
pinMode(pinC03, OUTPUT);
pinMode(pinC04, OUTPUT);
pinMode(pinC05, OUTPUT);
pinMode(pinC06, OUTPUT);
pinMode(pinC07, OUTPUT);
pinMode(pinC08, OUTPUT);
pinMode(pinC09, OUTPUT);
pinMode(pinC10, OUTPUT);
pinMode(pinC11, OUTPUT);
pinMode(pinC12, OUTPUT);
pinMode(pinC13, OUTPUT);
pinMode(pinC14, OUTPUT);
pinMode(pinC15, OUTPUT);
pinMode(pinC16, OUTPUT);
pinMode(pinRETUR, OUTPUT);
pinMode(pinPOMPA, OUTPUT);
digitalWrite(pinC01, HIGH);
digitalWrite(pinC02, HIGH);
digitalWrite(pinC03, HIGH);
digitalWrite(pinC04, HIGH);
digitalWrite(pinC05, HIGH);
digitalWrite(pinC06, HIGH);
digitalWrite(pinC07, HIGH);
digitalWrite(pinC08, HIGH);
digitalWrite(pinC09, HIGH);
digitalWrite(pinC10, HIGH);
digitalWrite(pinC11, HIGH);
digitalWrite(pinC12, HIGH);
digitalWrite(pinC13, HIGH);
digitalWrite(pinC14, HIGH);
digitalWrite(pinC15, HIGH);
digitalWrite(pinC16, HIGH);
digitalWrite(pinRETUR, HIGH);
digitalWrite(pinPOMPA, HIGH);
client.setServer(server, 1883);
client.setCallback(callback);
Ethernet.begin(mac, ip);
delay(1500);
}
void relay_on (byte pin){
digitalWrite(pin, LOW);
delay(50);
}
void relay_off (byte pin){
digitalWrite(pin, HIGH);
delay(50);
}
void loop() {
unsigned long currentMillis = millis();
if (!client.connected()) {
reconnect();
}
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
get_temps();
}
client.loop();
}
void get_temps() {
digitalWrite(13, HIGH);
sensors.requestTemperatures();
String distr_etaj_temp = String(sensors.getTempC(t_tur_intrare));
distr_etaj_temp = distr_etaj_temp + "|";
distr_etaj_temp = distr_etaj_temp + String(sensors.getTempC(t_tur));
distr_etaj_temp = distr_etaj_temp + "|";
distr_etaj_temp = distr_etaj_temp + String(sensors.getTempC(t_retur));
distr_etaj_temp = distr_etaj_temp + "|";
distr_etaj_temp = distr_etaj_temp + String(sensors.getTempC(t_c01));
distr_etaj_temp = distr_etaj_temp + "|";
distr_etaj_temp = distr_etaj_temp + String(sensors.getTempC(t_c02));
distr_etaj_temp = distr_etaj_temp + "|";
distr_etaj_temp = distr_etaj_temp + String(sensors.getTempC(t_c03));
distr_etaj_temp = distr_etaj_temp + "|";
distr_etaj_temp = distr_etaj_temp + String(sensors.getTempC(t_c04));
distr_etaj_temp = distr_etaj_temp + "|";
distr_etaj_temp = distr_etaj_temp + String(sensors.getTempC(t_c05));
distr_etaj_temp = distr_etaj_temp + "|";
distr_etaj_temp = distr_etaj_temp + String(sensors.getTempC(t_c06));
distr_etaj_temp = distr_etaj_temp + "|";
distr_etaj_temp = distr_etaj_temp + String(sensors.getTempC(t_c07));
distr_etaj_temp = distr_etaj_temp + "|";
distr_etaj_temp = distr_etaj_temp + String(sensors.getTempC(t_c08));
distr_etaj_temp = distr_etaj_temp + "|";
distr_etaj_temp = distr_etaj_temp + String(sensors.getTempC(t_c09));
distr_etaj_temp = distr_etaj_temp + "|";
distr_etaj_temp = distr_etaj_temp + String(sensors.getTempC(t_c10));
distr_etaj_temp = distr_etaj_temp + "|";
distr_etaj_temp = distr_etaj_temp + String(sensors.getTempC(t_c11));
distr_etaj_temp = distr_etaj_temp + "|";
distr_etaj_temp = distr_etaj_temp + String(sensors.getTempC(t_c12));
distr_etaj_temp = distr_etaj_temp + "|";
distr_etaj_temp = distr_etaj_temp + String(sensors.getTempC(t_c13));
distr_etaj_temp = distr_etaj_temp + "|";
distr_etaj_temp = distr_etaj_temp + String(sensors.getTempC(t_c14));
distr_etaj_temp = distr_etaj_temp + "|";
distr_etaj_temp = distr_etaj_temp + String(sensors.getTempC(t_c15));
int length = distr_etaj_temp.length();
const char *msgBuffer;
msgBuffer=distr_etaj_temp.c_str();
client.publish( "distribuitor-etaj" , msgBuffer );
digitalWrite(13, LOW);
}
Мне нужна помощь для оптимизации этого кода (во-первых, чтобы он не cra sh больше). Я знаю, что с использованием String не все в порядке, но я не смог найти способ пропустить его. Любая помощь будет высоко оценена Спасибо