ESP8266 reporting data to China Mobile IoT platform HTTP

Source: Internet
Author: User

#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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.