client.begin("broker.shiftr.io", WiFiclient);
client.connect("environment-monitor", "<username>", "<password>")
client.publish("environment-monitor/temperature", (String)temp_c);
client.publish("environment-monitor/temperature", (String)temp_c);
client.publish("environment-monitor/humidity", (String)humidity);
client.publish("environment-monitor/pressure", (String)pressure);
/*NodeMCU IoT Environment Monitor
*
* By Roland
*
* From https://www.teachmemicro.com/nodemcu-iot-environment-monitor
*/
#include <SHT1x.h>
#include <Wire.h>
#include <Adafruit_BMP085.h>
#include <ESP8266WiFi.h>
#include <MQTTClient.h>
// Replace with your network credentials
const char* ssid = "Les Boise Engr. Innovations";
const char* password = "tingkarol";
WiFiClient WiFiclient;
MQTTClient client;
unsigned long lastMillis = 0;
String page = "";
String text = "";
String data;
// Specify data and clock connections and instantiate SHT1x object
#define dataPin 2 //D4
#define clockPin 0 //D3
SHT1x sht1x(dataPin, clockPin);
Adafruit_BMP085 bmp;
float temp_c;
float humidity;
float pressure;
void setup()
{
Serial.begin(115200); // Open serial connection to report values to host
Serial.println("Starting up");
if (!bmp.begin()) {
Serial.println("Could not find a valid BMP085 sensor, check wiring!");
while (1) {}
}
WiFi.begin(ssid, password); //begin WiFi connection
// 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());
Serial.print("connecting to MQTT broker...");
client.begin("broker.shiftr.io", WiFiclient);
connect();
}
void connect() {
while (!client.connect("environment-monitor", "7fb2fd2f", "b9f1e26ae8d3dac8")) {
Serial.print(".");
}
Serial.println("\nconnected!");
client.subscribe("environment-data");
}
void loop()
{
// Read values from the sensor
temp_c = sht1x.readTemperatureC();
humidity = sht1x.readHumidity();
pressure = bmp.readPressure();
client.loop();
if(!client.connected()) {
connect();
}
if(millis() - lastMillis > 1000) {
lastMillis = millis();
client.publish("environment-monitor/temperature", (String)temp_c);
client.publish("environment-monitor/humidity", (String)humidity);
client.publish("environment-monitor/pressure", (String)pressure);
}
}