Skip to content

Instantly share code, notes, and snippets.

@snickerjp
Created January 31, 2015 15:37
Show Gist options
  • Save snickerjp/816935b5739ac57c62bd to your computer and use it in GitHub Desktop.
Save snickerjp/816935b5739ac57c62bd to your computer and use it in GitHub Desktop.
WebAPIから天気情報を取得して、LCDに表示する
#include <ArduinoJson.h>
#include <LiquidCrystal.h>
#include <SPI.h>
#include <WiFi.h>
// select the pins used on the LCD panel
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
// define some values used by the panel and buttons
int lcd_key = 0;
int adc_key_in = 0;
#define btnRIGHT 0
#define btnUP 1
#define btnDOWN 2
#define btnLEFT 3
#define btnSELECT 4
#define btnNONE 5
#define COUNTRY 0
#define CITY 1
#define WEATHER 2
int screen_status = 0;
int selected_city = 1;
char ssid[] = "YOUR SSID"; // your network SSID (name)
char pass[] = "YOUR PASSWORD"; // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0; // your network key Index number (needed only for WEP)
int status = WL_IDLE_STATUS;
char server[] = "api.openweathermap.org";
char parameter[] = "/data/2.5/weather?q=Tokyo,jp";
WiFiClient client;
// read the buttons
int read_LCD_buttons()
{
adc_key_in = analogRead(0); // read the value from the sensor
// my buttons when read are centered at these valies: 0, 144, 329, 504, 741
// we add approx 50 to those values and check to see if we are close
if (adc_key_in > 1000) return btnNONE; // We make this the 1st option for speed reasons since it will be the most likely result
// For V1.1 us this threshold
if (adc_key_in < 50) return btnRIGHT;
if (adc_key_in < 250) return btnUP;
if (adc_key_in < 450) return btnDOWN;
if (adc_key_in < 650) return btnLEFT;
if (adc_key_in < 850) return btnSELECT;
return btnNONE; // when all others fail, return this...
}
void clearTop() {
lcd.setCursor(0,0);
lcd.print(" ");
}
void clearBottom() {
lcd.setCursor(0,1);
lcd.print(" ");
}
void writeLcdTop(char* str) {
clearTop();
lcd.setCursor(0,0);
lcd.print(str);
}
void writeLcdTop(const char* str) {
clearTop();
lcd.setCursor(0,0);
lcd.print(str);
}
void writeLcdTop(String str) {
clearTop();
lcd.setCursor(0,0);
lcd.print(str);
}
void writeLcdBtm(char* str) {
clearBottom();
lcd.setCursor(0,1);
lcd.print(str);
}
void writeLcdBtm(const char* str) {
clearBottom();
lcd.setCursor(0,1);
lcd.print(str);
}
void writeLcdBtm(String str) {
clearBottom();
lcd.setCursor(0,1);
lcd.print(str);
}
String getCity() {
switch (selected_city) {
case 0:
{
return "Sapporo,jp";
break;
}
case 1:
{
return "Tokyo,jp";
break;
}
case 2:
{
return "Osaka,jp";
break;
}
case 3:
{
return "Fukuoka,jp";
break;
}
case 4:
{
return "NY,us";
break;
}
case 5:
{
return "Paris,fr";
break;
}
}
}
void getWeather() {
writeLcdTop(getCity());
writeLcdBtm("--Preparing WiFi");
while (status != WL_CONNECTED) {
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass);
delay(300);
}
writeLcdBtm("--Connecting");
client.stop();
if (client.connect(server, 80)) {
client.println("GET /data/2.5/weather?q=" + getCity() + " HTTP/1.1");
client.println("Host: api.openweathermap.org");
client.println("Connection: close");
client.println();
// read weather
char tmp;
int cnt = 0;
int braceCnt = 0;
char json[511];
while (client.available() && cnt < 1000) {
tmp = client.read();
if (tmp == '{') {
braceCnt++;
}
if (braceCnt > 0) {
json[cnt] = tmp;
cnt++;
}
if (tmp == '}') {
braceCnt--;
}
}
json[cnt] = '\0';
Serial.println();
Serial.println("read");
StaticJsonBuffer<1000> jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(json);
if (!root.success())
{
Serial.println("parseObject() failed");
return;
}
// set weather
const char* wmain = root["weather"][0]["main"];
const char* wdesc = root["weather"][0]["description"];
writeLcdTop(wmain);
writeLcdBtm(wdesc);
}
else {
writeLcdTop("Not Connected");
}
}
void setup()
{
Serial.begin(9600);
lcd.begin(16, 2);
screen_status = CITY;
writeLcdTop("Select City");
writeLcdBtm("> " + getCity());
}
void loop()
{
lcd_key = read_LCD_buttons(); // read the buttons
switch (lcd_key) {
case btnRIGHT:
{
screen_status = WEATHER;
getWeather();
break;
}
case btnLEFT:
{
break;
}
case btnUP:
{
if (screen_status == CITY) {
selected_city = (--selected_city + 6) % 6;
Serial.println(selected_city);
writeLcdBtm("> " + getCity());
delay(600);
break;
}
}
case btnDOWN:
{
if (screen_status == CITY) {
selected_city = (++selected_city) % 6;
Serial.println(selected_city);
writeLcdBtm("> " + getCity());
delay(600);
break;
}
}
case btnSELECT:
{
screen_status = CITY;
writeLcdTop("Select City");
writeLcdBtm("> " + getCity());
break;
}
case btnNONE:
{
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment