主要内容

使用Arduino或ESP8266的批量更新

这个例子展示了如何使用Arduino®MKR1000板或连接到Wi-Fi®网络的ESP8266板连续收集Wi-Fi信号强度和批量更新ThingSpeak™通道。你可以使用Bulk-Write JSON数据API来批量收集数据并将其发送到ThingSpeak通道。这一策略可以减少设备的电力消耗。在本例中,您每15秒收集一次数据,每2分钟使用Arduino MKR1000板更新一次通道。因为Arduino MKR1000和ESP8266板没有实时时钟,所以可以对批量更新消息使用相对时间戳。

设置

  1. 创建一个频道,如图所示在新频道中收集数据

  2. 如果您使用的是Arduino MKR1000板,则包括库wifi101.h.spi.h.你的Arduino草图如果您使用的是ESP8266板,请包含库EthernetClient.hESP8266WiFi.h你的Arduino草图

代码

1)首先包含适合您的硬件的库。

// #include //取消注释this library to work with ESP8266 // #include //取消注释this library to work with ESP8266 #include //注释this library to work with ESP8266 board #include

2)初始化杰森累累持有JSON数据。

char jsonBuffer[500] = "[";//初始化jsonBuffer来保存数据

3)定义WiFi凭据连接Arduino板到网络,初始化WiFi客户端库。

char ssid [] =“您的网络-SSID”;//您的网络SSID(NAME)CHAR PASS [] =“您的网络-PWD”;//您的网络密码Wificlient客户端;//初始化WiFi客户端库

4)定义ThingSpeak服务器。

Char server[] = "api.thingspeak.com";/ / ThingSpeak服务器

5)定义其他全局变量,跟踪最后一次连接时间和最后一次更新时间。另外,定义更新数据的时间间隔,并将数据发布到ThingSpeak。

/*每15秒收集一次数据,每2分钟发送一次数据到ThingSpeak通道*/ unsigned long lastConnectionTime = 0;//跟踪最后一次连接时间unsigned long laststupdatetime = 0;//跟踪最后一次更新时间const unsigned long postingInterval = 120L * 1000L;//每2分钟Post一次数据const unsigned long updateInterval = 15L * 1000L;//每15秒更新一次

6)使用设置初始化串行数据传输,连接WiFi网络。

void setup() {Serial.begin(9600);//尝试连接WiFi网络while (WiFi.status() != WL_CONNECTED) {Serial. status();print(" attempt to connect to SSID: ");以(ssid);WiFi。开始(ssid,通过);//连接到WPA/WPA2网络。如果使用开放或WEP网络延迟(10000),请更改此线路;//等待10秒连接}Serial。println(“连接到wifi”);printWiFiStatus ();//打印WiFi连接信息}

7)环形方法,打电话给Updatesjson.方法更新杰森累累每秒一次的数据。

void loop(){//如果更新时间已达到1秒,然后更新JSONBUFFER if(millis() -  lastupdatetime> = updateinterval){Updatesjson(JSONBuffer);}}

8)定义Updatesjson.方法来不断更新杰森累累与数据。由于Arduino MKR1000没有实时时钟,所以使用“delta_t”参数来定义连续消息之间的相对时间戳(以秒为单位)。如果您的设备有一个实时时钟,您可以使用绝对时间戳。取代“delta_t”参数“created_at”范围。以一种格式格式化为JSON的格式Bulk-Write JSON数据。打电话给httprequest每2分钟将数据发送到物质的方法。

//使用数据void更新更新JosnBuffer(Char * JSONBUFFER){/ * JSON格式用于API中的更新参数*此示例使用相对时间戳,因为它使用“delta_t”。您还可以使用“created_at”参数*而不是“delta_t”提供绝对时间戳。*“[{\”delta_t \“:0,\”field1 \“: -  70},{\”delta_t \“:3,\”field1 \“: -  66}]”* ///格式化jsonbuffer注意到Strcat(JsonBuffer,“{\”delta_t \“:”);unsigned long deltat =(millis() -  lastupdatetime)/ 1000;size_t lendelt = string(deltat).length();Char Temp [4];字符串(Deltat).teCharArray(TEMP,LOWNTT + 1);Strcat(JsonBuffer,Temp);Strcat(jsonbuffer,“,”); long rssi = WiFi.RSSI(); strcat(jsonBuffer, "\"field1\":"); lengthT = String(rssi).length(); String(rssi).toCharArray(temp,lengthT+1); strcat(jsonBuffer,temp); strcat(jsonBuffer,"},"); // If posting interval time has reached 2 minutes, update the ThingSpeak channel with your data if (millis() - lastConnectionTime >= postingInterval) { size_t len = strlen(jsonBuffer); jsonBuffer[len-1] = ']'; httpRequest(jsonBuffer); } lastUpdateTime = millis(); // Update the last update time }

9)定义httprequest将数据发送到orithspeak的方法并从服务器打印响应代码。响应代码202指示服务器已接受待处理的请求。

//使用数据void httprequest(char * jsonbuffer)更新optionspeakchannel(char * jsonbuffer){/ * json格式在API中的数据缓冲区*此示例使用相对时间戳,因为它使用“delta_t”。您还可以使用“created_at”参数*而不是“delta_t”提供绝对时间戳。*“{\”write_api_key \“:\”您的频道 -  reiteakikey \“,\”更新\“:[{\”delta_t \“:0,\”field1 \“: -  60},{\”delta_t \“:15,\”Field1“:200},{\”delta_t \“:15,\”field1 \“: -  66}] * ///格式化数据缓冲区,如上所述[500] =”{\“write_api_key \”:\“您的频道 -  reiteakikey \”,\“更新\”:“; //用您的频道 - 译文替换您的频道录音机写入API密钥Strcat(数据,jsonbuffer); strcat(数据,“}”); //在发送新请求client之前关闭任何连接.stop(); string data_length = string(strlen(data)+1); //计算数据缓冲区长度串行.println(数据); // postapate of thingspeak of thingspeak(client.connect(server,80)){client.println(“post /channels/your-channel -d/bulk_update.json http / 1.1”); //替换您的通道ID您的ithingspectrand id client.println(“host:api.thingspeak.com”); client.println(“用户 - 代理:mw.doc.bulk-更新(Arduino esp8266)”); client.println(“连接:关闭“); client.println(”content-type:application / json“); client.println(”内容长度:“+ data_length);client.println();client.println(数据);} else {serial.println(“失败:无法连接到thingspeak”);延迟(250); //Wait to receive the response client.parseFloat(); String resp = String(client.parseInt()); Serial.println("Response code:"+resp); // Print the response code. 202 indicates that the server has accepted the response jsonBuffer[0] = '['; //Reinitialize the jsonBuffer for next batch of data jsonBuffer[1] = '\0'; lastConnectionTime = millis(); //Update the last conenction time }

10)定义printWiFiStatus方法打印您的设备IP地址和信号强度。

void printWiFiStatus(){//打印连接到的网络的SSID: Serial。打印(“SSID: ");以WiFi.SSID ());//打印你的设备IP地址:IPAddress IP = WiFi.localIP();系列。打印(“IP地址:”);以(ip);//打印收到的信号强度:long rssi = WiFi.RSSI();系列。print(“信号强度(RSSI):”);并同时(rssi); Serial.println(" dBm"); }

相关例子

更多关于