Obsah
ESP8266 a Node-RED server
Návod zaslal Radim Sejk, radimsejk@gmail.com.
1. Instalace MQTT brokeru (Linux)
Přeloženo a upraveno z https://www.digitalocean.com/community/tutorials/how-to-install-and-secure-the-mosquitto-mqtt-messaging-broker-on-ubuntu-16-04.
sudo apt-get install mosquitto mosquitto-clients
1.1. Test MQTT
Do jednoho terminálového okna zadejte:
mosquitto_sub -h localhost -t test
Do druhého terminálového okna zadejte:
mosquitto_pub -h localhost -t test -m "hello world"
Pokud je vše správně, mělo by do prvního okna dorazit „hello world“-
1.2. Nastavení hesla
Místo username vložte své uživatelské jméno a poté zadejte heslo:
sudo mosquitto_passwd -c /etc/mosquitto/passwd username
sudo nano /etc/mosquitto/conf.d/default.conf
Do právě otevřeného (ve výchozím stavu prázdném souboru) vložte:
allow_anonymous false password_file /etc/mosquitto/passwd
Uložte a poté:
sudo systemctl restart mosquitto
1.3. Test MQTT
mosquitto_pub -h localhost -t test -m „hello world“
Do jednoho terminálového okna zadejte (vyplňte vaše jméno a heslo místo username a password):
mosquitto_sub -h localhost -t test -u username -P password
Do druhého terminálového okna zadejte (vyplňte vaše jméno a heslo místo username a password):
mosquitto_pub -h localhost -t test -m "hello world" -u username -P password
Pokud je vše správně, mělo by do prvního okna dorazit „hello world“.
2. Instalace NR
Podle manuálu na webu (http://nodered.org/docs/getting-started/)
2.1. Vložení flow
Pomocí zkratky Ctrl+i otevřete okno pro import a do něj vložte následující (adresu ahoj.tmep.cz nahraďte vlastní):
[{"id":"c20df5a9.12163","type":"function","z":"4ee77fcb.856d4","name":"compose request to ahoj.tmep.cz","func":"msg.url = \"http://ahoj.tmep.cz/?T=\" + msg.payload;\nnode.status({fill:\"green\",shape:\"dot\",text:msg.url});\nreturn msg;","outputs":1,"noerr":0,"x":360,"y":300,"wires":[["df0bbacb.4261e8"]]},{"id":"df0bbacb.4261e8","type":"http request","z":"4ee77fcb.856d4","name":"","method":"GET","ret":"txt","url":"","tls":"","x":590,"y":300,"wires":[["36edf69b.2b1e52"]]},{"id":"36edf69b.2b1e52","type":"debug","z":"4ee77fcb.856d4","name":"","active":false,"console":"false","complete":"false","x":750,"y":300,"wires":[]},{"id":"cfdb600c.f52a6","type":"mqtt in","z":"4ee77fcb.856d4","name":"","topic":"topic","qos":"0","broker":"","x":150,"y":300,"wires":[["c20df5a9.12163"]]}]
2.2. Vložení flow
V první MQTT input node nastavte údaje pro připojení k vašemu brokeru (stačí server, port a autorizační údaje).
3. ESP8266 Zdrojový kód pro Arduino IDE
Na začátku zdrojového kódu nastavte hodnoty konstant podle vašeho nastavení.
#include <ESP8266WiFi.h> // WiFi library
#include <OneWire.h> // OneWire communication library for DS18B20
#include <DallasTemperature.h> // DS18B20 library
#include <PubSubClient.h> // MQTT library
// Define settings
const char ssid[] = "SSID"; // WiFi SSID
const char pass[] = "PASSWORD"; // WiFi password
unsigned int interval = 60000; // How often send data to the server. In seconds
const byte oneWireBus = 5; // Pin where is DS18B20 connected
// MQTT settings
const char* user = "USERNAME"; // MQTT auth username
const char* password = "PASSWORD"; // MQTT auth password
const char* topic = "TOPIC"; // MQTT topic to publish
const char* clientName = "CLIENTNAME"; //MQTT client name (must be unique for all clients)
IPAddress server(0, 0, 0, 0); // MQTT server address
// Global variables
char msgBuffer[16]; // buffer to hold TX message
unsigned long toa; // time of last update
// Create Temperature object "sensors"
OneWire oneWire(oneWireBus); // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
DallasTemperature sensors(&oneWire); // Pass our oneWire reference to Dallas Temperature.
//WiFi and MQTT connection
WiFiClient wifiClient; // Setup a WiFiclient instance
PubSubClient mqtt(server, 1883, wifiClient); // Setup a PubSubClient instance
void setup() {
pinMode(2, OUTPUT); // GPIO2, LED on ESP8266
// Start serial
Serial.begin(115200);
delay(10);
Serial.println();
// Connect to the WiFi
Serial.print(F("Connecting to ")); Serial.println(ssid);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
digitalWrite(2, LOW); delay(100); digitalWrite(2, HIGH); // Blinking with LED during connecting to WiFi
delay(500);
//digitalWrite(2, HIGH); // Blinking with LED during connecting to WiFi
Serial.print(F("."));
}
Serial.println();
delay(1000);
// Connect to MQTT
Serial.print(F("Connecting to MQTT server"));
mqtt.setServer(server, 1883);
while (!mqtt.connected()) {
if (mqtt.connect(clientName, user, password)) {
Serial.println(F("connected"));
}
else {
Serial.print(F("failed, rc="));
Serial.print(mqtt.state());
digitalWrite(2, LOW); delay(100); digitalWrite(2, HIGH); // Blinking with LED during connecting to WiFi
delay(500);
Serial.print(F("."));
}
}
Serial.println();
Serial.println(F("WiFi & MQTT connected "));
Serial.println(F("IP address: ")); Serial.println(WiFi.localIP());
sensors.begin(); // Initialize the DallasTemperature DS18B20 class (not strictly necessary with the client class, but good practice).
// LED on for 5 seconds after setup is done
digitalWrite(2, LOW);
delay(5000);
digitalWrite(2, HIGH);
}
void loop() {
unsigned long actualTime = millis();
sensors.requestTemperatures(); // Send the command to get temperatures. request to all devices on the bus
float t = sensors.getTempCByIndex(0); // Read temperature in "t" variable
if (t == -127.00) { // If you have connected it wrong, Dallas read this temperature! :)
Serial.println(F("Temp error!"));
// Blink 2 time when temp error
digitalWrite(2, LOW); delay(100); digitalWrite(2, HIGH); delay(100); digitalWrite(2, LOW); delay(100); digitalWrite(2, HIGH);
delay(1000);
return;
}
if (actualTime > toa + interval) { // if it is time
String outString = String(t); // convert float to string
outString.toCharArray(msgBuffer, outString.length() + 1); // convert string to char array
mqtt.publish(topic, msgBuffer); // publish data
Serial.println(F("Published"));
toa = actualTime;
}
mqtt.loop(); // let some time for MQTT functions to apply it's magic
}