esp8266 lcd 天气_ESP8266 显示实时天气信息

代码文件

9f5b29e073a086b09201e78430065416.png

getdata.h

#include

#include

#include

#include

#include

#include

#include

#define DEBUG 1

#define MAX_CONTENT_SIZE 2000

const char* ssid = "weather";

const char* password = "mymymymy";

WiFiClient client;

HTTPClient http;

char response[MAX_CONTENT_SIZE];

const char* HOST = "api.seniverse.com";

const char* APIKEY = "**";

const char* CITY = "haikou";

typedef struct UserData{

char city[16];

char weather[32];

char temp[16];

}data;

data d;

bool parseData(data &d);

data getdata();

bool WifiConfig();

void weather_init();

bool sendRequest(const char* host, const char* cityid, const char* apiKey);

void clearResponseBuffer();

void clearResponseBuffer(){

memset(response, 0, MAX_CONTENT_SIZE); //���

memset(d.city,0,sizeof(d.city));

memset(d.weather,0,sizeof(d.weather));

memset(d.temp,0,sizeof(d.temp));

}

data getdata(){

char error[5] = "NULL";

clearResponseBuffer();

if(sendRequest(HOST,CITY,APIKEY)){

Serial.println("Request success!\n\n");

}

if(parseData(d)){

return d;

}else{

strcpy(d.city,error);

strcpy(d.weather,error);

strcpy(d.temp,error);

return d;

}

}

bool WifiConfig(){

WiFi.begin(ssid,password);

while (WiFi.status() != WL_CONNECTED){

delay(500);

Serial.print(".");

}

Serial.println("Connected success!");

Serial.println("");

Serial.println("IP address: ");

Serial.println(WiFi.localIP());

return true;

}

bool parseData(data &d){

DynamicJsonBuffer jsonBuffer;

JsonObject& root = jsonBuffer.parseObject(response);

if(!root.success()){

Serial.println("JSON parse failed!\n");

return false;

}

strcpy(d.city,root["results"][0]["location"]["name"]);

strcpy(d.weather,root["results"][0]["now"]["text"]);

strcpy(d.temp,root["results"][0]["now"]["temperature"]);

return true;

}

bool sendRequest(const char* host, const char* cityid, const char* apiKey) {

//const char *response;

char *Url = (char *)malloc(0x100);

String payload;

sprintf(Url,"%s%s%s%s%s%s%s","http://",host,"/v3/weather/now.json?key=",apiKey,"&location=",cityid,"&language=en");

Serial.printf("GET URL: %s\n",Url);

http.begin(Url);

int httpCode = http.GET();

if(httpCode>0){

Serial.printf("[HTTP] GET... code: %d\n",httpCode);

if(httpCode == 200){

payload = http.getString();

Serial.println(payload);

}else{

delay(5000);

sendRequest(HOST,CITY,APIKEY);

}

}else{

delay(5000);

sendRequest(HOST,CITY,APIKEY);

}

strcpy(response,payload.c_str()); // convert to const char *

free(Url);

return true;

}

void weather_init(){

WifiConfig();

}

配置完 wifi 之后,请求心知天气的 API 接口,得到 JSON 数据之后,解析返回主代码文件。

主代码文件(.ino)

得到返回的数据体解析并显示在屏幕上(drawstring),5s 钟更新一次。

#include // Only needed for Arduino 1.6.5 and earlier

#include "SSD1306Wire.h" // legacy include: `#include "SSD1306.h"`

// or #include "SH1106Wire.h", legacy include: `#include "SH1106.h"`

// For a connection via I2C using brzo_i2c (must be installed) include

// #include // Only needed for Arduino 1.6.5 and earlier

// #include "SSD1306Brzo.h"

// #include "SH1106Brzo.h"

#include // Only needed for Arduino 1.6.5 and earlier

#include "SSD1306Spi.h"

// #include "SH1106SPi.h"

// Initialize the OLED display using SPI

// D5 -> CLK

// D7 -> MOSI (DOUT)

// D0 -> RES

// D2 -> DC

// D8 -> CS

SSD1306Spi display(D0, D2, D8);

// or

// SH1106Spi display(D0, D2);

