From 90bb8779f66d091b616c65a2edae489f4193b6b8 Mon Sep 17 00:00:00 2001 From: "iot-playground.com" Date: Sat, 18 Apr 2015 16:52:42 +0200 Subject: [PATCH] added DS18B20_temperature_sensor added DS18B20_temperature_sensor ESP8266 Arduino IDE example --- .../DS18B20_temperature_sensor.ino | 124 ++++++++++++++++++ Esp8266EasyIoT/Esp8266EasyIoT.cpp | 3 +- Esp8266EasyIoT/Esp8266EasyIoT.h | 3 +- Esp8266EasyIoT/Esp8266EasyIoTConfig.h | 7 +- Esp8266EasyIoT/Esp8266EasyIoTMsg.cpp | 1 + Esp8266EasyIoT/Esp8266EasyIoTMsg.h | 1 + 6 files changed, 134 insertions(+), 5 deletions(-) create mode 100644 ESP8266ArduinoIDE/DS18B20_temperature_sensor/DS18B20_temperature_sensor.ino diff --git a/ESP8266ArduinoIDE/DS18B20_temperature_sensor/DS18B20_temperature_sensor.ino b/ESP8266ArduinoIDE/DS18B20_temperature_sensor/DS18B20_temperature_sensor.ino new file mode 100644 index 0000000..40bc773 --- /dev/null +++ b/ESP8266ArduinoIDE/DS18B20_temperature_sensor/DS18B20_temperature_sensor.ino @@ -0,0 +1,124 @@ + /* + Created by Igor Jarc + See http://iot-playground.com for details + Please use community fourum on website do not contact author directly + + Code based on https://github.com/DennisSc/easyIoT-ESPduino/blob/master/sketches/ds18b20.ino + + External libraries: + - https://github.com/adamvr/arduino-base64 + - https://github.com/milesburton/Arduino-Temperature-Control-Library + + 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 +#include +#include +#include + +//AP definitions +#define AP_SSID "your ssid" +#define AP_PASSWORD "ap password" + +// EasyIoT server definitions +#define EIOT_USERNAME "admin" +#define EIOT_PASSWORD "test" +#define EIOT_IP_ADDRESS "192.168.1.5" +#define EIOT_PORT 80 +#define EIOT_NODE "N13S0" + +#define REPORT_INTERVAL 60 // in sec + + +#define ONE_WIRE_BUS 2 // DS18B20 pin +OneWire oneWire(ONE_WIRE_BUS); +DallasTemperature DS18B20(&oneWire); + + +#define USER_PWD_LEN 40 +char unameenc[USER_PWD_LEN]; +float oldTemp; + + +void setup() { + Serial.begin(115200); + + wifiConnect(); + + char uname[USER_PWD_LEN]; + String str = String(EIOT_USERNAME)+":"+String(EIOT_PASSWORD); + str.toCharArray(uname, USER_PWD_LEN); + memset(unameenc,0,sizeof(unameenc)); + base64_encode(unameenc, uname, strlen(uname)); + oldTemp = -1; +} + +void loop() { + float temp; + + do { + DS18B20.requestTemperatures(); + temp = DS18B20.getTempCByIndex(0); + Serial.print("Temperature: "); + Serial.println(temp); + } while (temp == 85.0 || temp == (-127.0)); + + if (temp != oldTemp) + { + sendTeperature(temp); + oldTemp = temp; + } + + int cnt = REPORT_INTERVAL; + + while(cnt--) + delay(1000); +} + +void wifiConnect() +{ + Serial.print("Connecting to AP"); + WiFi.begin(AP_SSID, AP_PASSWORD); + while (WiFi.status() != WL_CONNECTED) { + delay(1000); + Serial.print("."); + } + + Serial.println(""); + Serial.println("WiFi connected"); +} + +void sendTeperature(float temp) +{ + WiFiClient client; + + while(!client.connect(EIOT_IP_ADDRESS, EIOT_PORT)) { + Serial.println("connection failed"); + wifiConnect(); + } + + String url = ""; + url += "/Api/EasyIoT/Control/Module/Virtual/"+ String(EIOT_NODE) + "/ControlLevel/"+String(temp); // generate EasIoT server node URL + + Serial.print("POST data to URL: "); + Serial.println(url); + + client.print(String("POST ") + url + " HTTP/1.1\r\n" + + "Host: " + String(EIOT_IP_ADDRESS) + "\r\n" + + "Connection: close\r\n" + + "Authorization: Basic " + unameenc + " \r\n" + + "Content-Length: 0\r\n" + + "\r\n"); + + delay(100); + while(client.available()){ + String line = client.readStringUntil('\r'); + Serial.print(line); + } + + Serial.println(); + Serial.println("Connection closed"); +} + diff --git a/Esp8266EasyIoT/Esp8266EasyIoT.cpp b/Esp8266EasyIoT/Esp8266EasyIoT.cpp index fe447f6..a330a7a 100644 --- a/Esp8266EasyIoT/Esp8266EasyIoT.cpp +++ b/Esp8266EasyIoT/Esp8266EasyIoT.cpp @@ -8,7 +8,8 @@ See http://iot-playground.com for details Please use community fourum on website do not contact author directly - + Code based on MySensors datatypes and API. http://www.mysensors.org + 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. diff --git a/Esp8266EasyIoT/Esp8266EasyIoT.h b/Esp8266EasyIoT/Esp8266EasyIoT.h index 9695000..0b47d19 100644 --- a/Esp8266EasyIoT/Esp8266EasyIoT.h +++ b/Esp8266EasyIoT/Esp8266EasyIoT.h @@ -9,7 +9,8 @@ See http://iot-playground.com for details Please use community fourum on website do not contact author directly - + Code based on MySensors datatypes and API. http://www.mysensors.org + 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. diff --git a/Esp8266EasyIoT/Esp8266EasyIoTConfig.h b/Esp8266EasyIoT/Esp8266EasyIoTConfig.h index 3106b83..d8af01b 100644 --- a/Esp8266EasyIoT/Esp8266EasyIoTConfig.h +++ b/Esp8266EasyIoT/Esp8266EasyIoTConfig.h @@ -1,9 +1,10 @@ #ifndef EasyIoTEspConfig_h #define EasyIoTEspConfig_h -#define AP_SSID "XXXXXXXX" //ACCESS POINT ID -#define AP_PASSWORD "XXXXXXXX" //ACCES POINT password -#define SERVER_IP "192.168.1.4" //EasyIoT server IP address +#define AP_SSID "JAI" //ACCESS POINT ID +#define AP_PASSWORD "danesjelepdan" //ACCES POINT password +#define SERVER_IP "192.168.1.8" //EasyIoT server IP address +//#define SERVER_IP "192.168.1.5" //EasyIoT server IP address #define SERVER_PORT "37602" //EasyIoT server port diff --git a/Esp8266EasyIoT/Esp8266EasyIoTMsg.cpp b/Esp8266EasyIoT/Esp8266EasyIoTMsg.cpp index 797a6bf..0b6ea9e 100644 --- a/Esp8266EasyIoT/Esp8266EasyIoTMsg.cpp +++ b/Esp8266EasyIoT/Esp8266EasyIoTMsg.cpp @@ -9,6 +9,7 @@ See http://iot-playground.com for details Please use community fourum on website do not contact author directly + Code based on MySensors datatypes and API. http://www.mysensors.org This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License diff --git a/Esp8266EasyIoT/Esp8266EasyIoTMsg.h b/Esp8266EasyIoT/Esp8266EasyIoTMsg.h index a29330b..f5841bf 100644 --- a/Esp8266EasyIoT/Esp8266EasyIoTMsg.h +++ b/Esp8266EasyIoT/Esp8266EasyIoTMsg.h @@ -8,6 +8,7 @@ See http://iot-playground.com for details Please use community fourum on website do not contact author directly + Code based on MySensors datatypes and API. http://www.mysensors.org This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License