Skip to content

Instantly share code, notes, and snippets.

@nicfv
Created June 5, 2018 22:05
Show Gist options
  • Save nicfv/1ab585ad7b4cdf63c423739c5f30a627 to your computer and use it in GitHub Desktop.
Save nicfv/1ab585ad7b4cdf63c423739c5f30a627 to your computer and use it in GitHub Desktop.
Final Project for Maker Revolution FRS 002 UC Davis
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <ArduinoJson.h>
// pin identification numbers
const int datapin1 = 4;
const int loadpin1 = 5;
const int clockpin1 = 14;
const int datapin2 = 12;
const int loadpin2 = 13;
const int clockpin2 = 15;
// wifi credentials
const char ssid[] = "SSID";
const char pw[] = "WIFI_PASSWORD";
// http request
#define refresh (1000*60*1)
#define key "YOUR_API_KEY_HERE"
#define url "http://api.openweathermap.org"
#define zip "95616"
const char req[] = url "/data/2.5/weather?zip=" zip "&APPID=" key;
// data for LEDs
int data1 = 0;
int data2 = 0;
float temp = 0;
float tempmin = 0;
float tempmax = 0;
// weather conditions
typedef enum {
none = 0,
sun = 1,
rain = 2,
clouds = 3
}conditions;
// current display
typedef enum {
none2 = 0,
minimum = 1,
maximum = 2,
current = 3
}cdisp;
conditions curr = none;
// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pins as outputs.
pinMode(datapin1, OUTPUT);
pinMode(loadpin1, OUTPUT);
pinMode(clockpin1, OUTPUT);
pinMode(datapin2, OUTPUT);
pinMode(loadpin2, OUTPUT);
pinMode(clockpin2, OUTPUT);
wificonnect();
}
void wificonnect() {
Serial.begin(115200);
Serial.println("Starting...");
WiFi.begin(ssid, pw);
while(WiFi.status()!=WL_CONNECTED) {
delay(1000);
Serial.println("Connecting...");
}
Serial.println("Connected!");
}
void getrequest() {
if(WiFi.status()==WL_CONNECTED) {
HTTPClient http;
Serial.println(req);
http.begin(req);
int code = http.GET();
Serial.println("Status Code: " + (String)code);
if(code>0) {
String response = http.getString();
Serial.println(response);
parsejson(response);
}
http.end();
}
}
void parsejson(String j) {
StaticJsonBuffer<3*1024> jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(j);
temp = root["main"]["temp"];
tempmin = root["main"]["temp_min"];
tempmax = root["main"]["temp_max"];
const String cond = root["weather"][0]["main"];
Serial.println("Temperature: " + (String)temp + " K, (" + (String)tempmin + ", " + (String)tempmax + ")");
Serial.println("Condition: " + cond);
if(cond=="Clear") {
curr = sun;
} else if(cond=="Haze"||cond=="Clouds"||cond=="Snow") {
curr = clouds;
} else if(cond=="Drizzle"||cond=="Rain"||cond=="Extreme") {
curr = rain;
} else {
curr = none;
}
}
// the loop function runs over and over again forever
void loop() {
getrequest();
for(int i=0; i<3; i++) {
data1 = 0;
data2 = 0;
switch(i) {
case 0: // min temp
show(minimum);
temperature(tempmin);
weather(curr);
lightpin(1, (char)data1);
lightpin(2, (char)data2);
delay(refresh / 3);
break;
case 1: // max temp
show(maximum);
temperature(tempmax);
weather(curr);
lightpin(1, (char)data1);
lightpin(2, (char)data2);
delay(refresh / 3);
break;
case 2: // current temp
show(current);
temperature(temp);
weather(curr);
lightpin(1, (char)data1);
lightpin(2, (char)data2);
delay(refresh / 3);
break;
default: // error
return;
}
}
}
// lights specific LEDs based on the temperature (Kelvin)
void temperature(float K) {
const int F = ((K - 273) * 9) / 5 + 32;
if(F<45) { // 45 F (or lower)
// 00100000
data1 += 32;
} else if(F<60) { // 45-60 F
// 00110000
data1 += 48;
} else if(F<75) { // 60-75 F
// 01110000
data1 += 112;
} else if(F<90) { // 75-90 F
// 11110000
data1 += 240;
} else { // 90s F (and higher)
// 11111000
data1 += 248;
}
}
// lights specific LEDs based on the weather conditions
void weather(conditions c) {
switch(c) {
case (sun):
// 00000100
data1 += 4; return;
case (rain):
// 00000010
data1 += 2; return;
case (clouds):
// 00000001
data1 += 1; return;
default:
return;
}
}
// lights specific LEDs based on the current display
void show(cdisp d) {
switch(d) {
case (minimum):
// 01000100
data2 += 64; return;
case (maximum):
// 00100001
data2 += 33; return;
case (current):
// 10000010
data2 += 130; return;
default:
return;
}
}
// lights specific LEDs
void lightpin(int shiftout, byte data) {
if(shiftout == 1) { // use shift register 1
digitalWrite(loadpin1, LOW);
shiftOut(datapin1, clockpin1, MSBFIRST, data);
digitalWrite(loadpin1, HIGH);
} else { // use shift register 2
digitalWrite(loadpin2, LOW);
shiftOut(datapin2, clockpin2, MSBFIRST, data);
digitalWrite(loadpin2, HIGH);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment