1
+ #include " RMaker.h"
2
+ #include " WiFi.h"
3
+ #include " WiFiProv.h"
4
+ #include " DHT.h"
5
+ #include < SimpleTimer.h>
6
+ #include < wifi_provisioning/manager.h>
7
+
8
+ // Set Defalt Values
9
+ #define DEFAULT_LED_MODE true
10
+ #define DEFAULT_Temperature 0
11
+ #define DEFAULT_Humidity 0
12
+
13
+ // BLE Credentils
14
+ const char *service_name = " RainMakerBLE" ;
15
+ const char *pop = " 1234567" ;
16
+
17
+ // GPIO
18
+ static uint8_t gpio_reset = 0 ;
19
+ static uint8_t DHTPIN = 21 ;
20
+ static uint8_t led = 23 ;
21
+ bool led_state = true ;
22
+
23
+ bool wifi_connected = 0 ;
24
+
25
+ DHT dht (DHTPIN, DHT11);
26
+
27
+ SimpleTimer Timer;
28
+
29
+ // ------------------------------------------- Declaring Devices -----------------------------------------------------//
30
+
31
+ // The framework provides some standard device types like switch, lightbulb, fan, temperature sensor.
32
+ static TemperatureSensor temperature (" Temperature" );
33
+ static TemperatureSensor humidity (" Humidity" );
34
+ static Switch my_switch (" LED" , &led);
35
+
36
+ void sysProvEvent (arduino_event_t *sys_event)
37
+ {
38
+ switch (sys_event->event_id ) {
39
+ case ARDUINO_EVENT_PROV_START:
40
+ #if CONFIG_IDF_TARGET_ESP32
41
+ Serial.printf (" \n Provisioning Started with name \" %s\" and PoP \" %s\" on BLE\n " , service_name, pop);
42
+ printQR (service_name, pop, " ble" );
43
+ #else
44
+ Serial.printf (" \n Provisioning Started with name \" %s\" and PoP \" %s\" on SoftAP\n " , service_name, pop);
45
+ printQR (service_name, pop, " softap" );
46
+ #endif
47
+ break ;
48
+ case ARDUINO_EVENT_WIFI_STA_CONNECTED:
49
+ Serial.printf (" \n Connected to Wi-Fi!\n " );
50
+ wifi_connected = 1 ;
51
+ delay (500 );
52
+ break ;
53
+ case ARDUINO_EVENT_PROV_CRED_RECV: {
54
+ Serial.println (" \n Received Wi-Fi credentials" );
55
+ Serial.print (" \t SSID : " );
56
+ Serial.println ((const char *) sys_event->event_info .prov_cred_recv .ssid );
57
+ Serial.print (" \t Password : " );
58
+ Serial.println ((char const *) sys_event->event_info .prov_cred_recv .password );
59
+ break ;
60
+ }
61
+ }
62
+ }
63
+
64
+ void write_callback (Device *device, Param *param, const param_val_t val, void *priv_data, write_ctx_t *ctx)
65
+ {
66
+ const char *device_name = device->getDeviceName ();
67
+ Serial.println (device_name);
68
+ const char *param_name = param->getParamName ();
69
+
70
+ if (strcmp (device_name, " LED" ) == 0 )
71
+ {
72
+ if (strcmp (param_name, " Power" ) == 0 )
73
+ {
74
+ Serial.printf (" Received value = %s for %s - %s\n " , val.val .b ? " true" : " false" , device_name, param_name);
75
+ led_state = val.val .b ;
76
+ (led_state == false ) ? digitalWrite (led, LOW) : digitalWrite (led, HIGH);
77
+ param->updateAndReport (val);
78
+ }
79
+ }
80
+ }
81
+
82
+
83
+ void setup ()
84
+ {
85
+
86
+ Serial.begin (115200 );
87
+
88
+ // Configure the input GPIOs
89
+ pinMode (gpio_reset, INPUT);
90
+ pinMode (led, OUTPUT);
91
+ digitalWrite (led, DEFAULT_LED_MODE);
92
+
93
+ // Beginning Sensor
94
+ dht.begin ();
95
+
96
+ // ------------------------------------------- Declaring Node -----------------------------------------------------//
97
+ Node my_node;
98
+ my_node = RMaker.initNode (" ESP RainMaker" );
99
+
100
+ // Standard switch device
101
+ my_switch.addCb (write_callback);
102
+
103
+ // ------------------------------------------- Adding Devices in Node -----------------------------------------------------//
104
+ my_node.addDevice (temperature);
105
+ my_node.addDevice (humidity);
106
+ my_node.addDevice (my_switch);
107
+
108
+
109
+ // This is optional
110
+ RMaker.enableOTA (OTA_USING_PARAMS);
111
+ // If you want to enable scheduling, set time zone for your region using setTimeZone().
112
+ // The list of available values are provided here https://rainmaker.espressif.com/docs/time-service.html
113
+ // RMaker.setTimeZone("Asia/Shanghai");
114
+ // Alternatively, enable the Timezone service and let the phone apps set the appropriate timezone
115
+ RMaker.enableTZService ();
116
+ RMaker.enableSchedule ();
117
+
118
+ Serial.printf (" \n Starting ESP-RainMaker\n " );
119
+ RMaker.start ();
120
+
121
+ // Timer for Sending Sensor's Data
122
+ Timer.setInterval (3000 );
123
+
124
+ WiFi.onEvent (sysProvEvent);
125
+
126
+ #if CONFIG_IDF_TARGET_ESP32
127
+ WiFiProv.beginProvision (WIFI_PROV_SCHEME_BLE, WIFI_PROV_SCHEME_HANDLER_FREE_BTDM, WIFI_PROV_SECURITY_1, pop, service_name);
128
+ #else
129
+ WiFiProv.beginProvision (WIFI_PROV_SCHEME_SOFTAP, WIFI_PROV_SCHEME_HANDLER_NONE, WIFI_PROV_SECURITY_1, pop, service_name);
130
+ #endif
131
+
132
+ }
133
+
134
+
135
+ void loop ()
136
+ {
137
+
138
+ if (Timer.isReady () && wifi_connected) { // Check is ready a second timer
139
+ Serial.println (" Sending Sensor's Data" );
140
+ Send_Sensor ();
141
+ Timer.reset (); // Reset a second timer
142
+ }
143
+
144
+
145
+
146
+ // ----------------------------------------------------------- Logic to read button
147
+
148
+ // Read GPIO0 (external button to reset device
149
+ if (digitalRead (gpio_reset) == LOW) { // Push button pressed
150
+ Serial.printf (" Reset Button Pressed!\n " );
151
+ // Key debounce handling
152
+ delay (100 );
153
+ int startTime = millis ();
154
+ while (digitalRead (gpio_reset) == LOW) delay (50 );
155
+ int endTime = millis ();
156
+
157
+ if ((endTime - startTime) > 10000 ) {
158
+ // If key pressed for more than 10secs, reset all
159
+ Serial.printf (" Reset to factory.\n " );
160
+ wifi_connected = 0 ;
161
+ RMakerFactoryReset (2 );
162
+ } else if ((endTime - startTime) > 3000 ) {
163
+ Serial.printf (" Reset Wi-Fi.\n " );
164
+ wifi_connected = 0 ;
165
+ // If key pressed for more than 3secs, but less than 10, reset Wi-Fi
166
+ RMakerWiFiReset (2 );
167
+ } else {
168
+ // Toggle device state
169
+ led_state = !led_state;
170
+ Serial.printf (" Toggle State to %s.\n " , led_state ? " true" : " false" );
171
+ my_switch.updateAndReportParam (ESP_RMAKER_DEF_POWER_NAME, led_state);
172
+ (led_state == false ) ? digitalWrite (led, LOW) : digitalWrite (led, HIGH);
173
+ }
174
+ }
175
+ delay (100 );
176
+ }
177
+
178
+ void Send_Sensor ()
179
+ {
180
+ float h = dht.readHumidity ();
181
+ float t = dht.readTemperature ();
182
+
183
+ Serial.print (" Temperature - " );
184
+ Serial.println (t);
185
+ Serial.print (" Humidity - " );
186
+ Serial.println (h);
187
+
188
+ temperature.updateAndReportParam (" Temperature" , t);
189
+ humidity.updateAndReportParam (" Temperature" , h);
190
+ }
0 commit comments