1- 7 发送数据到互联网

现在,让我们将相同的数据发送到互联网。首先要做的是在 http://thingspeak.com 上创建一个帐户并设置一个频道。每个通道都有八个字段,可用于存储ESP8266传输的数据。

作为免费帐户,您不需要每分钟发送三次以上的数据。优点是您的数据存储在他们的服务器上,您可以在漂亮的图形上看到它们或将它们作为IFRAME嵌入到另一个Web服务器中。

在“通道设置”中,创建一个字段并将其命名为 Light,然后转到API密钥选项卡并获取 Write API KEY。在这里,如果您有一个想要读取其他模块写入的数据的应用程序,您还可以定义读取 APY KEY。这是在模块之间共享数据的基本方法。由于Witty模块具有LDR,因此我们可以使用它在 api.thingspeak.com 上每分钟记录一次数据:

#include <ESP8266WiFi.h> 
 
const char* WIFI_SSID     = "YOUR_WIFI_SSID"; 
const char* WIFI_PASSWORD = "YOUR WIFI_PASSWORD"; 
const char* host = "api.thingspeak.com"; 
const char* writeAPIKey = "YOUR_WRITE_API_KEY"; 
 
#define LDR     A0 

在执行一次的 setup()函数中,LDR引脚设置为 INPUT 引脚,并通过 WiFi.begin(WIFI_SSID,WIFI_PASSWORD)功能将ESP8266连接到Wi-Fi网络:

void setup()  
{ 
  Serial.begin(115200); 
  delay(10); 
  pinMode(LDR, INPUT); 
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD); 
  delay(20); 
  Serial.print("Connecting to "); 
  Serial.println(WIFI_SSID); 
  while (WiFi.status() != WL_CONNECTED) { 
    delay(500); 
    Serial.print("."); 
  } 
  Serial.println(""); 
  Serial.println("WiFi connected!"); 
  Serial.println(); 
}

loop()函数中,每分钟都会从LDR传感器读取光强度并将其发布到 https://thingspeak.com/ 上的通道上的 Light 字段中:

void loop() 
{ 
  if ((millis() % (60 * 1000)) == 0) {  
    // make TCP connections 
    WiFiClient client; 
    const  int httpPort = 80; 
    if (!client.connect(host, httpPort)) { 
      return; 
    } 
     
    String url = "/update?key="; 
    url+=writeAPIKey; 
    url+="&field1="; 
    url+=String(analogRead(LDR)); 
    url+="\r\n"; 
    Serial.println(url); 
    // Request to the server 
    client.print(String("GET ") + url + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" +   "Connection: close\r\n\r\n"); 
  }
}

让我们看看几分钟后数据的样子:

现在,让我们结合从 wunderground.com 读取天气的草图和将数据发送到 thingspeak.com 的草图。它需要温度,湿度,露点和降水,它会将它们存储在 thinkspeak.com 上,以便以后我们可以导入它们。基本上,这将是一个天气记录器:

#include <ESP8266WiFi.h> 
#include <JsonListener.h> 
#include "WundergroundClient.h"

以下是 Wunderground 设置:

const String  WUNDERGRROUND_API_KEY = "58dfbeb30d02af26";
const Boolean IS_METRIC = true; 
WundergroundClient weather_data(IS_METRIC); 
const char* WIFI_SSID     = "YOUR_WIFI_SSID"; 
const char* WIFI_PASSWORD = "YOUR_WIFI_PASSWORD"; 
const String WUNDERGROUND_LANGUAGE = "EN"; 
const String WUNDERGROUND_COUNTRY = "NL"; 
const String WUNDERGROUND_CITY = "Eindhoven"; 
const char* host = "api.thingspeak.com"; 
const char* writeAPIKey = "YOUR_WRITE_API_KEY"; 
WiFiClient wifiClient

以下是连接到Wi-Fi网络的 setup()函数:

void setup() { 
  Serial.begin(115200); 
  delay(10); 
 
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD); 
  delay(20); 
  Serial.print("Connecting to "); 
  Serial.println(WIFI_SSID); 
  while (WiFi.status() != WL_CONNECTED) {
    delay(500); 
    Serial.print("."); 
  } 
  Serial.println(""); 
  Serial.println("WiFi connected!"); 
  Serial.println(); 
}