// Initialize the OLED display using brzo_i2c

// D3 -> SDA

// D5 -> SCL

// SSD1306Brzo display(0x3c, D3, D5);

// or

// SH1106Brzo display(0x3c, D3, D5);

// Initialize the OLED display using Wire library

//SSD1306Wire display(0x3c, D3, D5);

// SH1106 display(0x3c, D3, D5);

#include "getdata.h"

#define DEMO_DURATION 3000

typedef void (*Demo)(void);

int demoMode = 0;

int counter = 1;

void setup() {

Serial.begin(115200);

Serial.println();

Serial.println();

weather_init();

// Initialising the UI will init the display too.

display.init();

display.flipScreenVertically();

display.setFont(ArialMT_Plain_10);

}

void drawFontFaceDemo() {

char city[10];

char weather[20];

char temp[10];

sprintf(city,"%s: %s","City",getdata().city);

sprintf(weather,"%s: %s","Weather",getdata().weather);

sprintf(temp,"%s: %s","temp",getdata().temp);

// create more fonts at http://oleddisplay.squix.ch/

display.setFont(ArialMT_Plain_16);

display.setTextAlignment(TEXT_ALIGN_LEFT);

display.drawString(10, 0, city);

display.drawString(0, 20, weather);

display.drawString(0, 40, temp);

}

void drawTextFlowDemo() {

display.setFont(ArialMT_Plain_10);

display.setTextAlignment(TEXT_ALIGN_LEFT);

display.drawStringMaxWidth(0, 0, 128,

"Lorem ipsum\n dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore." );

}

void drawTextAlignmentDemo() {

// Text alignment demo

display.setFont(ArialMT_Plain_10);

// The coordinates define the left starting point of the text

display.setTextAlignment(TEXT_ALIGN_LEFT);

display.drawString(0, 10, "Left aligned (0,10)");

// The coordinates define the center of the text

display.setTextAlignment(TEXT_ALIGN_CENTER);

display.drawString(64, 22, "Center aligned (64,22)");

// The coordinates define the right end of the text

display.setTextAlignment(TEXT_ALIGN_RIGHT);

display.drawString(128, 33, "Right aligned (128,33)");

}

void drawRectDemo() {

// Draw a pixel at given position

for (int i = 0; i < 10; i++) {

display.setPixel(i, i);

display.setPixel(10 - i, i);

}

display.drawRect(12, 12, 20, 20);

// Fill the rectangle

display.fillRect(14, 14, 17, 17);

// Draw a line horizontally

display.drawHorizontalLine(0, 40, 20);

// Draw a line horizontally

display.drawVerticalLine(40, 0, 20);

}

void drawCircleDemo() {

for (int i=1; i < 8; i++) {

display.setColor(WHITE);

display.drawCircle(32, 32, i*3);

if (i % 2 == 0) {

display.setColor(BLACK);

}

display.fillCircle(96, 32, 32 - i* 3);

}

}

void drawProgressBarDemo() {

int progress = (counter / 5) % 100;

// draw the progress bar

display.drawProgressBar(0, 32, 120, 10, progress);

// draw the percentage as String

display.setTextAlignment(TEXT_ALIGN_CENTER);

display.drawString(64, 15, String(progress) + "%");

display.setTextAlignment(TEXT_ALIGN_LEFT);

display.setFont(ArialMT_Plain_10);

display.drawString(10, 0, "H4lo nb!");

}

//Demo demos[] = {drawFontFaceDemo, drawTextFlowDemo, drawTextAlignmentDemo, drawRectDemo, drawCircleDemo, drawProgressBarDemo, drawImageDemo};

Demo demos[] = {drawFontFaceDemo};

int demoLength = (sizeof(demos) / sizeof(Demo));

long timeSinceLastModeSwitch = 0;

void loop() {

// clear the display

display.clear();

demos[demoMode]();

delay(15000); //延迟

display.setTextAlignment(TEXT_ALIGN_RIGHT);

display.drawString(10, 128, String(millis()));

// write the buffer to the display

display.display();

if (millis() - timeSinceLastModeSwitch > DEMO_DURATION) {

demoMode = (demoMode + 1) % demoLength;

timeSinceLastModeSwitch = millis();

}

counter++;

delay(10);

}

