Variables losing value
Posted: Thu Aug 22, 2019 8:46 pm
Hey Guys,
This is my first post so apologies if I don't explain well.
Long story short....I am doing an AWS Iot project, I generate the openssl certificates in my nodejs backend and then I use the http client on the esp32 to send a request to my backend to give the esp32 the certificates. Certificates arrive fine, I parse it with cJSON and I can print it out. I then store the certificate in a global variable and then pass it to a function in the xTaskCreate.
The problem is that variable I pass doesn't keep the certificate value, when I print it out after passing it, I see some corrupted gibberish. The variables are devName, devCert and devKey!
Here is my entire code
Please help!
This is my first post so apologies if I don't explain well.
Long story short....I am doing an AWS Iot project, I generate the openssl certificates in my nodejs backend and then I use the http client on the esp32 to send a request to my backend to give the esp32 the certificates. Certificates arrive fine, I parse it with cJSON and I can print it out. I then store the certificate in a global variable and then pass it to a function in the xTaskCreate.
The problem is that variable I pass doesn't keep the certificate value, when I print it out after passing it, I see some corrupted gibberish. The variables are devName, devCert and devKey!
Here is my entire code
- /* ethernet Example
- This example code is in the Public Domain (or CC0 licensed, at your option.)
- Unless required by applicable law or agreed to in writing, this
- software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
- CONDITIONS OF ANY KIND, either express or implied.
- */
- #include <stdio.h>
- #include <string.h>
- #include "freertos/FreeRTOS.h"
- #include "freertos/task.h"
- #include "freertos/event_groups.h"
- #include "esp_system.h"
- #include "esp_event_loop.h"
- #include "esp_event.h"
- #include "esp_log.h"
- #include "esp_eth.h"
- #include "rom/gpio.h"
- #include "tcpip_adapter.h"
- #include "driver/gpio.h"
- #include "driver/periph_ctrl.h"
- #include "eth_phy/phy_lan8720.h"
- #include "esp_http_client.h"
- #include "cJSON.h"
- #include <sys/unistd.h>
- #include <sys/stat.h>
- #include "esp_err.h"
- #include "esp_spiffs.h"
- const int CONNECTED_BIT = BIT0;
- char *devKey = NULL;
- char *devCert = NULL;
- char *devName = NULL;
- int val = 0;
- static EventGroupHandle_t ethernet_event_group;
- #define DEFAULT_ETHERNET_PHY_CONFIG phy_lan8720_default_ethernet_config
- static const char *TAG = "Spacr";
- #define PIN_PHY_POWER 12
- #define PIN_SMI_MDC 23
- #define PIN_SMI_MDIO 18
- static void phy_device_power_enable_via_gpio(bool enable)
- {
- assert(DEFAULT_ETHERNET_PHY_CONFIG.phy_power_enable);
- if (!enable) {
- DEFAULT_ETHERNET_PHY_CONFIG.phy_power_enable(false);
- }
- gpio_pad_select_gpio(PIN_PHY_POWER);
- gpio_set_direction(PIN_PHY_POWER, GPIO_MODE_OUTPUT);
- if (enable == true) {
- gpio_set_level(PIN_PHY_POWER, 1);
- ESP_LOGI(TAG, "Power On Ethernet PHY");
- } else {
- gpio_set_level(PIN_PHY_POWER, 0);
- ESP_LOGI(TAG, "Power Off Ethernet PHY");
- }
- vTaskDelay(1);
- if (enable) {
- DEFAULT_ETHERNET_PHY_CONFIG.phy_power_enable(true);
- }
- }
- //Setting up ethernet RMII mode
- static void eth_gpio_config_rmii(void)
- {
- phy_rmii_configure_data_interface_pins();
- phy_rmii_smi_configure_pins(PIN_SMI_MDC, PIN_SMI_MDIO);
- }
- //This is the function callback for ethernet events e.g. Ethernet Down, up etc.
- static esp_err_t eth_event_handler(void *ctx, system_event_t *event)
- {
- tcpip_adapter_ip_info_t ip;
- switch (event->event_id) {
- case SYSTEM_EVENT_ETH_CONNECTED:
- ESP_LOGI(TAG, "Ethernet Link Up");
- break;
- case SYSTEM_EVENT_ETH_DISCONNECTED:
- ESP_LOGI(TAG, "Ethernet Link Down");
- break;
- case SYSTEM_EVENT_ETH_START:
- ESP_LOGI(TAG, "Ethernet Started");
- break;
- case SYSTEM_EVENT_ETH_GOT_IP:
- memset(&ip, 0, sizeof(tcpip_adapter_ip_info_t));
- ESP_ERROR_CHECK(tcpip_adapter_get_ip_info(ESP_IF_ETH, &ip));
- ESP_LOGI(TAG, "Ethernet Got IP Addr");
- ESP_LOGI(TAG, "~~~~~~~~~~~");
- ESP_LOGI(TAG, "ETHIP:" IPSTR, IP2STR(&ip.ip));
- ESP_LOGI(TAG, "ETHMASK:" IPSTR, IP2STR(&ip.netmask));
- ESP_LOGI(TAG, "ETHGW:" IPSTR, IP2STR(&ip.gw));
- ESP_LOGI(TAG, "~~~~~~~~~~~");
- xEventGroupSetBits(ethernet_event_group, CONNECTED_BIT);
- break;
- case SYSTEM_EVENT_ETH_STOP:
- ESP_LOGI(TAG, "Ethernet Stopped");
- xEventGroupClearBits(ethernet_event_group, CONNECTED_BIT);
- break;
- default:
- break;
- }
- return ESP_OK;
- }
- void spiffInit(){
- esp_vfs_spiffs_conf_t spifConf = {
- .base_path = "/spiffs",
- .partition_label = NULL,
- .max_files = 1,
- .format_if_mount_failed = true
- };
- esp_err_t ret = esp_vfs_spiffs_register(&spifConf);
- if (ret != ESP_OK) {
- if (ret == ESP_FAIL) {
- ESP_LOGE(TAG, "Failed to mount or format filesystem");
- } else if (ret == ESP_ERR_NOT_FOUND) {
- ESP_LOGE(TAG, "Failed to find SPIFFS partition");
- } else {
- ESP_LOGE(TAG, "Failed to initialize SPIFFS (%s)", esp_err_to_name(ret));
- }
- }
- size_t total = 0, used = 0;
- ret = esp_spiffs_info(NULL, &total, &used);
- if (ret != ESP_OK) {
- ESP_LOGE(TAG, "Failed to get SPIFFS partition information (%s)", esp_err_to_name(ret));
- } else {
- ESP_LOGI(TAG, "Partition size: total: %d, used: %d", total, used);
- }
- }
- void parseDeviceCerts(char *data){
- cJSON *dataJson = cJSON_Parse(data);
- if (dataJson == NULL)
- {
- const char *error_ptr = cJSON_GetErrorPtr();
- if (error_ptr != NULL)
- {
- fprintf(stderr, "Error before: %s\n", error_ptr);
- esp_restart();
- }
- }else{
- spiffInit();
- struct stat st;
- if (stat("/spiffs/deviceInfo.txt", &st) == 0 ) {
- if (val == 1){
- devKey = cJSON_GetObjectItem(dataJson, "key")->valuestring;
- xEventGroupSetBits(ethernet_event_group, CONNECTED_BIT);
- }else{
- devCert = cJSON_GetObjectItem(dataJson, "cert")->valuestring;
- }
- esp_vfs_spiffs_unregister(NULL);
- }else{
- devName = cJSON_GetObjectItem(dataJson, "name")->valuestring;
- FILE* f = fopen("/spiffs/deviceInfo.txt", "w");
- if (f == NULL) {
- ESP_LOGE(TAG, "Failed to open file for writing");
- abort();
- }
- fprintf(f, devName);
- fclose(f);
- esp_vfs_spiffs_unregister(NULL);
- esp_restart();
- }
- cJSON_Delete(dataJson);
- }
- }
- esp_err_t _http_event_handler(esp_http_client_event_t *evt)
- {
- switch(evt->event_id) {
- case HTTP_EVENT_ERROR:
- ESP_LOGD(TAG, "HTTP_EVENT_ERROR");
- break;
- case HTTP_EVENT_ON_CONNECTED:
- ESP_LOGD(TAG, "HTTP_EVENT_ON_CONNECTED");
- break;
- case HTTP_EVENT_HEADER_SENT:
- ESP_LOGD(TAG, "HTTP_EVENT_HEADER_SENT");
- break;
- case HTTP_EVENT_ON_HEADER:
- ESP_LOGD(TAG, "HTTP_EVENT_ON_HEADER, key=%s, value=%s", evt->header_key, evt->header_value);
- break;
- case HTTP_EVENT_ON_DATA:
- ESP_LOGD(TAG, "HTTP_EVENT_ON_DATA, len=%d", evt->data_len);
- if (!esp_http_client_is_chunked_response(evt->client)) {
- ESP_LOGI(TAG, "Data Recieved");
- parseDeviceCerts((char*)evt->data);
- }
- break;
- case HTTP_EVENT_ON_FINISH:
- ESP_LOGD(TAG, "HTTP_EVENT_ON_FINISH");
- break;
- case HTTP_EVENT_DISCONNECTED:
- ESP_LOGD(TAG, "HTTP_EVENT_DISCONNECTED");
- break;
- }
- return ESP_OK;
- }
- extern void aws_task_run();
- void aws_task(void *pvParameter)
- {
- xEventGroupWaitBits(ethernet_event_group, CONNECTED_BIT, false, true, portMAX_DELAY);
- aws_task_run(devName, devCert, devKey);
- }
- void sendRequest(char url[], char pData[]){
- esp_http_client_config_t configg = {
- .url = url,
- .event_handler = _http_event_handler,
- .buffer_size = 6000,
- };
- esp_http_client_handle_t client = esp_http_client_init(&configg);
- const char *post_data = pData;
- esp_http_client_set_method(client, HTTP_METHOD_POST);
- esp_http_client_set_header(client, "Content-Type", "application/json");
- 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, "Request Finished");
- }
- esp_http_client_cleanup(client);
- }
- void app_main()
- {
- tcpip_adapter_init();
- //spiffInit();
- //unlink("/spiffs/deviceInfo.txt");
- ethernet_event_group = xEventGroupCreate();
- ESP_ERROR_CHECK(esp_event_loop_init(eth_event_handler, NULL));
- eth_config_t config = DEFAULT_ETHERNET_PHY_CONFIG;
- config.phy_addr = 0;
- config.gpio_config = eth_gpio_config_rmii;
- config.tcpip_input = tcpip_adapter_eth_input;
- config.clock_mode = 3;
- config.phy_power_enable = phy_device_power_enable_via_gpio;
- ESP_ERROR_CHECK(esp_eth_init(&config));
- ESP_ERROR_CHECK(esp_eth_enable());
- xEventGroupWaitBits(ethernet_event_group, CONNECTED_BIT,
- false, true, portMAX_DELAY);
- ESP_LOGI(TAG, "Connected to Internet success!");
- xEventGroupClearBits(ethernet_event_group, CONNECTED_BIT);
- spiffInit();
- struct stat st;
- if (stat("/spiffs/deviceInfo.txt", &st) == 0 ) {
- ESP_LOGI(TAG, "File exists");
- FILE* deviceInfo = fopen("/spiffs/deviceInfo.txt", "r");
- if (deviceInfo == NULL) {
- ESP_LOGI(TAG, "Failed to open file for reading");
- abort();
- }
- char line[8];
- fgets(line, sizeof(line), deviceInfo);
- fclose(deviceInfo);
- devName = line;
- cJSON *root = cJSON_CreateObject();
- cJSON_AddItemToObject(root, "name", cJSON_CreateString(line));
- esp_vfs_spiffs_unregister(NULL);
- sendRequest("http://172.16.1.128:3000/getDeviceCertificate", cJSON_PrintUnformatted(root));
- val = 1;
- sendRequest("http://172.16.1.128:3000/getDeviceKey", cJSON_PrintUnformatted(root));
- cJSON_Delete(root);
- }else{
- ESP_LOGI(TAG, "First time boot");
- esp_vfs_spiffs_unregister(NULL);
- sendRequest("http://172.16.1.128:3000/createDeviceCertificate","{\"cACert\":\"myCAcertificate\"}");
- }
- xTaskCreate(aws_task, "aws_task", 10000, NULL, 5, NULL);
- }