Skip to content

Instantly share code, notes, and snippets.

@clive520
Created February 22, 2020 14:12
Show Gist options
  • Save clive520/71f504eeaecb18438bb39e31a5a420b1 to your computer and use it in GitHub Desktop.
Save clive520/71f504eeaecb18438bb39e31a5a420b1 to your computer and use it in GitHub Desktop.
用RemoteXY看溫度濕度亮度_加上圖表,利用Wifi上傳資料到1.Google 表單 2.LINE 3.Thinkspeak 4.IFTTT
/*
-- 網路連接 --
RemoteXY
Software Serial RX==>D1 TX==>D2 speed9600
tex_1 和 text_2
motoduino
D4=2 接DHT11
A0=17 接光敏電阻
上傳溫度和濕度資料到
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__ESP8266WIFI_LIB_POINT
#include <ESP8266WiFi.h>
#include <RemoteXY.h>
// RemoteXY connection settings
#define REMOTEXY_WIFI_SSID "RemoteXY"
#define REMOTEXY_WIFI_PASSWORD "12345678"
#define REMOTEXY_SERVER_PORT 6377
// RemoteXY configurate
#pragma pack(push, 1)
uint8_t RemoteXY_CONF[] =
{ 255,0,0,42,0,111,0,8,13,1,
67,4,26,7,32,10,2,26,11,67,
4,27,42,28,8,2,26,11,129,0,
7,42,18,9,17,230,186,188,229,186,
166,0,129,0,7,7,16,8,17,230,
186,171,229,186,166,0,129,0,2,1,
19,7,17,231,167,146,230,149,184,61,
0,67,4,22,1,27,6,2,26,11,
68,17,4,18,57,23,8,36,68,17,
4,51,56,25,8,36,66,131,20,80,
38,17,2,24,129,0,2,83,16,8,
17,228,186,174,229,186,166,0 };
// 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;
int8_t level_1; // =0..100 level position
// 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;
float LIGHT;
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP);
DHTesp dht;
void read_data() {
TEMP = dht.getTemperature();
HUM = dht.getHumidity();
LIGHT = 1024 - analogRead(17) ;
Serial.println((String("TEMP:") + String(TEMP)));
Serial.println((String("humidity:") + String(HUM)));
Serial.println((String("LIGHT:") + String(LIGHT)));
}
WiFiClientSecure Secure_client;
void google() {
if (Secure_client.connect("docs.google.com", 443)) {
Secure_client.println("GET /forms/d/e/1FAIpQLSds-XQIiJUJIAZb1UJjnTPnuzCVhIo3r5W4QKHaI2aWjGzYJg/formResponse?entry.810346006="+ String(timeClient.getFormattedTime())+"&entry.420407957="+ String(TEMP)+"&entry.525344760="+ String(HUM)+"&entry.2056052835="+ String(LIGHT)+"&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)+ String("亮度:") + String(LIGHT)));
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)+"&field3="+ String(LIGHT)+"\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) + String("現在濕度:") + String(HUM)))+"\",\"value3\":\""+String((String("現在亮度:") + String(LIGHT)))+"\"}";
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;
LIGHT = 0;
WiFi.begin("Free","ruby3217");
while(WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println(WiFi.localIP());
pinMode(2, INPUT);
pinMode(17, INPUT);
dht.setup(2);
}
void loop()
{
RemoteXY_Handler ();
// TODO you loop code
// use the RemoteXY structure for data transfer
timeClient.update();
NU = NU + 1; //計算秒數
if(NU % 10 == 0){read_data();}//每10秒讀一次資料
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;
RemoteXY.level_1 = (int)( LIGHT *100 / 1024 );
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(1000); //暫停1秒
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment