|
发表于 2015-6-14 20:54:37
|
显示全部楼层
崩溃了,,怎么改都只显示AT+RST。。。yeelink也没有任何数据
用的LM35,0模拟口读取温度。因为debugserial提示有错,所以改成了serial。麻烦帮看看啊~~~实在是感激不尽~~~!!!
#define SSID "323" //type your own SSID name
#define PASSWORD "abc123abc123" //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 "f9722******4d5fece00" //省略了
//replace the device ID and sensor ID for temperature sensor.
#define DEVICEID0 35782 // replace your device ID
#define SENSORID0 52997 // 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 LM35PIN=0; //Connect D25 to data pin of DHT11
int temperature;
void setup()
{
wifi.begin();
bool b = wifi.Initialize(STA, SSID, PASSWORD);
if(!b)
{
Serial.println("Init error");
}
delay(5000); //make sure the module can have enough time to get an IP address
String ipstring = wifi.showIP();
Serial.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 = lm35_read(0);
if(chk==0)
{
sendData(DEVICEID0,SENSORID0,temperature);
}
}
// 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))
{
Serial.println(message);
}
delay(10);
}
// this method makes a HTTP connection to the server:
void sendData(int device_id,int sensor_id,int thisData) {
// if there's a successful connection:
if (wifi.ipConfig(TCP,server, 80)) {
Serial.println("connecting...");
// send the HTTP PUT request:
String cmd;
cmd = "POST /v1.1/device/"; //我的这里是1.1
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";
Serial.println(cmd);
wifi.Send(cmd);
// note the time that the connection was made:
lastConnectionTime = millis();
}
else {
// if you couldn't make a connection:
Serial.println("connection failed");
Serial.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 lm35_read(int pin)
{
int data = analogRead(0);
temperature = (125*data)>>8;
Serial.print(temperature);
return true;
}
|
|