效果

d05a761cdae1a96839205094c225e1b0.png

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/372317.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【VS开发】visual studio 2015的NuGet Manager解决方案管理功能

NuGet的官方说明是&#xff1a;NuGet是一款Visual Studio的扩展&#xff0c;它可以简单的安装、升级开源库和工具。 官网地址&#xff1a;http://www.nuget.org/ 官网最醒目的位置就是下载链接&#xff0c;安装完成后我们来快速体验一把。 手上有个小项目需要使用到json格式&am…

五. 面向对象高级特性4. 接口的概念和使用

在抽象类中&#xff0c;可以包含一个或多个抽象方法&#xff1b;但在接口(interface)中&#xff0c;所有的方法必须都是抽象的&#xff0c;不能有方法体&#xff0c;它比抽象类更加“抽象”。接口使用 interface 关键字来声明&#xff0c;可以看做是一种特殊的抽象类&#xff0…

智能配料

我们都有多少次听说“分批处理”会增加延迟&#xff1f; 作为对低延迟系统充满热情的人&#xff0c;这让我感到惊讶。 以我的经验&#xff0c;正确完成批处理不仅可以提高吞吐量&#xff0c;还可以减少平均延迟并保持一致。 那么&#xff0c;批处理如何神奇地减少延迟呢&#x…

mysql从myisam_将MySQL从MyISAM转换成InnoDB错误和解决办法

原来自己用的是为了装的&#xff0c; 所以在设置database usage(如下图1)的时候按照discuz官方的建议&#xff0c;选的都是Non-Transactional Database Only(只支持MyISAM数据引擎的非事务数据库)&#xff0c;用MyISAM数据库&#xff0c;还没涉及到需要InnoDB&#xff0c;因此打…

相似性度量中用到的一些距离函数

本文目录 1. 欧氏距离 2. 曼哈顿距离 3. 切比雪夫距离 4. 闵可夫斯基距离 5. 标准化欧氏距离 6. 马氏距离 7. 汉明距离 8. 杰卡德距离 & 杰卡德相似系数 9. 相关系数 & 相关距离 10. 信息熵 1. 欧氏距离(Euclidean Distance) 欧氏距离是最易于理解的一种距离计算方法&a…

Spring 3.1配置文件和Tomcat配置

Spring 3.1引入了非常有用的功能&#xff0c;称为配置文件 。 因此&#xff0c;它易于构建&#xff0c;可以在所有环境&#xff08;开发&#xff0c;测试&#xff0c;生产等&#xff09;中部署的软件包。 通过定义系统属性spring.profiles.active&#xff0c; Spring允许我们使…

计算1~n之间所有奇数之和_所有奇数长度子数组的和

所有奇数长度子数组的和题目&#xff1a;给你一个正整数数组 arr &#xff0c;请你计算所有可能的奇数长度子数组的和。子数组 定义为原数组中的一个连续子序列。请你返回 arr 中 所有奇数长度子数组的和 。示例 1&#xff1a;输入&#xff1a;arr [1,4,2,5,3]输出&#xff1a…

MYSQL AND OR的联用

MYSQL AND OR的联用 MYSQL中”AND”和”OR”都是条件控制符。”AND”是求交集&#xff0c;而”OR”则是求并集&#xff0c;非常多情况下&#xff0c;须要联用它们两个。下面是两张表,我仅仅列出实用的字段。 Table:student_score 学生成绩 sid(学生ID) cid(课程ID) score(分数)…

九度oj 题目1456:胜利大逃亡

题目描述&#xff1a;Ignatius被魔王抓走了,有一天魔王出差去了,这可是Ignatius逃亡的好机会.魔王住在一个城堡里,城堡是一个A*B*C的立方体,可以被表示成A个B*C的矩阵,刚开始Ignatius被关在(0,0,0)的位置,离开城堡的门在(A-1,B-1,C-1)的位置,现在知道魔王将在T分钟后回到城堡,I…

JMX:一些入门说明

