ESP_IOT v2.5
IOT ESP Coding
ATOM_SocketModule.cpp
Go to the documentation of this file.
1/**
2 Use ATOM Socket to monitor the socket power, press the middle button of ATOM
3 to switch the socket power on and off. Connect to the AP hotspot of the device
4 and access 192.168.4.1 to wirelessly control the socket power and view the power
5 voltage, current and power information.
6 使用ATOM插座监控插座电源,按ATOM的中间按钮即可打开和关闭插座电源。
7 连接到设备的AP热点,接入192.168.4.1,无线控制插座电源,查看电源电压、电流和电源信息。
8 */
9
10
11#include "../../Defines.h"
12#ifdef ATOM_SOCKET_MODULE
13#include "ATOM_SocketModule.h"
14
15#include "AtomSocket.h"
16
17
18#ifdef USE_FAST_LED
19#include "../ATOM_LED_Module/M5Display.h"
20#endif
21
22//! info from the socket power
23int _Voltage = 0;
25float _Current = 0;
26
27//! create the ATOMSOCKET connection to the power socket
29
30#define RXD 22
31#define RELAY 23
32
33//! interface to the hardware
34HardwareSerial AtomSerial(2);
35
36//! LOOP code.. refactored so it's also called when a buttonPress message arrives
38
39//!big button on front of M5StickC Plus
41
42
43//! not beware of linking another ATOM these names will overload..
47
48//! whether the device is ON (default of)
49//! called "RelayFlag" is the example
51//! turn on/off the socket
53
54//#define KEY_UNIT_SENSOR_CLASS in defines.h
55#ifdef KEY_UNIT_SENSOR_CLASS
56
57#include "../SensorClass/SensorClassType.h"
58#include "../SensorClass/KeyUnitSensorClass.h"
59
61
62
63//a pointer to a callback function that takes (char*) and returns void
64void M5AtomSocketCallback(char *parameter, boolean flag)
65{
66 SerialDebug.printf("M5AtomSocket.sensorCallbackSignature(%s,%d)\n", parameter, flag);
67
68 sendMessageString_mainModule((char*)"M5AtomSocket.KEY Pressed ");
69
70#ifdef USE_FAST_LED
71 fillpix(L_YELLOW);
72#else
73 //! 8.2.26 LED working
75#endif
77
78}
79#endif
80
81//! turn on/off the socket
82void set_ATOM_SocketModule(boolean flag)
83{
84 //!store globally
86 SerialDebug.printf("set_ATOM_SocketModule: %s\n",flag?"ON":"OFF");
87
89 {
91#ifdef USE_FAST_LED
92 fillpix(L_GREEN);
93#else
94 //! 8.2.26 LED working
96#endif
97 //! 1.1.24 syntax: socket=on or socket=off (somewhere in the string..) .. this version, only string avaialble..
99 }
100 else
101 {
103#ifdef USE_FAST_LED
104 fillpix(L_YELLOW);
105 //or turn light off??
106#else
107 //! 8.2.26 LED working
109#endif
110 //! 1.1.24 syntax: socket=on or socket=off (somewhere in the string..)
112 }
113
114 //!send status after power on/off change..
116
117}
118
119//! turn on/off the socket
121{
123}
124
125
126//! 8.28.23 Adding a way for others to get informed on messages that arrive
127//! for the set,val
128//! 2.27.23 support setName == "socket"
129//! 1.10.24 if deviceNameSpecified then this matches this device, otherwise for all.
130//! It's up to the receiver to decide if it has to be specified
131//! 1.12.24 AtomSocketGlobalOnOff to turn on/off global onoff
132void messageSetVal_ATOM_SocketModule(char *setName, char* valValue, boolean deviceNameSpecified)
133{
134 //! see if global on/off is supported..
135 //! So if globalOnOff definitely perform action. If deviceNameSpecified - perform action so OR of them
137
138 boolean isTrue = isTrueString_mainModule(valValue);
139 if (strcmp(setName,"socket")==0)
140 {
141 SerialDebug.printf("messageSetVal_ATOM_SocketModule(%s - global =%d, dev=%d)\n", valValue, globalOnOff, deviceNameSpecified);
142 if (globalOnOff || deviceNameSpecified)
143 {
144 set_ATOM_SocketModule(isTrue);
145 }
146 }
147}
148
149//! 12.28.23, 8.28.23 Adding a way for others to get informed on messages that arrive
150//! for the send
151void messageSend_ATOM_SocketModule(char *sendValue)
152{
153 SerialDebug.printf("messageSend_ATOM_SocketModule(%s)\n", sendValue);
154 if (strcasecmp(sendValue, "togglesocket")==0)
155 {
157 }
158}
159
160//! init any globals
161//! 1.5.24
163{
164}
165
166
167//!THIS IS the setup() and loop() but using the "component" name, eg AudioModule()
168//!This will perform preference initializtion as well
169//! called from the setup()
170//! Pass in the method to call on a loud (over a threshhold. The parameter for value will be sent
171//void setup_AudioModule(void (*loudCallback)(int));
173{
175
176 //! 5.3.25 register our PIN use
177 registerPinUse_mainModule(RXD, "RXD", "ATOM_SocketModule", false);
178 registerPinUse_mainModule(RELAY, "RELAY", "ATOM_SocketModule", false);
179
180 //! 1.1.24 syntax: socket=on or socket=off (somewhere in the string..)
182 SerialDebug.printf("ATOM Pref = %s\n", preference);
183 if (containsSubstring(preference,(char*)"socket=on"))
185 else
187
188 SerialDebug.println("setup_ATOM_SocketModule.M5.begin");
189
190
191#ifdef USE_FAST_LED
192 //!NOTE: this could probably be done by ESP_IOT.ino .. but for now keep here (and in the other ATOM code..)
194 fillpix(L_GREEN);
195#else
196 //! 8.2.26 LED working
198#endif
199
200 //! ijnit the ATOM Socket
202
203 //!default whatever was the last mode Persistently..
205
206#ifdef KEY_UNIT_SENSOR_CLASS
207 //SensorStruct* sensor = getSensor_mainModule("KeyUnitInstanceM5AtomSocket");
208 //!NOTE: error without (char*) -- *warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
209 SensorStruct* sensor = getSensor_mainModule((char*)"BuzzerSensorClass");
210
211 //! if the Buzzer isn't on .. then we are good to go
212 //! try this 5.14.25 .. But this is probablly wrong .. is should be just the KeyUnitSensorClass .. and maybe more generic..
213 if (!sensor)
214 {
215 //! create instance..
216 _KeyUnitSensorClass_ATOMSocketModule = new KeyUnitSensorClass((char*)"KeyUnitInstanceM5AtomSocket");
217 //! specify the callback
219 //! call the setup
221 }
222 else
223 {
224 SerialDebug.println("BuzzerSensorClass already specified in sensors");
225 }
226#endif
227}
228
229//! called for the loop() of this plugin
231{
232 //! TODO..
233 //!
234#ifdef CALL_LATER
236 if (ATOM.SerialRead == 1)
237 {
238 //SerialDebug.println("*** ATOM.SerialRead == 1");
239
240 _Voltage = ATOM.GetVol();
243
244 //! use STATUS, and Long Press to get info..
245 }
246#endif
247
248 espUpdate_mainModule(2); //7.26.26 //M5.update();
249 // checkButtonA_ATOM_SocketModule();
251
252 //! do loop code
254
255#ifdef KEY_UNIT_SENSOR_CLASS
258#endif
259}
260
261//! BUTTON PROCESSING abstraction
262//!short press on buttonA (top button)
264{
268
271}
272
273//!long press on buttonA (top button)
275{
279
282}
283
284
285//!big button on front of M5StickC Plus
287{
291
292#ifdef ESP_M5
293
294#ifdef ESP_M5_ATOM_S3
295#else
296 //!NOTE: ths issue is the timer is interruped by the scanner.. so make long-long very long..
297 //was 1000 (from 500)
298 if (M5.BtnB.wasReleasefor(3500))
299 {
300 // buttonA_longPress_ATOM_SocketModule();
301 SerialDebug.println("ATOM_SocketModule **** LONG LONG PRESS ***");
303 }
304 else if (M5.BtnB.wasReleasefor(1000))
305 {
306 // buttonA_longPress_ATOM_SocketModule();
307 SerialDebug.println("ATOM_SocketModule **** LONG PRESS ***");
309 }
310 else if (M5.BtnB.wasReleased())
311 {
312 // buttonA_shortPress_ATOM_SocketModule();
313 SerialDebug.println("ATOM_SocketModule **** SHORT PRESS ***");
315 }
316#endif
317#endif //ESP_M5
318}
319
320//! LOOP code.. refactored so it's also called when a buttonPress message arrives
322{
323
324
325 //#define read_serial_monitor
326#ifdef read_serial_monitor
327 //??? reads from the serial monitor it seems
328 // Actually .. there is a way to send a message form the serial monitor..
329 boolean serialData = false;
330 while (Serial.available()) {
331 serialData = true;
332 int ch = Serial.read();
333 // Serial2.write(ch);
334 SerialDebug.write(ch);
335 }
336 if (serialData)
337 SerialDebug.println();
338
339#endif
340
341 //! 1.11.24 one idea, use the PIR_SM and let this send that message
342 //! That way a long press could turn on all the devices, for example..
343 //! OR: just have a long press toggle but for everyone..
344 //! TODO..
346 {
347#ifdef USE_FAST_LED
348 // fillpix(L_YELLOW);
349#endif
351 if (ATOM.SerialRead == 1)
352 {
353 SerialDebug.println("*** ATOM.SerialRead == 1");
354 _Voltage = ATOM.GetVol();
357
358 //! use STATUS, and Long Press to get info..
359 }
360 else
361 {
362 _Voltage = 0;
363 _Current = 0;
364 _ActivePower = 0;
365 }
366
367 SerialDebug.printf("Voltage=%d, Current=%d, ActivePower=%f\n", _Voltage, _Current,_ActivePower);
368 //TODO .. send this message out on MQTT
369 //TODO: add a callback for requesting "STATUS"
370 }
372 {
373#ifdef USE_FAST_LED
374 fillpix(L_RED);
375#else
376 //! 8.2.26 LED working
378#endif
379 SerialDebug.printf("CLEAN CREDENTIALS and reboot to AP mode\n");
380
381 //! dispatches a call to the command specified. This is run on the next loop()
383 }
385 {
387 }
388}
389
390//!returns a string in in URL so: status&battery=84'&buzzon='off' } .. etc
392{
393 //! 8.4.25 add k=PWR
395 return (char*)"&socket=on&k=PWR";
396 else
397 return (char*)"&socket=off&k=PWR";
398}
399
400//!returns a string in in JSON so: status&battery=84'&buzzon='off' } .. etc
401//!starts with "&"*
403{
405 return (char*)"'socket':'on'";
406 else
407 return (char*)"'socket':'off'";
408}
409
410#else
411
412
413//!THIS IS the setup() and loop() but using the "component" name, eg AudioModule()
414//!This will perform preference initializtion as well
415//! called from the setup()
416//! Pass in the method to call on a loud (over a threshhold. The parameter for value will be sent
417//void setup_AudioModule(void (*loudCallback)(int));
419
420//! called for the loop() of this plugin
422
423
424//! 8.28.23 Adding a way for others to get informed on messages that arrive
425//! for the set,val
426//! 12.27.23 support setName == "socket"
427//! 1.10.24 if deviceNameSpecified then this matches this device, otherwise for all.
428//! It's up to the receiver to decide if it has to be specified
429void messageSetVal_ATOM_SocketModule(char *setName, char* valValue, boolean deviceNameSpecified){}
430
431//! 12.28.23, 8.28.23 Adding a way for others to get informed on messages that arrive
432//! for the set,val
433//! 5.21.22 note tthe SEND and CMD are both routed through "send"
434void messageSend_ATOM_SocketModule(char *sendValue) {}
435
436//! BUTTON PROCESSING abstraction
437//!short press on buttonA (top button)
439//!long press on buttonA (top button)
441
442//!returns a string in in URL so: status&battery=84'&buzzon='off' } .. etc
443//!starts with "&"*
444char * currentStatusURL_ATOM_SocketModule() { return (char*)"";}
445
446//!returns a string in in JSON so: status&battery=84'&buzzon='off' } .. etc
447//!starts with "&"*
448char * currentStatusJSON_ATOM_SocketModule(){return (char*)"";}
449#endif // ATOM_SOCKET_MODULE
boolean _longLongPress_ATOM_SocketModule
void checkButtonB_ATOM_SocketModule()
big button on front of M5StickC Plus
void set_ATOM_SocketModule(boolean flag)
turn on/off the socket
void M5AtomSocketCallback(char *parameter, boolean flag)
char * currentStatusURL_ATOM_SocketModule()
returns a string in in URL so: status&battery=84'&buzzon='off' } .. etc
boolean _shortPress_ATOM_SocketModule
not beware of linking another ATOM these names will overload..
void buttonA_LongPress_ATOM_SocketModule()
long press on buttonA (top button)
void setup_ATOM_SocketModule()
#define RELAY
void loopCode_ATOM_SocketModule()
LOOP code.. refactored so it's also called when a buttonPress message arrives.
int _ActivePower
ATOMSOCKET ATOM
create the ATOMSOCKET connection to the power socket
void loop_ATOM_SocketModule()
called for the loop() of this plugin
void messageSend_ATOM_SocketModule(char *sendValue)
void messageSetVal_ATOM_SocketModule(char *setName, char *valValue, boolean deviceNameSpecified)
char * currentStatusJSON_ATOM_SocketModule()
boolean _longPress_ATOM_SocketModule
void toggle_ATOM_SocketModule()
turn on/off the socket
#define RXD
void buttonA_ShortPress_ATOM_SocketModule()
int _Voltage
info from the socket power
boolean _isOn_ATOM_SocketModule
void initGlobals_ATOM_SocketModule()
HardwareSerial AtomSerial(2)
interface to the hardware
float _Current
KeyUnitSensorClass * _KeyUnitSensorClass_ATOMSocketModule
void setup_M5Display()
the setup() no loop()
Definition: M5Display.cpp:5
boolean registerPinUse_mainModule(long pin, String pinName, String moduleName, boolean isI2C)
void sendStatusMQTT_mainModule()
1.1.24 send status of this device after events..
boolean isTrueString_mainModule(String valCmdString)
whether the string is TRUE, ON, 1
Definition: MainModule.cpp:918
bool containsSubstring(String message, String substring)
check if the string contains the other string. This is a poor man's grammer checker
Definition: MainModule.cpp:765
void sendMessageString_mainModule(char *messageString)
adding a synchronous call to send a message over the network (assuming MQTT but not specified),...
void main_dispatchAsyncCommand(int asyncCallCommand)
checks if any async commands are in 'dispatch' mode, and if so, invokes them, and sets their flag to ...
void espUpdate_mainModule(int num)
the Update aggrgate
Definition: MainModule.cpp:521
void setLEDColor(ColorsUnified color)
uses colors.. _GREEN to be typed values
#define ASYNC_CALL_CLEAN_CREDENTIALS
cleans out the credentials and restarts in AP (Access Point) mode.
Definition: MainModule.h:203
@ YELLOW_LED
Definition: MainModule.h:540
@ GREEN_LED
Definition: MainModule.h:538
@ RED_LED
Definition: MainModule.h:536
boolean getPreferenceBoolean_mainModule(int preferenceID)
called to set a preference (which will be an identifier and a string, which can be converted to a num...
void storePreference_mainModule(int preferenceID, String preferenceValue)
called to append to a a preference (which will be an identifier and a string, which can be converted ...
char * getPreference_mainModule(int preferenceID)
SensorStruct * getSensor_mainModule(char *sensorName)
return the sensor specified or null
#define PREFERENCE_ATOMS_SETTING
1.1.24 the preference for all the ATOM plugs (format: atomType:value} .. for now just use socket:on
#define PREFERENCE_ATOM_SOCKET_GLOBAL_ONOFF_SETTING
1.12.24 Whether the AtomSocket accepts global on/off messages
float GetCurrent()
Definition: AtomSocket.cpp:92
void SetPowerOff()
Definition: AtomSocket.cpp:25
float GetActivePower()
Definition: AtomSocket.cpp:103
void SetPowerOn()
Definition: AtomSocket.cpp:21
float GetVol()
Definition: AtomSocket.cpp:81
void Init(HardwareSerial &SerialData, int _RelayIO, int _RXD)
from: https://github.com/m5stack/M5Atom/blob/master/examples/ATOM_BASE/ATOM_Socket/AtomSocket....
Definition: AtomSocket.cpp:6
bool SerialRead
Definition: AtomSocket.h:30
void SerialReadLoop()
Definition: AtomSocket.cpp:37
void setup()
Pure Virtual Function.
void loop()
Pure Virtual Function.
void registerCallback(sensorCallbackSignature *callback)