mirror of
https://github.com/IoTManagerProject/IoTManager.git
synced 2026-03-26 14:12:16 +03:00
broken - need repair
This commit is contained in:
132
lib/ESP8266-StringCommand/StringCommand.cpp
Normal file
132
lib/ESP8266-StringCommand/StringCommand.cpp
Normal file
@@ -0,0 +1,132 @@
|
||||
/**
|
||||
* SerialCommand - A Wiring/Arduino library to tokenize and parse commands
|
||||
* received over a serial port.
|
||||
*
|
||||
* Copyright (C) 2012 Stefan Rado
|
||||
* Copyright (C) 2011 Steven Cogswell <steven.cogswell@gmail.com>
|
||||
* http://husks.wordpress.com
|
||||
*
|
||||
* Version 20120522
|
||||
*
|
||||
* This library is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this library. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include "StringCommand.h"
|
||||
|
||||
/**
|
||||
* Constructor makes sure some things are set.
|
||||
*/
|
||||
StringCommand::StringCommand()
|
||||
: commandList(NULL),
|
||||
commandCount(0),
|
||||
defaultHandler(NULL),
|
||||
term('\n'), // default terminator for commands, newline character
|
||||
last(NULL)
|
||||
{
|
||||
strcpy(delim, " "); // strtok_r needs a null-terminated string
|
||||
clearBuffer();
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a "command" and a handler function to the list of available commands.
|
||||
* This is used for matching a found token in the buffer, and gives the pointer
|
||||
* to the handler function to deal with it.
|
||||
*/
|
||||
void StringCommand::addCommand(const char *command, void (*function)()) {
|
||||
#ifdef SERIALCOMMAND_DEBUG
|
||||
Serial.print("Adding command (");
|
||||
Serial.print(commandCount);
|
||||
Serial.print("): ");
|
||||
Serial.println(command);
|
||||
#endif
|
||||
|
||||
commandList = (StringCommandCallback *) realloc(commandList, (commandCount + 1) * sizeof(StringCommandCallback));
|
||||
strncpy(commandList[commandCount].command, command, SERIALCOMMAND_MAXCOMMANDLENGTH);
|
||||
commandList[commandCount].function = function;
|
||||
commandCount++;
|
||||
}
|
||||
|
||||
/**
|
||||
* This sets up a handler to be called in the event that the receveived command string
|
||||
* isn't in the list of commands.
|
||||
*/
|
||||
void StringCommand::setDefaultHandler(void (*function)(const char *)) {
|
||||
defaultHandler = function;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* This checks the Serial stream for characters, and assembles them into a buffer.
|
||||
* When the terminator character (default '\n') is seen, it starts parsing the
|
||||
* buffer for a prefix command, and calls handlers setup by addCommand() member
|
||||
*/
|
||||
void StringCommand::readStr(String sBuffer ) {
|
||||
sBuffer.toCharArray(buffer, SERIALCOMMAND_BUFFER);
|
||||
#ifdef SERIALCOMMAND_DEBUG
|
||||
Serial.print("Received: ");
|
||||
Serial.println(buffer);
|
||||
#endif
|
||||
|
||||
char *command = strtok_r(buffer, delim, &last); // Search for command at start of buffer
|
||||
if (command != NULL) {
|
||||
boolean matched = false;
|
||||
for (int i = 0; i < commandCount; i++) {
|
||||
#ifdef SERIALCOMMAND_DEBUG
|
||||
Serial.print("Comparing [");
|
||||
Serial.print(command);
|
||||
Serial.print("] to [");
|
||||
Serial.print(commandList[i].command);
|
||||
Serial.println("]");
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
// Compare the found command against the list of known commands for a match
|
||||
if (strncmp(command, commandList[i].command, SERIALCOMMAND_MAXCOMMANDLENGTH) == 0) {
|
||||
#ifdef SERIALCOMMAND_DEBUG
|
||||
Serial.print("Matched Command: ");
|
||||
Serial.println(command);
|
||||
#endif
|
||||
|
||||
// Execute the stored handler function for the command
|
||||
(*commandList[i].function)();
|
||||
matched = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (!matched && (defaultHandler != NULL)) {
|
||||
(*defaultHandler)(command);
|
||||
}
|
||||
|
||||
|
||||
clearBuffer();
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Clear the input buffer.
|
||||
*/
|
||||
void StringCommand::clearBuffer() {
|
||||
buffer[0] = '\0';
|
||||
bufPos = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the next token ("word" or "argument") from the command buffer.
|
||||
* Returns NULL if no more tokens exist.
|
||||
*/
|
||||
char *StringCommand::next() {
|
||||
return strtok_r(NULL, delim, &last);
|
||||
}
|
||||
75
lib/ESP8266-StringCommand/StringCommand.h
Normal file
75
lib/ESP8266-StringCommand/StringCommand.h
Normal file
@@ -0,0 +1,75 @@
|
||||
/**
|
||||
* SerialCommand - A Wiring/Arduino library to tokenize and parse commands
|
||||
* received over a serial port.
|
||||
*
|
||||
* Copyright (C) 2012 Stefan Rado
|
||||
* Copyright (C) 2011 Steven Cogswell <steven.cogswell@gmail.com>
|
||||
* http://husks.wordpress.com
|
||||
*
|
||||
* Version 20120522
|
||||
*
|
||||
* This library is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this library. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#ifndef StringCommand_h
|
||||
#define StringCommand_h
|
||||
|
||||
#if defined(WIRING) && WIRING >= 100
|
||||
#include <Wiring.h>
|
||||
#elif defined(ARDUINO) && ARDUINO >= 100
|
||||
#include <Arduino.h>
|
||||
#else
|
||||
#include <WProgram.h>
|
||||
#endif
|
||||
#include <string.h>
|
||||
|
||||
// Size of the input buffer in bytes (maximum length of one command plus arguments)
|
||||
#define SERIALCOMMAND_BUFFER 128 //256
|
||||
// Maximum length of a command excluding the terminating null
|
||||
#define SERIALCOMMAND_MAXCOMMANDLENGTH 16
|
||||
|
||||
// Uncomment the next line to run the library in debug mode (verbose messages)
|
||||
//#define SERIALCOMMAND_DEBUG
|
||||
|
||||
|
||||
class StringCommand {
|
||||
public:
|
||||
StringCommand(); // Constructor
|
||||
void addCommand(const char *command, void(*function)()); // Add a command to the processing dictionary.
|
||||
void setDefaultHandler(void (*function)(const char *)); // A handler to call when no valid command received.
|
||||
|
||||
void readStr(String sBuffer ); // Main entry point.
|
||||
void clearBuffer(); // Clears the input buffer.
|
||||
char *next(); // Returns pointer to next token found in command buffer (for getting arguments to commands).
|
||||
|
||||
private:
|
||||
// Command/handler dictionary
|
||||
struct StringCommandCallback {
|
||||
char command[SERIALCOMMAND_MAXCOMMANDLENGTH + 1];
|
||||
void (*function)();
|
||||
}; // Data structure to hold Command/Handler function key-value pairs
|
||||
StringCommandCallback *commandList; // Actual definition for command/handler array
|
||||
byte commandCount;
|
||||
|
||||
// Pointer to the default handler function
|
||||
void (*defaultHandler)(const char *);
|
||||
|
||||
char delim[2]; // null-terminated list of character to be used as delimeters for tokenizing (default " ")
|
||||
char term; // Character that signals end of command (default '\n')
|
||||
|
||||
char buffer[SERIALCOMMAND_BUFFER + 1]; // Buffer of stored characters while waiting for terminator character
|
||||
byte bufPos; // Current position in the buffer
|
||||
char *last; // State variable used by strtok_r during processing
|
||||
};
|
||||
|
||||
#endif //StringCommand_h
|
||||
23
lib/ESP8266-StringCommand/keywords.txt
Normal file
23
lib/ESP8266-StringCommand/keywords.txt
Normal file
@@ -0,0 +1,23 @@
|
||||
#######################################
|
||||
# Datatypes (KEYWORD1)
|
||||
#######################################
|
||||
|
||||
StringCommand KEYWORD1
|
||||
|
||||
#######################################
|
||||
# Methods and Functions (KEYWORD2)
|
||||
#######################################
|
||||
|
||||
addCommand KEYWORD2
|
||||
setDefaultHandler KEYWORD2
|
||||
readString KEYWORD2
|
||||
clearBuffer KEYWORD2
|
||||
next KEYWORD2
|
||||
|
||||
#######################################
|
||||
# Instances (KEYWORD2)
|
||||
#######################################
|
||||
|
||||
#######################################
|
||||
# Constants (LITERAL1)
|
||||
#######################################
|
||||
5
lib/ESP8266-StringCommand/readme.md
Normal file
5
lib/ESP8266-StringCommand/readme.md
Normal file
@@ -0,0 +1,5 @@
|
||||
StringCommand
|
||||
=============
|
||||
Библиотека для ESP8266 позволяющая связать запуск пользовательских функций с строковой переменной.
|
||||
|
||||
|
||||
17
lib/ESPrelay/.gitattributes
vendored
Normal file
17
lib/ESPrelay/.gitattributes
vendored
Normal file
@@ -0,0 +1,17 @@
|
||||
# Auto detect text files and perform LF normalization
|
||||
* text=auto
|
||||
|
||||
# Custom for Visual Studio
|
||||
*.cs diff=csharp
|
||||
|
||||
# Standard to msysgit
|
||||
*.doc diff=astextplain
|
||||
*.DOC diff=astextplain
|
||||
*.docx diff=astextplain
|
||||
*.DOCX diff=astextplain
|
||||
*.dot diff=astextplain
|
||||
*.DOT diff=astextplain
|
||||
*.pdf diff=astextplain
|
||||
*.PDF diff=astextplain
|
||||
*.rtf diff=astextplain
|
||||
*.RTF diff=astextplain
|
||||
215
lib/ESPrelay/TheThingsMQTT.cpp
Normal file
215
lib/ESPrelay/TheThingsMQTT.cpp
Normal file
@@ -0,0 +1,215 @@
|
||||
/*
|
||||
Copyright (c) 2016 TheThings.
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining
|
||||
a copy of this software and associated documentation files (the
|
||||
"Software"), to deal in the Software without restriction, including
|
||||
without limitation the rights to use, copy, modify, merge, publish,
|
||||
distribute, sublicense, and/or sell copies of the Software, and to
|
||||
permit persons to whom the Software is furnished to do so, subject to
|
||||
the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be
|
||||
included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
Original Maker: Mateo Velez - Metavix for TheThings Inc
|
||||
Modified and Maintened by: Jose Garcia - TheThings Inc
|
||||
|
||||
*/
|
||||
|
||||
#include "TheThingsMQTT.h"
|
||||
|
||||
|
||||
TheThings::TheThings(char* token){
|
||||
initialize(token, NULL);
|
||||
}
|
||||
|
||||
|
||||
TheThings::TheThings(char* token, char* clientName) {
|
||||
initialize(token, clientName);
|
||||
}
|
||||
|
||||
|
||||
void TheThings::begin(void (*callback)(char*,uint8_t*,unsigned int)) {
|
||||
this->callback = callback;
|
||||
_client.setServer(_server, MQTT_PORT);
|
||||
_client.setCallback(callback);
|
||||
}
|
||||
|
||||
|
||||
bool TheThings::add(char* variableLabel, float value) {
|
||||
return add(variableLabel, value, "NULL", "NULL");
|
||||
}
|
||||
|
||||
|
||||
bool TheThings::add(char* variableLabel, float value, char *context) {
|
||||
return add(variableLabel, value, context, "NULL");
|
||||
}
|
||||
|
||||
|
||||
bool TheThings::add(char* variableLabel, float value, char *context, char *timestamp) {
|
||||
(val+currentValue)->_variableLabel = variableLabel;
|
||||
(val+currentValue)->_value = value;
|
||||
(val+currentValue)->_context = context;
|
||||
(val+currentValue)->_timestamp = timestamp;
|
||||
currentValue++;
|
||||
if (currentValue > MAX_VALUES) {
|
||||
Serial.println(F("You are sending more than the maximum of consecutive variables"));
|
||||
currentValue = MAX_VALUES;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool TheThings::connected(){
|
||||
return _client.connected();
|
||||
}
|
||||
|
||||
|
||||
char* TheThings::getMac(){
|
||||
// Obtains the MAC of the device
|
||||
Serial.println("entra");
|
||||
byte mac[6];
|
||||
WiFi.macAddress(mac);
|
||||
char macAddr[18];
|
||||
sprintf(macAddr, "%2X%2X%2X%2X%2X%2X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
|
||||
return macAddr;
|
||||
}
|
||||
|
||||
|
||||
void TheThings::initialize(char* token, char* clientName){
|
||||
_server = SERVER;
|
||||
_token = token;
|
||||
currentValue = 0;
|
||||
val = (Value *)malloc(MAX_VALUES*sizeof(Value));
|
||||
if(clientName!=NULL){
|
||||
_clientName = clientName;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool TheThings::loop() {
|
||||
if (!_client.connected()) {
|
||||
reconnect();
|
||||
}
|
||||
return _client.loop();
|
||||
}
|
||||
|
||||
|
||||
void TheThings::reconnect() {
|
||||
while (!_client.connected()) {
|
||||
Serial.print("Attempting MQTT connection...");
|
||||
if (_client.connect(_clientName, _token, NULL)) {
|
||||
Serial.println("connected");
|
||||
break;
|
||||
} else {
|
||||
Serial.print("failed, rc=");
|
||||
Serial.print(_client.state());
|
||||
Serial.println(" try again in 3 seconds");
|
||||
delay(3000);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void TheThings::setDebug(bool debug){
|
||||
_debug = debug;
|
||||
}
|
||||
|
||||
|
||||
void TheThings:: setBroker(char* broker){
|
||||
if (_debug){
|
||||
Serial.println("Broker set for Business Account");
|
||||
}
|
||||
_server = broker;
|
||||
}
|
||||
|
||||
|
||||
bool TheThings::subscribe() {
|
||||
char topic[150];
|
||||
sprintf(topic, "%s%s", FIRST_PART_TOPIC, _token);
|
||||
if (!_client.connected()) {
|
||||
reconnect();
|
||||
}
|
||||
if (_debug){
|
||||
Serial.println("Subscribed to: ");
|
||||
Serial.println(topic);
|
||||
}
|
||||
return _client.subscribe(topic);
|
||||
}
|
||||
|
||||
|
||||
bool TheThings::publish() {
|
||||
|
||||
char topic[150];
|
||||
char payload[500];
|
||||
String str;
|
||||
sprintf(topic, "%s%s", FIRST_PART_TOPIC, _token);
|
||||
sprintf(payload, "[");
|
||||
for (int i = 0; i <= currentValue; ) {
|
||||
str = String((val+i)->_value, 1);
|
||||
sprintf(payload,"%s{\"key\":\"%s\",\"value\":%s",payload,(val+i)->_variableLabel, str.c_str());
|
||||
if ((val+i)->_timestamp != "NULL") {
|
||||
sprintf(payload, "%s, \"timestamp\": %s", payload, (val+i)->_timestamp);
|
||||
}
|
||||
if ((val+i)->_context != "NULL") {
|
||||
sprintf(payload, "%s, \"context\": {%s}", payload, (val+i)->_context);
|
||||
}
|
||||
i++;
|
||||
if (i >= currentValue) {
|
||||
sprintf(payload, "%s}]", payload);
|
||||
break;
|
||||
} else {
|
||||
sprintf(payload, "%s},", payload);
|
||||
}
|
||||
}
|
||||
if (_debug){
|
||||
Serial.println("publishing to TOPIC: ");
|
||||
Serial.println(topic);
|
||||
Serial.print("JSON dict: ");
|
||||
Serial.println(payload);
|
||||
}
|
||||
currentValue = 0;
|
||||
return _client.publish(topic, payload, 512);
|
||||
}
|
||||
|
||||
|
||||
bool TheThings::wifiConnection(char* ssid, char* pass) {
|
||||
WiFi.begin(ssid, pass);
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
delay(500);
|
||||
Serial.print(".");
|
||||
}
|
||||
Serial.println(F("WiFi connected"));
|
||||
Serial.println(F("IP address: "));
|
||||
Serial.println(WiFi.localIP());
|
||||
if(_clientName==NULL){
|
||||
_clientName = getMac();
|
||||
}
|
||||
}
|
||||
|
||||
bool TheThings::wifiConnection2() {
|
||||
WiFiManager wifiManager;
|
||||
wifiManager.setTimeout(180);
|
||||
|
||||
//if(!wifiManager.autoConnect("AutoConnectAP")) {
|
||||
if(!wifiManager.autoConnect()) {
|
||||
Serial.println("failed to connect and hit timeout");
|
||||
delay(3000);
|
||||
ESP.reset();
|
||||
delay(5000);
|
||||
}
|
||||
}
|
||||
|
||||
int TheThings::state() {
|
||||
return _client.state();
|
||||
}
|
||||
|
||||
58
lib/ESPrelay/TheThingsMQTT.h
Normal file
58
lib/ESPrelay/TheThingsMQTT.h
Normal file
@@ -0,0 +1,58 @@
|
||||
|
||||
#ifndef TheThingsMQTT_H
|
||||
#define TheThingsMQTT_H
|
||||
//#include <ESP8266WiFi.h>
|
||||
#include <PubSubClient.h>
|
||||
#include <WiFiManager.h>
|
||||
|
||||
#define MQTT_PORT 1883
|
||||
#define SERVER "mqtt.thethings.io"
|
||||
#define MAX_VALUES 5
|
||||
#define FIRST_PART_TOPIC "v2/things/"
|
||||
|
||||
#define META_DEBUG Serial
|
||||
|
||||
typedef struct Value {
|
||||
char* _variableLabel;
|
||||
float _value;
|
||||
char* _context;
|
||||
char* _timestamp;
|
||||
} Value;
|
||||
|
||||
class TheThings {
|
||||
private:
|
||||
void (*callback)(char*, uint8_t*, unsigned int);
|
||||
char* getMac();
|
||||
void initialize(char* token, char* clientName);
|
||||
WiFiClient espClient;
|
||||
PubSubClient _client = PubSubClient(espClient);
|
||||
WiFiManager wifiManager;
|
||||
char* _clientName = NULL;
|
||||
bool _debug = false;
|
||||
uint8_t currentValue;
|
||||
char* _password;
|
||||
char* _server;
|
||||
char* _ssid;
|
||||
char* _token;
|
||||
Value* val;
|
||||
|
||||
public:
|
||||
TheThings(char* token);
|
||||
TheThings(char* token, char* clientName);
|
||||
bool add(char* variableLabel, float value);
|
||||
bool add(char* variableLabel, float value, char* context);
|
||||
bool add(char* variableLabel, float value, char* context, char* timestamp);
|
||||
void begin(void (*callback)(char*, uint8_t*, unsigned int));
|
||||
bool connected();
|
||||
bool loop();
|
||||
bool subscribe();
|
||||
bool publish();
|
||||
void setBroker(char* broker);
|
||||
void reconnect();
|
||||
void setDebug(bool debug);
|
||||
bool wifiConnection(char* ssid, char* pass);
|
||||
bool wifiConnection2();
|
||||
int state();
|
||||
};
|
||||
|
||||
#endif
|
||||
116
lib/ESPrelay/TickerScheduler.cpp
Normal file
116
lib/ESPrelay/TickerScheduler.cpp
Normal file
@@ -0,0 +1,116 @@
|
||||
#include "TickerScheduler.h"
|
||||
|
||||
TickerScheduler::TickerScheduler(uint8_t size)
|
||||
{
|
||||
this->items = new TickerSchedulerItem[size];
|
||||
this->size = size;
|
||||
}
|
||||
|
||||
TickerScheduler::~TickerScheduler()
|
||||
{
|
||||
for (uint8_t i = 0; i < this->size; i++)
|
||||
{
|
||||
this->remove(i);
|
||||
yield();
|
||||
}
|
||||
|
||||
delete[] this->items;
|
||||
this->items = NULL;
|
||||
this->size = 0;
|
||||
}
|
||||
|
||||
void TickerScheduler::handleTickerFlag(volatile bool * flag)
|
||||
{
|
||||
if (!*flag)
|
||||
*flag = true;
|
||||
}
|
||||
|
||||
void TickerScheduler::handleTicker(tscallback_t f, void * arg, volatile bool * flag)
|
||||
{
|
||||
if (*flag)
|
||||
{
|
||||
yield();
|
||||
*flag = false;
|
||||
yield();
|
||||
f(arg);
|
||||
yield();
|
||||
}
|
||||
}
|
||||
|
||||
bool TickerScheduler::add(uint8_t i, uint32_t period, tscallback_t f, void* arg, boolean shouldFireNow)
|
||||
{
|
||||
if (i >= this->size || this->items[i].is_used)
|
||||
return false;
|
||||
|
||||
this->items[i].cb = f;
|
||||
this->items[i].cb_arg = arg;
|
||||
this->items[i].flag = shouldFireNow;
|
||||
this->items[i].period = period;
|
||||
this->items[i].is_used = true;
|
||||
|
||||
enable(i);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool TickerScheduler::remove(uint8_t i)
|
||||
{
|
||||
if (i >= this->size || !this->items[i].is_used)
|
||||
return false;
|
||||
|
||||
this->items[i].is_used = false;
|
||||
this->items[i].t.detach();
|
||||
this->items[i].flag = false;
|
||||
this->items[i].cb = NULL;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool TickerScheduler::disable(uint8_t i)
|
||||
{
|
||||
if (i >= this->size || !this->items[i].is_used)
|
||||
return false;
|
||||
|
||||
this->items[i].t.detach();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool TickerScheduler::enable(uint8_t i)
|
||||
{
|
||||
if (i >= this->size || !this->items[i].is_used)
|
||||
return false;
|
||||
|
||||
volatile bool * flag = &this->items[i].flag;
|
||||
this->items[i].t.attach_ms(this->items[i].period, TickerScheduler::handleTickerFlag, flag);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void TickerScheduler::disableAll()
|
||||
{
|
||||
for (uint8_t i = 0; i < this->size; i++)
|
||||
disable(i);
|
||||
}
|
||||
|
||||
void TickerScheduler::enableAll()
|
||||
{
|
||||
for (uint8_t i = 0; i < this->size; i++)
|
||||
enable(i);
|
||||
}
|
||||
|
||||
void TickerScheduler::update()
|
||||
{
|
||||
for (uint8_t i = 0; i < this->size; i++)
|
||||
{
|
||||
if (this->items[i].is_used)
|
||||
{
|
||||
#ifdef ARDUINO_ARCH_AVR
|
||||
this->items[i].t.Tick();
|
||||
#endif
|
||||
|
||||
handleTicker(this->items[i].cb, this->items[i].cb_arg, &this->items[i].flag);
|
||||
}
|
||||
yield();
|
||||
}
|
||||
}
|
||||
79
lib/ESPrelay/TickerScheduler.h
Normal file
79
lib/ESPrelay/TickerScheduler.h
Normal file
@@ -0,0 +1,79 @@
|
||||
#pragma once
|
||||
|
||||
#include <arduino.h>
|
||||
#include <stdint.h>
|
||||
|
||||
class Ticker {
|
||||
typedef void (*ticker_callback_t)(bool *);
|
||||
|
||||
private:
|
||||
bool is_attached = false;
|
||||
uint32_t period = 0;
|
||||
uint32_t last_tick = 0;
|
||||
ticker_callback_t callback;
|
||||
bool *callback_argument;
|
||||
|
||||
public:
|
||||
void Tick() {
|
||||
if (is_attached && millis() - last_tick >= period) {
|
||||
callback(callback_argument);
|
||||
last_tick = millis();
|
||||
}
|
||||
}
|
||||
|
||||
void detach() {
|
||||
this->is_attached = true;
|
||||
}
|
||||
|
||||
template <typename TArg>
|
||||
void attach_ms(uint32_t milliseconds, void (*callback)(TArg), TArg arg) {
|
||||
this->period = milliseconds;
|
||||
this->callback = callback;
|
||||
this->callback_argument = arg;
|
||||
this->is_attached = true;
|
||||
}
|
||||
};
|
||||
|
||||
#ifdef ARDUINO_ARCH_ESP8266
|
||||
#include <Ticker.h>
|
||||
|
||||
#include <functional>
|
||||
#endif
|
||||
|
||||
void tickerFlagHandle(volatile bool *flag);
|
||||
|
||||
#ifdef _GLIBCXX_FUNCTIONAL
|
||||
typedef std::function<void(void *)> tscallback_t;
|
||||
#else
|
||||
typedef void (*tscallback_t)(void *);
|
||||
#endif
|
||||
|
||||
struct TickerSchedulerItem {
|
||||
Ticker t;
|
||||
volatile bool flag = false;
|
||||
tscallback_t cb;
|
||||
void *cb_arg;
|
||||
uint32_t period;
|
||||
volatile bool is_used = false;
|
||||
};
|
||||
|
||||
class TickerScheduler {
|
||||
private:
|
||||
uint8_t size;
|
||||
TickerSchedulerItem *items = NULL;
|
||||
|
||||
void handleTicker(tscallback_t, void *, volatile bool *flag);
|
||||
static void handleTickerFlag(volatile bool *flag);
|
||||
|
||||
public:
|
||||
TickerScheduler(uint8_t size);
|
||||
~TickerScheduler();
|
||||
|
||||
bool add(uint8_t i, uint32_t period, tscallback_t, void *, boolean shouldFireNow = false);
|
||||
bool remove(uint8_t i);
|
||||
bool enable(uint8_t i);
|
||||
bool disable(uint8_t i);
|
||||
void enableAll();
|
||||
void disableAll();
|
||||
void update();
|
||||
};
|
||||
182
lib/ESPrelay/subscribe_and_publish.ino
Normal file
182
lib/ESPrelay/subscribe_and_publish.ino
Normal file
@@ -0,0 +1,182 @@
|
||||
/****************************************
|
||||
* Include Libraries
|
||||
****************************************/
|
||||
#include "TheThingsMQTT.h"
|
||||
#include "TickerScheduler.h"
|
||||
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <PubSubClient.h>
|
||||
|
||||
#include <DNSServer.h>
|
||||
#include <ESP8266WebServer.h>
|
||||
#include <WiFiManager.h>
|
||||
#include <Adafruit_Sensor.h>
|
||||
#include <DHT.h>
|
||||
#include <DHT_U.h>
|
||||
|
||||
/****************************************
|
||||
* Define Constants
|
||||
****************************************/
|
||||
#define TOKEN "" // Your TheThings TOKEN
|
||||
#define WIFINAME "" //Your SSID
|
||||
#define WIFIPASS "" // Your Wifi Pass
|
||||
|
||||
TheThings client(TOKEN);
|
||||
|
||||
#define DHTPIN 14 // Pin which is cosnnected to the DHT sensor.
|
||||
// Uncomment the type of sensor in use:
|
||||
//#define DHTTYPE DHT11 // DHT 11
|
||||
#define DHTTYPE DHT22 // DHT 22 (AM2302)
|
||||
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
|
||||
|
||||
DHT_Unified dht(DHTPIN, DHTTYPE);
|
||||
|
||||
uint32_t delayMS;
|
||||
#define TRIGGER_PIN 0
|
||||
#define FRECUENCY 2000
|
||||
char *AP="TEST_BQ";
|
||||
String prefix="rel";
|
||||
const char WiFiAPPSK[] = "prueba";
|
||||
|
||||
WiFiManager wifiManager;
|
||||
|
||||
#define RELAY1 12
|
||||
#define RELAY2 13
|
||||
|
||||
#define MEASUREMENT_TIME 30000 // Time in milisecons
|
||||
|
||||
float Temperature,Humidity;
|
||||
TickerScheduler ts(1);
|
||||
|
||||
/****************************************
|
||||
* Auxiliar Functions
|
||||
****************************************/
|
||||
|
||||
//void configWiFi(){
|
||||
// wifiManager.resetSettings();
|
||||
// if(!wifiManager.autoConnect(AP,WiFiAPPSK)) {
|
||||
// Serial.println("Reconfiguration called");
|
||||
// delay(3000);
|
||||
// //reset and try again, or maybe put it to deep sleep
|
||||
// ESP.reset();
|
||||
// delay(15000);
|
||||
// }
|
||||
//}
|
||||
|
||||
void callback(char* topic, byte* payload, unsigned int length) {
|
||||
Serial.print("Message arrived");
|
||||
// handle message arrived
|
||||
String text = "";
|
||||
for (int i = 0; i < length; i++)
|
||||
text.concat((char)payload[i]);
|
||||
Serial.println(text);
|
||||
text.replace(" ", "");
|
||||
text.toUpperCase();
|
||||
if (text.indexOf("\"KEY\":\"REL1\"") >= 0) {
|
||||
if (text.indexOf("\"VALUE\":\"ON\"") >= 0) digitalWrite(RELAY1,HIGH);
|
||||
else if (text.indexOf("\"VALUE\":\"OFF\"") >= 0) digitalWrite(RELAY1,LOW);
|
||||
}
|
||||
else if (text.indexOf("\"KEY\":\"REL2\"") >= 0) {
|
||||
if (text.indexOf("\"VALUE\":\"ON\"") >= 0) digitalWrite(RELAY2,HIGH);
|
||||
else if (text.indexOf("\"VALUE\":\"OFF\"") >= 0) digitalWrite(RELAY2,LOW);
|
||||
}
|
||||
}
|
||||
|
||||
float temperatureMeasurement(sensors_event_t event, DHT_Unified dht){
|
||||
// Get temperature event and print its value.
|
||||
dht.temperature().getEvent(&event);
|
||||
if (isnan(event.temperature)) {
|
||||
Serial.println("Error reading temperature!");
|
||||
}
|
||||
else {
|
||||
Serial.print("Temperature: ");
|
||||
Serial.print(event.temperature);
|
||||
Serial.println(" *C");
|
||||
return event.temperature;
|
||||
}
|
||||
}
|
||||
|
||||
float humidityMeasurement(sensors_event_t event, DHT_Unified dht){
|
||||
// Get humidity event and print its value.
|
||||
dht.humidity().getEvent(&event);
|
||||
if (isnan(event.relative_humidity)) {
|
||||
Serial.println("Error reading humidity!");
|
||||
}
|
||||
else {
|
||||
Serial.print("Humidity: ");
|
||||
Serial.print(event.relative_humidity);
|
||||
Serial.println("%");
|
||||
return event.relative_humidity;
|
||||
}
|
||||
}
|
||||
|
||||
void measureAndSend(){
|
||||
sensors_event_t event;
|
||||
Temperature=temperatureMeasurement(event,dht);
|
||||
Humidity=humidityMeasurement(event,dht);
|
||||
client.add("Temperature", Temperature);
|
||||
client.add("Humidity", Humidity);
|
||||
client.publish();
|
||||
}
|
||||
|
||||
/****************************************
|
||||
* Main Functions
|
||||
****************************************/
|
||||
|
||||
|
||||
void setup() {
|
||||
// put your setup code here, to run once:
|
||||
Serial.begin(115200);
|
||||
delay(100);
|
||||
pinMode(RELAY1,OUTPUT);
|
||||
pinMode(RELAY2,OUTPUT);
|
||||
|
||||
dht.begin();
|
||||
sensor_t sensor;
|
||||
|
||||
|
||||
|
||||
// pinMode(TRIGGER_PIN, INPUT);
|
||||
// attachInterrupt(digitalPinToInterrupt(TRIGGER_PIN), configWiFi, RISING);
|
||||
//
|
||||
// //sets timeout until configuration portal gets turned off
|
||||
// //useful to make it all retry or go to sleep
|
||||
// //in seconds
|
||||
// wifiManager.setTimeout(280);
|
||||
//
|
||||
// //fetches ssid and pass and tries to connect
|
||||
// //if it does not connect it starts an access point with the specified name
|
||||
// //here "AutoConnectAP"
|
||||
// //and goes into a blocking loop awaiting configuration
|
||||
// if(!wifiManager.autoConnect(AP,WiFiAPPSK)) {
|
||||
// Serial.println("failed to connect and hit timeout");
|
||||
// delay(3000);
|
||||
// //reset and try again, or maybe put it to deep sleep
|
||||
// //ESP.reset();
|
||||
// delay(FRECUENCY);
|
||||
// }
|
||||
// //if you get here you have connected to the WiFi
|
||||
// Serial.println("connected...yeey :)");
|
||||
|
||||
client.wifiConnection(WIFINAME, WIFIPASS);
|
||||
|
||||
//client.wifiConnection2();
|
||||
client.begin(callback);
|
||||
client.subscribe(); //Insert the dataSource and Variable's Labels
|
||||
|
||||
ts.add(0, MEASUREMENT_TIME, [&](void*) { measureAndSend(); }, nullptr, false);
|
||||
ts.enableAll();
|
||||
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// put your main code here, to run repeatedly:
|
||||
ts.update();
|
||||
|
||||
if(!client.connected()){
|
||||
client.reconnect();
|
||||
client.subscribe(); //Insert the dataSource and Variable's Labels
|
||||
}
|
||||
client.loop();
|
||||
|
||||
}
|
||||
46
lib/README
Normal file
46
lib/README
Normal file
@@ -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 <Foo.h>
|
||||
#include <Bar.h>
|
||||
|
||||
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
|
||||
Reference in New Issue
Block a user