Delted <20 characters>
Do I hear water sensor?? That would be awesome, given that most people would put them all over the place if the price was right.
I’m attempting to create the temperature sensor project, I’m totally new to the esp8266 boards, I’m trying to learn, but am having some difficultly. does it also require an Arduino board? How should I go about setting this up with a new esp8266 right out of the box? I’d really appreciate any help with this.
Thanks,
James
I’ve got this working great, now i’m attempting to modify the script to add a second ds18B20 sensor, i’m having some luck, the serial console I can see both temperatures, but they are both not making it to smart things, just the first one. Heres my .ino file
/*
Created by Charles Schwer
Code based on https://github.com/DennisSc/easyIoT-ESPduino/blob/master/sketches/ds18b20.ino
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
*/
#include <ESP8266WiFi.h>
#include <OneWire.h>
#include <DallasTemperature.h>
//AP definitions
const char* ssid = “My SSID”;
const char* password = “My Password”;
#define ONE_WIRE_BUS D4 // DS18B20 pin
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature DS18B20(&oneWire);
// the following 3 ip addresses are not necessary if you are using dhcp
IPAddress ip(192, 168, 2, 81); // hardcode ip for esp
IPAddress gateway(192, 168, 2, 1); //router gateway
IPAddress subnet(255, 255, 255, 0); //lan subnet
const unsigned int serverPort = 8090; // port to run the http server on
// Smartthings hub information
IPAddress hubIp(192, 168, 2, 170); // smartthings hub ip
const unsigned int hubPort = 39500; // smartthings hub port
// default time to report temp changes
int reportInterval = 10; // in secs
WiFiServer server(serverPort); //server
WiFiClient client; //client
DeviceAddress Probe01 = { 0x28, 0xCD, 0x4D, 0x5C, 0x06, 0x00, 0x00, 0xD5 }; // "4"
DeviceAddress Probe02 = { 0x28, 0x52, 0xC4, 0x5C, 0x06, 0x00, 0x00, 0x78 }; // “4”
String readString;
int rptCnt;
void setup() {
Serial.begin(115200);
delay(10);
// Connect to WiFi network
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
// Comment out this line if you want ip assigned by router
WiFi.config(ip, gateway, subnet);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
// Start the server
server.begin();
rptCnt=reportInterval;
DS18B20.setResolution(Probe01, 10);
DS18B20.setResolution(Probe02, 10);
}
float getTemp() {
float temp;
do {
DS18B20.requestTemperatures();
temp = DS18B20.getTempCByIndex(0);
Serial.print("Temperature1: ");
Serial.println(temp);
} while (temp == 85.0 || temp == (-127.0));
return temp;
}
float getTemp2() {
float temp2;
do {
DS18B20.requestTemperatures();
temp2 = DS18B20.getTempCByIndex(1);
Serial.print("Temperature2: ");
Serial.println(temp2);
} while (temp2 == 85.0 || temp2 == (-127.0));
return temp2;
}
// send json data to client connection
void sendTempJSONData(WiFiClient client) {
String tempString = String(getTemp());
client.println(F(“CONTENT-TYPE: application/json”));
client.print(F(“CONTENT-LENGTH: “));
client.println(31+tempString.length());
client.println();
client.print(F(”{“name”:“temperature”,“value”:”));
client.print(tempString);
client.println(F("}"));
// 31 chars plus temp;
String tempString2 = String(getTemp2());
client.println(F(“CONTENT-TYPE: application/json”));
client.print(F(“CONTENT-LENGTH: “));
client.println(31+tempString2.length());
client.println();
client.print(F(”{“name”:“temperature2”,“value”:”));
client.print(tempString2);
client.println(F("}"));
// 31 chars plus temp;
}
// send json data to client connection
void sendRptIntJSONData(WiFiClient client) {
String rptIntString = String(reportInterval);
client.println(F(“CONTENT-TYPE: application/json”));
client.print(F(“CONTENT-LENGTH: “));
client.println(34+rptIntString.length());
client.println();
client.print(F(”{“name”:“reportInterval”,“value”:”));
client.print(rptIntString);
client.println(F("}"));
}
// send response to client for a request for status
void handleRequest(WiFiClient client)
{
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
//read char by char HTTP request
if (readString.length() < 100) {
//store characters to string
readString += c;
}
if (c == ‘\n’ && currentLineIsBlank) {
//now output HTML data header
if (readString.substring(readString.indexOf(’/’), readString.indexOf(’/’) + 8) == “/getTemp”) {
client.println(“HTTP/1.1 200 OK”); //send new page
sendTempJSONData(client);
} else if (readString.substring(readString.indexOf(’/’), readString.indexOf(’/’) + 11) == “/setRptInt/”) {
String newVal =readString.substring(readString.indexOf(’/’) +11);
reportInterval = newVal.substring(0,newVal.indexOf(’ ‘)).toInt();
rptCnt = reportInterval;
client.println(“HTTP/1.1 200 OK”); //send new page
sendRptIntJSONData(client);
} else if (readString.substring(readString.indexOf(’/’), readString.indexOf(’/’) + 10) == “/getRptInt”) {
client.println(“HTTP/1.1 200 OK”); //send new page
sendRptIntJSONData(client);
} else {
client.println(F(“HTTP/1.1 204 No Content”));
client.println();
client.println();
}
break;
}
if (c == ‘\n’) {
// you’re starting a new line
currentLineIsBlank = true;
} else if (c != ‘\r’) {
// you’ve gotten a character on the current line
currentLineIsBlank = false;
}
}
}
readString = “”;
delay(1);
//stopping client
client.stop();
}
// send data
int sendNotify() //client function to send/receieve POST data.
{
int returnStatus = 1;
if (client.connect(hubIp, hubPort)) {
client.println(F(“POST / HTTP/1.1”));
client.print(F(“HOST: “));
client.print(hubIp);
client.print(F(”:”));
client.println(hubPort);
sendTempJSONData(client);
}
else {
//connection failed
returnStatus = 0;
}
// read any data returned from the POST
while(client.connected() && !client.available()) delay(1); //waits for data
while (client.connected() || client.available()) { //connected or data available
char c = client.read();
}
delay(1);
client.stop();
return returnStatus;
}
void loop() {
if(rptCnt<=0) {
sendNotify();
rptCnt=reportInterval;
}
if(rptCnt>0) {
delay(1000);
rptCnt–;
}
// Handle any incoming requests
WiFiClient client = server.available();
if (client) {
handleRequest(client);
}
}
here is the groovy file:
/**
- Copyright 2016 Charles Schwer
- Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except
- in compliance with the License. You may obtain a copy of the License at:
-
http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
- on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
- for the specific language governing permissions and limitations under the License.
- ESP8266 Based Temperature Sensor
- Author: cschwer
- Date: 2016-01-23
*/
preferences {
input(“ip”, “text”, title: “IP Address”, description: “ip”, required: true)
input(“port”, “text”, title: “Port”, description: “port”, required: true)
input(“mac”, “text”, title: “MAC Addr”, description: “mac”)
}
metadata {
definition (name: “ESP8266 Dual Temperature Sensor”, namespace: “cschwer”, author: “Charles Schwer”) {
capability "Refresh"
capability "Temperature Measurement"
capability "Sensor"
capability “Polling”
}
// UI tile definitions
tiles {
valueTile("temperature", "device.temperature", width: 2, height: 2) {
state("temperature", label:'${currentValue}°', unit:"F",
backgroundColors:[
[value: 31, color: "#153591"],
[value: 44, color: "#1e9cbb"],
[value: 59, color: "#90d2a7"],
[value: 74, color: "#44b621"],
[value: 84, color: "#f1d801"],
[value: 95, color: "#d04e00"],
[value: 96, color: "#bc2323"]
]
)
}
valueTile("temperature2", "device.temperature2", width: 2, height: 2) {
state("temperature", label:'${currentValue}°', unit:"F",
backgroundColors:[
[value: 31, color: "#153591"],
[value: 44, color: "#1e9cbb"],
[value: 59, color: "#90d2a7"],
[value: 74, color: "#44b621"],
[value: 84, color: "#f1d801"],
[value: 95, color: "#d04e00"],
[value: 96, color: "#bc2323"]
]
)
}
standardTile("refresh", "device.backdoor", inactiveLabel: false, decoration: "flat") {
state "default", label:'', action:"refresh.refresh", icon:"st.secondary.refresh"
}
main "temperature"
details("temperature","refresh")
}
}
// Parse incoming device messages to generate events
def parse(String description) {
def msg = parseLanMessage(description)
def headerString = msg.header
def result = []
def bodyString = msg.body
def value = "";
if (bodyString) {
def json = msg.json;
if( json?.name == "temperature") {
if(getTemperatureScale() == "F"){
value = (celsiusToFahrenheit(json.value) as Float).round(0) as Integer
} else {
value = json.value
}
log.debug "temperature value ${value}"
result << createEvent(name: "temperature", value: value)
if( json?.name == "temperature2") {
if(getTemperatureScale() == "F"){
value2 = (celsiusToFahrenheit(json.value) as Float).round(0) as Integer
} else {
value2 = json.value
}
log.debug "temperature value ${value}"
result << createEvent(name: "temperature", value: value)
log.debug "temperature2 value ${value2}"
result << createEvent(name: "temperature2", value: value2)
}
}
result
}
}
private Integer convertHexToInt(hex) {
Integer.parseInt(hex,16)
}
private getHostAddress() {
def ip = settings.ip
def port = settings.port
log.debug "Using ip: ${ip} and port: ${port} for device: ${device.id}"
return ip + ":" + port
}
def refresh() {
log.debug "Executing ‘refresh’ ${getHostAddress()}"
if(!settings.mac) {
// if mac address is blank in settings, then use ip:port, but ST will not get updates it will only get Poll results.
log.debug "setting device network id to ip:port"
def hosthex = convertIPtoHex(settings.ip)
def porthex = convertPortToHex(settings.port)
device.deviceNetworkId = “$hosthex:$porthex”
} else {
if(device.deviceNetworkId!=settings.mac) {
log.debug "setting device network id to mac"
device.deviceNetworkId = settings.mac;
}
}
poll()
}
def poll() {
log.debug "Executing ‘poll’ ${getHostAddress()}"
new physicalgraph.device.HubAction(
method: “GET”,
path: “/getstatus”,
headers: [
HOST: “${getHostAddress()}”
]
)
}
private String convertIPtoHex(ipAddress) {
String hex = ipAddress.tokenize( ‘.’ ).collect { String.format( ‘%02x’, it.toInteger() ) }.join()
return hex
}
private String convertPortToHex(port) {
String hexport = port.toString().format( ‘%04x’, port.toInteger() )
return hexport
}
Any help would be greatly appreciated, i’m still very new to the ESP8266 as well as smart things coding.
Thanks,
James
Just wondering if there was any more work done on this? I have it running and do the http get with \getTemp, \getstatus does not work for me. I can’t however get it to display in my device. Basically when I press the refresh button, I see the esp send the data, but it never displays. If someone has copies of the old working code, I would greatly appreciate it.
Thanks,
Jason