Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- #include <ESP8266WiFiMulti.h>
- #include <PubSubClient.h>
- #include <OneWire.h>
- #include <DallasTemperature.h>
- // not important config (not used anywhere)
- const char* nazwa = "iotHOME_esp8266_relay_temps";
- const char* wersja = "0.6.9";
- // wifi config
- const char* ssid = "PanRouter";
- const char* password = "odjedendoosiem";
- // mqtt config
- const char* mqtt_server = "192.168.1.11";
- const int mqtt_port = 1883;
- // one wire config
- #define ONE_WIRE_BUS 5
- OneWire oneWire(ONE_WIRE_BUS);
- DallasTemperature sensors(&oneWire);
- DeviceAddress sensor1 = { 0x28, 0x73, 0x81, 0x3A, 0xA, 0x0, 0x0, 0xE0 }; // na zewnątrz
- //DeviceAddress sensor1 = { 0x28, 0xDA, 0xEF, 0x3A, 0xA, 0x0, 0x0, 0x99 }; // wewnątrz
- long lastMsg = 0;
- char msg[50];
- int value = 0;
- String topic[5];
- String topic_status[5];
- int pin[5];
- int howManyPins = 1;
- int sensor = 0;
- int lastSensor = 0;
- boolean alarm_occured = false;
- const char* outlet1_state;
- bool czy_przycisk = false;
- ESP8266WiFiMulti wifiMulti;
- WiFiClient espClient;
- PubSubClient client(espClient);
- void mqttAssign() {
- topic[0] = "home/outlets/outlet1";
- pin[0] = D5;
- }
- void outputState() {
- if (alarm_occured == false) {
- if (digitalRead(pin[0]) == HIGH) {
- outlet1_state = "1";
- } else outlet1_state = "0";
- } else outlet1_state = "alarm";
- client.publish("home/outlets/outlet1/state", outlet1_state);
- }
- void getAlarms() {
- if (alarm_occured == false) {
- delay(100);
- if (analogRead(A0) > 800) {
- client.publish("home/outlets/outlet1/alarm", "Circuit breaker has been tripped.");
- Serial.println("Circuit breaker has been tripped.");
- alarm_occured = true;
- }
- } else if (analogRead(A0) < 800) alarm_occured = false;
- delay(100);
- }
- // funkcje do czujników i innych rzeczy
- void getTemperature() {
- float temp_1;
- char temp_1_string[7];
- sensors.requestTemperatures(); // Send the command to get temperatures
- temp_1 = sensors.getTempC(sensor1);
- dtostrf(temp_1, 2, 2, temp_1_string);
- Serial.println(temp_1_string);
- client.publish("home/temps/outside", temp_1_string);
- }
- // koniec
- void mqttSubscribe() {
- for (int i = 0; i < howManyPins; i++) {
- char mqtt_topic[40];
- topic[i].toCharArray(mqtt_topic, 40);
- client.subscribe(mqtt_topic);
- }
- }
- void mqttAction(char* topic_received, char* payload) {
- for (int i = 0; i < howManyPins; i++) {
- char mqtt_topic[40];
- topic[i].toCharArray(mqtt_topic, 40);
- if (topic[i] == String(topic_received)) {
- if ((char)payload[0] == '1') {
- digitalWrite(pin[i], HIGH);
- }
- else if ((char)payload[0] == '0') {
- digitalWrite(pin[i], LOW);
- }
- }
- }
- }
- void callback(char* topic, byte* payload, unsigned int length) {
- char payload_all[30];
- Serial.print("Message arrived [");
- Serial.print(topic);
- Serial.print("] ");
- for (int i = 0; i < length; i++) {
- Serial.print((char)payload[i]);
- payload_all[i] = + payload[i];
- }
- Serial.println();
- mqttAction(topic, payload_all);
- }
- void reconnect() {
- while (!client.connected()) {
- Serial.println("Attempting MQTT connection...");
- String clientId = nazwa;
- if (client.connect(clientId.c_str())) {
- Serial.println("Connected!");
- mqttSubscribe();
- } else {
- delay(5000);
- }
- }
- }
- void przycisk() {
- if (digitalRead(D6) == LOW) {
- delay(200);
- if (digitalRead(D6) == LOW) {
- if (czy_przycisk == false) {
- client.publish("home/outlets/outlet1","1");
- czy_przycisk = true;
- }
- else {
- client.publish("home/outlets/outlet1","0");
- czy_przycisk = false;
- }
- }
- }
- }
- void setup() {
- mqttAssign();
- pinMode(pin[0], OUTPUT);
- pinMode(A0,INPUT);
- pinMode(D6, INPUT_PULLUP);
- Serial.begin(115200);
- WiFi.mode(WIFI_STA);
- wifiMulti.addAP(ssid, password);
- while (wifiMulti.run() != WL_CONNECTED) delay(500);
- Serial.println(WiFi.localIP());
- // one wire temperature sensors
- sensors.begin();
- client.setServer(mqtt_server, mqtt_port);
- client.setCallback(callback);
- }
- void loop() {
- if (!client.connected()) {
- reconnect();
- }
- client.loop();
- przycisk();
- getAlarms();
- // do every 5 seconds
- long now = millis();
- if (now - lastMsg > 5000) {
- lastMsg = now;
- getTemperature();
- outputState();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement