element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Azure Sphere Starter Kit
  • Products
  • Dev Tools
  • Avnet Boards Community
  • Azure Sphere Starter Kit
  • More
  • Cancel
Azure Sphere Starter Kit
Blog Smart Temperature and Motion Tracking
  • Blog
  • Forum
  • Documents
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Azure Sphere Starter Kit to participate - click to join for free!
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: timseitz
  • Date Created: 10 Jun 2020 12:20 PM Date Created
  • Views 887 views
  • Likes 1 like
  • Comments 0 comments
  • azurespherehomehacks
  • homewhackdch
Related
Recommended

Smart Temperature and Motion Tracking

timseitz
timseitz
10 Jun 2020

  • Overview
  • Bill of Materials
  • Code (Starting)

 

Overview

The goal was to be able to control the window fan to turn on when the temperature outside reached higher than the inside temp (for the summer) and turn on when it was cooler outside. Adding a PIR also would enable turning on lights or alerts when it detected motion. All of this would use io.adafruit.com and IFTTT.

Bill of Materials

 

DescriptionPriceLink
IFTTT complient smart plugs$6/eahttps://www.amazon.com/gp/product/B07FVST9YN/ref=ppx_yo_dt_b_search_asin_title?ie=UTF8&psc=1
Azure Sphere MT Starter Kit$83https://www.newark.com/avnet/aes-ms-mt3620-sk-g/starter-kit-arm-cortex-a7-cortex/dp/02AH9206?gclid=Cj0KCQjwiYL3BRDVARIsA…
Mikroe-2979 Thermo 7$16https://www.newark.com/mikroelektronika/mikroe-2979/thermo-7-click-board/dp/77AC3696?COM=eml-AzureSphere-102_94716-MIKRO…
PIR$2/eahttps://www.amazon.com/HC-SR501-Pyroelectric-Infrared-Detector-Modules/dp/B07R5Q2QKN/ref=sr_1_7?dchild=1&keywords=pir+se…
ESP8266$5/eahttps://www.amazon.com/gp/product/B010N1SPRK/ref=ppx_yo_dt_b_search_asin_title?ie=UTF8&psc=1

 

Code (Starting)

I started the coding with arduino and was able to log temperatures and trigger events through IFTTT. This is as far as I have gotten for now. Search --- for fields that need to be replaced.

You can subscribe to accuweather to get outdoor temperatures using their api. I also tried to use open weather maps' api, but it was hanging up my code. Hence why it is commented out. This code can be incorporated into the Azure sphere using this helpful tutorials:

  1. [PROJECT] Part 1... Getting Started With Microsoft Azure Guide, and a Simple Azure Sphere Sensor Project

     One comment I will make on the Azure that took me several days to debug is that to log in and claim the Azure Sphere you must create an account as in the above link, the important part is that it      creates a really long username with "onmicrosoft" in it. This is the username you need to log into visual studio and the Azure Sphere.

Adafruit has many helpful tutorials for using their service https://learn.adafruit.com/welcome-to-adafruit-io

#include <ESP8266WiFi.h>
#include "Adafruit_Si7021.h"
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
#include <TridentTD_OpenWeather.h>
#include <JsonListener.h>
#include "AccuWeatherLibrary.h"


/************************* WiFi Access Point *********************************/


#define WLAN_SSID       "---"
#define WLAN_PASS       "---"


/************************* Adafruit.io Setup *********************************/


#define AIO_SERVER      "io.adafruit.com"
#define AIO_SERVERPORT  1883                   // use 8883 for SSL
#define AIO_USERNAME    "---"
#define AIO_KEY         "---"


/************************* OpenWeather Setup *********************************/
#define OpenWeather_APIKEY    "---"
#define lat        ---
#define lon       ---


#define timezone  ----
TridentTD_OpenWeather myPlace(OpenWeather_APIKEY);
/************************* AccuWeather Setup *********************************/
AccuweatherCurrentData dataC;


Accuweather aw("3fDpgMJiWzq6YPYKZuP0TCrdWsCMjh5X", ---, "en-us", false);
/************ Global State (you don't need to change this!) ******************/


