commit a1d1374b856d09f497d03fac5c59a46850a36bce Author: brammp Date: Fri Jul 17 00:22:51 2020 +0200 Inital commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4d37315 --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +.pio +.vscode/.browse.c_cpp.db* +.vscode/c_cpp_properties.json +.vscode/launch.json +.vscode/ipch +src/config.h \ No newline at end of file diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..7c486f1 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,67 @@ +# Continuous Integration (CI) is the practice, in software +# engineering, of merging all developer working copies with a shared mainline +# several times a day < https://docs.platformio.org/page/ci/index.html > +# +# Documentation: +# +# * Travis CI Embedded Builds with PlatformIO +# < https://docs.travis-ci.com/user/integration/platformio/ > +# +# * PlatformIO integration with Travis CI +# < https://docs.platformio.org/page/ci/travis.html > +# +# * User Guide for `platformio ci` command +# < https://docs.platformio.org/page/userguide/cmd_ci.html > +# +# +# Please choose one of the following templates (proposed below) and uncomment +# it (remove "# " before each line) or use own configuration according to the +# Travis CI documentation (see above). +# + + +# +# Template #1: General project. Test it using existing `platformio.ini`. +# + +# language: python +# python: +# - "2.7" +# +# sudo: false +# cache: +# directories: +# - "~/.platformio" +# +# install: +# - pip install -U platformio +# - platformio update +# +# script: +# - platformio run + + +# +# Template #2: The project is intended to be used as a library with examples. +# + +# language: python +# python: +# - "2.7" +# +# sudo: false +# cache: +# directories: +# - "~/.platformio" +# +# env: +# - PLATFORMIO_CI_SRC=path/to/test/file.c +# - PLATFORMIO_CI_SRC=examples/file.ino +# - PLATFORMIO_CI_SRC=path/to/test/directory +# +# install: +# - pip install -U platformio +# - platformio update +# +# script: +# - platformio ci --lib="." --board=ID_1 --board=ID_2 --board=ID_N diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 0000000..0f0d740 --- /dev/null +++ b/.vscode/extensions.json @@ -0,0 +1,7 @@ +{ + // See http://go.microsoft.com/fwlink/?LinkId=827846 + // for the documentation about the extensions.json format + "recommendations": [ + "platformio.platformio-ide" + ] +} diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..528cbd4 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,38 @@ +{ + "files.associations": { + "array": "cpp", + "atomic": "cpp", + "*.tcc": "cpp", + "cctype": "cpp", + "clocale": "cpp", + "cmath": "cpp", + "cstdarg": "cpp", + "cstdint": "cpp", + "cstdio": "cpp", + "cstdlib": "cpp", + "cwchar": "cpp", + "cwctype": "cpp", + "deque": "cpp", + "list": "cpp", + "unordered_map": "cpp", + "vector": "cpp", + "exception": "cpp", + "fstream": "cpp", + "functional": "cpp", + "initializer_list": "cpp", + "iosfwd": "cpp", + "istream": "cpp", + "limits": "cpp", + "new": "cpp", + "ostream": "cpp", + "numeric": "cpp", + "sstream": "cpp", + "stdexcept": "cpp", + "streambuf": "cpp", + "cinttypes": "cpp", + "tuple": "cpp", + "type_traits": "cpp", + "utility": "cpp", + "typeinfo": "cpp" + } +} \ No newline at end of file diff --git a/include/README b/include/README new file mode 100644 index 0000000..194dcd4 --- /dev/null +++ b/include/README @@ -0,0 +1,39 @@ + +This directory is intended for project header files. + +A header file is a file containing C declarations and macro definitions +to be shared between several project source files. You request the use of a +header file in your project source file (C, C++, etc) located in `src` folder +by including it, with the C preprocessing directive `#include'. + +```src/main.c + +#include "header.h" + +int main (void) +{ + ... +} +``` + +Including a header file produces the same results as copying the header file +into each source file that needs it. Such copying would be time-consuming +and error-prone. With a header file, the related declarations appear +in only one place. If they need to be changed, they can be changed in one +place, and programs that include the header file will automatically use the +new version when next recompiled. The header file eliminates the labor of +finding and changing all the copies as well as the risk that a failure to +find one copy will result in inconsistencies within a program. + +In C, the usual convention is to give header files names that end with `.h'. +It is most portable to use only letters, digits, dashes, and underscores in +header file names, and at most one dot. + +Read more about using header files in official GCC documentation: + +* Include Syntax +* Include Operation +* Once-Only Headers +* Computed Includes + +https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html diff --git a/lib/README b/lib/README new file mode 100644 index 0000000..6debab1 --- /dev/null +++ b/lib/README @@ -0,0 +1,46 @@ + +This directory is intended for project specific (private) libraries. +PlatformIO will compile them to static libraries and link into executable file. + +The source code of each library should be placed in a an own separate directory +("lib/your_library_name/[here are source files]"). + +For example, see a structure of the following two libraries `Foo` and `Bar`: + +|--lib +| | +| |--Bar +| | |--docs +| | |--examples +| | |--src +| | |- Bar.c +| | |- Bar.h +| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html +| | +| |--Foo +| | |- Foo.c +| | |- Foo.h +| | +| |- README --> THIS FILE +| +|- platformio.ini +|--src + |- main.c + +and a contents of `src/main.c`: +``` +#include +#include + +int main (void) +{ + ... +} + +``` + +PlatformIO Library Dependency Finder will find automatically dependent +libraries scanning project source files. + +More information about PlatformIO Library Dependency Finder +- https://docs.platformio.org/page/librarymanager/ldf.html diff --git a/platformio.ini b/platformio.ini new file mode 100644 index 0000000..3290d3d --- /dev/null +++ b/platformio.ini @@ -0,0 +1,19 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter +; Upload options: custom upload port, speed and extra flags +; Library options: dependencies, extra library storages +; Advanced options: extra scripting +; +; Please visit documentation for the other options and examples +; https://docs.platformio.org/page/projectconf.html + +[env:esp01_1m] +platform = espressif8266 +board = esp01_1m +framework = arduino + + +lib_deps = + PubSubClient + ArduinoJson \ No newline at end of file diff --git a/src/config.h-Example b/src/config.h-Example new file mode 100644 index 0000000..86e3731 --- /dev/null +++ b/src/config.h-Example @@ -0,0 +1,30 @@ +// +//Rename me to config.h +// + +// Wifi Config +#define CONFIG_WIFI_SSID "" +#define CONFIG_WIFI_PASS "" +#define CONFIG_WIFI_NAME "TempProbe" + +// STATIC IP (enable by setting CONFIG_IP_STATIC to 1) +#define CONFIG_IP_STATIC 0 +IPAddress local_IP(192,168,1,10); +IPAddress gateway(192,168,1,1); +IPAddress subnet(255,255,255,0); +IPAddress dns(1,1,1,1); +// MQTT Config +#define CONFIG_MQTT_HOST "" +#define CONFIG_MQTT_PORT 1883 +#define CONFIG_MQTT_USER "username" +#define CONFIG_MQTT_PASS "password" +#define CONFIG_MQTT_CLIENT_ID "TempProbe" + +//Domoticz Config +#define CONFIG_MQTT_TOPIC "domoticz/in" +//IDX for virtual Temp + Humidity sensor +#define CONFIG_MQTT_IDX_Comb +//IDX for virtual Temp sensor +#define CONFIG_MQTT_IDX_Temp +//IDX for virtual Humidity sensor +#define CONFIG_MQTT_IDX_Hum diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..12bc902 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,164 @@ +#include +#include +#include +#include +#include +#include + +// Default(A) SHT30 I2C address is 0x44 +// B SHT30 I2C address is 0x45 +#define Addr 0x44 + +//WIFI +const char* ssid = CONFIG_WIFI_SSID; // Enter your WiFi name +const char* password = CONFIG_WIFI_PASS; // Enter WiFi password + +//MQTT +const char* mqttServer = CONFIG_MQTT_HOST; +const char* mqttUser = CONFIG_MQTT_USER; +const int mqttPort = CONFIG_MQTT_PORT; +const char* mqttPassword = CONFIG_MQTT_PASS; +const char* mqttClientId = CONFIG_MQTT_CLIENT_ID; + +//Domoticz +const char* domotopic = CONFIG_MQTT_TOPIC; +const int idxcomb = CONFIG_MQTT_IDX_Comb; +const int idxtemp = CONFIG_MQTT_IDX_Temp; +const int idxhum = CONFIG_MQTT_IDX_Hum; + +WiFiClient espClient; +PubSubClient client(espClient); + +void setup() { + // Initialise I2C communication as MASTER + Wire.begin(0, 2); + // Initialise serial communication, set baud rate = 9600 + Serial.begin(9600); + + + if (CONFIG_IP_STATIC == 1){ + if (!WiFi.config(local_IP, gateway, subnet,dns)) { + Serial.println("Static IP configration error"); + } + } + WiFi.begin(ssid, password); + + while (WiFi.status() != WL_CONNECTED) { + delay(500); + Serial.println("Connecting to WiFi.."); + } + Serial.println("Connected to the WiFi"); + Serial.println("IP address: "); + Serial.println(WiFi.localIP()); + + client.setServer(mqttServer, mqttPort); + while (!client.connected()) { + Serial.println("Connecting to MQTT..."); + + if (client.connect("TempOutdoor", mqttUser, mqttPassword )) { + + Serial.println("Connected to MQTT"); + + } else { + + Serial.print("failed with state "); + Serial.print(client.state()); + delay(2000); + + } + } +} + +void loop() +{ + +//WiFi Check + if ( WiFi.status() != WL_CONNECTED ) { + Serial.println("WiFi disconnected"); + WiFi.disconnect(); + WiFi.begin(ssid, password); + while (WiFi.status() != WL_CONNECTED) { + delay(1000); + Serial.println("Connecting to WiFi.."); + } + Serial.println("Connected to the WiFi"); + Serial.println("IP address: "); + Serial.println(WiFi.localIP()); + } + +//MQTT Check + while (!client.connected()) { + client.connect("TempOutdoor", mqttUser, mqttPassword); + } + + unsigned int data[6]; + + // Start I2C Transmission + Wire.beginTransmission(Addr); + // Send measurement command + Wire.write(0x2C); + Wire.write(0x06); + // Stop I2C transm ission + Wire.endTransmission(); + delay(500); + + // Request 6 bytes of data + Wire.requestFrom(Addr, 6); + + // Read 6 bytes of data + // cTemp msb, cTemp lsb, cTemp crc, humidity msb, humidity lsb, humidity crc + if (Wire.available() == 6) + { + data[0] = Wire.read(); + data[1] = Wire.read(); + data[2] = Wire.read(); + data[3] = Wire.read(); + data[4] = Wire.read(); + data[5] = Wire.read(); + } + + // Convert the data + float cTemp = ((((data[0] * 256.0) + data[1]) * 175) / 65535.0) - 45; + float CalcHumidity = ((((data[3] * 256.0) + data[4]) * 100) / 65535.0); + int RoundHumidity = CalcHumidity; + // Output data to serial monitor + Serial.print("Relative Humidity : "); + Serial.print(CalcHumidity); + Serial.println(" %RH"); + Serial.print("Temperature in Celsius : "); + Serial.print(cTemp); + Serial.println(" C"); + + StaticJsonDocument<100> JSONHum; + StaticJsonDocument<100> JSONTemp; + StaticJsonDocument<100> JSONComb; + + JSONHum["idx"] = idxhum; + JSONHum["nvalue"] = RoundHumidity; + JSONHum["svalue"] = "0"; + + JSONTemp["idx"] = idxtemp; + JSONTemp["nvalue"] = 0; + JSONTemp["svalue"] = String(cTemp) ; + + JSONComb["idx"] = idxcomb; + JSONComb["nvalue"] = 0; + JSONComb["svalue"] = String(cTemp) + String(";") + String(CalcHumidity) + String(";0"); + + char buffer1[256]; + char buffer2[256]; + char buffer3[256]; + serializeJson(JSONTemp, buffer1); + serializeJson(JSONHum, buffer2); + serializeJson(JSONComb, buffer3); + + if (client.publish(domotopic, buffer1) == true) { + Serial.println("Published MQTT messages"); + } else { + Serial.println("Error sending MQTT messages"); + } + client.publish(domotopic, buffer2); + client.publish(domotopic, buffer3); + client.loop(); + delay(60000); //1min +} \ No newline at end of file diff --git a/test/README b/test/README new file mode 100644 index 0000000..df5066e --- /dev/null +++ b/test/README @@ -0,0 +1,11 @@ + +This directory is intended for PIO Unit Testing and project tests. + +Unit Testing is a software testing method by which individual units of +source code, sets of one or more MCU program modules together with associated +control data, usage procedures, and operating procedures, are tested to +determine whether they are fit for use. Unit testing finds problems early +in the development cycle. + +More information about PIO Unit Testing: +- https://docs.platformio.org/page/plus/unit-testing.html