První commit
This commit is contained in:
128
nano-pt100.ino
Normal file
128
nano-pt100.ino
Normal file
@@ -0,0 +1,128 @@
|
||||
/*
|
||||
Použité knihovny:
|
||||
- MQTT PubSub: https://github.com/knolleary/pubsubclient
|
||||
- UIPEthernet pro ENC28J60: https://github.com/UIPEthernet/UIPEthernet
|
||||
- Interval (časovače): https://git.xpablo.cz/pablo2048/Interval
|
||||
|
||||
Použitý hardware:
|
||||
- Arduino Nano
|
||||
- Ethernet shield ENC28J60
|
||||
- Terminal shield pro snadné připojení vodičů
|
||||
|
||||
Odkazy na hardware:
|
||||
https://dratek.cz/arduino-platforma/1164-arduino-nano-v3.0-atmega328-16m-5v-ch340g-klon.html
|
||||
https://dratek.cz/arduino-platforma/936-ethernet-shield-pro-arduino-nano.html
|
||||
https://dratek.cz/arduino-platforma/1196-terminal-shield-pro-arduino-nano.html
|
||||
*/
|
||||
|
||||
#include <UIPEthernet.h>
|
||||
#include <PubSubClient.h>
|
||||
#include "src/Adafruit_MAX31865_library/Adafruit_MAX31865.h"
|
||||
#include "src/SharpIR/SharpIR.h"
|
||||
#include "src/Interval/src/interval.h"
|
||||
|
||||
// MQTT identifikace a připojení
|
||||
#define CLIENT_ID "nano-pt100"
|
||||
#define MQTT_SERVER "IP"
|
||||
#define MQTT_PORT "PORT"
|
||||
#define MQTT_USER "USER"
|
||||
#define MQTT_PASSWD "PASSWORD"
|
||||
|
||||
// Konfigurace MAX31865
|
||||
#define RREF 430.0 // Referenční odpor pro MAX31865 (PT100)
|
||||
|
||||
// SharpIR senzor
|
||||
#define ir A0 // Pin SharpIR
|
||||
#define model 1080 // Model senzoru SharpIR
|
||||
|
||||
// Časové intervaly
|
||||
const long sendDataInterval = 1000 * 20; // Interval 20 vteřin
|
||||
const long resetnanoInterval = 1000L * 60 * 60 * 5; // Interval 5 hodin
|
||||
|
||||
// MAC adresa pro ENC28J60
|
||||
uint8_t mac[6] = { 0x5C, 0xC0, 0xC3, 0x21, 0x6A, 0xE6 };
|
||||
|
||||
int dis = 0;
|
||||
|
||||
EthernetClient ethClient;
|
||||
PubSubClient mqttClient;
|
||||
Interval sender;
|
||||
Interval reseter;
|
||||
|
||||
// Čip PT100 – MAX31865
|
||||
Adafruit_MAX31865 max = Adafruit_MAX31865(3, 4, 5, 6);
|
||||
|
||||
// Optický senzor vzdálenosti
|
||||
SharpIR SharpIR(ir, model);
|
||||
|
||||
void setup() {
|
||||
reseter.set(resetnanoInterval); // Nastavení intervalu pro restart
|
||||
|
||||
// Start ethernetu
|
||||
if (Ethernet.begin(mac) == 0) {
|
||||
// Pokud selže inicializace, zůstane zde
|
||||
for (;;)
|
||||
;
|
||||
}
|
||||
|
||||
// MQTT nastavení
|
||||
mqttClient.setClient(ethClient);
|
||||
mqttClient.setServer(MQTT_SERVER, MQTT_PORT);
|
||||
|
||||
// Inicializace MAX31865 (PT100)
|
||||
max.begin(MAX31865_2WIRE); // Lze změnit na 4WIRE dle zapojení
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// Odesílání dat po vypršení intervalu
|
||||
if (sender.expired()) {
|
||||
sender.set(sendDataInterval);
|
||||
sendData();
|
||||
read_sharpIr();
|
||||
}
|
||||
|
||||
mqttClient.loop();
|
||||
|
||||
// Restart Arduino po vypršení intervalu
|
||||
if (reseter.expired()) {
|
||||
delay(sendDataInterval); // Malé zpoždění před resetem
|
||||
reseter.set(resetnanoInterval);
|
||||
arduinoReset();
|
||||
}
|
||||
}
|
||||
|
||||
// Odeslání dat z MAX31865 (PT100)
|
||||
void sendData() {
|
||||
char szNumber[10];
|
||||
|
||||
if (mqttClient.connect(CLIENT_ID, MQTT_USER, MQTT_PASSWD)) {
|
||||
mqttClient.publish("sensors/" CLIENT_ID "/pt100", itoa(int(max.temperature(100, RREF)), szNumber, 10));
|
||||
}
|
||||
}
|
||||
|
||||
// Čtení dat ze SharpIR a jejich odeslání
|
||||
void read_sharpIr() {
|
||||
if (mqttClient.connect(CLIENT_ID, MQTT_USER, MQTT_PASSWD)) {
|
||||
char send_dist[10];
|
||||
|
||||
dis = SharpIR.distance();
|
||||
|
||||
if (dis > 1 && dis < 80) {
|
||||
// Poslání surové vzdálenosti
|
||||
mqttClient.publish("sensors/" CLIENT_ID "/distance", itoa(int(dis), send_dist, 10));
|
||||
|
||||
// Přemapování vzdálenosti (např. pro procenta)
|
||||
dis = map(dis, 5, 70, 100, 0);
|
||||
mqttClient.publish("sensors/" CLIENT_ID "/distance_map", itoa(int(dis), send_dist, 10));
|
||||
} else {
|
||||
// Mimo rozsah – hláška o chybě
|
||||
mqttClient.publish("sensors/" CLIENT_ID "/chyba", itoa(int(dis), send_dist, 10));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Restart Arduino (nutné kvůli občasnému zamrznutí)
|
||||
// Používá skok na adresu 0 v paměti – reset MCU
|
||||
void arduinoReset() {
|
||||
asm volatile("jmp 0");
|
||||
}
|
||||
Reference in New Issue
Block a user