commit 18038ecdb9a9a82c0f5fb610faa2d7a7acf47767 Author: Sebastian Hernø Gabrielli (Elev) Date: Mon Apr 19 10:54:48 2021 +0200 Initial commit diff --git a/ESP-MQTT-client-temp.ino b/ESP-MQTT-client-temp.ino new file mode 100644 index 0000000..96b1729 --- /dev/null +++ b/ESP-MQTT-client-temp.ino @@ -0,0 +1,116 @@ +#include +#include +#include + +#define O_LED 2 +int PIN_ANALOG_IN = 39; + +const char* ssid = "BFKBYOD"; +const char* password = "bfkskoleelev"; +const char* mqtt_broker = "109.74.205.66"; +int mqtt_port = 443; + +WiFiClient espClient; + +PubSubClient mqtt_client(espClient); + +String lastMsg; + +#define MSG_BUFFER_SIZE (50) +char msg[MSG_BUFFER_SIZE]; + +void setup_wifi() { + delay(10); + + Serial.println(); + Serial.print("Connecting to "); + Serial.println(ssid); + + WiFi.begin(ssid, password); + + while (WiFi.status() != WL_CONNECTED) { + delay(500); + Serial.print("."); + } + + Serial.println(""); + Serial.println("WiFi connected"); + Serial.print("IP address: "); + Serial.println(WiFi.localIP()); +} + +void callback(char* topic, byte* payload, unsigned int length) { + Serial.print("Message arrived ["); + Serial.print(topic); + Serial.print("] "); + + lastMsg = ""; + + for (int i = 0; i < length; i++) { + Serial.print((char)payload[i]); + lastMsg += (char)payload[i]; + } + Serial.println(); + + if ((String)topic == "sebastian/control" && lastMsg == "on") { + digitalWrite(O_LED, HIGH); + } else if ((String)topic == "sebastian/control" && lastMsg == "off") { + digitalWrite(O_LED, LOW); + } +} + +void reconnect () { + while (!mqtt_client.connected()) { + Serial.print("Attempting MQTT connection..."); + + String clientId = "ESP32Client-"; + clientId += String(random(0xffff), HEX); + + if (mqtt_client.connect(clientId.c_str())) { + Serial.println("connected"); + + mqtt_client.publish("ClientAnnouncements", "Sebastian"); + + mqtt_client.subscribe("inTopic"); + mqtt_client.subscribe("sebastian/control"); + } else { + Serial.print("failed, rc="); + Serial.print(mqtt_client.state()); + Serial.println(" trying again in 5 seconds"); + delay(5000); + } + } +} + +void setup() { + pinMode(O_LED, OUTPUT); + + Serial.begin(9600); + + setup_wifi(); + + mqtt_client.setServer(mqtt_broker, mqtt_port); + mqtt_client.setCallback(callback); +} + +void loop() { + if (!mqtt_client.connected()) { + reconnect(); + } + + int adcValue = analogRead(PIN_ANALOG_IN); //read ADC pin + double voltage = (float)adcValue / 4095.0 * 3.3; // calculate voltage + double Rt = 10 * voltage / (3.3 - voltage); //calculate resistance value of thermistor + double tempK = 1 / (1 / (273.15 + 25) + log(Rt / 10) / 3950.0); //calculate temperature (Kelvin) + double tempC = tempK - 273.15; //calculate temperature (Celsius) + //Serial.printf("ADC value : %d,\tVoltage : %.2fV, \tTemperature : %.2fC\n", adcValue, voltage, tempC); + + char CharArrTemp[6]; + dtostrf(tempC, 5, 2, CharArrTemp); + + mqtt_client.publish("sebastian/temp", CharArrTemp); + + delay(500); + + mqtt_client.loop(); +} diff --git a/README.md b/README.md new file mode 100644 index 0000000..1d39011 --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +#ESP MQTT Client Temperature +This is example code for sending temperature data to a MQTT broker