|
| 1 | +/* |
| 2 | + * ESP8266+DS18B20 temperature sensor for MQTT |
| 3 | + * |
| 4 | + * Copy sample_config.h to new file config.h and edit |
| 5 | + * |
| 6 | + * Copyright 2018 Anton Piatek |
| 7 | + * |
| 8 | + * Released under MIT license - https://anton.mit-license.org/ |
| 9 | + * |
| 10 | + */ |
| 11 | + |
| 12 | +#include "config.h" |
| 13 | +#include <ESP8266WiFi.h> |
| 14 | +#include <PubSubClient.h> |
| 15 | +#include <OneWire.h> |
| 16 | +#include <DallasTemperature.h> // https://github.com/adafruit/MAX31850_DallasTemp/tree/master/examples/Single |
| 17 | + |
| 18 | +#ifndef WIFI_SSID |
| 19 | + #error "WIFI_SSID must be defined in config" |
| 20 | +#endif |
| 21 | + |
| 22 | +#ifndef WIFI_PASS |
| 23 | + #error "WIFI_PASS must be defined in config" |
| 24 | +#endif |
| 25 | + |
| 26 | +#ifndef BROKER |
| 27 | + #error "BROKER must be defined in config" |
| 28 | +#endif |
| 29 | + |
| 30 | +#ifndef BROKER_PASS |
| 31 | + #error "BROKER_PASS must be defined in config" |
| 32 | +#endif |
| 33 | + |
| 34 | +#ifndef TOPIC_ROOT |
| 35 | + #error "TOPIC_ROOT must be defined in config" |
| 36 | +#endif |
| 37 | + |
| 38 | +#ifndef ONE_WIRE_BUS |
| 39 | + #error "ONE_WIRE_BUS must be defined in config" |
| 40 | +#endif |
| 41 | + |
| 42 | +#ifndef DELAY |
| 43 | + #define DELAY 10000 |
| 44 | +#endif |
| 45 | + |
| 46 | + |
| 47 | +// Main program |
| 48 | +OneWire oneWire(ONE_WIRE_BUS); |
| 49 | +DallasTemperature sensors(&oneWire); |
| 50 | +DeviceAddress themometer; |
| 51 | +WiFiClient espClient; |
| 52 | +PubSubClient client(espClient); |
| 53 | +String clientId; |
| 54 | +String topic; |
| 55 | + |
| 56 | +void wait10(){ |
| 57 | + for(int i=0; i<10; i++){ |
| 58 | + delay(1000); |
| 59 | + Serial.print("_"); |
| 60 | + } |
| 61 | + Serial.println(); |
| 62 | +} |
| 63 | + |
| 64 | +void setup(void) |
| 65 | +{ |
| 66 | + Serial.begin(9600); |
| 67 | + |
| 68 | + //set wifi |
| 69 | + Serial.println(); |
| 70 | + Serial.print("WIFI configured to "); |
| 71 | + Serial.println(WIFI_SSID); |
| 72 | + WiFi.mode(WIFI_STA); |
| 73 | + WiFi.begin(WIFI_SSID, WIFI_PASS); |
| 74 | + topic = String(TOPIC_ROOT)+"/"+WiFi.macAddress()+"/temp"; |
| 75 | + Serial.print("TOPIC: "+topic); |
| 76 | + |
| 77 | + //set mqtt |
| 78 | + clientId = WiFi.macAddress(); |
| 79 | + client.setServer(BROKER, 1883); |
| 80 | + |
| 81 | + checkComms(); |
| 82 | + |
| 83 | + //setup temp sensors |
| 84 | + initSensors(); |
| 85 | +} |
| 86 | + |
| 87 | +void initSensors(){ |
| 88 | + while(true){ |
| 89 | + sensors.begin(); |
| 90 | + Serial.print("Found "); |
| 91 | + Serial.print(sensors.getDeviceCount(), DEC); |
| 92 | + Serial.println(" devices."); |
| 93 | + if (!sensors.getAddress(themometer, 0)){ |
| 94 | + Serial.println("Unable to find address for Device 0"); |
| 95 | + continue; |
| 96 | + } |
| 97 | + sensors.setResolution(themometer, 10); |
| 98 | + break; |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +void checkComms(){ |
| 103 | + if(WiFi.status() != WL_CONNECTED){ |
| 104 | + while (WiFi.status() != WL_CONNECTED) { |
| 105 | + Serial.print("waiting for wifi, rc= "); |
| 106 | + Serial.println(WiFi.status()); |
| 107 | + delay(1000); |
| 108 | + //TODO while connecting flash led |
| 109 | + Serial.print("."); |
| 110 | + } |
| 111 | + Serial.println(""); |
| 112 | + Serial.println("WiFi connected"); |
| 113 | + Serial.print("IP address: "); |
| 114 | + Serial.println(WiFi.localIP()); |
| 115 | + } |
| 116 | + if(!client.connected()){ |
| 117 | + Serial.print("MQTT connecting..."); |
| 118 | + while (!client.connected()) { |
| 119 | + Serial.print("."); |
| 120 | + if (client.connect(clientId.c_str(), "use-token-auth", BROKER_PASS)) { |
| 121 | + Serial.println("connected"); |
| 122 | + }else { |
| 123 | + Serial.print("failed, rc="); |
| 124 | + Serial.println(client.state()); |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | + |
| 131 | +void loop(void) |
| 132 | +{ |
| 133 | + checkComms(); |
| 134 | + |
| 135 | + sensors.requestTemperatures(); |
| 136 | + float tempC = sensors.getTempC(themometer); |
| 137 | + Serial.print("Temp C: "); |
| 138 | + Serial.print(tempC); |
| 139 | + Serial.println("C"); |
| 140 | + |
| 141 | + //TODO something with boolean RC |
| 142 | + client.publish (topic.c_str(), String(tempC,2).c_str(), false); |
| 143 | + |
| 144 | + |
| 145 | +//TODO #def - also how much does this affect an accurate reading |
| 146 | + delay(DELAY); |
| 147 | +} |
0 commit comments