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
72#endif
74
75}
76#endif
77
78//! turn on/off the socket
79void set_ATOM_SocketModule(boolean flag)
80{
81 //!store globally
83 SerialDebug.printf("set_ATOM_SocketModule: %s\n",flag?"ON":"OFF");
84
86 {
88#ifdef USE_FAST_LED
90#endif
91 //! 1.1.24 syntax: socket=on or socket=off (somewhere in the string..) .. this version, only string avaialble..
93 }
94 else
95 {
97#ifdef USE_FAST_LED
99 //or turn light off??
100#endif
101 //! 1.1.24 syntax: socket=on or socket=off (somewhere in the string..)
103 }
104
105 //!send status after power on/off change..
107
108}
109
110//! turn on/off the socket
112{
114}
115
116
117//! 8.28.23 Adding a way for others to get informed on messages that arrive
118//! for the set,val
119//! 2.27.23 support setName == "socket"
120//! 1.10.24 if deviceNameSpecified then this matches this device, otherwise for all.
121//! It's up to the receiver to decide if it has to be specified
122//! 1.12.24 AtomSocketGlobalOnOff to turn on/off global onoff
123void messageSetVal_ATOM_SocketModule(char *setName, char* valValue, boolean deviceNameSpecified)
124{
125 //! see if global on/off is supported..
126 //! So if globalOnOff definitely perform action. If deviceNameSpecified - perform action so OR of them
128
129 boolean isTrue = isTrueString_mainModule(valValue);
130 if (strcmp(setName,"socket")==0)
131 {
132 SerialDebug.printf("messageSetVal_ATOM_SocketModule(%s - global =%d, dev=%d)\n", valValue, globalOnOff, deviceNameSpecified);
133 if (globalOnOff || deviceNameSpecified)
134 {
135 set_ATOM_SocketModule(isTrue);
136 }
137 }
138}
139
140//! 12.28.23, 8.28.23 Adding a way for others to get informed on messages that arrive
141//! for the send
142void messageSend_ATOM_SocketModule(char *sendValue)
143{
144 SerialDebug.printf("messageSend_ATOM_SocketModule(%s)\n", sendValue);
145 if (strcasecmp(sendValue, "togglesocket")==0)
146 {
148 }
149}
150
151//! init any globals
152//! 1.5.24
154{
155}
156
157
158//!THIS IS the setup() and loop() but using the "component" name, eg AudioModule()
159//!This will perform preference initializtion as well
160//! called from the setup()
161//! Pass in the method to call on a loud (over a threshhold. The parameter for value will be sent
162//void setup_AudioModule(void (*loudCallback)(int));
164{
166
167 //! 5.3.25 register our PIN use
168 registerPinUse_mainModule(RXD, "RXD", "ATOM_SocketModule", false);
169 registerPinUse_mainModule(RELAY, "RELAY", "ATOM_SocketModule", false);
170
171 //! 1.1.24 syntax: socket=on or socket=off (somewhere in the string..)
173 SerialDebug.printf("ATOM Pref = %s\n", preference);
174 if (containsSubstring(preference,(char*)"socket=on"))
176 else
178
179 //! TODO..
180#ifdef ESP_M5_ATOM_S3
181 M5.begin();
182#else
183 SerialDebug.println("setup_ATOM_SocketModule.M5.begin");
184
185 M5.begin(true, false, true);
186#endif
187
188
189#ifdef USE_FAST_LED
190 //!NOTE: this could probably be done by ESP_IOT.ino .. but for now keep here (and in the other ATOM code..)
193#endif
194
195 //! ijnit the ATOM Socket
197
198 //!default whatever was the last mode Persistently..
200
201#ifdef KEY_UNIT_SENSOR_CLASS
202 //SensorStruct* sensor = getSensor_mainModule("KeyUnitInstanceM5AtomSocket");
203 //!NOTE: error without (char*) -- *warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
204 SensorStruct* sensor = getSensor_mainModule((char*)"BuzzerSensorClass");
205
206 //! if the Buzzer isn't on .. then we are good to go
207 //! try this 5.14.25 .. But this is probablly wrong .. is should be just the KeyUnitSensorClass .. and maybe more generic..
208 if (!sensor)
209 {
210 //! create instance..
211 _KeyUnitSensorClass_ATOMSocketModule = new KeyUnitSensorClass((char*)"KeyUnitInstanceM5AtomSocket");
212 //! specify the callback
214 //! call the setup
216 }
217 else
218 {
219 SerialDebug.println("BuzzerSensorClass already specified in sensors");
220 }
221#endif
222}
223
224//! called for the loop() of this plugin
226{
227 //! TODO..
228 //!
229#ifdef CALL_LATER
231 if (ATOM.SerialRead == 1)
232 {
233 //SerialDebug.println("*** ATOM.SerialRead == 1");
234
235 _Voltage = ATOM.GetVol();
238
239 //! use STATUS, and Long Press to get info..
240 }
241#endif
242
243 M5.update();
244 // checkButtonA_ATOM_SocketModule();
246
247 //! do loop code
249
250#ifdef KEY_UNIT_SENSOR_CLASS
253#endif
254}
255
256//! BUTTON PROCESSING abstraction
257//!short press on buttonA (top button)
259{
263
266}
267
268//!long press on buttonA (top button)
270{
274
277}
278
279
280//!big button on front of M5StickC Plus
282{
286
287#ifdef ESP_M5
288
289#ifdef ESP_M5_ATOM_S3
290#else
291 //!NOTE: ths issue is the timer is interruped by the scanner.. so make long-long very long..
292 //was 1000 (from 500)
293 if (M5.BtnB.wasReleasefor(3500))
294 {
295 // buttonA_longPress_ATOM_SocketModule();
296 SerialDebug.println("ATOM_SocketModule **** LONG LONG PRESS ***");
298 }
299 else if (M5.BtnB.wasReleasefor(1000))
300 {
301 // buttonA_longPress_ATOM_SocketModule();
302 SerialDebug.println("ATOM_SocketModule **** LONG PRESS ***");
304 }
305 else if (M5.BtnB.wasReleased())
306 {
307 // buttonA_shortPress_ATOM_SocketModule();
308 SerialDebug.println("ATOM_SocketModule **** SHORT PRESS ***");
310 }
311#endif
312#endif //ESP_M5
313}
314
315//! LOOP code.. refactored so it's also called when a buttonPress message arrives
317{
318
319
320 //#define read_serial_monitor
321#ifdef read_serial_monitor
322 //??? reads from the serial monitor it seems
323 // Actually .. there is a way to send a message form the serial monitor..
324 boolean serialData = false;
325 while (Serial.available()) {
326 serialData = true;
327 int ch = Serial.read();
328 // Serial2.write(ch);
329 SerialDebug.write(ch);
330 }
331 if (serialData)
332 SerialDebug.println();
333
334#endif
335
336 //! 1.11.24 one idea, use the PIR_SM and let this send that message
337 //! That way a long press could turn on all the devices, for example..
338 //! OR: just have a long press toggle but for everyone..
339 //! TODO..
341 {
342#ifdef USE_FAST_LED
343 // fillpix(L_YELLOW);
344#endif
346 if (ATOM.SerialRead == 1)
347 {
348 SerialDebug.println("*** ATOM.SerialRead == 1");
349 _Voltage = ATOM.GetVol();
352
353 //! use STATUS, and Long Press to get info..
354 }
355 else
356 {
357 _Voltage = 0;
358 _Current = 0;
359 _ActivePower = 0;
360 }
361
362 SerialDebug.printf("Voltage=%d, Current=%d, ActivePower=%f\n", _Voltage, _Current,_ActivePower);
363 //TODO .. send this message out on MQTT
364 //TODO: add a callback for requesting "STATUS"
365 }
367 {
368#ifdef USE_FAST_LED
369 fillpix(L_RED);
370#endif
371 SerialDebug.printf("CLEAN CREDENTIALS and reboot to AP mode\n");
372
373 //! dispatches a call to the command specified. This is run on the next loop()
375 }
377 {
379 }
380}
381
382//!returns a string in in URL so: status&battery=84'&buzzon='off' } .. etc
384{
385 //! 8.4.25 add k=PWR
387 return (char*)"&socket=on&k=PWR";
388 else
389 return (char*)"&socket=off&k=PWR";
390}
391
392//!returns a string in in JSON so: status&battery=84'&buzzon='off' } .. etc
393//!starts with "&"*
395{
397 return (char*)"'socket':'on'";
398 else
399 return (char*)"'socket':'off'";
400}
401
402#else
403
404
405//!THIS IS the setup() and loop() but using the "component" name, eg AudioModule()
406//!This will perform preference initializtion as well
407//! called from the setup()
408//! Pass in the method to call on a loud (over a threshhold. The parameter for value will be sent
409//void setup_AudioModule(void (*loudCallback)(int));
411
412//! called for the loop() of this plugin
414
415
416//! 8.28.23 Adding a way for others to get informed on messages that arrive
417//! for the set,val
418//! 12.27.23 support setName == "socket"
419//! 1.10.24 if deviceNameSpecified then this matches this device, otherwise for all.
420//! It's up to the receiver to decide if it has to be specified
421void messageSetVal_ATOM_SocketModule(char *setName, char* valValue, boolean deviceNameSpecified){}
422
423//! 12.28.23, 8.28.23 Adding a way for others to get informed on messages that arrive
424//! for the set,val
425//! 5.21.22 note tthe SEND and CMD are both routed through "send"
426void messageSend_ATOM_SocketModule(char *sendValue) {}
427
428//! BUTTON PROCESSING abstraction
429//!short press on buttonA (top button)
431//!long press on buttonA (top button)
433
434//!returns a string in in URL so: status&battery=84'&buzzon='off' } .. etc
435//!starts with "&"*
436char * currentStatusURL_ATOM_SocketModule() { return (char*)"";}
437
438//!returns a string in in JSON so: status&battery=84'&buzzon='off' } .. etc
439//!starts with "&"*
440char * currentStatusJSON_ATOM_SocketModule(){return (char*)"";}
441#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:11
void fillpix(CRGB Color)
color the button light
Definition: M5Display.cpp:65
#define L_RED
colors
Definition: M5Display.h:36
#define L_YELLOW
Definition: M5Display.h:40
#define L_GREEN
Definition: M5Display.h:37
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:549
void registerPinUse_mainModule(long pin, String pinName, String moduleName, boolean isI2C)
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:396
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 ...
#define ASYNC_CALL_CLEAN_CREDENTIALS
cleans out the credentials and restarts in AP (Access Point) mode.
Definition: MainModule.h:193
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)