Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
antonpiatek committed Jan 31, 2018
0 parents commit 9ef14cc
Show file tree
Hide file tree
Showing 3 changed files with 168 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/config.h
20 changes: 20 additions & 0 deletions sample_config.h
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
147 changes: 147 additions & 0 deletions wemos_temp.ino
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);
}

0 comments on commit 9ef14cc

Please sign in to comment.