Здравствуйте, есть код который работает
Как добавить в него етот код что бы работал ( код для внесения данных в гугл таблицу)
А то если добавляю то что-то не получается))
Заранее благодарен!
C++:
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <SimpleDHT.h>
#define ONE_WIRE_BUS 15
//набор переменных для подсчета uptime
long secs, mins;
int hours, days;
int pinDHT11 = 13;
SimpleDHT11 dht11;
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
MDNSResponder mdns;
// Wi-Fi
const char* ssid = "";
const char* password = "";
byte arduino_mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
IPAddress ip(192,168,4,35);
IPAddress gateway(192,168,4,1);
IPAddress subnet(255,255,255,0);
ESP8266WebServer server(80);
int D0_pin = 16;
int D2_pin = 2;//d4
int D1_pin = 5;
void setup(void){
// preparing GPIOs
pinMode(D0_pin, OUTPUT);
digitalWrite(D0_pin, LOW);
pinMode(D2_pin, OUTPUT);
digitalWrite(D2_pin, LOW);
pinMode(D1_pin, OUTPUT);
digitalWrite(D1_pin, LOW);
sensors.begin();
delay(100);
Serial.begin(115200);
WiFi.begin(ssid, password);
WiFi.config(ip, gateway, subnet);
Serial.println("");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
if (mdns.begin("esp8266", WiFi.localIP())) {
Serial.println("MDNS responder started");
}
//+++++++++++++++++++++++ START LED-1 ++++++++++++++++++++
server.on("/", [](){
server.send(200, "text/html", webPage());
});
server.on("/socket1On", [](){
digitalWrite(D0_pin, HIGH);
server.send(200, "text/html", webPage());
delay(100);
});
server.on("/socket1Off", [](){
digitalWrite(D0_pin, LOW);
server.send(200, "text/html", webPage());
delay(100);
});
//+++++++++++++++++++++++ END LED-1 ++++++++++++++++++++
//+++++++++++++++++++++++ START LED-2 ++++++++++++++++++++
server.on("/socket2On", [](){
digitalWrite(D2_pin, HIGH);
server.send(200, "text/html", webPage());
delay(100);
});
server.on("/socket2Off", [](){
digitalWrite(D2_pin, LOW);
server.send(200, "text/html", webPage());
delay(100);
});
// +++++++++++++++++++++++ END LED-2 ++++++++++++++++++++
//+++++++++++++++++++++++ START LED-3 ++++++++++++++++++++
server.on("/socket3On", [](){
digitalWrite(D1_pin, HIGH);
server.send(200, "text/html", webPage());
delay(100);
});
server.on("/socket3Off", [](){
digitalWrite(D1_pin, LOW);
server.send(200, "text/html", webPage());
delay(100);
});
// +++++++++++++++++++++++ END LED-3 ++++++++++++++++++++
server.begin();
Serial.println("HTTP server started");
}
void loop(void){
server.handleClient();
//########### Пересчет millis для uptime ###############
secs = millis() / 1000;
mins = secs / 60;
hours = mins / 60;
days = hours / 24;
secs -= mins * 60;
mins -= hours * 60;
hours -= days * 24;
//######################################################
}
String webPage()
{
sensors.requestTemperatures();
byte temperature = 0;
byte humidity = 0;
dht11.read(pinDHT11, &temperature, &humidity, NULL);
String web;
web += "<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"/> <meta charset=\"utf-8\"><title>ESP 8266</title><style>button{color:red;padding: 10px 27px;}</style></head>";
web += "<h1 style=\"text-align: center;font-family: Open sans;font-weight: 100;font-size: 20px;\">ESP8266 Web Server</h1><div>";
web += "<p style=\"text-align: left;margin-top: 0px;margin-bottom: 5px;\">UPTIME: " + String(days) + " дней " + String(hours) + " час. " + String(mins) + " мин. " + String(secs) + " сек. </p>";
//++++++++++ LED-1 +++++++++++++
web += "<p style=\"text-align: center;margin-top: 0px;margin-bottom: 5px;\">----LED 1----</p>";
if (digitalRead(D0_pin) == 1)
{
web += "<div style=\"text-align: center;width: 98px;color:white ;padding: 10px 30px;background-color: #43a209;margin: 0 auto;\">ON</div>";
}
else
{
web += "<div style=\"text-align: center;width: 98px;color:white ;padding: 10px 30px;background-color: #ec1212;margin: 0 auto;\">OFF</div>";
}
web += "<div style=\"text-align: center;margin: 5px 0px;\"> <a href=\"socket1On\"><button>ON</button></a> <a href=\"socket1Off\"><button>OFF</button></a></div>";
// ++++++++ LED-1 +++++++++++++
//++++++++++ LED-2 +++++++++++++
web += "<p style=\"text-align: center;margin-top: 0px;margin-bottom: 5px;\">----LED 2----</p>";
if (digitalRead(D2_pin) == 1)
{
web += "<div style=\"text-align: center;width: 98px;color:white ;padding: 10px 30px;background-color: #43a209;margin: 0 auto;\">ON</div>";
}
else
{
web += "<div style=\"text-align: center;width: 98px;color:white ;padding: 10px 30px;background-color: #ec1212;margin: 0 auto;\">OFF</div>";
}
web += "<div style=\"text-align: center;margin: 5px 0px;\"> <a href=\"socket2On\"><button>ON</button></a> <a href=\"socket2Off\"><button>OFF</button></a></div>";
// ++++++++ LED-2 +++++++++++++
//++++++++++ LED-3 +++++++++++++
web += "<p style=\"text-align: center;margin-top: 0px;margin-bottom: 5px;\">----LED 3----</p>";
if (digitalRead(D1_pin) == 1)
{
web += "<div style=\"text-align: center;width: 98px;color:white ;padding: 10px 30px;background-color: #43a209;margin: 0 auto;\">ON</div>";
}
else
{
web += "<div style=\"text-align: center;width: 98px;color:white ;padding: 10px 30px;background-color: #ec1212;margin: 0 auto;\">OFF</div>";
}
web += "<div style=\"text-align: center;margin: 5px 0px;\"> <a href=\"socket3On\"><button>ON</button></a> <a href=\"socket3Off\"><button>OFF</button></a></div>";
// ++++++++ LED-3 +++++++++++++
//++++++++++ DS18B20 TEMP +++++++++++++
web += "<p style=\"text-align: center;margin-top: 0px;margin-bottom: 5px;\">----DS18B20 TEMP----</p>";
web += "<div style=\"text-align: center;width: 98px;color:white ;padding: 10px 30px;background-color: #5191e4;margin: 0 auto;\">"+ String(sensors.getTempCByIndex(0))+"</div>";
// ++++++++ DS18B20 TEMP +++++++++++++
//++++++++++ DHT11 TEMP +++++++++++++
web += "<p style=\"text-align: center;margin-top: 0px;margin-bottom: 5px;\">----DHT11 TEMP----</p>";
web += "<div style=\"text-align: center;width: 98px;color:white ;padding: 10px 30px;background-color: #5191e4;margin: 0 auto;\">"+ String((int)temperature)+"</div>";
// ++++++++ DHT11 TEMP +++++++++++++
//++++++++++ DHT11 H +++++++++++++
web += "<p style=\"text-align: center;margin-top: 0px;margin-bottom: 5px;\">----DHT11 H----</p>";
web += "<div style=\"text-align: center;width: 98px;color:white ;padding: 10px 30px;background-color: #5191e4;margin: 0 auto;\">"+ String((int)humidity)+"</div>";
// ++++++++ DHT11 H +++++++++++++
// ========REFRESH=============
web += "<div style=\"text-align:center;margin-top: 20px;\"><a href=\"/\"><button style=\"width:158px;\">REFRESH</button></a></div>";
// ========REFRESH=============
web += "</div>";
return(web);
}
Как добавить в него етот код что бы работал ( код для внесения данных в гугл таблицу)
C++:
//----------------------------------------Include the NodeMCU ESP8266 Library
//----------------------------------------see here: https://www.youtube.com/watch?v=8jMr94B8iN0 to add NodeMCU ESP12E ESP8266 library and board (ESP8266 Core SDK)
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
//----------------------------------------
//----------------------------------------Include the DHT Library
#include "DHT.h"
//----------------------------------------
#define DHTTYPE DHT11 //--> Defines the type of DHT sensor used (DHT11, DHT21, and DHT22), in this project the sensor used is DHT11.
const int DHTPin = 5; //--> The pin used for the DHT11 sensor is Pin D1 = GPIO5
DHT dht(DHTPin, DHTTYPE); //--> Initialize DHT sensor, DHT dht(Pin_used, Type_of_DHT_Sensor);
#define ON_Board_LED 2 //--> Defining an On Board LED, used for indicators when the process of connecting to a wifi router
//----------------------------------------SSID and Password of your WiFi router.
const char* ssid = ""; //--> Your wifi name or SSID.
const char* password = ""; //--> Your wifi password.
//----------------------------------------
//----------------------------------------Host & httpsPort
const char* host = "script.google.com";
const int httpsPort = 443;
//----------------------------------------
WiFiClientSecure client; //--> Create a WiFiClientSecure object.
String GAS_ID = ""; //--> spreadsheet script ID
//============================================================================== void setup
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
delay(500);
dht.begin(); //--> Start reading DHT11 sensors
delay(500);
WiFi.begin(ssid, password); //--> Connect to your WiFi router
Serial.println("");
pinMode(ON_Board_LED,OUTPUT); //--> On Board LED port Direction output
digitalWrite(ON_Board_LED, HIGH); //--> Turn off Led On Board
//----------------------------------------Wait for connection
Serial.print("Connecting");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
//----------------------------------------Make the On Board Flashing LED on the process of connecting to the wifi router.
digitalWrite(ON_Board_LED, LOW);
delay(250);
digitalWrite(ON_Board_LED, HIGH);
delay(250);
//----------------------------------------
}
//----------------------------------------
digitalWrite(ON_Board_LED, HIGH); //--> Turn off the On Board LED when it is connected to the wifi router.
//----------------------------------------If successfully connected to the wifi router, the IP Address that will be visited is displayed in the serial monitor
Serial.println("");
Serial.print("Successfully connected to : ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
Serial.println();
//----------------------------------------
client.setInsecure();
}
//==============================================================================
//============================================================================== void loop
void loop() {
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
int h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor !");
delay(500);
return;
}
String Temp = "Temperature : " + String(t) + " °C";
String Humi = "Humidity : " + String(h) + " %";
Serial.println(Temp);
Serial.println(Humi);
sendData(t, h); //--> Calls the sendData Subroutine
}
//==============================================================================
//============================================================================== void sendData
// Subroutine for sending data to Google Sheets
void sendData(float tem, int hum) {
Serial.println("==========");
Serial.print("connecting to ");
Serial.println(host);
//----------------------------------------Connect to Google host
if (!client.connect(host, httpsPort)) {
Serial.println("connection failed");
return;
}
//----------------------------------------
//----------------------------------------Processing data and sending data
String string_temperature = String(tem);
// String string_temperature = String(tem, DEC);
String string_humidity = String(hum, DEC);
String url = "/macros/s/" + GAS_ID + "/exec?temperature=" + string_temperature + "&humidity=" + string_humidity;
Serial.print("requesting URL: ");
Serial.println(url);
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"User-Agent: BuildFailureDetectorESP8266\r\n" +
"Connection: close\r\n\r\n");
Serial.println("request sent");
//----------------------------------------
//----------------------------------------Checking whether the data was sent successfully or not
while (client.connected()) {
String line = client.readStringUntil('\n');
if (line == "\r") {
Serial.println("headers received");
break;
}
}
String line = client.readStringUntil('\n');
if (line.startsWith("{\"state\":\"success\"")) {
Serial.println("esp8266/Arduino CI successfull!");
} else {
Serial.println("esp8266/Arduino CI has failed");
}
Serial.print("reply was : ");
Serial.println(line);
Serial.println("closing connection");
Serial.println("==========");
Serial.println();
//----------------------------------------
}
//==============================================================================
Заранее благодарен!
Изменено: