本帖最后由 天天煎蛋 于 2013-1-19 23:10 编辑
我单独用uno就能pmw控制成功。但是把这段代码复制进去远程控制小灯(yeelink的例程)里面,pmw就失效了~~搞不明白
补充:我试了所有的口。都不行。
不管我程序里面定义的是什么口,1,2,3,4,5,6,7,8,9,12都不会亮。10,11,13都亮。
/*
Yeelink sensor client power switch example
*/
#include <SPI.h>
#include <Ethernet.h>
#include <Wire.h>
#include <math.h>
byte buff[2];
// for yeelink api
#define APIKEY "dfa7e3490189215d6a2feb11fc68e49e" // replace your yeelink api key here
#define DEVICEID 958 // replace your device ID
#define SENSORID 1237 // replace your sensor ID
// assign a MAC address for the ethernet controller.
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
// initialize the library instance:
EthernetClient client;
char server[] = "api.yeelink.net"; // name address for yeelink API
//IPAddress server(202,136,60,231); // numeric IP for api.pachube.com
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 = 3*1000; // delay between 2 datapoints, 30s
String returnValue = "";
boolean ResponseBegin = false;
void setup() {
pinMode(11, OUTPUT);
Wire.begin();
// start serial port:
Serial.begin(57600);
// start the Ethernet connection with DHCP:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
for(;;)
;
}
else {
Serial.println("Ethernet configuration OK");
}
}
void loop() {
// if there's incoming data from the net connection.
// send it out the serial port. This is for debugging
// purposes only:
if (client.available()) {
char c = client.read();
// Serial.print(c);
if (c == '{')
ResponseBegin = true;
else if (c == '}')
ResponseBegin = false;
if (ResponseBegin)
returnValue += c;
}
if (returnValue.length() !=0 && (ResponseBegin == false))
{
Serial.println(returnValue);
if (returnValue.charAt(returnValue.length() - 1) == '1') {
Serial.println("turn on the LED");
for (int a=0; a<=255;a++) //循环语句,控制PWM亮度的增加
{
analogWrite(11,a);
delay(8); //当前亮度级别维持的时间,单位毫秒
}
for (int a=255; a>=0;a--) //循环语句,控制PWM亮度减小
{
analogWrite(11,a);
delay(8); //当前亮度的维持的时间,单位毫秒
}
delay(800);
}
else if(returnValue.charAt(returnValue.length() - 1) == '0') {
Serial.println("turn off the LED");
digitalWrite(7, LOW);
}
returnValue = "";
}
// if there's no net connection, but there was one last time
// through the loop, then stop the client:
if (!client.connected() && lastConnected) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
}
// if you're not connected, and ten seconds have passed since
// your last connection, then connect again and send data:
if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
// read sensor data, replace with your code
//int sensorReading = readLightSensor();
Serial.print("yeelink:");
//get data from server
getData();
}
// store the state of the connection for next time through
// the loop:
lastConnected = client.connected();
}
// this method makes a HTTP connection to the server and get data back
void getData(void) {
// if there's a successful connection:
if (client.connect(server, 80)) {
Serial.println("connecting...");
// send the HTTP GET request:
client.print("GET /v1.0/device/");
client.print(DEVICEID);
client.print("/sensor/");
client.print(SENSORID);
client.print("/datapoints");
client.println(" HTTP/1.1");
client.println("Host: api.yeelink.net");
client.print("Accept: *");
client.print("/");
client.println("*");
client.print("U-ApiKey: ");
client.println(APIKEY);
client.println("Content-Length: 0");
client.println("Connection: close");
client.println();
Serial.println("print get done.");
}
else {
// if you couldn't make a connection:
Serial.println("connection failed");
Serial.println();
Serial.println("disconnecting.");
client.stop();
}
// note the time that the connection was made or attempted:
lastConnectionTime = millis();
}
|