Seth Sawant Iteration Assignment

IMG_20191202_120145-e1575310993915-1024x589

This week’s assignment was to to either build upon a previous week’s project or to start it over from scratch. Because we’re soon starting the final project, I decided this would be the perfect opportunity to combine two projects into one to create a larger more and ambitious design. I also wanted to make something that was functional and something that I think that I think would hard to find commercially, so those things with in mind I brainstormed various ideas. The final project I settled on was creating a temperature display for my apartment, as one of the first pieces of information that I need in the morning is the outside temperature so I can dress accordingly. At the same time, I wanted this to be something I would want to display in my apartment, so the means of displaying the temperature will be a mechanical display similar to that of an odometer or a mechanical counter. For the iteration project portion of this, I tackled just the electronics aspect of the piece, namely the method of acquiring the temperature information and driving a motor based on that data, leaving the mechanical (and more complex) part for my final project.

The DHT22 temperature and humidity sensor I used.

For the backbone of this project, I decided to use Adafruit’s Feather HUZZAH micro controller, which is an Internet of Things focused device with built-in support for low energy Bluetooth, WiFi, and useful power-saving features built in. The chip can be programmed numerous ways; I started by trying to use MicroPython, but got frustrated by the unreliable programming method which involved connecting to the HUZZAH over WiFi and using a web-based terminal to update the code. Instead, I used the Arduino IDE which was less effort but required a little setup by installing some special board packages.

For acquiring the temperature data, I used two methods: one was to use a temperature sensor to get the ambient temperature of the room, and the second was to pull the weather data from the internet to get the outside temperature. The thinking behind this is that for my final design, I want the display to periodically switch between the inside and outside temperature to highlight  the ccool transition between digits that mechanical displays have. For the ambient temperature I used another piece of Adafruit kit, a DHT22 temperature and humidity sensor, which comes with an Arduino library which makes getting readings from the device easy. Although I didn’t use the humidity readings in this project, I’m thinking of adding the ability in the final project to alternate between showing the temperature and the humidity. To get the outside temperature from the internet, I used a weather API called OpenWeather which gives basic weather information for free, and used the Arduino HTTP library to send the API requests. Finally, to display the temperature data to the user I made a simple dial out of cardboard and a servo to act as a stand-in for the final, more complex display. Although the servo worked in this case, I realized that because servos can’t rotate more than one full rotation it probably won’t work for my final design; I’m considering other options like using a stepper motor or a regular DC motor, which can rotate indefinitely in one direction.

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <ArduinoJson.h>
#include <Servo.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>

#define WIFI_TIMEOUT 5

const char* ssid = "NETWORK_NAME";
const char* password = "NETWORK_PASSWORD";

HTTPClient http; //Declare an object of class HTTPClient

double outside_temp_k = 273.15;
double outside_temp_c = 0;
double outside_temp_f = 32;

double inside_temp_c = 0;

Servo dial;
DHT_Unified dht(14, DHT22);
sensor_t sensor;

void setup () {
dial.attach(2); //attach servo object to pin 2 of the Feather
dial.write(0);
dht.begin();
Serial.begin(115200);
WiFi.begin(ssid, password);

int timeout = 0;
Serial.print("Connecting");
while (WiFi.status() != WL_CONNECTED && timeout < WIFI_TIMEOUT) {
delay(1000);
Serial.print(".");
timeout++;
}
}

void loop() {
dht.temperature().getSensor(&sensor);
if (WiFi.status() == WL_CONNECTED) { //Check WiFi connection status
http.begin("http://api.openweathermap.org/data/2.5/weather?id=4887158&appid=API_KEY"); //Specify request destination
int httpCode = http.GET();
if (httpCode > 0) { //Check the returning code
String payload = http.getString(); //Get the request response payload
DynamicJsonDocument doc(1024);
DeserializationError error = deserializeJson(doc, payload);
if (!error) {
outside_temp_k = doc["main"]["temp"]; // gets the temperature in kelvin
outside_temp_c = outside_temp_k - 273.15; // convert to celcius
outside_temp_f = (outside_temp_c*1.8) + 32; // convert to fahrenheit
}
} else {Serial.println("Error contacting OpenWeather API!");}
http.end(); //Close connection
delay(2500);
} else {
Serial.println("No network connection!");
}

dial.write((int)outside_temp_f); //-20 to 120 to 0 to 180 range.

sensors_event_t event;
dht.temperature().getEvent(&event);
if (isnan(event.temperature)) {
Serial.println(F("Error reading temperature!"));
}
else {
if ((int)event.temperature != (int)inside_temp_c) {
inside_temp_c = event.temperature;
dial.write((int)inside_temp_c);
}
}

// ESP.deepSleep(2000000); connect GPIO pin 16 to RST for this to work
delay(500);

}

Share on facebook
Facebook
Share on twitter
Twitter
Share on linkedin
LinkedIn
Share on pinterest
Pinterest