Commit 01fc8e00 authored by KSGoncharov's avatar KSGoncharov
Browse files

Update sketch_sep27a.ino

parents
#include <ESP8266WiFi.h>
#include <Adafruit_BME280.h>
#include <Adafruit_Sensor.h>
#include <PubSubClient.h>
#define MQTT_client "KIRILLGONCHAROVah76kkjl"
const char *ssid = "Nokia3110";
const char *pass = "11345276";
float humidity;
float temperature;
float pressure;
const char *mqtt_server = "broker.hivemq.com"; // адрес сервера MQTT
const int mqtt_port = 1883; // Порт для подключения к серверу MQTT
const char *mqtt_user = ""; // Логин от сервера MQTT
const char *mqtt_pass = ""; // Пароль от сервера MQTT
const char *data_topic="test9999/dataaa"; // топик для данных
const char *time_topic="test9999/timeee"; // топик для времени
int T=500;
int pause = 1000;
String tempstr ;
WiFiClient wclient;
PubSubClient client(wclient, mqtt_server, mqtt_port);
Adafruit_BME280 bme;
void callback(const MQTT::Publish& pub)
{
String topic = pub.topic();
Serial.print(topic);
Serial.print(" => ");
String payload = pub.payload_string();
Serial.print(payload);
Serial.println();
if (topic==time_topic)
T = payload.toInt();
}
void refreshData() {
getSensorReadings();
if (pause == 0) {
client.publish(data_topic,tempstr);
pause = T;
}
pause--;
delay(1);
}
void initBME(){
if (!bme.begin(0x76)) {
Serial.println("BE280 Error"); //Адрес I2C BME280 0x76 или 0x77
while (1);
}
}
void getSensorReadings(){
temperature = bme.readTemperature();
pressure = bme.readPressure()/ 100.0F;
tempstr = "Temperature="+String(temperature)+" Pressure="+String(pressure);
}
void setup() {
Serial.begin(115200);
//WiFi.mode(WIFI_STA);
WiFi.begin(ssid, pass);
initBME();
delay(1000);
Serial.println();
}
void loop() {
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");
}
if (WiFi.status() == WL_CONNECTED) {
if (!client.connected()) {
if (client.connect(MQTT::Connect(MQTT_client).set_auth(mqtt_user, mqtt_pass))) {
client.set_callback(callback);
client.subscribe(time_topic);
client.subscribe(data_topic);
} else {
Serial.println("MQTT - error");
}
}
if (client.connected()){
client.loop();
refreshData();
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment