/** * @brief MQTT client that publishes humidity data to the connected \p mqtt * server on topic "garden/humidity" in JSON format: * {"Name":"[sensor_name]","value":"[humidity_sensor_data_in_percentage]"} */ static void main_humidity_sensor( struct pico_mqtt* mqtt, char* sensor_name ) { struct pico_mqtt_message* message = NULL; uint8_t humidity = 0; char buffer[128]; // Clear buffer memset(buffer, 0, sizeof(buffer)); // Seed the random generator srand(time(NULL)); running = 1; while(running) { // Send a random number between 0-99 ever 7 seconds humidity = (uint8_t)(rand() % 100); // Build JSON message snprintf(buffer, 128, "{\"Name\":\"%s\",\"Value\":\"%03d\"}\n", sensor_name, humidity); // [MQTT] Create mqtt message message = pico_mqtt_create_message(TOPIC_HUM, buffer, strlen(buffer)); pico_mqtt_message_set_quality_of_service(message, QOS); // [MQTT] PUBLISH MESSAGE on topic pico_mqtt_publish(mqtt, message, 5000); // [MQTT] Clear message + internal buffers pico_mqtt_destroy_message(message); message = NULL; // Print sensor data locally print_humidity(humidity, sensor_name); sleep_override(7); } return; }
int main(void) { CURL *curl_handle; CURLcode res; char *townName = (char *) secure_malloc(1); char readKeyBoard_char=' '; size_t townName_size=1; int choice=TOWN_NAME; char *name_validity_pointer=NULL; int query_strin_Size=strlen(initValue_queryValue)+1; char *query_string=(char *)secure_malloc(query_strin_Size); struct MemoryStruct chunk; secure_strcpy(query_string,initValue_queryValue); ////////////////////////////////begin big while LOOP while(1) { switch(choice) { case PRINT_TEMPERATURE: system("cls"); print_temp(chunk.memory); printf("\nPress anything to go back.."); _getch(); break; case PRINT_HUMIDITY: system("cls"); print_humidity(chunk.memory); printf("\nPress anything to back.."); _getch(); break; case TOWN_NAME: name_validity_pointer= NULL; while(name_validity_pointer==NULL){ chunk.memory =(char*) secure_malloc(1); /* will be grown as needed by the realloc above */ chunk.size = 0; /* no data at this point */ curl_global_init(CURL_GLOBAL_ALL); /* init the curl session */ curl_handle = curl_easy_init(); /* specify URL to get */ printf("\nplease enter townName:"); if(townName) { free(townName); townName=NULL; } townName_size=1; readKeyBoard_char=' '; townName=(char *) secure_malloc(1); while(1) { readKeyBoard_char=_getch(); if((int)readKeyBoard_char == ASCII_Code_ENTER) break; printf("%c",readKeyBoard_char); townName[townName_size-1]=readKeyBoard_char; townName[townName_size]='\0'; townName_size++; townName=(char *) realloc(townName,townName_size+1); } townName[townName_size]='\0'; secure_strcpy(query_string,initValue_queryValue); query_strin_Size = strlen(initValue_queryValue)+townName_size+strlen(lastPart_queryValue)+1; query_string=(char *) realloc(query_string,query_strin_Size); strcat_s(query_string,townName_size,townName); strcat_s(query_string,strlen(lastPart_queryValue),lastPart_queryValue); query_string[query_strin_Size]='\0'; curl_easy_setopt(curl_handle, CURLOPT_URL, query_string); /* send all data to this function */ curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback); /* we pass our 'chunk' struct to the callback function */ curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&chunk); /* some servers don't like requests that are made without a user-agent field, so we provide one */ curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "libcurl-agent/1.0"); /* get it! */ res = curl_easy_perform(curl_handle); /* check for errors */ if(res != CURLE_OK) { fprintf(stderr, "Connection to the server failed: %s\n", curl_easy_strerror(res)); } else { /* * Now, our chunk.memory points to a memory block that is chunk.size * bytes big and contains the remote file.*/ name_validity_pointer=strstr(chunk.memory,"Error: Not found city"); if(name_validity_pointer!=NULL) { printf("please enter a valid town name ,you have entered %s\n",townName); free_dynamicVar(chunk.memory); name_validity_pointer=NULL; } else{ name_validity_pointer = (char *) !NULL; } } } printf("\nThe town name %s is accepted",townName); Sleep(SECOND); break; case ABOUT_APPLICATION: system("cls"); printAbout(); printf("\nPress anything to back.."); _getch(); break; case EXIT: printf("Please enter any key to exit the Weather Application.."); _getch(); break; } if(choice == EXIT) break; system("cls"); printf("Welcome to Embedded Weather Station !\n"); printf("************The Menu ***************\n"); printf("1) Consult the temperature\n"); printf("2) Consult the humidity\n"); printf("3) change the town's name\n"); printf("4) About the application\n"); printf("5) exit the application\n"); printf("\nPlease enter a valid choice: (between 1 and 5):"); choice = DEFAULT; choice = (int)_getch() - ASCII_Code_0; while((choice < PRINT_TEMPERATURE) || (choice > EXIT)) { choice=(int)getchar()-ASCII_Code_0; } } ///////////////////////////////end big while /* cleanup curl stuff */ curl_easy_cleanup(curl_handle); free_dynamicVar(chunk.memory); free_dynamicVar(query_string); /* we're done with libcurl, so clean it up */ curl_global_cleanup(); return 0; }
/** * @brief This humidity sensor is not randomized, * When your sprinkler is turned "OFF", the humidity will gradually decrease over time, * When the sprinkler is turned "ON", the humidity will gradually increase over time */ static void main_better_humidity_sensor( struct pico_mqtt* mqtt, char* sensor_name ) { struct pico_mqtt_message* message = NULL; uint8_t humidity = 50; uint8_t temperature = 20; char buffer[128]; uint32_t sprinkler_state = 0; // Clear buffer memset(buffer, 0, sizeof(buffer)); // [MQTT] SUBSCRIBE to topic TOPIC_SPR if (pico_mqtt_subscribe(mqtt, TOPIC_SPR, QOS, 5000)) { printf("Failed to subscribe on topic %s", TOPIC_HUM); return; } running = 1; while(running) { // Update humidity based on sprinkler state and previous value if (sprinkler_state) { // Sprinkler ON, humidity should be increasing humidity = (humidity + 6 * temperature / 20) % 100; } else { // Sprinkler OFF, humidity should be decreasing humidity = (humidity - 7 * temperature / 20) % 100 ; } // Build JSON message snprintf(buffer, 128, "{\"Name\":\"%s\",\"Value\":\"%03d\"}\n", sensor_name, humidity); // [MQTT] Create mqtt message message = pico_mqtt_create_message(TOPIC_HUM, buffer, strlen(buffer)); pico_mqtt_message_set_quality_of_service(message, QOS); // [MQTT] PUBLISH MESSAGE on topic pico_mqtt_publish(mqtt, message, 5000); // [MQTT] Clear message + internal buffers pico_mqtt_destroy_message(message); message = NULL; // Print sensor data locally print_humidity(humidity, sensor_name); uint32_t seconds_to_wait = 7; /*********************************************************************** * POLL SPRINKLER STATE (via subscribed topic) **********************************************************************/ // Check every half a second if we should still sleep for (seconds_to_wait*=2; running && seconds_to_wait > 0; --seconds_to_wait) { // [MQTT] READ message (if any) pico_mqtt_receive(mqtt, &message, 500); // blocking wait for max 500 ms for a message if(message != NULL) { // Validate the ID (Name) of the message matches our own actuator_name /* TODO IoTFundamentalist * Use the ID of your garden in the topic * This pushes the parsing of the ID to the MQTT internal working * instead of validating it in the user application */ int result = get_value_via_key(message->data->data, "Name", 128, buffer); if (result || strncmp(sensor_name, buffer, 128)) { printf("Ignoring data from: %s\n", buffer); } else { get_value_via_key(message->data->data, "Value", 128, buffer); printf("Received data from your sprinkler >> %s\n", buffer); if (strncmp(buffer, "ON", 128) == 0) { sprinkler_state = 1; } else { sprinkler_state = 0; } } } // [MQTT] Clear the message and internal buffering pico_mqtt_destroy_message(message); message = NULL; } } // [MQTT] UNSUBSCRIBE from topic TOPIC_SPR pico_mqtt_unsubscribe(mqtt, TOPIC_SPR, 2000); return; }