#include #include #include "esp_wifi.h" #include "esp_event.h" #include "esp_log.h" #include "nvs_flash.h" #include "esp_http_client.h" #include "freertos/FreeRTOS.h" #include "freertos/task.h" #define WIFI_SSID "UCLA_WEB" // Your Wi-Fi network #define WIFI_PASSWORD "" // No password for UCLA_WEB (if required, add it here) #define DEVICE_CONNECTION_STRING "HostName=ESP32-IoTHub.azure-devices.net;DeviceId=Simulated-ESP32;SharedAccessKey=hGbt7EnzeJDtfCirbRuNVLr/6AMSXyLgVqTo4DNlTuI=" #define UPDATE_INTERVAL 60000 // Update every 60 seconds static const char *TAG = "ESP32_IoT_Hub"; void send_data_to_azure(void *pvParameter) { char post_data[256]; char url[] = "https://ESP32-IoTHub.azure-devices.net/devices/Simulated-ESP32/messages/events?api-version=2020-09-30"; esp_http_client_config_t config = { .url = url, .method = HTTP_METHOD_POST, .transport_type = HTTP_TRANSPORT_OVER_SSL, // Ensures the data is sent securely over HTTPS }; while (1) { // Random simulated data float temperature = 25.0 + (rand() % 10); // Simulated temperature float humidity = 50.0 + (rand() % 20); // Simulated humidity // Format the data as JSON string snprintf(post_data, sizeof(post_data), "{\"temperature\": %.2f, \"humidity\": %.2f}", temperature, humidity); esp_http_client_handle_t client = esp_http_client_init(&config); esp_http_client_set_header(client, "Content-Type", "application/json"); esp_http_client_set_header(client, "Authorization", "SharedAccessSignature sr=ESP32-IoTHub.azure-devices.net%2Fdevices%2FSimulated-ESP32&sig=DoH%2FR8ZjZeRnZF68b91IYP6xXTX9u89C9u1r8GA0keY%3D&se=1742276297"); // Send the POST request with the data to IoT Hub esp_http_client_set_post_field(client, post_data, strlen(post_data)); esp_err_t err = esp_http_client_perform(client); if (err == ESP_OK) { ESP_LOGI(TAG, "Message sent successfully: %s", post_data); } else { ESP_LOGE(TAG, "HTTP request failed: %s", esp_err_to_name(err)); } esp_http_client_cleanup(client); // Wait for the specified interval before sending next data vTaskDelay(UPDATE_INTERVAL / portTICK_PERIOD_MS); } } void wifi_event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) { if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) { esp_wifi_connect(); } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) { ESP_LOGI(TAG, "Connected to Wi-Fi!"); xTaskCreate(&send_data_to_azure, "send_data_to_azure", 4096, NULL, 5, NULL); } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) { ESP_LOGI(TAG, "Disconnected! Reconnecting..."); esp_wifi_connect(); } } void wifi_init() { esp_netif_init(); esp_event_loop_create_default(); esp_netif_create_default_wifi_sta(); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); esp_wifi_init(&cfg); esp_event_handler_instance_t instance_any_id; esp_event_handler_instance_t instance_got_ip; esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL, &instance_any_id); esp_event_handler_instance_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &wifi_event_handler, NULL, &instance_got_ip); wifi_config_t wifi_config = { .sta = { .ssid = WIFI_SSID, .password = WIFI_PASSWORD, // Use your Wi-Fi password if required }, }; esp_wifi_set_mode(WIFI_MODE_STA); esp_wifi_set_config(WIFI_IF_STA, &wifi_config); esp_wifi_start(); } void app_main() { ESP_ERROR_CHECK(nvs_flash_init()); wifi_init(); }