arduino通过esp8266模块发送数据到云服务器

我是代码小白,一个正在做毕设的秃头少年。鄙人拙作,有不当之处,还请指教。

最近买了一套arduino设备,打算做一个物联网设备小玩意,可是怎么把数据上传到云服务器可愁坏我了。通过对比实验,我决定用esp8266wifi模块进行通信。

云服务器的话,我现在还没写相应的代码,所以先用Onenet平台进行配置。

Onenet平台进行配置

1.进入Onenet网站,注册账号,登录到开发者中心。网址 https://open.iot.10086.cn/

2.进入开发者中心界面后选中全部产品,点击多协议接入。

手机app发送指令设置esp8266 联网 esp8266发送数据到手机_服务器

3.选择HTTP协议,点击添加产品。

手机app发送指令设置esp8266 联网 esp8266发送数据到手机_#define_02

4.填上相关信息点击确定。

手机app发送指令设置esp8266 联网 esp8266发送数据到手机_#define_03

5.然后点击设备列表。点击添加设备。

手机app发送指令设置esp8266 联网 esp8266发送数据到手机_服务器_04

6.填上相关信息,点击添加。

手机app发送指令设置esp8266 联网 esp8266发送数据到手机_服务器发送信息给arduino_05

7.记住产品的APIkey和设备ID

手机app发送指令设置esp8266 联网 esp8266发送数据到手机_服务器_06

手机app发送指令设置esp8266 联网 esp8266发送数据到手机_#define_07

arduino代码

#include

#include

#include"dht11.h"

dht11 DHT11;

#define DHT11PIN 7 // 将把 DHT11 的 data pin 连到 arduino Pin 7

#define SSID "xxxx" //wifi名

#define PASS "xxxxxxx" //wifi密码

#define IP "api.heclouds.com" // 连接thingspeak.com服务器

const char OneNetServer[] = "api.heclouds.com";

//String GET = "GET /update?key=ylZJctZfVULDM4NvVRfec97Axxw="; //输入前面记下的API

const char APIKEY[] = "ylZJctZfVULDM4NvVRfec97Axxw="; // 使用时请修改为你的API KEY -- Please use your own API KEY

int32_t DeviceId = 559228290; // 使用时请修改为你的设备ID -- Please use your own device ID

const char DS_Temp[] = "TEMP"; // 数据流 温度TEMP -- Stream "TEMP"

const char DS_Hum[] = "HUMI"; // 数据流 湿度HUMI -- Stream "HUMI"

SoftwareSerial monitor(3, 2); // 定义软串口RX, TX

const int tcpPort = 80;

//初始化-----------------------------------------

void setup()

{

monitor.begin(9600);

Serial.begin(9600);

pinMode(DHT11PIN, OUTPUT);

monitor.println("AT");//指令测试

delay(1000);

if (monitor.find("OK")) //接收指令正常则返回OK

{

Serial.println("Wifi module connection is normal");

connectWiFi();

}

else {

Serial.println("Wifi module connection failed");

}

}

//主循环-----------------------------------------

void loop()

{

int chk = DHT11.read(DHT11PIN);

//Serial.println((float)DHT11.temperature);

//Serial.println((float)DHT11.humidity);

float tempH = DHT11.humidity;

float tempT = DHT11.temperature;

char buffer[10];

String temph = dtostrf(tempH, 4, 1, buffer);

String tempt = dtostrf(tempT, 4, 1, buffer);

updateTemp(temph,tempt);

delay(5000);

}

void updateTemp(String temph, String tempt)

{

String cmd = "AT+CIPSTART=\"TCP\",\"";

cmd += IP;

cmd += "\",80";

//sendDebug(cmd); //发送指令,链接服务器

monitor.println(cmd);

delay(2000);

if (monitor.find("Error"))

{

Serial.print("Connection to server failed");

return;

}

//cmd = GET + "&field1=" + temph + "&field2=" + tempt + "\r\n"; //记录T和H的值

cmd = postData(DeviceId, tempt, temph);

monitor.print("AT+CIPSEND=");

monitor.println(cmd.length());

if (monitor.find(">"))

{

Serial.print(">");

monitor.print(cmd);

Serial.print(cmd);

}

else

{

Serial.println("Data transmission failure");

}

if (monitor.find("OK"))

{

Serial.println("RECEIVED: OK");

monitor.println("AT+CIPCLOSE");

}

else

{

Serial.println("Data transmission failure");

}

}

boolean connectWiFi()

{

//Serial.println("AT+CIPMUX=0");

monitor.println("AT+CWMODE=1");

monitor.println("AT+RST");

delay(2000);

String cmd = "AT+CWJAP=\"";

cmd += SSID;

cmd += "\",\"";

cmd += PASS;

cmd += "\"";

monitor.println(cmd);

delay(1000);

if (monitor.find("OK"))

{

Serial.println("Wifi connection successful");

return true;

} else

{

Serial.println("Wifi connection failed");

return false;

}

}

String postData(int dId, String val_t, String val_h)

{

// 创建发送请求的URL -- We now create a URI for the request

String url = "/devices/559228290";//此处修改为你的设备id

//url += String(dId);

url += "/datapoints?type=3";

String data = "{\"" + String(DS_Temp) + "\":" + val_t + ",\"" +

String(DS_Hum) + "\":" + val_h + "}";

// 创建发送指令 -- We now combine the request to the server

String post_data = "POST " + url + " HTTP/1.1\r\n" +

"api-key:" + APIKEY + "\r\n" +

"Host:" + OneNetServer + "\r\n" +

"Content-Length: " + String(data.length()) + "\r\n" + //发送数据长度

"Connection: close\r\n\r\n" +

data;

// 发送指令 -- This will send the request to server

return post_data;

}

注意postData()函数中,url中还要改成自己的设备id。由于调用宏定义老是出错,不得已才直接写上去

接线方式

手机app发送指令设置esp8266 联网 esp8266发送数据到手机_服务器发送信息给arduino_08

dh11温湿度传感器的话,data引脚接arduino的引脚7

注意esp8266wifi模块输入电压均为3.3v,温湿度 传感器为5v。

运行结果

接线完毕运行代码,打开串口助手。出现以下画面即为数据上传成功。

手机app发送指令设置esp8266 联网 esp8266发送数据到手机_服务器_09

接下来我们看服务器方面

手机app发送指令设置esp8266 联网 esp8266发送数据到手机_#define_10

手机app发送指令设置esp8266 联网 esp8266发送数据到手机_服务器_11

手机app发送指令设置esp8266 联网 esp8266发送数据到手机_服务器_12

果然收到数据。接下来就可以自己编写服务器的api接口接收数据,做一下更加有意思的事了。