Created
September 12, 2023 01:37
-
-
Save Tomcat-42/6abd9efece3ef0005ecc9b8131fc3f25 to your computer and use it in GitHub Desktop.
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> | |
#include <WiFiClient.h> | |
#include <ESP8266WebServer.h> | |
#include <ESP8266mDNS.h> | |
#include <SPI.h> | |
#include <Wire.h> | |
#include <Adafruit_SSD1306.h> | |
#include <Adafruit_Sensor.h> | |
#include <DHT.h> | |
#include <DHT_U.h> | |
// DHT config | |
#define DHTPIN D4 | |
#define DHTTYPE DHT11 | |
DHT_Unified dht(DHTPIN, DHTTYPE); | |
uint32_t delayMS; | |
// OLED config | |
#define SCREEN_WIDTH 128 | |
#define SCREEN_HEIGHT 64 | |
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin) | |
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32 | |
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); | |
// Light config | |
const int lightPin = A0; | |
// Led config | |
const int led = D5; | |
// Wifi config | |
const char* ssid = "LSC_IOT"; | |
const char* password = "12345678"; | |
const char* mqtt_server = "10.3.141.1"; | |
WiFiClient espClient; | |
PubSubClient client(espClient); | |
unsigned long lastMsg = 0; | |
#define MSG_BUFFER_SIZE (50) | |
char msg[MSG_BUFFER_SIZE]; | |
int value = 0; | |
String getTemperature() { | |
sensors_event_t event; | |
dht.temperature().getEvent(&event); | |
if (isnan(event.temperature)) { | |
Serial.println("Error reading temperature!"); | |
return "Error reading temperature!"; | |
} else { | |
Serial.print("Temperature: "); | |
Serial.print(event.temperature); | |
Serial.println(" *C"); | |
return String(event.temperature); | |
} | |
} | |
String getHumidity() { | |
sensors_event_t event; | |
dht.humidity().getEvent(&event); | |
if (isnan(event.relative_humidity)) { | |
Serial.println("Error reading humidity!"); | |
return "Error reading humidity!"; | |
} else { | |
Serial.print("Humidity: "); | |
Serial.print(event.relative_humidity); | |
Serial.println("%"); | |
return String(event.relative_humidity); | |
} | |
} | |
String getLight() { | |
int lightValue = analogRead(lightPin); | |
Serial.print("Light: "); | |
Serial.println(lightValue); | |
return String(lightValue); | |
} | |
void setup_wifi() { | |
delay(10); | |
// We start by connecting to a WiFi network | |
Serial.println(); | |
Serial.print("Connecting to "); | |
Serial.println(ssid); | |
WiFi.mode(WIFI_STA); | |
WiFi.begin(ssid, password); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
randomSeed(micros()); | |
Serial.println(""); | |
Serial.println("WiFi connected"); | |
Serial.println("IP address: "); | |
Serial.println(WiFi.localIP()); | |
} | |
// If light > 700 then turn on led | |
void lightCallback(double light){ | |
if(light >= 700) { | |
digitalWrite(led, 1); | |
} else { | |
digitalWrite(led, 0); | |
} | |
} | |
void callback(char* topic, byte* payload, unsigned int length) { | |
Serial.print("Message arrived ["); | |
Serial.print(topic); | |
Serial.print("] "); | |
for (int i = 0; i < length; i++) { | |
Serial.print((char)payload[i]); | |
} | |
Serial.println(); | |
if(String(topic) == "equipe4/luz") { | |
double light = String((char*)payload).toDouble(); | |
Serial.println(light); | |
lightCallback(light); | |
} | |
} | |
void reconnect() { | |
// Loop until we're reconnected | |
while (!client.connected()) { | |
Serial.print("Attempting MQTT connection..."); | |
// Create a random client ID | |
String clientId = "ESP8266Client-"; | |
clientId += String(random(0xffff), HEX); | |
// Attempt to connect | |
if (client.connect(clientId.c_str())) { | |
Serial.println("connected"); | |
// Once connected, publish an announcement... | |
client.publish("outTopic", "hello world"); | |
// ... and resubscribe | |
client.subscribe("inTopic"); | |
} else { | |
Serial.print("failed, rc="); | |
Serial.print(client.state()); | |
Serial.println(" try again in 5 seconds"); | |
// Wait 5 seconds before retrying | |
delay(5000); | |
} | |
} | |
} | |
void setup() { | |
pinMode(led, OUTPUT); // Initialize the BUILTIN_LED pin as an output | |
Serial.begin(115200); | |
setup_wifi(); | |
client.setServer(mqtt_server, 1883); | |
client.setCallback(callback); | |
sensor_t sensor; | |
dht.temperature().getSensor(&sensor); | |
dht.humidity().getSensor(&sensor); | |
delayMS = sensor.min_delay / 1000; | |
//digitalWrite(led, 1); | |
} | |
/* the loop function runs over and over again forever, sending: | |
- temperature to: equipe2/temperature | |
- humidity to: equipe2/humidity | |
- light to: equipe2/light | |
*/ | |
void loop() { | |
if (!client.connected()) { | |
reconnect(); | |
} | |
client.loop(); | |
unsigned long now = millis(); | |
if (now - lastMsg > 2000) { | |
lastMsg = now; | |
String temperature = getTemperature(); | |
String humidity = getHumidity(); | |
String light = getLight(); | |
client.publish("equipe2/temperatura", temperature.c_str()); | |
client.publish("equipe2/humidade", humidity.c_str()); | |
client.publish("equipe2/luz", light.c_str()); | |
client.subscribe("equipe4/luz"); | |
// client.subscribe("+/temperatura"); | |
// client.subscribe("+/humidade"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment