-
-
Save funrcpilot/0c3871083989544169217682e8efa8d2 to your computer and use it in GitHub Desktop.
prog
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <ESP8266WiFi.h> | |
#include <PubSubClient.h> | |
const char *ssid = "you_LINK"; // Имя вайфай точки доступа | |
const char *pass = "password"; // Пароль от точки доступа | |
const char *mqtt_server = "m21.cloudmqtt.com"; // Имя сервера MQTT | |
const int mqtt_port = 18281; // Порт для подключения к серверу MQTT | |
const char *mqtt_user = "1xvaefjxj2"; // Логи от сервер | |
const char *mqtt_pass = "3koLSriTsz0_B4"; // Пароль от сервера | |
#define BUFFER_SIZE 100 | |
//bool LedState = false; | |
int tm=0; | |
unsigned long lastMeasurement = 0; | |
unsigned long lastMeasurement2 = 0; | |
int stm =LOW; | |
// Функция получения данных от сервера | |
void callback(const MQTT::Publish& pub) { | |
Serial.print(pub.topic()); // выводим в сериал порт название топика | |
Serial.print(" => "); | |
Serial.print(pub.payload_string()); // выводим в сериал порт значение полученных данных | |
String payload = pub.payload_string(); | |
if(String(pub.topic()) == "test/fish") // проверяем из нужного ли нам топика пришли данные | |
{ | |
int stled = payload.toInt(); // преобразуем полученные данные в тип integer | |
//digitalWrite(13,stled); // включаем или выключаем светодиод в зависимоти от полученных значений данных | |
if(stled==1) | |
tm=1; | |
} | |
} | |
WiFiClient wclient; | |
PubSubClient client(wclient, mqtt_server, mqtt_port); | |
void setup() { | |
//sensors.begin(); | |
Serial.begin(115200); | |
delay(10); | |
Serial.println(); | |
Serial.println(); | |
pinMode(13, OUTPUT); //D7 | |
digitalWrite(13, stm); | |
//pinMode(12, OUTPUT); | |
//digitalWrite(12,0); | |
} | |
void loop() { | |
// подключаемся к wi-fi | |
if (WiFi.status() != WL_CONNECTED) { | |
Serial.print("Connecting to "); | |
Serial.print(ssid); | |
Serial.println("..."); | |
WiFi.begin(ssid, pass); | |
if (WiFi.waitForConnectResult() != WL_CONNECTED) | |
return; | |
Serial.println("WiFi connected"); | |
} | |
// подключаемся к MQTT серверу | |
if (WiFi.status() == WL_CONNECTED) { | |
if (!client.connected()) { | |
Serial.println("Connecting to MQTT server"); | |
if (client.connect(MQTT::Connect("arduinoClient2") | |
.set_auth(mqtt_user, mqtt_pass))) { | |
Serial.println("Connected to MQTT server"); | |
client.set_callback(callback); | |
client.subscribe("test/fish"); // подписывааемся по топик с данными для светодиода | |
} else { | |
Serial.println("Could not connect to MQTT server"); | |
} | |
} | |
if (client.connected()){ | |
client.loop(); | |
if (tm==1) | |
{ | |
client.publish("test/ok","1"); // отправляем в топик значение 1 | |
lastMeasurement = millis(); | |
delay(1); | |
lastMeasurement2 = millis(); | |
while(millis() - lastMeasurement <= 2000) | |
{ | |
if(millis() - lastMeasurement2 >= 3) | |
{ | |
stm = !stm; | |
digitalWrite(13, stm); | |
lastMeasurement2 = millis(); | |
} | |
//delay(2); | |
} | |
digitalWrite(stm, LOW); | |
TempSend(); | |
} | |
delay(1000); | |
} | |
} | |
} // конец основного цикла | |
// Функция автовыключения | |
void TempSend(){ | |
//delay(4000); | |
client.publish("test/ok","0"); // отправляем в топик значение 0 | |
client.publish("test/fish","0"); | |
tm=0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment