Skip to content
This repository has been archived by the owner on Aug 6, 2021. It is now read-only.

Commit

Permalink
fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
gotbadger committed Apr 24, 2018
1 parent 692b609 commit 1e791d0
Show file tree
Hide file tree
Showing 6 changed files with 319 additions and 0 deletions.
67 changes: 67 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -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 < http://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
# < http://docs.platformio.org/page/ci/travis.html >
#
# * User Guide for `platformio ci` command
# < http://docs.platformio.org/page/userguide/cmd_ci.html >
#
#
# Please choice 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 by 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
7 changes: 7 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
// See http://go.microsoft.com/fwlink/?LinkId=827846
// for the documentation about the extensions.json format
"recommendations": [
"platformio.platformio-ide"
]
}
6 changes: 6 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"terminal.integrated.env.windows": {
"PATH": "C:\\Users\\Phil\\.platformio\\penv\\Scripts;C:\\Users\\Phil\\.platformio\\penv;D:\\_ARDUINO\\WinAVR-20100110\\bin;D:\\_ARDUINO\\WinAVR-20100110\\utils\\bin;C:\\Program Files (x86)\\Intel\\iCLS Client\\;C:\\Program Files\\Intel\\iCLS Client\\;C:\\WINDOWS\\system32;C:\\WINDOWS;C:\\WINDOWS\\System32\\Wbem;C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\;C:\\Program Files\\Intel\\Intel(R) Management Engine Components\\DAL;C:\\Program Files\\Intel\\Intel(R) Management Engine Components\\IPT;C:\\Program Files (x86)\\Intel\\Intel(R) Management Engine Components\\DAL;C:\\Program Files (x86)\\Intel\\Intel(R) Management Engine Components\\IPT;C:\\Program Files (x86)\\Heroku\\bin;C:\\Program Files (x86)\\git\\cmd;C:\\Program Files (x86)\\ATI Technologies\\ATI.ACE\\Core-Static;C:\\Program Files\\nodejs\\;C:\\Program Files\\Calibre2\\;C:\\Program Files (x86)\\AMD\\ATI.ACE\\Core-Static;C:\\Program Files\\OpenVPN\\bin;D:\\Program Files\\AMD\\ATI.ACE\\Core-Static;C:\\Program Files (x86)\\PuTTY\\;D:\\Program Files\\Amazon\\AWSCLI\\;C:\\Program Files (x86)\\Skype\\Phone\\;C:\\Program Files (x86)\\NVIDIA Corporation\\PhysX\\Common;C:\\WINDOWS\\system32;C:\\WINDOWS;C:\\WINDOWS\\System32\\Wbem;C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\;C:\\Program Files\\Git\\cmd;D:\\Program Files (x86)\\OpenVPN\\bin;C:\\Program Files (x86)\\Nmap;C:\\Program Files (x86)\\OpenVPN\\bin;C:\\Chocolatey\\bin;C:\\Program Files\\erl5.10.2\\bin;C:\\tools\\Elixir\\bin;C:\\Users\\Phil\\AppData\\Roaming\\npm;C:\\tools\\Python27;C:\\tools\\Python27\\Scripts;C:\\tools\\Python27\\lib\\site-packages\\django\\bin;C:\\Users\\Phil\\AppData\\Local\\Microsoft\\WindowsApps;C:\\Program Files\\Microsoft VS Code\\bin;D:\\_ARDUINO\\WinAVR-20100110\\bin;D:\\_ARDUINO\\WinAVR-20100110\\utils\\bin;C:\\Program Files (x86)\\Intel\\iCLS Client\\;C:\\Program Files\\Intel\\iCLS Client\\;C:\\WINDOWS\\system32;C:\\WINDOWS;C:\\WINDOWS\\System32\\Wbem;C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\;C:\\Program Files\\Intel\\Intel(R) Management Engine Components\\DAL;C:\\Program Files\\Intel\\Intel(R) Management Engine Components\\IPT;C:\\Program Files (x86)\\Intel\\Intel(R) Management Engine Components\\DAL;C:\\Program Files (x86)\\Intel\\Intel(R) Management Engine Components\\IPT;C:\\Program Files (x86)\\Heroku\\bin;C:\\Program Files (x86)\\git\\cmd;C:\\Program Files (x86)\\ATI Technologies\\ATI.ACE\\Core-Static;C:\\Program Files\\nodejs\\;C:\\Program Files\\Calibre2\\;C:\\Program Files (x86)\\AMD\\ATI.ACE\\Core-Static;C:\\Program Files\\OpenVPN\\bin;D:\\Program Files\\AMD\\ATI.ACE\\Core-Static;C:\\Program Files (x86)\\PuTTY\\;D:\\Program Files\\Amazon\\AWSCLI\\;C:\\Program Files (x86)\\Skype\\Phone\\;C:\\Program Files (x86)\\NVIDIA Corporation\\PhysX\\Common;C:\\WINDOWS\\system32;C:\\WINDOWS;C:\\WINDOWS\\System32\\Wbem;C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\;C:\\Program Files\\Git\\cmd;D:\\Program Files (x86)\\OpenVPN\\bin;C:\\Program Files (x86)\\Nmap;C:\\Program Files (x86)\\OpenVPN\\bin;C:\\Chocolatey\\bin;C:\\Program Files\\erl5.10.2\\bin;C:\\tools\\Elixir\\bin;C:\\Users\\Phil\\AppData\\Roaming\\npm;C:\\tools\\Python27;C:\\tools\\Python27\\Scripts;C:\\tools\\Python27\\lib\\site-packages\\django\\bin;C:\\Users\\Phil\\AppData\\Local\\Microsoft\\WindowsApps;C:\\Program Files\\Microsoft VS Code\\bin",
"PLATFORMIO_CALLER": "vscode"
}
}
41 changes: 41 additions & 0 deletions lib/readme.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@

This directory is intended for the project specific (private) libraries.
PlatformIO will compile them to static libraries and link to executable file.

The source code of each library should be placed in separate directory, like
"lib/private_lib/[here are source files]".

For example, see how can be organized `Foo` and `Bar` libraries:

|--lib
| |
| |--Bar
| | |--docs
| | |--examples
| | |--src
| | |- Bar.c
| | |- Bar.h
| | |- library.json (optional, custom build options, etc) http://docs.platformio.org/page/librarymanager/config.html
| |
| |--Foo
| | |- Foo.c
| | |- Foo.h
| |
| |- readme.txt --> THIS FILE
|
|- platformio.ini
|--src
|- main.c

Then in `src/main.c` you should use:

#include <Foo.h>
#include <Bar.h>

// rest H/C/CPP code

PlatformIO will find your libraries automatically, configure preprocessor's
include paths and build them.

More information about PlatformIO Library Dependency Finder
- http://docs.platformio.org/page/librarymanager/ldf.html
17 changes: 17 additions & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
; 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
; http://docs.platformio.org/page/projectconf.html

[env:d1_mini_lite_sensor]
platform = espressif8266
board = d1_mini_lite
framework = arduino
lib_deps =
PubSubClient
WEMOS SHT3x
181 changes: 181 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
/*
*******************************************************************************
* Project URL: https://github.com/gotbadger/HomeIot
*
*******************************************************************************
* Copyright Phil Hayton 2018.
* Distributed under the MIT License.
* (See accompanying file LICENSE or copy at http://opensource.org/licenses/MIT)
*******************************************************************************
*/

// set details here
#define NET "network_name"
#define PASS "password"
#define MQTT_ID "ESP_SENSOR_XX"
#define MQTT_SERVER "192.168.1.165"
#define MQTT_MAX_MESSAGE_SIZE 50
#define MAX_SAMPLE_RATE 2000
#define HW_UART_SPEED 9600L

