while (status != WL_CONNECTED) { Serial.print("Attempting to connect to SSID: "); Serial.println(WIFI_SSID); // Connect to WPA/WPA2 network. Change this line if using open or WEP network: status = WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
// wait 5 seconds for connection: delay(5000); }
Serial.println("Connected to wifi");
if(aws.connect(HOST_ADDRESS,CLIENT_ID)== 0) // Connect to AWS using Host Address and Cliend ID { Serial.println("Connected to AWS"); delay(1000); } else { Serial.println("AWS connection failed, Check the HOST Address"); while(1); }
delay(2000);
}
void loop() {
// Reading temperature or humidity takes about 250 milliseconds! // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor) float h = 11.11; // Read temperature as Celsius (the default) float t = 22.22; // Read temperature as Fahrenheit (isFahrenheit = true) float f = 33.33;
// Check if any reads failed and exit early (to try again). if (isnan(h) || isnan(t) || isnan(f)) { Serial.println("Failed to read from DHT sensor!"); } else { sprintf(payload,"Humidity:%f Temperature:%f'C",h,t); // Create the payload for publishing
if(aws.publish(TOPIC_NAME,payload) == 0) // Publish the message(Temp and humidity) { Serial.print("Publish Message:"); Serial.println(payload); } else { Serial.println("Publish failed"); } // publish the temp and humidity every 5 seconds. vTaskDelay(5000 / portTICK_RATE_MS);
}
}
AWS-IoT 에 사물관리에 들어가서, 상호작용에 MQTT의 토픽을 받을 수 있는 섀도우 업데이트 코드를 위의 TOPIC_NAME에 넣고, HTTPS의 Rest API 엔드포인트를 HOST_ADDRESS에 넣는다.