mirror of
https://bitbucket.org/myhomie/mycorerepository.git
synced 2025-12-06 17:51:20 +00:00
82 lines
1.6 KiB
C++
82 lines
1.6 KiB
C++
#include <ESP8266WiFi.h>
|
|
#include <PubSubClient.h>
|
|
|
|
const char* ssid = "VOO-375468";
|
|
const char* password = "UYQQMTHF";
|
|
const char* mqtt_server = "192.168.0.13";
|
|
String pubString ;
|
|
|
|
char message_buff[100];
|
|
|
|
WiFiClient espClient;
|
|
PubSubClient client(espClient);
|
|
|
|
const int Led = 14;
|
|
|
|
int i;
|
|
int val;
|
|
int redpin=0;
|
|
void setup() {
|
|
pinMode(Led, OUTPUT);
|
|
pinMode(redpin,OUTPUT);
|
|
digitalWrite(Led, LOW);
|
|
Serial.begin(9600);
|
|
WiFi.softAPdisconnect(true);
|
|
WiFi.begin(ssid, password);
|
|
|
|
// Wait for connection
|
|
while (WiFi.status() != WL_CONNECTED) {
|
|
delay(500);
|
|
Serial.print(".");
|
|
}
|
|
Serial.println("");
|
|
Serial.print("Connected to ");
|
|
Serial.println(ssid);
|
|
Serial.print("IP address: ");
|
|
Serial.println(WiFi.localIP());
|
|
client.setServer(mqtt_server, 1883);
|
|
|
|
}
|
|
|
|
void reconnect() {
|
|
// Loop until we're reconnected
|
|
while (!client.connected()) {
|
|
Serial.print("Attempting MQTT connection...");
|
|
// Attempt to connect
|
|
if (client.connect("ESP8266Client-TempSensor")) {
|
|
Serial.println("connected");
|
|
} else {
|
|
Serial.print("failed, rc=");
|
|
Serial.print(client.state());
|
|
Serial.println(" try again in 5 seconds");
|
|
delay(5000);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
void loop() {
|
|
|
|
if (!client.connected()) {
|
|
reconnect();
|
|
}else{
|
|
digitalWrite(Led, HIGH);
|
|
|
|
i=analogRead(redpin);
|
|
val=(6762/(i-9))-4;
|
|
if(5<val && val<50){
|
|
pubString = String((int)val);
|
|
// pubString = "Sound : " + pubString;
|
|
pubString.toCharArray(message_buff, pubString.length()+1);
|
|
Serial.println(val);
|
|
client.publish("Sound",message_buff); }
|
|
delay(130);
|
|
}
|
|
|
|
client.loop();
|
|
|
|
|
|
|
|
|
|
}
|