// Include library
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <WEMOS_SHT3X.h>


#define LOG_PRINTFLN(fmt, ...) logfln(fmt, ##__VA_ARGS__)
#define LOG_SIZE_MAX 128
void logfln(const char *fmt, ...)
{
char buf[LOG_SIZE_MAX];
va_list ap;
va_start(ap, fmt);
vsnprintf(buf, LOG_SIZE_MAX, fmt, ap);
va_end(ap);
Serial.println(buf);
}

//setup some globals
SHT3X sht30(0x45);
WiFiClient espClient;
PubSubClient client(espClient);
long lastSample = 0;

//setup topics
const char *topics[3] = {
MQTT_ID "/ip",
MQTT_ID "/temperature",
MQTT_ID "/humidity"
};
char topics_cache[sizeof(topics)][MQTT_MAX_MESSAGE_SIZE];

void setup_wifi()
{
// wifi
WiFi.mode(WIFI_STA);
WiFi.hostname(MQTT_ID);
WiFi.begin(NET, PASS);
LOG_PRINTFLN("\n");
LOG_PRINTFLN("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
LOG_PRINTFLN(".");
}
LOG_PRINTFLN("Connected to WiFi");
LOG_PRINTFLN("IP: %s", WiFi.localIP().toString().c_str());
}

// callback for mqtt
void callback(char *topic, byte *payload, unsigned int length)
{
LOG_PRINTFLN("callback on %s", topic);
}

void publish(const int topic_id, const char *message)
{
LOG_PRINTFLN("%s %s", topics[topic_id], message);
client.publish(topics[topic_id], message);
}

void publish_if_changed(const int topic_id, const char *message)
{
LOG_PRINTFLN("comparing %s:%s", message, topics_cache[topic_id]);
//compare the strings see if they are equal
if (strcmp(message, topics_cache[topic_id]) != 0)
{
//update cache with new value limited to max lenth of cache
strncpy(topics_cache[topic_id], message, sizeof(topics_cache[topic_id]));
//publish as normal
publish(topic_id, message);
}
else
{
LOG_PRINTFLN("value for %s unchanged", topics[topic_id]);
}
}

void publish_if_changed(const int topic_id, const float value){
//take only 5 chars. This will give numbers like XX.X when including null terminator
char buffer[5];
snprintf(buffer, sizeof(buffer), "%f",value);
publish_if_changed(topic_id, buffer);
}

void reconnect()
{
// Loop until we're reconnected
while (!client.connected())
{
// Attempt to connect
if (client.connect(MQTT_ID))
{
LOG_PRINTFLN("MQTT connected");
publish(0, WiFi.localIP().toString().c_str());
}
else
{
LOG_PRINTFLN("MQTT failed sate: %i", client.state());
//check to see if wifi got killed also
if(WiFi.status() != WL_CONNECTED){
LOG_PRINTFLN("WIFI connection failed restarting...");
//reset and start again
ESP.reset();
}
// Wait 5 seconds before retrying
delay(5000);
}
}
}

bool sample()
{
//rarther than blocking return true every so often to sample data
long now = millis();
if (now - lastSample > MAX_SAMPLE_RATE)
{
lastSample = now;
return true;
}
return false;
}

void setup()
{
// serial
Serial.begin(HW_UART_SPEED);
while (!Serial)
;
setup_wifi();
client.setServer(MQTT_SERVER, 1883);
client.setCallback(callback);
}

void loop()
{

if (!client.connected())
{
reconnect();
}

client.loop();

if (sample())
{
if (sht30.get() == 0)
{
//we only are intrested in publishing changes so...

publish_if_changed(1, sht30.cTemp);
publish_if_changed(2, sht30.humidity);
}
else
{
LOG_PRINTFLN("sht30 read failed");
}
}
}

0 comments on commit 1e791d0

Please sign in to comment.