Created
April 12, 2017 15:03
-
-
Save SaheblalBagwan/b39e80d2b0de87731fce7a7a5f37bea4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <AWS_IOT.h> | |
#include <WiFi.h> | |
#include "DHT.h" | |
#define DHTPIN 4 // what digital pin we're connected to | |
// Uncomment whatever type you're using! | |
#define DHTTYPE DHT11 // DHT 11 | |
//#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321 | |
//#define DHTTYPE DHT21 // DHT 21 (AM2301) | |
DHT dht(DHTPIN, DHTTYPE); | |
AWS_IOT hornbill; // AWS_IOT instance | |
char WIFI_SSID[]="your Wifi SSID"; | |
char WIFI_PASSWORD[]="Wifi Password"; | |
char HOST_ADDRESS[]="AWS host address"; | |
char CLIENT_ID[]= "client id"; | |
char TOPIC_NAME[]= "your thing/topic name"; | |
int status = WL_IDLE_STATUS; | |
int tick=0,msgCount=0,msgReceived = 0; | |
char payload[512]; | |
char rcvdPayload[512]; | |
void setup() { | |
Serial.begin(115200); | |
delay(2000); | |
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(hornbill.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); | |
dht.begin(); //Initialize the DHT11 sensor | |
} | |
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 = dht.readHumidity(); | |
// Read temperature as Celsius (the default) | |
float t = dht.readTemperature(); | |
// Read temperature as Fahrenheit (isFahrenheit = true) | |
float f = dht.readTemperature(true); | |
// 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(hornbill.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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment