创作者:giveup | 更新日期:2017-03-11 | 在线时长:3天
All Source! Arduino UNO, using an Arduino Wiznet Ethernet shield.
//Arduino UNO, using an Arduino Wiznet Ethernet shield, DHT11, I2C 1602 LCD.
/*
using an Arduino Wiznet Ethernet shield.
Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13
*/
#include
#include
//PCF8574A地址为0x38~0x3f
LiquidCrystal_I2C lcd(0x3f, 16, 2); // set the LCD address to 0x27 for a 16 chars and 2 line display
#include
DS1307 RTC;
#include
#include
// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };
// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;
// fill in an available IP address on your network here,
// for manual configuration:
IPAddress ip(192, 168, 2, 177);
IPAddress gateway(192, 168, 2, 1);
IPAddress subnet(255, 255, 255, 0);
// fill in your Domain Name Server address here:
IPAddress myDns(8, 8, 8, 8);
//const char server[] = "www.bigiot.net";
const char server[] = "192.168.2.104";
//IPAddress server(121.42.180.30);
const int port = 8181;
#include
//============= 此处必须修改============
const char APIKEY[] = "hahaha"; // 此处替换为你自己的API KEY
char DEVICEID[] = "1"; // 此处替换为你的设备编号
const char INPUTID1[] = "1";
const char INPUTID2[] = "1";
const char INPUTID3[] = "1";
const char INPUTID4[] = "1";
//=======================================
aJsonStream serial_stream(&client);
unsigned long lastCheckInTime = 0;
unsigned long lastUpdateTime = 0;
const unsigned long postingInterval = 40000; // delay between 2 datapoints, 40s
const unsigned long updateInterval = 5000; // delay between 2 datapoints, 5s
boolean isCheckIn = false;
#include "DHT.h"
#define DHTPIN 9 // what digital pin we're connected to
// Uncomment whatever type you're using!
#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
// Connect pin 1 (on the left) of the sensor to +5V
// NOTE: If using a board with 3.3V logic like an Arduino Due connect pin 1
// to 3.3V instead of 5V!
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor
// Initialize DHT sensor.
// Note that older versions of this library took an optional third parameter to
// tweak the timings for faster processors. This parameter is no longer needed
// as the current DHT reading algorithm adjusts itself to work on faster procs.
DHT dht(DHTPIN, DHTTYPE);
//out to lcd & Serial.
#define OUT(s) do{Serial.print(s);lcd.print(s);}while(0)
#define OUT_ln(s) do{Serial.println(s);lcd.print(s);}while(0)
#define OUT_FMT(s,v) do{Serial.print(s,v);lcd.print(s,v);}while(0)
// This is optional, but set to false to disable
#define LED_DEBUG true
#include
// Set this to override the default value
#define LED_DEBUG_PIN 2
//update values
//同时上传n个接口数据调用此函数
#define UPDATE_BEGIN(did) do{ client.print(F("{\"M\":\"update\",\"ID\":\"")); client.print(did);\
client.print(F("\",\"V\":{\""));Serial.print(F("update:"));}while(0)
//repeats as many as you want
#define UPDATE_V(id,value) do{client.print(id); client.print(F("\":\"")); client.print(value); \
Serial.print(id); Serial.print(F("->")); Serial.print(value); }while(0)
#define UPDATE_V_NEXT() do{client.print(F("\",\""));Serial.print(":");}while(0)
#define UPDATE_END() do{client.println(F("\"}}"));\
lastCheckInTime = millis(); \
lastUpdateTime= millis(); \
Serial.println(); }while(0)
#define sayToClient(client_id, content) do{ \
client.print(F("{\"M\":\"say\",\"ID\":\"")); \
client.print(client_id); \
client.print(F("\",\"C\":\"")); \
client.print(content); \
client.println(F("\"}")); \
lastCheckInTime = millis(); \
Serial.print(F("say to ")); Serial.print(client_id); Serial.print(F(":")); \
Serial.println(content); }while(0)
void checkIn();
void printTime() {
DateTime now = RTC.now();
char buf[26];
strncpy(buf, "YYYY-MM-DD hh:mm:ss \0", sizeof(buf));
Serial.print(now.format(buf));
}
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(115200);
// this check is only needed on the Leonardo:
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
// Set current time only the first to values, hh,mm are needed
// Wire.begin();
lcd.init(); // initialize the lcd
// Print a message to the LCD.
lcd.backlight();
delay(300);
lcd.setCursor(1, 0);
lcd.print(F("Hello, world!"));
lcd.setCursor(0, 1);
if (!RTC.isrunning()) {
OUT(F("RTC is NOT running!\n"));
// following line sets the RTC to the date & time this sketch was compiled
RTC.adjust(DateTime(__DATE__, __TIME__));
}
printTime();
Serial.println();
dht.begin();
// start the Ethernet connection:
Serial.println(F("Trying to get an IP address using DHCP"));
// initialize the ethernet device not using DHCP:
if (Ethernet.begin(mac) == 0) {
OUT(F("Failed to configure Ethernet using DHCP\n"));
// start the Ethernet connection using a fixed IP address and DNS server:
Ethernet.begin(mac, ip, myDns, gateway, subnet);
}
// print your local IP address:
ip = Ethernet.localIP();
Serial.print(F("My IP address: "));
lcd.setCursor(0, 1);
lcd.print(F("IP:"));
for (byte thisByte = 0; thisByte < 4; thisByte++) {
// print the value of each byte of the IP address:
OUT_FMT(ip[thisByte], DEC);
OUT(".");
}
Serial.println();
Serial.println(F("Try Connecting..."));
Serial.println(F("connecting..."));
if(client.connect(server, port)){
Serial.println(F("connected"));
delay(2000);
checkIn();
}else{
Serial.println(F("connection failed"));
}
}
unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
const unsigned long Interval = 1L * 1000L; // delay between updates, in milliseconds
float h,t;
void loop() {
// Wait a few seconds between measurements.
// delay(2000);
if (millis() - lastCheckInTime > postingInterval) {
Serial.println(F("checking in"));
checkIn();
}
if (millis() - lastConnectionTime > Interval) {
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
h = dht.readHumidity();
// Read temperature as Celsius (the default)
t = dht.readTemperature();
// // Read temperature as Fahrenheit (isFahrenheit = true)
// float f = dht.readTemperature(true);
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t)/* || isnan(f)*/) {
Serial.println(F("Failed to read from DHT sensor!"));
// return;
}
// // Compute heat index in Fahrenheit (the default)
// float hif = dht.computeHeatIndex(f, h);
// // Compute heat index in Celsius (isFahreheit = false)
// float hic = dht.computeHeatIndex(t, h, false);
Serial.print(F("Humidity: "));
Serial.print(h);
Serial.print(F(" %\t"));
Serial.print(F("Temperature: "));
Serial.print(t);
Serial.print(F(" *C\n"));
// Serial.print(f);
// Serial.print(" *F\t");
// Serial.print("Heat index: ");
// Serial.print(hic);
// Serial.print(" *C ");
// Serial.print(hif);
// Serial.println(" *F");
lcd.setCursor(0, 0);
lcd.print(F("H:"));
lcd.print(h);
lcd.print(F("% T:"));
lcd.print(t);
lcd.print(F("*C"));
lastConnectionTime=millis();
}
if ((millis() - lastUpdateTime > updateInterval) && isCheckIn) {
printTime();
int val; //定义变量
int dat; //定义变量
dat = analogRead(A1); // 读取传感器的模拟值并赋值给dat
val = dat * (4.76 / 1023.0 * 100);
UPDATE_BEGIN(DEVICEID);//上传接口数据
UPDATE_V(INPUTID1, val); //V1
UPDATE_V_NEXT();
UPDATE_V(INPUTID2, h); //V2
UPDATE_V_NEXT();
UPDATE_V(INPUTID3, t); //V3
UPDATE_V_NEXT();
UPDATE_V(INPUTID4, digitalRead(8)); //Pin8
UPDATE_END();
}
if (serial_stream.available()) {
/* First, skip any accidental whitespace like newlines. */
serial_stream.skip();
}
if (serial_stream.available()) {
/* Something real on input, let's take a look. */
aJsonObject *msg = aJson.parse(&serial_stream);
processMessage(msg);
aJson.deleteItem(msg);
}
}
void checkIn() {
if (!client.connected()) {
Serial.println(F("connection failed"));
client.stop();
isCheckIn = false;
client.connect(server, port);
Serial.println(F("connecting..."));
delay(10000);
} else {
Serial.println(F("connection success"));
client.print(F("{\"M\":\"checkin\",\"ID\":\""));
client.print(DEVICEID);
client.print(F("\",\"K\":\""));
client.print(APIKEY);
client.println(F("\"}"));
lastCheckInTime = millis();
Serial.println(F("checking..."));
PULSE(); // Emits pulses
}
}
void processMessage(aJsonObject *msg) {
aJsonObject* method = aJson.getObjectItem(msg, "M");
aJsonObject* content = aJson.getObjectItem(msg, "C");
aJsonObject* client_id = aJson.getObjectItem(msg, "ID");
String M = method->valuestring;
// Emits multiple pulses
PULSE(5);
Serial.println(M);
// char* st = aJson.print(msg);//注意:大缓冲区PRINT_BUFFER_LEN=256字节
//
// if (st != NULL) {
// Serial.println(st);
// free(st);
if (M == "checkinok") {
isCheckIn = true;
Serial.println(F("check in OK!"));
}else if(M=="say" ){
String C=content->valuestring;
String F_C_ID=client_id->valuestring;
if(C=="play"){
PULSE(3, 250);
sayToClient(F_C_ID,"LED on!");
}else if(C=="stop"){
PULSE(3, 100);
sayToClient(F_C_ID,"LED off!");
}else{
sayToClient(F_C_ID,"You say "+C);
}
}
// }
}
building log:
bigiot/learn01/Release/learn01.elf :
section size addr
.data 368 8388864
.text 29444 0
.bss 918 8389232
.comment 17 0
.debug_aranges 6688 0
.debug_info 365660 0
.debug_abbrev 48709 0
.debug_line 42898 0
.debug_frame 25208 0
.debug_str 46477 0
.debug_loc 150009 0
.debug_ranges 8616 0
Total 725012
Finished building