loop()函数中,将从 wunderground.com 检索每分钟天气数据,并将其发布到 thingspeak.com 。除了温度,压力,降水和露点之外,还会在月相,日出或日落等串行输出中打印更多信息,如果您计划添加显示模块以显示所有天气状况,则可以使用这些信息:

void loop() { 
if ((millis() % (60 * 1000)) == 0) {  
  Serial.println(); 
  Serial.println("\n\nNext Loop-Step: " + String(millis()) + ":"); 
   
  weather_data.updateConditions(WUNDERGRROUND_API_KEY, WUNDERGROUND_LANGUAGE,WUNDERGROUND_COUNTRY, WUNDERGROUND_CITY); 
   
  Serial.println("wundergroundHours: " + weather_data.getHours()); 
  Serial.println("wundergroundMinutes: " + weather_data.getMinutes()); 
  Serial.println("wundergroundSeconds: " + weather_data.getSeconds()); 
  Serial.println("wundergroundDate: " + weather_data.getDate()); 
   
  Serial.println("wundergroundMoonPctIlum: " + weather_data.getMoonPctIlum()); 
  Serial.println("wundergroundMoonAge: " + weather_data.getMoonAge()); 
  Serial.println("wundergroundMoonPhase: " + weather_data.getMoonPhase()); 
  Serial.println("wundergroundSunriseTime: " + weather_data.getSunriseTime()); 
  Serial.println("wundergroundSunsetTime: " + weather_data.getSunsetTime()); 
  Serial.println("wundergroundMoonriseTime: " + weather_data.getMoonriseTime()); 
  Serial.println("wundergroundMoonsetTime: " + weather_data.getMoonsetTime());
  Serial.println("wundergroundWindSpeed: " + weather_data.getWindSpeed()); 
  Serial.println("wundergroundWindDir: " + weather_data.getWindDir()); 
   
  Serial.println("wundergroundCurrentTemp: " + weather_data.getCurrentTemp()); 
  Serial.println("wundergroundTodayIcon: " + weather_data.getTodayIcon()); 
  Serial.println("wundergroundTodayIconText: " + weather_data.getTodayIconText()); 
  Serial.println("wundergroundMeteoconIcon: " + weather_data.getMeteoconIcon(weather_data.getTodayIconText())); 
  Serial.println("wundergroundWeatherText: " + weather_data.getWeatherText()); 
  Serial.println("wundergroundHumidity: " + weather_data.getHumidity()); 
  Serial.println("wundergroundPressure: " + weather_data.getPressure()); 
  Serial.println("wundergroundDewPoint: " + weather_data.getDewPoint()); 
  Serial.println("wundergroundPrecipitationToday: " + weather_data.getPrecipitationToday());
  WiFiClient client; 
  const int httpPort = 80;
  if (!client.connect(host, httpPort)) { 
    return; 
  } 
  String url = "/update?key="; 
  url+=writeAPIKey; 
  url+="&field1="; 
  url+=String(weather_data.getCurrentTemp()); 
  url+="&field2="; 
  url+=String(weather_data.getPressure()); 
  url+="&field3="; 
  url+=String(weather_data.getDewPoint());   
  url+="&field4="; 
  url+=String(weather_data.getPrecipitationToday());    
  url+="\r\n"; 
  Serial.println(url); 
      // Request to the server 
  client.print(String("GET ") + url + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" +  "Connection: close\r\n\r\n");
  Serial.println("---------------------------------------------------/\n"); 
  } 
}

几分钟后,您可以看到 ESP8266 从 wunderground.com 收到的值,并发布在 thingspeak.com 的四个图表上显示:

猜你喜欢

转载自blog.csdn.net/countofdane/article/details/85174510
今日推荐