#include <HttpPacket.h>#include<ArduinoJson.h>#include<ESP8266WiFi.h>httppackethead Packet;#defineMyperiodic 5intL =0;//LED Indicators pinintSent =0;CharOnenetserver[] ="api.heclouds.com";//No modification requiredConst CharSsid[] ="Plus";//Modify the user name for your routerConst CharPassword[] ="45623467981";//Modify the password for your routerCharDevice_id[] ="4983230";//Modify the device ID for yourselfCharApi_key[] ="ZNBTV5QPBRV";//Modify the Api_key for yourselfCharSensor_id1[] ="PM1.0";CharSensor_id2[] ="PM2.5";voidConnectwifi () {Serial.print ("connecting to"+ *SSID); Wifi.begin (SSID, password); while(Wifi.status ()! =wl_connected) {Delay ( +); Serial.print ("."); } serial.println (""); Serial.println ("Connected"); Serial.println (""); Digitalwrite (0, high); Delay ( +); Digitalwrite (0, low); Delay ( +); Digitalwrite (0, high);}voidPostdatatoonenet (Char* Api_value_temp,Char* Device_id_temp,Char* Sensor_id_temp,floatthisdata) {wificlient client; Staticjsonbuffer< ->Jsonbuffer; Jsonobject& myjson =Jsonbuffer.createobject (); Jsonarray& Datastreams= Myjson.createnestedarray ("Datastreams"); Jsonobject& id_datapoints =Datastreams.createnestedobject (); id_datapoints["ID"] =sensor_id_temp; Jsonarray& datapoints = Id_datapoints.createnestedarray ("datapoints"); Jsonobject& value =Datapoints.createnestedobject (); value["value"] =Thisdata; Charp[ the]; Myjson.printto (P,sizeof(p)); Packet.sethostaddress (Onenetserver); Packet.setdevid (device_id_temp); //device_idPacket.setaccesskey (api_value_temp);//Api_key /*Create the HTTP message about add Datapoint*/Packet.createcmdpacket (POST, Type_datapoint, p); if(strlen (packet.content)) Serial.print (packet.content); SERIAL.PRINTLN (P); Charcmd[ -]; memset (cmd,0, -); strcpy (cmd, packet.content); strcat (cmd, p); if(Client.connect (Onenetserver, the) {serial.println ("WiFi Client Connected"); Client.print (CMD); Delay ( +); }//End If//serial.println (cmd);client.stop ();}voidSetup () {Serial.begin (115200); Pinmode (0, OUTPUT); Connectwifi (); Serial.println ("Setup end!");}voidLoop () {inttemp = random (Ten, -);intHum = random ( -, -); //send data to Onenetpostdatatoonenet (Api_key, device_id, SENSOR_ID1, temp); Delay ( -); Postdatatoonenet (Api_key, device_id, Sensor_id2, hum); Serial.println ("N0"+ String (Sent) +"Stream:was Send"); Sent++; intCount =Myperiodic; while(count--) Delay ( +);}
ESP8266 reporting data to China Mobile IoT platform HTTP