// Create an ESP8266 WiFiClient class to connect to the MQTT server.
WiFiClient client;
// or... use WiFiFlientSecure for SSL
//WiFiClientSecure client;


// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);


/****************************** Feeds ***************************************/

// Setup a feed called 'CTem' for publishing.
// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>
Adafruit_MQTT_Publish CTem = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/CTemp");
Adafruit_MQTT_Publish OTem = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/OTemp");
Adafruit_MQTT_Publish CHu = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/CHum");

/*************************** Sketch Code ************************************/


Adafruit_Si7021 sensor = Adafruit_Si7021();

// Bug workaround for Arduino 1.6.6, it seems to need a function declaration
// for some reason (only affects ESP8266, likely an arduino-builder bug).
void MQTT_connect();

void setup() {
  Serial.begin(115200);
  delay(10);


  Serial.println(F("Adafruit MQTT demo"));


  // Connect to WiFi access point.
  Serial.println(); Serial.println();
  Serial.print("Connecting to ");
  Serial.println(WLAN_SSID);


  WiFi.begin(WLAN_SSID, WLAN_PASS);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println();

  Serial.println("WiFi connected");
  Serial.println("IP address: "); Serial.println(WiFi.localIP());
  sensor.begin();
  myPlace.setLocation(lat, lon );
  myPlace.setUnit("imperial");
}
int lastT = 0;
uint8_t lastH = 0;
uint8_t i = 0;
int OutTemp = 255;
void loop() {
 if(i>=3){i=1;}
 else{i++;}


//  myPlace.weatherNow();
  Serial.println("myPlace");
  MQTT_connect();
  uint8_t Hum = sensor.readHumidity();
  float Tem = sensor.readTemperature() * 9 / 5 + 32;
  if (i > 1) {
    
//    OutTemp = myPlace.readTemperature();
//    Serial.println("MyTemp");
  }
  else {
    int ret = aw.getCurrent(&dataC);
    if (ret != 0){
      Serial.println("ERROR");
      Serial.println(ret);
      return;
    }
    while (aw.continueDownload() > 0) {
    }
    OutTemp = dataC.Temperature;
    Serial.println("AccuTemp");
  }


  // this is our 'wait for incoming subscription packets' busy subloop
  // try to spend your time here


  Serial.print((int) Tem);
  Serial.println(Hum);
  // Now we can publish stuff!
  if (lastT != (int) (Tem * 5)) { //make it round to a certain value
    Serial.print(F("\nSending CTem val "));
    Serial.print(Tem);
    Serial.print("...");
    if (! CTem.publish(Tem)) {
      Serial.println(F("Temp Failed "));
    } else {
      Serial.println(F("Temp OK! "));
    }
  }
  if (! OTem.publish(OutTemp)) {
    Serial.println(F("Outdoor Temp Failed "));
  } else {
    Serial.println(F("Outdoor Temp OK! "));
  }
  if (Hum != lastH) {
    if (! CHu.publish(Hum)) {
      Serial.println(F("Hum Failed"));
    } else {
      Serial.println(F("Hum OK!"));
    }
  }
  // ping the server to keep the mqtt connection alive
  // NOT required if you are publishing once every KEEPALIVE seconds
  /*
    if(! mqtt.ping()) {
    mqtt.disconnect();
    }
  */
  lastT = (int) (Tem * 5);
  lastH = Hum;
  delay(600000);//make it go to sleep for 10 mins, no need to use up space for free accounts on io.adafruit.com
}


// Function to connect and reconnect as necessary to the MQTT server.
// Should be called in the loop function and it will take care if connecting.
void MQTT_connect() {
  int8_t ret;


  // Stop if already connected.
  if (mqtt.connected()) {
    return;
  }


  Serial.print("Connecting to MQTT... ");


  uint8_t retries = 3;
  while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
    Serial.println(mqtt.connectErrorString(ret));
    Serial.println("Retrying MQTT connection in 5 seconds...");
    mqtt.disconnect();
    delay(5000);  // wait 5 seconds
    retries--;
    if (retries == 0) {
      // basically die and wait for WDT to reset me
      while (1);
    }
  }
  Serial.println("MQTT Connected!");
}

  • Sign in to reply
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube