Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save clive520/84c0be11091884347edba41cbb6c487c to your computer and use it in GitHub Desktop.
Save clive520/84c0be11091884347edba41cbb6c487c to your computer and use it in GitHub Desktop.
用RemoteXY看溫度濕度_加上圖表,利用Wifi上傳資料到1.Google 表單 2.LINE 3.Thinkspeak 4.IFTTT
/*
-- 藍芽 --
RemoteXY
藍芽連接 NodeMCU V3 H05(06) Arduino IDE
Software Serial RX==>D1 TX==>D2 speed9600
tex_1 和 text_2
motoduino
D4=2 接DHT11
上傳溫度和濕度資料到
1.Google 表單
2.LINE
3.Thinkspeak
4.IFTTT
This source code of graphical user interface
has been generated automatically by RemoteXY editor.
To compile this code using RemoteXY library 2.4.3 or later version
download by link http://remotexy.com/en/library/
To connect using RemoteXY mobile app by link http://remotexy.com/en/download/
- for ANDROID 4.3.1 or later version;
- for iOS 1.3.5 or later version;
This source code is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
*/
//////////////////////////////////////////////
// RemoteXY include library //
//////////////////////////////////////////////
// RemoteXY select connection mode and include library
#define REMOTEXY_MODE__SOFTSERIAL
#include <SoftwareSerial.h>
#include <RemoteXY.h>
// RemoteXY connection settings
#define REMOTEXY_SERIAL_RX D1
#define REMOTEXY_SERIAL_TX D2
#define REMOTEXY_SERIAL_SPEED 9600
// RemoteXY configurate
#pragma pack(push, 1)
uint8_t RemoteXY_CONF[] =
{ 255,0,0,41,0,89,0,8,13,1,
67,4,26,14,32,10,2,26,11,67,
4,27,60,28,8,2,26,11,129,0,
5,59,18,9,17,230,186,188,229,186,
166,0,129,0,7,15,16,8,17,230,
186,171,229,186,166,0,129,0,2,3,
19,7,17,231,167,146,230,149,184,61,
0,67,4,22,3,27,6,2,26,11,
68,17,4,25,56,25,8,36,68,17,
4,70,56,25,8,36 };
// this structure defines all the variables of your control interface
struct {
// output variable
char text_1[11]; // string UTF8 end zero
char text_2[11]; // string UTF8 end zero
char text_3[11]; // string UTF8 end zero
float onlineGraph_1;
float onlineGraph_2;
// other variable
uint8_t connect_flag; // =1 if wire connected, else =0
} RemoteXY;
#pragma pack(pop)
/////////////////////////////////////////////
// END RemoteXY include //
/////////////////////////////////////////////
#include <ESP8266WiFi.h>
#include "esp_NTPClient.h"
#include <WiFiUdp.h>
#include "esp_DHTesp.h"
#include <WiFiClientSecure.h>
int NU;
float HUM;
float TEMP;
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP);
DHTesp dht;
void read_data() {
TEMP = dht.getTemperature();
HUM = dht.getHumidity();
Serial.println((String("TEMP:") + String(TEMP)));
Serial.println((String("humidity:") + String(HUM)));
}
WiFiClientSecure Secure_client;
void google() {
if (Secure_client.connect("docs.google.com", 443)) {
Secure_client.println("GET /forms/d/e/1FAIpQLSeLWa_PI8hEiTb-GeVonYqT958ImtdnQDJmW8KCfP3J1zdiSA/formResponse?entry.235066965="+ String(timeClient.getFormattedTime())+"&entry.2029008000="+ String(TEMP)+"&entry.1165699370="+ String(HUM)+"&submit=Submit HTTP/1.1");
Secure_client.println("Host: docs.google.com");
Secure_client.println("Connection: close");
Secure_client.println();
Secure_client.stop();
}
}
void LINE() {
if (Secure_client.connect("notify-api.line.me",443)) {
String data = "message="+String((String("時間:") + String(timeClient.getFormattedTime()) + String(" 溫度:") + String(TEMP) + String(" 濕度:") + String(HUM)));
Secure_client.println("POST /api/notify HTTP/1.1");
Secure_client.println("Host: notify-api.line.me");
Secure_client.println("Authorization: Bearer uGBowGLFtrQ9BbBNukTDEJ9GCFkGgOcqg29koYnOcHd");
Secure_client.println("Content-Type: application/x-www-form-urlencoded");
Secure_client.print("Content-Length: ");
Secure_client.println(data.length());
Secure_client.println();
Secure_client.println(data);
Secure_client.stop();
}
}
WiFiClient client;
void Thingspeak() {
if(client.connect("api.thingspeak.com", 80)) {
String things_request = "GET /update?api_key=2SSH0JIHXNOK63H6&field1="+ String(TEMP)+"&field2="+ String(HUM)+"\r\n\r\n";
client.print(things_request);
client.stop();
}
}
void IFTTT() {
if (client.connect("maker.ifttt.com", 80)) {
String data = "\r\n{\"value1\":\""+ String((String("現在時間:") + String(timeClient.getFormattedTime())))+"\",\"value2\":\""+String((String("現在溫度:") + String(TEMP)))+"\",\"value3\":\""+String((String("現在濕度:") + String(HUM)))+"\"}";
client.println("POST /trigger/test_ESP8266/with/key/N5z6afVE7IWy5_DkQBbaH HTTP/1.1");
client.println("Host: maker.ifttt.com");
client.println("User-Agent: Esp8266");
client.println("Accept: */*");
client.print("Content-Length: ");
client.println(data.length());
client.println("Content-Type: application/json");
client.println("Connection: close");
client.println(data);
}
}
void setup()
{
RemoteXY_Init ();
// TODO you setup code
Serial.begin(9600);
timeClient.begin();
timeClient.setTimeOffset(28800);
NU = 0;
HUM = 0;
TEMP = 0;
WiFi.begin("Free","ruby3217");
while(WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println(WiFi.localIP());
pinMode(2, INPUT);
dht.setup(2);
}
void loop()
{
RemoteXY_Handler ();
// TODO you loop code
// use the RemoteXY structure for data transfer
timeClient.update();
read_data();
NU = NU + 5; //計算秒數
dtostrf(TEMP, 0, 0, RemoteXY.text_1);
dtostrf(HUM, 0, 0, RemoteXY.text_2);
dtostrf(NU, 0, 0, RemoteXY.text_3);
RemoteXY.onlineGraph_1 = TEMP;
RemoteXY.onlineGraph_2 = HUM;
if(NU % 300 == 0){google();} //每5分鐘上傳資料到google表單
if(NU % 3600 == 0){LINE();} //每小時上傳資料到LINE
if(NU % 600 == 0){Thingspeak();} //每10分鐘上傳資料到Thingspeak
if(NU % 7200 == 0){IFTTT();} //每2小時上傳資料到IFTTT
if(NU==86400){NU = 0 ;} //一天後秒數歸零
delay(5000); //暫停5秒
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment