EnigmaIOT  0.9.8
Secure sensor and gateway platform based on ESP8266 and ESP32
ds18b20Controller.cpp
Go to the documentation of this file.
1 //
2 //
3 //
4 
5 #include <functional>
6 #include "ds18b20Controller.h"
7 
8 using namespace std;
9 using namespace placeholders;
10 
11 #define TEST 0
12 
13 constexpr auto CONFIG_FILE = "/customconf.json";
14 
15 // -----------------------------------------
16 // You may add some global variables you need here,
17 // like serial port instances, I2C, etc
18 // -----------------------------------------
19 
20 #define ONE_WIRE_BUS 4
21 
22 
23 bool CONTROLLER_CLASS_NAME::processRxCommand (const uint8_t* address, const uint8_t* buffer, uint8_t length, nodeMessageType_t command, nodePayloadEncoding_t payloadEncoding) {
24  // Process incoming messages here
25  // They are normally encoded as MsgPack so you can confert them to JSON very easily
26  return true;
27 }
28 
29 
30 bool CONTROLLER_CLASS_NAME::sendCommandResp (const char* command, bool result) {
31  // Respond to command with a result: true if successful, false if failed
32  return true;
33 }
34 
36  const size_t capacity = JSON_OBJECT_SIZE (2);
37  DynamicJsonDocument json (capacity);
38  json["temp"] = temp;
39 
40  return sendJson (json);
41 }
42 
44 
45 #if SUPPORT_HA_DISCOVERY
46  // Register every HAEntity discovery function here. As many as you need
47  addHACall (std::bind (&CONTROLLER_CLASS_NAME::buildHADiscovery, this));
48 #endif
49 
51 }
52 
54  enigmaIotNode = node;
55 
56  // You do node setup here. Use it as it was the normal setup() Arduino function
57 
58 #if !TEST
59  oneWire = new OneWire (ONE_WIRE_BUS);
60  sensors = new DallasTemperature (oneWire);
61  sensors->begin ();
62  sensors->setWaitForConversion (false);
63  sensors->requestTemperatures ();
64 #endif
65  time_t start = millis ();
66 
67  // Send a 'hello' message when initalizing is finished
68  if (!enigmaIotNode->getNode ()->getSleepy ()) {
69  if (!(enigmaIotNode->getNode ()->getSleepy ())) {
70  sendStartAnouncement (); // Disable this if node is sleepy
71  }
72  }
73 
74 #if !TEST
75  while (!sensors->isConversionComplete ()) {
76  delay (0);
77  }
78  DEBUG_WARN ("Conversion completed in %d ms", millis () - start);
79  tempC = sensors->getTempCByIndex (0);
80 #else
81  tempC = 25.8;
82 #endif
83 
84  // Send a 'hello' message when initalizing is finished
85  //sendStartAnouncement ();
86 
87  DEBUG_DBG ("Finish begin");
88 
89  // If your node should sleep after sending data do all remaining tasks here
90 }
91 
93 
94  // If your node stays allways awake do your periodic task here
95 
96  // You can send your data as JSON. This is a basic example
97 
98  if (!tempSent && enigmaIotNode->isRegistered()) {
99  if (sendTemperature (tempC)) {
100  tempSent = true;
101  }
102  // else {
103  //}
104  }
105 
106  //const size_t capacity = JSON_OBJECT_SIZE (4);
107  //DynamicJsonDocument json (capacity);
108  //json["sensor"] = data_description;
109  //json["meas"] = measurement;
110 
111  //sendJson (json);
112 
113 }
114 
116  // It your class uses dynamic data free it up here
117  // This is normally not needed but it is a good practice
118 }
119 
121  DEBUG_INFO ("==== CCost Controller Configuration start ====");
122  // If you need to add custom configuration parameters do it here
123 }
124 
126  DEBUG_INFO ("==== CCost Controller Configuration result ====");
127  // You can read configuration paramenter values here
128 }
129 
131  // If you need to read custom configuration data do it here
132  return true;
133 }
134 
136  // If you need to save custom configuration data do it here
137  return true;
138 }
139 
140 #if SUPPORT_HA_DISCOVERY
141 // Repeat this method for every entity
143  // Select corresponding HAEntiny type
144  HASensor* haEntity = new HASensor ();
145 
146  uint8_t* msgPackBuffer;
147 
148  if (!haEntity) {
149  DEBUG_WARN ("JSON object instance does not exist");
150  return;
151  }
152 
153  // *******************************
154  // Add your characteristics here
155  // There is no need to futher modify this function
156 
157  haEntity->setNameSufix ("temp");
159  haEntity->setExpireTime (3600);
160  haEntity->setUnitOfMeasurement ("ºC");
161  haEntity->setValueField ("temp");
162  //haEntity->setValueTemplate ("{%if value_json.dp==2-%}{{value_json.temp}}{%-else-%}{{states('sensor.***_temp')}}{%-endif%}");
163 
164  // *******************************
165 
166  size_t bufferLen = haEntity->measureMessage ();
167 
168  msgPackBuffer = (uint8_t*)malloc (bufferLen);
169 
170  size_t len = haEntity->getAnounceMessage (bufferLen, msgPackBuffer);
171 
172  DEBUG_INFO ("Resulting MSG pack length: %d", len);
173 
174  if (!sendHADiscovery (msgPackBuffer, len)) {
175  DEBUG_WARN ("Error sending HA discovery message");
176  }
177 
178  if (haEntity) {
179  delete (haEntity);
180  }
181 
182  if (msgPackBuffer) {
183  free (msgPackBuffer);
184  }
185 }
186 #endif // SUPPORT_HA_DISCOVERY
CONFIG_FILE
constexpr auto CONFIG_FILE
Custom configuration file name.
Definition: ds18b20Controller.cpp:13
ds18b20Controller.h
HAEntity::measureMessage
size_t measureMessage()
Gets needed buffer size for discovery message.
Definition: haEntity.h:217
nodeMessageType
nodeMessageType
Message code definition.
Definition: EnigmaIOTNode.h:35
HAEntity::getAnounceMessage
size_t getAnounceMessage(int bufferlen, uint8_t *buffer)
Gets entity anounce message to be sent over EnigmaIOT message.
Definition: haEntity.h:153
EnigmaIOTjsonController::connectInform
virtual void connectInform()
Used to notify controller that it is registered on EnigmaIOT network.
Definition: EnigmaIOTjsonController.h:81
CONTROLLER_CLASS_NAME::loop
void loop() override
This should be called periodically for module handling.
Definition: ButtonController.cpp:53
HASensor::setValueField
void setValueField(const char *payload)
Defines a json key that defines sensor value. When setting this, setValueTemplate () should not be ca...
Definition: haSensor.cpp:32
ONE_WIRE_BUS
#define ONE_WIRE_BUS
Definition: ds18b20Controller.cpp:20
HASensor::setDeviceClass
void setDeviceClass(haSensorClass_t devClass)
Define sensor class as haSensorClass_t https://www.home-assistant.io/integrations/sensor....
Definition: haSensor.cpp:13
CONTROLLER_CLASS_NAME::buildHADiscovery
void buildHADiscovery()
Definition: ButtonController.cpp:124
CONTROLLER_CLASS_NAME::configManagerStart
void configManagerStart() override
Called when wifi manager starts config portal.
Definition: ButtonController.cpp:103
sensor_temperature
@ sensor_temperature
Definition: haEntity.h:124
CONTROLLER_CLASS_NAME::~CONTROLLER_CLASS_NAME
~CONTROLLER_CLASS_NAME()
Definition: ButtonController.cpp:98
nodePayloadEncoding_t
nodePayloadEncoding_t
Definition: EnigmaIOTNode.h:58
CONTROLLER_CLASS_NAME::saveConfig
bool saveConfig() override
Saves output module configuration.
Definition: ButtonController.cpp:119
HASensor::setUnitOfMeasurement
void setUnitOfMeasurement(const char *payload)
Set unit of measure https://www.home-assistant.io/integrations/sensor.mqtt/#unit_of_measurement.
Definition: haSensor.cpp:26
CONTROLLER_CLASS_NAME::connectInform
void connectInform()
Used to notify controller that it is registered on EnigmaIOT network.
Definition: ButtonController.cpp:31
data
@ data
Definition: GwOutput_generic.h:23
CONTROLLER_CLASS_NAME::sendCommandResp
bool sendCommandResp(const char *command, bool result) override
Sends command processing response acknowledge.
Definition: ButtonController.cpp:26
CONTROLLER_CLASS_NAME::configManagerExit
void configManagerExit(bool status) override
Called when wifi manager exits config portal.
Definition: ButtonController.cpp:109
EnigmaIOTNodeClass
Main node class. Manages communication with gateway and allows sending and receiving user data.
Definition: EnigmaIOTNode.h:134
HAEntity::setNameSufix
void setNameSufix(const char *payload)
Sets name suffix. Used for multi entity nodes.
Definition: haEntity.h:200
CONTROLLER_CLASS_NAME::setup
void setup(EnigmaIOTNodeClass *node, void *data=NULL)
Initialize data structures.
Definition: ButtonController.cpp:40
HASensor
Definition: haSensor.h:81
CONTROLLER_CLASS_NAME::sendTemperature
bool sendTemperature(float temp)
Definition: ds18b20Controller.cpp:35
CONTROLLER_CLASS_NAME::processRxCommand
bool processRxCommand(const uint8_t *address, const uint8_t *buffer, uint8_t length, nodeMessageType_t command, nodePayloadEncoding_t payloadEncoding) override
Called to process a downlink command.
Definition: ButtonController.cpp:19
CONTROLLER_CLASS_NAME::loadConfig
bool loadConfig() override
Loads output module configuration.
Definition: ButtonController.cpp:114
HASensor::setExpireTime
void setExpireTime(uint payload)
Defines the number of seconds after the sensor’s state expires, if it’s not updated....
Definition: haSensor.cpp:20
status
@ status
Definition: GwOutput_generic.h:25