ArduinoESP8266DHT11物联网平台的 程序

作者:我 姓林 | 更新时间:2017-05-31 | 浏览量:2189

这是一个不错的想法...


评论:共2条

我 姓林 评论于:2017-05-31 15:17:39
#define SSID "HUAWEI-70AC" //type your own SSID name
#define PASSWORD "90417807" //type your own WIFI password


#include "uartWIFI.h"
#include <SoftwareSerial.h>
WIFI wifi;

extern int chlID; //client id(0-4)

// for yeelink api
#define APIKEY "bb802c56bdebfcf43d1d3843c432361d" // replace your yeelink api key here

//replace the device ID and sensor ID for temperature sensor.
#define DEVICEID0 358269 // replace your device ID
#define SENSORID0 407758 // replace your sensor ID

//replace the device ID and sensor ID for humidity sensor.
#define DEVICEID1 358269 // replace your device ID
#define SENSORID1 407760 // replace your sensor ID

char server[] = "api.yeelink.net"; // name address for yeelink API

unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
boolean lastConnected = false; // state of the connection last time through the main loop
const unsigned long postingInterval = 5*1000; // delay between 2 datapoints, 5s
String returnValue = "";
boolean ResponseBegin = false;


int DHT11PIN=4; //Connect D4 to data pin of DHT11


int humidity;
int temperature;

int post_number;

void setup()
{

wifi.begin();
bool b = wifi.Initialize(STA, SSID, PASSWORD);
if(!b)
{
DebugSerial.println("Init error");
}
delay(8000); //make sure the module can have enough time to get an IP address
String ipstring = wifi.showIP();
DebugSerial.println(ipstring); //show the ip address of module




}
void loop()
{
char message[400];
// if you're not connected, and ten seconds have passed since
// your last connection, then connect again and send data:
if((millis() - lastConnectionTime > postingInterval)) {

//read dht11
int chk = dht11_read(DHT11PIN);
if(chk==0)
{
if(post_number==0)
{
sendData(DEVICEID0,SENSORID0,temperature);
post_number++;
}
else
{
post_number = 0;
sendData(DEVICEID1,SENSORID1,humidity);
}

}

}

// if there's incoming data from the net connection.
// send it out the serial port. This is for debugging
// purposes only:
if(wifi.ReceiveMessage(message))
{
DebugSerial.println(message);
}


delay(10);

}

// this method makes a HTTP connection to the server:
void sendData(long device_id,long sensor_id,int thisData) {
// if there's a successful connection:
if (wifi.ipConfig(TCP,server, 80)) {
DebugSerial.println("connecting...");
// send the HTTP PUT request:
String cmd;
cmd = "POST /v1.0/device/";
cmd += String(device_id);
cmd += "/sensor/";
cmd += String(sensor_id);
cmd += "/datapoints";
cmd += " HTTP/1.1\r\n";
cmd += "Host: api.yeelink.net\r\n";
cmd += "Accept: *";
cmd += "/";
cmd += "*\r\n";
cmd += "U-ApiKey: ";
cmd += APIKEY;
cmd += "\r\n";
cmd += "Content-Length: ";
int thisLength = 10 + getLength(thisData);
cmd += String(thisLength);
cmd += "\r\n";
cmd += "Content-Type: application/x-www-form-urlencoded\r\n";
cmd += "Connection: close\r\n";
cmd += "\r\n";
cmd += "{"value":";
cmd += String(thisData);
cmd += "}\r\n";


DebugSerial.println(cmd);

wifi.Send(cmd);
// note the time that the connection was made:
lastConnectionTime = millis();
}
else {
// if you couldn't make a connection:
DebugSerial.println("connection failed");
DebugSerial.println("disconnecting.");
wifi.closeMux();
}
}

int getLength(int someValue) {
// there's at least one byte:
int digits = 1;
// continually divide the value by ten,
// adding one to the digit count for each
// time you divide, until you're at 0:
int dividend = someValue /10;
while (dividend > 0) {
dividend = dividend /10;
digits++;
}
// return the number of digits:
return digits;
}




int dht11_read(int pin)
{
// BUFFER TO RECEIVE
int bits[5];
int cnt = 7;
int idx = 0;

// EMPTY BUFFER
for (int i=0; i< 5; i++)
{bits[i]= 0;}

// REQUEST SAMPLE
pinMode(pin, OUTPUT);
digitalWrite(pin, LOW);
delay(18);
digitalWrite(pin, HIGH);
delayMicroseconds(40);
pinMode(pin, INPUT);

// ACKNOWLEDGE or TIMEOUT
unsigned int loopCnt = 10000;
while(digitalRead(pin) == LOW)
if (loopCnt-- == 0) return -2;

loopCnt = 10000;
while(digitalRead(pin) == HIGH)
if (loopCnt-- == 0) return -2;

// READ OUTPUT - 40 BITS => 5 BYTES or TIMEOUT
for (int i=0; i<40; i++)
{
loopCnt = 10000;
while(digitalRead(pin) == LOW)
if (loopCnt-- == 0) return -2;

unsigned long t = micros();

loopCnt = 10000;
while(digitalRead(pin) == HIGH)
if (loopCnt-- == 0) return -2;

if ((micros() - t) > 40) bits[idx] |= (1 << cnt);
if (cnt == 0) // next byte?
{
cnt = 7; // restart at MSB
idx++; // next byte!
}
else cnt--;
}

// WRITE TO RIGHT VARS
// as bits[1] and bits[3] are allways zero they are omitted in formulas.
humidity = bits[0];
temperature = bits[2];

int sum = bits[0] + bits[2];

if (bits[4] != sum) return -1;
return 0;
}
为什么提示expected ';' before 'value'出错。@贝壳物联
贝壳物联 回复于:2017-06-03 17:37:59
回复 @我 姓林:别的平台没有研究过
返回顶部