kris080
发表于 2018-12-15 16:52
HTTP怎么简单的连接到onenet的数据流并且上传数据,除了发送
POST /devices/79146/datapoints HTTP/1.1
api-key: pmWLtnkDBSreKfvg7GsClUXdXa4A
Host:api.heclouds.com
Connection:close
Content-Length:59
{"datastreams":[{"id":"TEMP","datapoints":[{"value":50}]}]}
之后还需要什么条件才能在网页上显示收到的数据
shenmaolin
发表于 2019-1-11 11:50
YEmax 发表于 2017-7-26 17:50
我把楼主的程序改了一下,把文件启动那一块直接去了,然后直接定义ssid pass APIkeky ID,然后再直接引用这 ...
你好,能把你的代码发给我看看吗,我也去了一部分,但是无法连接onennet
shenmaolin
发表于 2019-1-11 12:00
我直接使用STA模式创建WiFi连接,然后改了部分代码,发现一直连接不到onenet
#include "edp.c"
#include <ESP8266WiFi.h>
char * ssid="ShenzhenWatertek";
char * pass="20080808";
WiFiClient client; //connect to EDP server
#define SERVER"jjfaedp.hedevice.com" //EDP server
#define PORT 876
String key = "IDP5ya9DA6Alu2OgMgqhgmmYTc4= ";
String id = "514723118";
static boolean tcpConnected = false, edpConnected = false, wifiConnected = false;
/***************************************************************************************/
#define u8 unsigned char
//IO方向设置
#define DHT11_IO_IN()pinMode(4, INPUT)
#define DHT11_IO_OUT() pinMode(4, OUTPUT)
////IO操作函数
#define DHT11_DQ_OUT 4 //数据端口 4
#define DHT11_DQ_IN4//数据端口 4
unsigned long MS_TIMER = 0;
unsigned long lastMSTimer = 0;
String comdata = "";
char flag = false;
int flag1=0;
/*******************************DHT11协议****************************************/
u8 temperature;
u8 humidity;
u8 t = 0;
//复位DHT11
void DHT11_Rst(void)
{
DHT11_IO_OUT(); //SET OUTPUT
digitalWrite(DHT11_DQ_OUT, LOW); //拉低DQ
delay(20); //拉低至少18ms
digitalWrite(DHT11_DQ_OUT, HIGH); //DQ=1
delayMicroseconds(30); //主机拉高20~40us
}
//等待DHT11的回应
//返回1:未检测到DHT11的存在
//返回0:存在
u8 DHT11_Check(void)
{
u8 retry = 0;
DHT11_IO_IN();//SET INPUT
while (digitalRead(DHT11_DQ_IN) && retry < 100) //DHT11会拉低40~80us
{
retry++;
delayMicroseconds(1);
};
if (retry >= 100)return 1;
else retry = 0;
while (!digitalRead(DHT11_DQ_IN) && retry < 100) //DHT11拉低后会再次拉高40~80us
{
retry++;
delayMicroseconds(1);
};
if (retry >= 100)return 1;
return 0;
}
//从DHT11读取一个位
//返回值:1/0
u8 DHT11_Read_Bit(void)
{
u8 retry = 0;
while (digitalRead(DHT11_DQ_IN) && retry < 100) //等待变为低电平
{
retry++;
delayMicroseconds(1);
}
retry = 0;
while (!digitalRead(DHT11_DQ_IN) && retry < 100) //等待变高电平
{
retry++;
delayMicroseconds(1);
}
delayMicroseconds(40);//等待40us
if (digitalRead(DHT11_DQ_IN))return 1;
else return 0;
}
//从DHT11读取一个字节
//返回值:读到的数据
u8 DHT11_Read_Byte(void)
{
u8 i, dat;
dat = 0;
for (i = 0; i < 8; i++)
{
dat <<= 1;
dat |= DHT11_Read_Bit();
}
return dat;
}
//从DHT11读取一次数据
//temp:温度值(范围:0~50°)
//humi:湿度值(范围:20%~90%)
//返回值:0,正常;1,读取失败
u8 DHT11_Read_Data(u8 *temp, u8 *humi)
{
u8 buf;
u8 i;
DHT11_Rst();
if (DHT11_Check() == 0)
{
for (i = 0; i < 5; i++) //读取40位数据
{
buf = DHT11_Read_Byte();
}
if ((buf + buf + buf + buf) == buf)
{
*humi = buf;
*temp = buf;
}
} else return 1;
return 0;
}
//初始化DHT11的IO口 DQ 同时检测DHT11的存在
//返回1:不存在
//返回0:存在
u8 DHT11_Init(void)
{
pinMode(4, OUTPUT);
DHT11_Rst();
return DHT11_Check();
}
/******************************************************************************/
void setup()
{
pinMode(16, OUTPUT); // Initialize the BUILTIN_LED1 pin as an output
Serial.begin(9600);
delay(10);
if (DHT11_Init()) //DHT11初始化
{
Serial.println("DHT11 Error");
delay(200);
}
Serial.println("DHT11 OK");
/* We start by connecting to a WiFi network */
WiFiStart();
}
/*************************************************************/
void loop()
{
uint32 temp, humi, tmp;
static unsigned long time = 0, timeOld = 0;
edp_pkt rcv_pkt;
uint8 pkt_type;
/* check wifi connection */
if (WiFi.status() != WL_CONNECTED)
{
if (wifiConnected == true)
Serial.println("wifi disconnected!");
wifiConnected = false;
tcpConnected = false;
edpConnected = false;
}
else
wifiConnected = true;
/* TCP connect */
if (wifiConnected)
{
if (!tcpConnected) client.connect(SERVER, PORT);//connect to edp server
if (!tcpConnected && client.connected())
{
Serial.print("connected to ");
Serial.println((String)SERVER);
tcpConnected = true;
}
else if (tcpConnected && !client.connected())//check TCP connnection
{
Serial.print("disconnected to ");
Serial.println((String)SERVER);
tcpConnected = false;
edpConnected = false;
}
//edp connect
if (tcpConnected && !edpConnected)
{
while (client.available()) client.read(); //清空接收缓存
packetSend(packetConnect(id.c_str(), key.c_str())); //发送EPD连接包
Serial.println("packetConnect(id.c_str(), key.c_str()) ok");
uint32 time = millis() + 5000;
while (!client.available() && millis() < time); //等待EDP连接应答
if ((tmp = client.readBytes(rcv_pkt.data, sizeof(rcv_pkt.data))) > 0 )
{
rcvDebug(rcv_pkt.data, tmp);
if (rcv_pkt.data == 0x20 && rcv_pkt.data == 0x00 && rcv_pkt.data == 0x00)
{
edpConnected = true;
Serial.println("EDP connected.");
}
else
Serial.println("EDP connect error.");
}
else
Serial.println("EDP connect no response.");
packetClear(&rcv_pkt);
}
}
}
/*
* packetSend: 发送包
*/
void packetSend(edp_pkt* pkt)
{
int16 len = pkt->len;
int16 i = 0;
if (pkt != NULL)
{
//client.write(pkt->data, pkt->len); //串口发送
while (len--)
client.write(pkt->data); //串口发送
free(pkt); //回收内存
}
}
/*
*rcvDebug: 将接受数据按照十六进制打印
*/
void rcvDebug(uint8* rcv, int len)
{
int i;
Serial.print("rcv len: ");
Serial.println(len, DEC);
for (i = 0; i < len; i++)
{
Serial.println(*rcv);
Serial.print(" ");
}
Serial.println("");
}
/*
readEdpPkt
从串口缓存中读数据到接收缓存
*/
bool readEdpPkt(edp_pkt *p)
{
int tmp;
if ((tmp = client.readBytes(p->data + p->len, sizeof(p->data))) > 0 )
{
rcvDebug(p->data + p->len, tmp);
p->len += tmp;
}
return true;
}
void WiFiStart()
{
WiFi.mode(WIFI_STA);
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid,pass);//配置连接的wifi热点
while (WiFi.status() != WL_CONNECTED ) {
delay(500);
Serial.print(".");
}
wifiConnected = true;
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
guotl111930
发表于 2019-6-13 15:56
感谢分享~