-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 9ef14cc
Showing
3 changed files
with
168 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/config.h |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* | ||
* Config settings - copy to new file called "config.h" and edit | ||
*/ | ||
// Wifi settings | ||
#define WIFI_SSID "my-wifi" | ||
#define WIFI_PASS "My Password" | ||
|
||
// broker IP/host | ||
#define BROKER "10.0.0.1" | ||
// broker password or blank | ||
#define BROKER_PASS "" | ||
// base topic to publish on (will append "/<wifi MAC id>/temp") | ||
#define TOPIC_ROOT "/devices" | ||
// override default timeout from PubSubClient lib | ||
#define MQTT_KEEPALIVE 120 | ||
|
||
//Pin to use for onewire device, don't forget 4.7k resistor from +5v to data | ||
#define ONE_WIRE_BUS D4 | ||
// How long to sleep between reads (in milliseconds) - Short values seem to really warm up the DS18B20 sensor | ||
#define DELAY 10000 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
/* | ||
* ESP8266+DS18B20 temperature sensor for MQTT | ||
* | ||
* Copy sample_config.h to new file config.h and edit | ||
* | ||
* Copyright 2018 Anton Piatek | ||
* | ||
* Released under MIT license - https://anton.mit-license.org/ | ||
* | ||
*/ | ||
|
||
#include "config.h" | ||
#include <ESP8266WiFi.h> | ||
#include <PubSubClient.h> | ||
#include <OneWire.h> | ||
#include <DallasTemperature.h> // https://github.com/adafruit/MAX31850_DallasTemp/tree/master/examples/Single | ||
|
||
#ifndef WIFI_SSID | ||
#error "WIFI_SSID must be defined in config" | ||
#endif | ||
|
||
#ifndef WIFI_PASS | ||
#error "WIFI_PASS must be defined in config" | ||
#endif | ||
|
||
#ifndef BROKER | ||
#error "BROKER must be defined in config" | ||
#endif | ||
|
||
#ifndef BROKER_PASS | ||
#error "BROKER_PASS must be defined in config" | ||
#endif | ||
|
||
#ifndef TOPIC_ROOT | ||
#error "TOPIC_ROOT must be defined in config" | ||
#endif | ||
|
||
#ifndef ONE_WIRE_BUS | ||
#error "ONE_WIRE_BUS must be defined in config" | ||
#endif | ||
|
||
#ifndef DELAY | ||
#define DELAY 10000 | ||
#endif | ||
|
||
|
||
// Main program | ||
OneWire oneWire(ONE_WIRE_BUS); | ||
DallasTemperature sensors(&oneWire); | ||
DeviceAddress themometer; | ||
WiFiClient espClient; | ||
PubSubClient client(espClient); | ||
String clientId; | ||
String topic; | ||
|
||
void wait10(){ | ||
for(int i=0; i<10; i++){ | ||
delay(1000); | ||
Serial.print("_"); | ||
} | ||
Serial.println(); | ||
} | ||
|
||
void setup(void) | ||
{ | ||
Serial.begin(9600); | ||
|
||
//set wifi | ||
Serial.println(); | ||
Serial.print("WIFI configured to "); | ||
Serial.println(WIFI_SSID); | ||
WiFi.mode(WIFI_STA); | ||
WiFi.begin(WIFI_SSID, WIFI_PASS); | ||
topic = String(TOPIC_ROOT)+"/"+WiFi.macAddress()+"/temp"; | ||
Serial.print("TOPIC: "+topic); | ||
|
||
//set mqtt | ||
clientId = WiFi.macAddress(); | ||
client.setServer(BROKER, 1883); | ||
|
||
checkComms(); | ||
|
||
//setup temp sensors | ||
initSensors(); | ||
} | ||
|
||
void initSensors(){ | ||
while(true){ | ||
sensors.begin(); | ||
Serial.print("Found "); | ||
Serial.print(sensors.getDeviceCount(), DEC); | ||
Serial.println(" devices."); | ||
if (!sensors.getAddress(themometer, 0)){ | ||
Serial.println("Unable to find address for Device 0"); | ||
continue; | ||
} | ||
sensors.setResolution(themometer, 10); | ||
break; | ||
} | ||
} | ||
|
||
void checkComms(){ | ||
if(WiFi.status() != WL_CONNECTED){ | ||
while (WiFi.status() != WL_CONNECTED) { | ||
Serial.print("waiting for wifi, rc= "); | ||
Serial.println(WiFi.status()); | ||
delay(1000); | ||
//TODO while connecting flash led | ||
Serial.print("."); | ||
} | ||
Serial.println(""); | ||
Serial.println("WiFi connected"); | ||
Serial.print("IP address: "); | ||
Serial.println(WiFi.localIP()); | ||
} | ||
if(!client.connected()){ | ||
Serial.print("MQTT connecting..."); | ||
while (!client.connected()) { | ||
Serial.print("."); | ||
if (client.connect(clientId.c_str(), "use-token-auth", BROKER_PASS)) { | ||
Serial.println("connected"); | ||
}else { | ||
Serial.print("failed, rc="); | ||
Serial.println(client.state()); | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
||
void loop(void) | ||
{ | ||
checkComms(); | ||
|
||
sensors.requestTemperatures(); | ||
float tempC = sensors.getTempC(themometer); | ||
Serial.print("Temp C: "); | ||
Serial.print(tempC); | ||
Serial.println("C"); | ||
|
||
//TODO something with boolean RC | ||
client.publish (topic.c_str(), String(tempC,2).c_str(), false); | ||
|
||
|
||
//TODO #def - also how much does this affect an accurate reading | ||
delay(DELAY); | ||
} |