|
|
借用了czad大大的Timer, 想同時控制fan和上传DHT11数据
但一直上传不了想請教各位大大帮助
#include "Timers.h"
#include <SPI.h>
#include <Ethernet.h>
#include <LeweiTcpClient.h>
#include <EEPROM.h>
#include <dht11.h>
#define LW_USERKEY "073193205b564a9b90207133a9a65be4"
#define LW_GATEWAY "01"
dht11 DHT11;
//DHT11 vcc pin->+5v
//DHT11 data pin->d2
//DHT11 gnd pin->gnd
#define DHT11PIN 2
//byte mac[] = {0x74,0x69,0x69,0x2D,0x30,0x31};
//IPAddress ip(192,168,1, 15);
//IPAddress mydns(8,8,8,8);
//IPAddress gw(192,168,1,1);
//IPAddress subnet(255,255,255,0);
LeweiTcpClient *client;
long starttime;
int postInterval = 10000;
#define HG7881_B_IA 10 // D10 --> Motor B Input A --> MOTOR B +
#define HG7881_B_IB 11 // D11 --> Motor B Input B --> MOTOR B -
// functional connections
#define MOTOR_B_PWM HG7881_B_IA // Motor B PWM Speed
#define MOTOR_B_DIR HG7881_B_IB // Motor B Direction
// the actual values for "fast" and "slow" depend on the motor
#define PWM_SLOW 50 // arbitrary slow speed PWM duty cycle
#define PWM_FAST 200 // arbitrary fast speed PWM duty cycle
#define DIR_DELAY 1000 // brief delay for abrupt motor changes
void setup()
{
Serial.begin(9600);
client = new LeweiTcpClient(LW_USERKEY, LW_GATEWAY);
pinMode( MOTOR_B_DIR, OUTPUT );
pinMode( MOTOR_B_PWM, OUTPUT );
digitalWrite( MOTOR_B_DIR, LOW );
digitalWrite( MOTOR_B_PWM, LOW );
starttime = millis();
TCs.AddFunc(0, sendDht11Value, 200);
TCs.AddFunc(1, fan, 300);
}
void loop()
{
}
void sendDht11Value()
{
if ((millis()-starttime) > postInterval)
{
//read DHT11 sensor
int chk = DHT11.read(DHT11PIN);
switch (chk)
{
case DHTLIB_OK:
//Serial.print("*** start data collection ");
//Serial.println(DHT11.temperature);
//sending data
//you need to setup sensors named "temperature" and "humidity" on the web before you store the value.
client->appendSensorValue("temp",(String)DHT11.temperature);
//you can append more sensors here,and use sendSensorValue to send
client->sendSensorValue("hum",(String)DHT11.humidity);
break;
/*
case DHTLIB_ERROR_CHECKSUM:
Serial.println("Checksum error");
break;
case DHTLIB_ERROR_TIMEOUT:
Serial.println("Time out error,check your pin");
break;
default:
Serial.println("Unknown error");
break;
*/
starttime = millis();
}
}
}
void fan()
{
if(DHT11.temperature >=1)
digitalWrite( MOTOR_B_DIR, LOW );
digitalWrite( MOTOR_B_PWM, LOW );
delay( DIR_DELAY );
digitalWrite( MOTOR_B_DIR, HIGH ); // direction = forward
analogWrite( MOTOR_B_PWM, 255-PWM_FAST ); // PWM speed = fast
} |
|