├── .gitattributes ├── DHT11_Relay_ESP32 └── DHT11_Relay_ESP32.ino └── README.md /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /DHT11_Relay_ESP32/DHT11_Relay_ESP32.ino: -------------------------------------------------------------------------------- 1 | /* 2 | This example demonstrates the ESP RainMaker with a Relay and DHT sensor. 3 | It's detailed explanation video is uploaded on our YouTube channel 4 | https://www.youtube.com/techiesms 5 | */ 6 | 7 | #include "RMaker.h" 8 | #include "WiFi.h" 9 | #include "WiFiProv.h" 10 | #include "DHT.h" 11 | #include 12 | #include 13 | 14 | // Set Defalt Values 15 | #define DEFAULT_RELAY_MODE true 16 | #define DEFAULT_Temperature 0 17 | #define DEFAULT_Humidity 0 18 | 19 | // BLE Credentils 20 | const char *service_name = "PROV_12345"; 21 | const char *pop = "1234567"; 22 | 23 | // GPIO 24 | static uint8_t gpio_reset = 0; 25 | static uint8_t DHTPIN = 19; 26 | static uint8_t relay = 21; 27 | bool relay_state = true; 28 | 29 | bool wifi_connected = 0; 30 | 31 | DHT dht(DHTPIN, DHT11); 32 | 33 | SimpleTimer Timer; 34 | 35 | //------------------------------------------- Declaring Devices -----------------------------------------------------// 36 | 37 | //The framework provides some standard device types like switch, lightbulb, fan, temperature sensor. 38 | static TemperatureSensor temperature("Temperature"); 39 | static TemperatureSensor humidity("Humidity"); 40 | static Switch my_switch("Relay", &relay); 41 | 42 | void sysProvEvent(arduino_event_t *sys_event) 43 | { 44 | switch (sys_event->event_id) { 45 | case ARDUINO_EVENT_PROV_START: 46 | #if CONFIG_IDF_TARGET_ESP32 47 | Serial.printf("\nProvisioning Started with name \"%s\" and PoP \"%s\" on BLE\n", service_name, pop); 48 | printQR(service_name, pop, "ble"); 49 | #else 50 | Serial.printf("\nProvisioning Started with name \"%s\" and PoP \"%s\" on SoftAP\n", service_name, pop); 51 | printQR(service_name, pop, "softap"); 52 | #endif 53 | break; 54 | case ARDUINO_EVENT_WIFI_STA_CONNECTED: 55 | Serial.printf("\nConnected to Wi-Fi!\n"); 56 | wifi_connected = 1; 57 | delay(500); 58 | break; 59 | case ARDUINO_EVENT_PROV_CRED_RECV: { 60 | Serial.println("\nReceived Wi-Fi credentials"); 61 | Serial.print("\tSSID : "); 62 | Serial.println((const char *) sys_event->event_info.prov_cred_recv.ssid); 63 | Serial.print("\tPassword : "); 64 | Serial.println((char const *) sys_event->event_info.prov_cred_recv.password); 65 | break; 66 | } 67 | case ARDUINO_EVENT_PROV_INIT: 68 | wifi_prov_mgr_disable_auto_stop(10000); 69 | break; 70 | case ARDUINO_EVENT_PROV_CRED_SUCCESS: 71 | Serial.println("Stopping Provisioning!!!"); 72 | wifi_prov_mgr_stop_provisioning(); 73 | break; 74 | } 75 | } 76 | 77 | void write_callback(Device *device, Param *param, const param_val_t val, void *priv_data, write_ctx_t *ctx) 78 | { 79 | const char *device_name = device->getDeviceName(); 80 | Serial.println(device_name); 81 | const char *param_name = param->getParamName(); 82 | 83 | if (strcmp(device_name, "Relay") == 0) 84 | { 85 | if (strcmp(param_name, "Power") == 0) 86 | { 87 | Serial.printf("Received value = %s for %s - %s\n", val.val.b ? "true" : "false", device_name, param_name); 88 | relay_state = val.val.b; 89 | (relay_state == false) ? digitalWrite(relay, LOW) : digitalWrite(relay, HIGH); 90 | param->updateAndReport(val); 91 | } 92 | } 93 | } 94 | 95 | 96 | void setup() 97 | { 98 | 99 | Serial.begin(115200); 100 | 101 | // Configure the input GPIOs 102 | pinMode(gpio_reset, INPUT); 103 | pinMode(relay, OUTPUT); 104 | digitalWrite(relay, DEFAULT_RELAY_MODE); 105 | 106 | //Beginning Sensor 107 | dht.begin(); 108 | 109 | //------------------------------------------- Declaring Node -----------------------------------------------------// 110 | Node my_node; 111 | my_node = RMaker.initNode("Techiesms"); 112 | 113 | //Standard switch device 114 | my_switch.addCb(write_callback); 115 | 116 | //------------------------------------------- Adding Devices in Node -----------------------------------------------------// 117 | my_node.addDevice(temperature); 118 | my_node.addDevice(humidity); 119 | my_node.addDevice(my_switch); 120 | 121 | 122 | //This is optional 123 | RMaker.enableOTA(OTA_USING_PARAMS); 124 | //If you want to enable scheduling, set time zone for your region using setTimeZone(). 125 | //The list of available values are provided here https://rainmaker.espressif.com/docs/time-service.html 126 | // RMaker.setTimeZone("Asia/Shanghai"); 127 | // Alternatively, enable the Timezone service and let the phone apps set the appropriate timezone 128 | RMaker.enableTZService(); 129 | RMaker.enableSchedule(); 130 | 131 | Serial.printf("\nStarting ESP-RainMaker\n"); 132 | RMaker.start(); 133 | 134 | // Timer for Sending Sensor's Data 135 | Timer.setInterval(3000); 136 | 137 | WiFi.onEvent(sysProvEvent); 138 | 139 | #if CONFIG_IDF_TARGET_ESP32 140 | WiFiProv.beginProvision(WIFI_PROV_SCHEME_BLE, WIFI_PROV_SCHEME_HANDLER_FREE_BTDM, WIFI_PROV_SECURITY_1, pop, service_name); 141 | #else 142 | WiFiProv.beginProvision(WIFI_PROV_SCHEME_SOFTAP, WIFI_PROV_SCHEME_HANDLER_NONE, WIFI_PROV_SECURITY_1, pop, service_name); 143 | #endif 144 | 145 | } 146 | 147 | 148 | void loop() 149 | { 150 | 151 | if (Timer.isReady() && wifi_connected) { // Check is ready a second timer 152 | Serial.println("Sending Sensor's Data"); 153 | Send_Sensor(); 154 | Timer.reset(); // Reset a second timer 155 | } 156 | 157 | 158 | 159 | //----------------------------------------------------------- Logic to Reset RainMaker 160 | 161 | // Read GPIO0 (external button to reset device 162 | if (digitalRead(gpio_reset) == LOW) { //Push button pressed 163 | Serial.printf("Reset Button Pressed!\n"); 164 | // Key debounce handling 165 | delay(100); 166 | int startTime = millis(); 167 | while (digitalRead(gpio_reset) == LOW) delay(50); 168 | int endTime = millis(); 169 | 170 | if ((endTime - startTime) > 10000) { 171 | // If key pressed for more than 10secs, reset all 172 | Serial.printf("Reset to factory.\n"); 173 | wifi_connected = 0; 174 | RMakerFactoryReset(2); 175 | } else if ((endTime - startTime) > 3000) { 176 | Serial.printf("Reset Wi-Fi.\n"); 177 | wifi_connected = 0; 178 | // If key pressed for more than 3secs, but less than 10, reset Wi-Fi 179 | RMakerWiFiReset(2); 180 | } 181 | } 182 | delay(100); 183 | } 184 | 185 | void Send_Sensor() 186 | { 187 | float h = dht.readHumidity(); 188 | float t = dht.readTemperature(); 189 | 190 | Serial.print("Temperature - "); Serial.println(t); 191 | Serial.print("Humidity - "); Serial.println(h); 192 | 193 | temperature.updateAndReportParam("Temperature", t); 194 | humidity.updateAndReportParam("Temperature", h); 195 | } 196 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ESP-RainMaker-Demo-Code 2 | 3 | ESP RainMaker is the cloud platform developed by ESPRESSIF Systems for ESPRESSIF Chipset only. It has a lot of features as compared to commonly used IOT Platform for ABSOLUTELY FREE 4 | 5 | Important Link 6 | 7 | You need to add ESP32 board Packages version 2.0 to add support for ESP RainMaker for your ESP32 board. Here is the link that you need to paste inside your Arduino Preferences 8 | 9 | Link - https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json 10 | 11 | For this project, I have used DHT11 sensor and Relay module with ESP32 board and made a demo code out of it. Here are the connections of all the components 12 | 13 | 14 | ![connections](https://user-images.githubusercontent.com/10785352/178114367-d192bc00-fd16-42c2-add0-8bbff2ed70ca.jpg) 15 | 16 | 17 | You can watch out it's detailed explanatory video on our YouTube channel to understand about ESP RainMaker in simpler way. 18 | https://youtu.be/651EoGQHWck 19 | --------------------------------------------------------------------------------