ESP_IOT v2.5
IOT ESP Coding
ESP_IOT.ino
Go to the documentation of this file.
1/*
2 Name: NimBLE_PetTutor_Server.ino
3 Created: 8/25/2021 7:49:11 PM
4 Author: wes and scott
5*/
6
7//!defines.h defines the #ifdef USE_MODULE statements, as they are included elsewhere.. Don't define them here.
8#include "Defines.h"
9
10#ifdef ESP_M5
11//NEED to fix the port to use... /dev/cu.usbserial-61CEEBBFAD
12//todo..
13//#define HIGH 1
14#define LED 2
15//#include "src/ButtonModule/ButtonProcessing.h"
16//NOTE: the Board has to be modified to M5Stick-C-Plus
17
18//called from BLEServerNetworking, MainModule and StepperModule(Dispense.cpp)
19void stepperModule_ProcessClientCmd(char cmd)
20{
21 SerialError.printf("UNDEFINED: stepperModule_processClientCmd: %c\n", cmd);
22}
23#else
24
25#endif //ESP_M5
26
27
28
29//NOTE: there might be an issue that the REGISTERED methods aren't done yet .. so cannot call them..
31
32//forward definition
33void finishSetup();
34
35#include "nvs_flash.h"
36// ******************************************
37//!note: setup only called one time...
38//! so, the first time the doneWIFIModuleCredentials is set.. call setup()
39//!main setup
40void setup() {
41
42#ifdef ERASE_ALL
43//https://github.com/espressif/arduino-esp32/issues/2144
44 ESP_ERROR_CHECK(nvs_flash_erase());
45 nvs_flash_init();
46
47 #endif
48 Serial.begin(115200);
49 Serial.println();
50 pinMode(LED, OUTPUT);
51
52 //This must be first .. as it inializes the display and Serial Ports..
53 //! called for the loop() of this plugin
55
56
57
58
59 SerialInfo.println("******************");
60 SerialMin.println(VERSION);
61
62 //Timestamp the compile
63 SerialDebug.print("Compiled ");
64 SerialDebug.print(__DATE__);
65 SerialDebug.print(" @ ");
66 SerialDebug.println(__TIME__);
67
68 //not good time .. since no WIFI to get global time..
69// printInt_SPIFFModule(getTimeStamp_mainModule());
70// println_SPIFFModule((char*)" time");
71
72
73
74 //called in setup() to init the async calls
76
77 //!4.17.22
78 //! MainModule does all the preferences, etc..
80
81
83 SerialInfo.println("******************");
84
85#ifdef USE_SPIFF_MODULE
86 //!SPIFF must be after main_setup() since it reads preferences .. is first so debug can be saved
88 println_SPIFFModule((char*)VERSION);
89
90#endif
91
92#ifdef USE_CAMERA_MODULE
93 setup_CameraModule();
94#endif
95
96#ifdef USE_BUTTON_MODULE
97 //calls the setup() for ButtonModule
99
100 //register the single click
102
103#endif
104
105 //!12.8.22 add audio
106#ifdef USE_AUDIO_MODULE
108#endif
109
110#ifdef USE_STEPPER_MODULE
111 //reads credentials, etc..
113#endif
114
115#ifdef USE_UI_MODULE
116 //setup the stepper for the feeder
118#endif
119
120 SerialInfo.println("Starting PetTutor_Server");
121 SerialDebug.printf("M5STACK VERSION = %s\n", M5STACK_VERSION);
122
123 //APril 8, refactoring. Moved up to here..
124#ifdef USE_MQTT_NETWORKING
125
126 //pass the callbacks, (1) the message callback, and the (blinkLed) implementation
127 //setMessageCallbacks(&feedMessageCallback, &blinkMessageCallback);
128 //register the 2 callbacks for now..
132
133 //3.25.22 -- register the cleanSSID_EPROM
135
136 //NOTE: 6.3.22 .. this is breaking the AP mode, preSetup_WIF_APModule must be called first..
137 //try call from loop():
138// setup_MQTTNetworking();
139
140
141 //the tilt 5.2.22 (no one calls this ...)
143#endif
144
145#ifdef USE_WIFI_AP_MODULE
146 //called to validate the doneWIFI() call...
148#endif
149
150
151 //init the BLE here.
152#ifdef USE_BLE_SERVER_NETWORKING //yes
153 //!if the BLE_SERVER is turned on..
154#ifdef ESP_M5
156 {
157#endif
158
159 //Initialize the BLE Server, using the service name PTFeeder
160 //https://stackoverflow.com/questions/20944784/why-is-conversion-from-string-constant-to-char-valid-in-c-but-invalid-in-c
161#ifdef USE_MQTT_NETWORKING
162
163 //*** The callback for "onWrite" of the bluetooth "onWrite'
165 //*** The callback for "onWrite" of the bluetooth "onWrite'
167
168 //strdup() get away from the
169 setup_BLEServerNetworking(MAIN_BLE_SERVER_SERVICE_NAME, getDeviceNameMQTT(), strdup(PT_SERVICE_UUID), strdup(PT_CHARACTERISTIC_UUID));
170
171 SerialDebug.println("done setupBLEServerNetworking");
172#else
173 setup_BLEServerNetworking(MAIN_BLE_SERVER_SERVICE_NAME, (char*)"undefined", strdup(PT_SERVICE_UUID), strdup(PT_CHARACTERISTIC_UUID));
174#endif //MQTT_NETWORKING
175#ifdef ESP_M5
176 }
177#endif
178
179#endif //USE_BLE_SERVER_NETWORKING
180
181 //BLE Client --
182#ifdef USE_BLE_CLIENT_NETWORKING
183 setup_BLEClientNetworking(MAIN_BLE_CLIENT_SERVICE, (char*) PT_SERVICE_UUID, (char*) PT_CHARACTERISTIC_UUID);
184#endif //USE_BLE_CLIENT_NETWORKING
185
186
187#ifdef USE_WIFI_AP_MODULE
188
189 //MAJOR ISSUE: if the port/server are wrong .. can never reset unless we can get a BUTTON..
190 //If BOOTSTRAP (a compile time ifdef in Defines.h is set, then bypass the AP mode, and use the constants in MQTTNetworking.cpp
191#ifndef BOOTSTRAP
193 {
194 //Turn on light until out of the AP mode..
195 solidLightOnOff(true);
196 //test
198
199 //set flag ..
201
203
204
205 }
206#endif //BOOTSTRAP
207#else //not USE_WIFI_AP_MODE
208 finishSetup();
209#endif
210}
211
212// **************** finishSetup()
213//! called to finsh the setup.. this is because the MQTT and WIFI aren't run if in AP mode..
215{
216#ifdef USE_WIFI_AP_MODULE
217 //basically after the AP module finished, the function WIFI_APModule_JsonOutputString() stores the credentials (versus them calling here ..)
219 {
220 //Turn on light until out of the AP mode..
221 solidLightOnOff(false);
222
223 String jsonOutputString = WIFI_APModule_JsonOutputString();
224 // now this could be passed onto the MQTT processor..
225 SerialDebug.print("From WIFI -> JSON = ");
226 SerialDebug.println(jsonOutputString);
227#endif
228
229#ifdef USE_MQTT_NETWORKING
230 //moved here 6.3.22
232#ifdef USE_WIFI_AP_MODULE
233 //pass the json string onto the MQTT code..
234 // 3.26.22
235 SerialDebug.println("calling .. processJSONMessage");
236 //NOTE: if done was because credentials .. then don't call this..
237 //basically if MQTT running .. don't call this..
238 processJSONMessageMQTT(&jsonOutputString[0], NULL);
239#endif
240 //NOTE: we are still in the BLE callback .. and then we call setup_MQTTNetworking..
241 //LETS try setting state in MQTT that is waiting for WIFI..
242
243
244 //perform the setup() in the MQTTNetworking
245 //NOTE: this is called 2 times if processJSONMessage() sets the eprom data .. since the network was already setup
246 //there is a state flag internally that won't setup 2 times (unless MQTT was kicked out via AP mode, or cleaned credentials)
247 //try call from loop():
248 //setup_MQTTNetworking();
249
250 // sets the _chipName (from MQTT Module)
251 char *deviceName = getDeviceNameMQTT();
252
253 //THIS should output the device name, user name, etc...
254 // SerialDebug.print("pFoodCharacteristic->setValue: ");
255 // SerialDebug.println(_fullJSONString);
256 SerialDebug.print("DeviceName: ");
257 SerialDebug.println(deviceName);
258
259#endif //USE_MQTT_NETWORKING
260
261
262#ifdef USE_WIFI_AP_MODULE
263
264 } //doneWIFI_APModule_Credentials()
265#endif
266
267}
268
269
270// ****************************************
271//! main loop() of the Arduino runtime
272void loop() {
273//#define USE_STOP_ALL
274
275 #ifdef USE_STOP_ALL
277 {
278 delay(100);
279 return;
280 }
281 #endif
282
283 //4.17.22
285
286 //!12.8.22 add audio
287#ifdef USE_AUDIO_MODULE
289#endif
290
291 #ifdef USE_CAMERA_MODULE
292 loop_CameraModule();
293#endif
294
295
296#ifdef USE_WIFI_AP_MODULE
297
299 {
300 //since the MQTT and BLE were not started, the first time the WIFI credentials are finished (after submit)
301 // finish the setup (which are the MQTT and WIFI.
302 //NOTE: this needs to get the credentials in JSON format and store ..
304 {
306 SerialDebug.println("doneWIFI_APModuleCredentials, and now calling finishSetup()");
307 finishSetup();
308 }
309 }
310
311#endif //USE_WIFI_AP_MODE
312
313 // called to check if any Async Commands are in dispatch mode
315
316 //! note: displayModule() musts be called before buttonModule (as the "model" is updated (initiated) in the display module
317 //! called for the loop() of this plugin (this is almost the "frames" of the graphic, as fast as the runtime can make this loop! FAST.. 24 fps ??
319
320#ifdef USE_BUTTON_MODULE
322#endif
323
324 /** Do your thing here, this just spams notifications to all connected clients */
325#ifdef USE_STEPPER_MODULE
326 loop_StepperModule(); //was FeederStateMachine
327#endif //USE_STEPPER_MODULE
328
329#ifdef USE_UI_MODULE
331#endif
332
333 //BLE Client --
334#ifdef USE_BLE_CLIENT_NETWORKING
336#endif //USE_BLE_CLIENT_NETWORKING
337
338
339 //** NOTE: the WIFI_AP_MODULE is tricky. The doneWIFI_APModule_Credentials() is set when done, but also when the MQTT networking is running (meaning the credentials were specified via BLE, or from EPROM)
340#ifdef USE_WIFI_AP_MODULE
341 //if the APmodule credentials are finished .. then continue normal loop
342 // Otherwise just BLE and AP (for now)
344 {
345#endif
346
347
348 /*******************************MQTT*************************************/
349#ifdef USE_MQTT_NETWORKING
351#endif
352
353
354#ifdef USE_BLE_SERVER_NETWORKING
355 //!if the BLE_SERVER is turned on..
356#ifdef ESP_M5
358 {
359#endif
361#ifdef ESP_M5
362 }
363#endif
364
365#else
366#endif // USE_BLE_SERVER_NETWORKING
367
368
369#ifdef USE_WIFI_AP_MODULE
370 } //!doneWIFI_APModuleCredentials
371#endif
372
373#ifdef USE_WIFI_AP_MODULE
375 {
377 }
378#endif
379 //APril 8, 2022
380 //loop always, we just don't have WIFI working yet...?
381 //NOTE: the loopBLE will call the register for a message (and call processJSONMessage on BLE commands)
382 //NEED: to set the doneWIFI_APModuleCredentials iff a processJSONMessage resulted in credential updates,
383 // not all JSON are credentials. Others are setting options, like OTA. -- and kill the AP module..
384 //UNFORTUNATELY .. the WIFI and MQTT might fail .. and slow things down. NEED a better way to
385 // run the MQTT loop (so others can get some time, like BLE).
386
387
388 delay(100); //this delays the feed trigger response
389
390}
void loop_AudioModule()
called for the loop() of this plugin
void setup_AudioModule()
void loop_BLEClientNetworking()
the loop()
void setup_BLEClientNetworking(char *serviceName, char *serviceUUID, char *characteristicUUID)
the setup() and loop() passing the serviceName to look for..
void loop_BLEServerNetworking()
the 'loop' for this module BLEServerNetworking.
void setup_BLEServerNetworking(char *serviceName, char *deviceName, char *serviceUUID, char *characteristicUUID)
the 'setup' for this module BLEServerNetworking. Here the service name is added (and potentially more...
#define BLE_SERVER_CALLBACK_STATUS_MESSAGE
used to send a string message back (which might be sent to MQTT for example)
#define BLE_SERVER_CALLBACK_ONWRITE
void loop_ButtonModule()
void setup_ButtonModule()
#define SINGLE_CLICK_BM
Definition: ButtonModule.h:24
void loop_displayModule()
called for the loop() of this plugin
void setup_displayModule()
for types: String, boolean ..
void setup()
Definition: ESP_IOT.ino:40
boolean _finishSetupCalledAlready
defines.h defines the #ifdef USE_MODULE statements, as they are included elsewhere....
Definition: ESP_IOT.ino:30
void finishSetup()
called to finsh the setup.. this is because the MQTT and WIFI aren't run if in AP mode....
Definition: ESP_IOT.ino:214
void loop()
main loop() of the Arduino runtime
Definition: ESP_IOT.ino:272
void setup_MQTTNetworking()
setup the MQTT part of networking
void loop_MQTTNetworking()
called for the loop() of this plugin
boolean processJSONMessageMQTT(char *ascii, char *topic)
process the JSON message (looking for FEED, etc). Note: topic can be nil, or if not,...
char * getDeviceNameMQTT()
called for things like the advertisement
#define MQTT_CLEAN_SSID_EPROM
#define MQTT_CALLBACK_BLINK
#define MQTT_CALLBACK_FEED
#define MQTT_CALLBACK_SOLID_LIGHT
#define MQTT_CALLBACK_TILT_MOTION_DETECTED
#define CALLBACKS_MQTT
Definition: MainModule.cpp:221
void cleanSSID_EPROM_MessageCallback(char *message)
clean the SSID eprom (MQTT_CLEAN_SSID_EPROM)
void loop_mainModule()
called for the loop() of this plugin
Definition: MainModule.cpp:114
void registerCallbackMain(int callbacksModuleId, int callbackType, void(*callback)(char *))
register the callback based on the callbackType. use the callbacksModuleId for which one....
Definition: MainModule.cpp:316
#define CALLBACKS_BLE_SERVER
Definition: MainModule.cpp:224
void onStatusMessageBLEServerCallback(char *message)
The callback for "status messages" of the bluetooth.
Definition: MainModule.cpp:411
void solidLightOnOff(boolean onOff)
callback for SOLID blinking led
void setup_mainModule()
called from the setup()
Definition: MainModule.cpp:72
boolean stopAllProcesses_mainModule()
if stopped
Definition: MainModule.cpp:21
void initAsyncCallFlags()
initialize the async call flags (with and without parameters)
Definition: MainModule.cpp:770
void solidLightMessageCallback(char *message)
callback for SOLID blinking led
void feedMessageCallback(char *message)
Definition: MainModule.cpp:352
void invokeAsyncCommands()
checks if any async commands are in 'dispatch' mode, and if so, invokes them, and sets their flag to ...
Definition: MainModule.cpp:829
void onWriteBLEServerCallback(char *message)
The callback for "onWrite" of the bluetooth "onWrite'.
Definition: MainModule.cpp:511
void blinkMessageCallback(char *message)
callback for blinking led
void main_printModuleConfiguration()
void singleClickTouched(char *whichButton)
#define CALLBACKS_BUTTON_MODULE
Definition: MainModule.cpp:222
#define PT_CHARACTERISTIC_UUID
Definition: MainModule.h:35
#define PT_SERVICE_UUID
Definition: MainModule.h:34
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...
#define PREFERENCE_MAIN_BLE_SERVER_VALUE
void println_SPIFFModule(char *string)
print a string to spiff (a new line is added)
void setup_SPIFFModule()
the setup for this module
void loop_StepperModule()
the main loop fro the StepperModule. This used be called FeederStateMachine()
void setup_StepperModule()
called on setup()
#define LED
Definition: StepperModule.h:54
void setup_UIModule()
UIModule
Definition: UI.cpp:16
void loop_UIModule()
the main loop()
Definition: UI.cpp:47
String WIFI_APModule_JsonOutputString()
retrieves the JSON string
void loop_WIFI_APModule()
main loop
void setup_WIFI_APModule()
sets the config in the EPPROM called wifi-config. NOTE: the _done isn't valid until after this code....
void WIFI_APModule_ResetCredentials()
resets the credentials
boolean doneWIFI_APModule_Credentials()
called to see if the WIFIModule has finished bootstrapping..
void preSetup_WIFI_APModule()