JMX&#xff08;Java管理扩展&#xff09;是一种J2SE技术&#xff0c;可以管理和监视Java应用程序。 基本思想是实现一组管理对象&#xff0c;并将实现注册到平台服务器&#xff0c;在平台服务器上&#xff0c;可以使用一组连接器或适配器从本地或远程调用这些实现到JVM。 一个…

解释java程序中的异常机制_Java编程中的异常机制

本文旨在以初学者的角度来学习Java异常的知识&#xff0c;尽量简单&#xff0c;一些细枝末节的知识不会讲述&#xff0c;但不影响对知识的掌握。&#xff08;比如try-catch可以嵌套&#xff0c;不太会这么用&#xff09;1.什么是异常我们先举个例子int x 10/0;在IDE里输入这样…

keras做多层神经网络

一、 背景与目的 背景&#xff1a;配置好了theano&#xff0c;弄了gpu&#xff0c; 要学dnn方法。 目的&#xff1a;本篇学习keras基本用法&#xff0c; 学习怎么用keras写mlp&#xff0c;学keras搞文本的基本要点。 二、 准备 工具包&#xff1a; theano、numpy、keras等工具包…

配置环境变量

由于写了一个关于生成签名需要配置环境变量&#xff0c;所以在这里顺便把配置环境变量的步骤说一下 1.右键点击计算机&#xff0c;然后点击高级系统设置 2.点击环境变量&#xff0c;下方出现的即为系统变量&#xff0c;双击path就能直接修改&#xff0c; 转载于:https://www.cn…

使用JavaFX AnimationTimer

回想一下&#xff0c;给AnimationTimer起个名字可能不是一个好主意&#xff0c;因为它不仅可以用于动画&#xff0c;还可以用于测量&#xff1a;fps速率&#xff0c;碰撞检测&#xff0c;模拟步骤&#xff0c;游戏主循环等实际上&#xff0c;大部分时间我都看到了AnimationTime…

python列表姓氏_python数据分析实例(六) 中国姓氏数据

bokeh联动柱状图&#xff0c;Excel空间柱状图、空间热力图&#xff0c;Echarts空间柱状图&#xff0c;常用函数&#xff1a;df[工作地_省] df[工作地].str.split(省).str[0]df[工作地_市] df[工作地_市] df[工作地].str.split(省).str[1].str.split(市).str[0]df[工作地_市][…

JavaFX 2 GameTutorial第3部分

介绍 Ť他是与一个六个部分组成的系列的第3部分的JavaFX 2游戏教程。 如果您错过了第1部分和第2部分 &#xff0c;建议您在开始本教程之前先进行阅读。 回顾第二部分&#xff0c;我讨论了游戏循环的内部工作原理&#xff0c;其中我们使用动画&#xff08;JavaFX Timeline &…

Selenium WebDriver + python 自动化测试框架

目标 组内任何人都可以进行自动化测试用例的编写 完全分离测试用例和自动化测试代码&#xff0c;就像写手工测试用例一下&#xff0c;编写excel格式的测试用例&#xff0c;包括步骤、检查点&#xff0c;然后执行自动化工程&#xff0c;即可执行功能自动化测试用例&#xff0c;包…

mysql游戏减少积分活动图_plantuml-绘制状态图和活动图和部署图​

背景状态图&#xff1a;对象的所有状态&#xff0c;以及基于事件发生的状态改变的过程&#xff1b;活动图&#xff1a;用例的工作流程&#xff1b;部署图&#xff1a;系统的软硬件物理体系结构&#xff1b;状态图基本语法元素语法说明开始和结束状态[*]标识开始和结束状态箭头-…

windows中当你的键盘无法使用时我们可以用另一种方法哦

1.使用WinR打开cmd窗口 2.输入osk回车就出现了一个虚拟的小键盘啦&#xff0c;当你的键盘坏掉后非常实用哦 转载于:https://www.cnblogs.com/qianzf/p/6780496.html

python web.py 404_找不到web.py开发服务器-favicon.ico-404

py API文档引用了一个“web.SEE OTHER()”函数&#xff0c;该函数生成一个303 SEE OTHER响应&#xff0c;将浏览器重定向到另一个位置。(请参见http://webpy.org/docs/0.3/api#web.application)这是一个服务器端的解决方案&#xff0c;它不需要在html文件中更改头&#xff1b;如…