esp8266与ThinkSpeak的使用

在本篇文章中,我们使用ESP8266模块将LM35温度传感器的数据发送到ThingSpeak。 ThingSpeak是一个IOT平台,它允许我们将数据存储在云中并开发物联网(IOT)应用程序。

我们将在ThingSpeak上创建一个频道,在将ESP8266连接到Wi-Fi网络后,我们会使用API密钥将数据发送到ThingSpeak IP地址。

所需的组件

● Arduino开发板
● ESP-01适配器
● ESP-01模块
● LM35温度传感器
● 面包板

电路原理图

为了将ESP8266与Arduino连接,我们使用了ESP-01适配器,使连接更加容易。使用此适配器,我们不需要分压器电路或任何外部电源,因为此适配器具有内置电压调节器。

将ESP-01适配器的VCC连接到Arduino的5V,将适配器的GND连接到Arduino的GND。然后将适配器的TX连接到Arduino的引脚2,将适配器的RX连接到Arduino的引脚3。
在这里插入图片描述之后,将LM35温度传感器与Arduino连接。LM35温度传感器与Arduino的连接如下:

● LM35的左侧引脚连接到Arduino的5V
● LM35的中间引脚连接到Arduino的A0
● LM35的右引脚连接到Arduino的GND

在ThingSpeak上创建频道

首先,访问ThingSpeak.com,然后点击“Get Started for Free”。
在这里插入图片描述然后会出现一个注册表格。输入所需信息并注册ThingSpeak。
在这里插入图片描述之后,单击“New Channel”创建一个频道来存储信息。

在这里插入图片描述之后,转到API keys部分并复制您的写API密钥。您需要在下面的代码中输入。
在这里插入图片描述代码说明

首先,您需要包含软件串口库。软件串口将允许其他Arduino引脚上的串行通信。 Arduino的默认串行通信引脚为0和1。

#include <SoftwareSerial.h>
SoftwareSerial esp(2,3);

然后我们需要定义ThingSpeak IP地址。你不需要改变它,因为它对每个人都是一样的。然后我们将之前创建的通道的API密钥存储在字符串中。在下面的代码中,使用之前保存的API密钥更改“W7Q4FCJXJSX0BN5T”。这将是我们要发送数据的渠道API密钥。

#define IP "184.106.153.149"// thingspeak.com ip
String Api_key = "GET /update?key=W7Q4FCJXJSX0BN5T"; //change it with your api key like "GET /update?key=Your Api Key"

在setup()函数中,我们将模块设置为站模式并将其与Wi-Fi地址连接。在以下代码中输入您的Wi-Fi名称和密码。如果ESP8266与Wi-Fi网络的连接成功,则在串行监视器将显示“Connected”。

send_command("AT+RST\r\n", 2000, DEBUG); //reset module
send_command("AT+CWMODE=1\r\n", 1000, DEBUG); //set station mode
send_command("AT+CWJAP="Your wifi address","Your password"\r\n", 2000, DEBUG); //connect wifi network
while(!esp.find("OK")) { //wait for connection
Serial.println("Connected");}

在update()函数中,我们将命令发送到ESP8266,以便在我们之前存储的IP地址和端口80处发送数据。然后给出API密钥,以便将数据存储在该通道中。现在,ThingSpeak将根据时间为数据制作图表。

如果它无法将数据发送到ThingSpeak,则“AT + CIPClOSE”将显示在串行监视器上,这意味着它将关闭连接并再次重试。如果您无法向ThingSpeak发送数据,请重新检查您的API密钥,Wi-Fi名称和密码。

  String command = "AT+CIPSTART="TCP","";
  command += IP;
  command += "",80";
  Serial.println(command);
  esp.println(command);
  delay(2000);
  if(esp.find("Error")){
    return;
  }
  command = Api_key ;
  command += "&field1=";   
  command += temp;
  command += "\r\n";
  Serial.print("AT+CIPSEND=");
  esp.print("AT+CIPSEND=");
  Serial.println(command.length());
  esp.println(command.length());
  if(esp.find(">")){
    Serial.print(command);
    esp.print(command);
  }

如果它无法将数据发送到ThingSpeak,则在串行监视器将显示“AT + CIPClOSE”,这意味着它将关闭连接并再次重试。如果您无法向ThingSpeak发送数据,请重新检查您的API密钥、Wi-Fi名称和密码。

else{
    
   Serial.println("AT+CIPCLOSE");
   esp.println("AT+CIPCLOSE");
    //Resend...
    error=1;
  }
  }

全代码:

#include <SoftwareSerial.h>
SoftwareSerial esp(2,3);
 
#define DEBUG true 
#define IP "184.106.153.149"// thingspeak.com ip
String Api_key = "GET /update?key=AC5XM0WNA92LTACH"; //change it with your api key like "GET /update?key=Your Api Key"

int error;
const int sensor_pin = A0;
float temp;  
float output;  

void setup()
{ 
  Serial.begin(9600);
  esp.begin(112500);
  pinMode(sensor_pin,INPUT);
  
  send_command("AT+RST\r\n", 2000, DEBUG); //reset module
  send_command("AT+CWMODE=1\r\n", 1000, DEBUG); //set station mode
  send_command("AT+CWJAP=\"HUAWEI-3L9ML8\",\"75785302\"\r\n", 2000, DEBUG);   //connect wifi network
  while(!esp.find("OK")) { //wait for connection
  Serial.println("Connected");} 
}

void loop()
{
  output=analogRead(sensor_pin);
  temp =(output*500)/1023;
  start: //label 
  error=0;
  updatedata();
  if (error==1){
    goto start; //go to label "start"
  }
  delay(1000);
}

void updatedata(){
  String command = "AT+CIPSTART=\"TCP\",\"";
  command += IP;
  command += "\",80";
  Serial.println(command);
  esp.println(command);
  delay(2000);
  if(esp.find("Error")){
    return;
  }
  command = Api_key ;
  command += "&field1=";   
  command += temp;
  command += "\r\n";
  Serial.print("AT+CIPSEND=");
  esp.print("AT+CIPSEND=");
  Serial.println(command.length());
  esp.println(command.length());
  if(esp.find(">")){
    Serial.print(command);
    esp.print(command);
  }
  else{
    
   Serial.println("AT+CIPCLOSE");
   esp.println("AT+CIPCLOSE");
    //Resend...
    error=1;
  }
  }

String send_command(String command, const int timeout, boolean debug)
{
  String response = "";
  esp.print(command);
  long int time = millis();
  while ( (time + timeout) > millis())
  {
    while (esp.available())
    {
      char c = esp.read();
      response += c;
    }
  }
  if (debug)
  {
    Serial.print(response);
  }
  return response;
}
  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值