hi
in this code i want give data from google Sheet and send data in telegram bot,
i use this project to give data from google sheet: Google Sheet and Esp8266
and use this project for recive and send data from telegram robat: universal telegram bot
i use esp8266
i merge two above project but i cant compile because i give error , because two project use WiFiClientSecure.h library , and same client
this is my merge code:
#include <ESP8266WiFi.h>
#include <ArduinoJson.h>
#include "HTTPSRedirect.h"
//======================From Telegram===============================
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
#define BOTtoken "339475786:AAH7IjXu51xPom_sMx8IRnvQbcTD5FfO2Wc" // your Bot Token (Get from Botfather)
WiFiClientSecure client;
UniversalTelegramBot bot(BOTtoken, client);
int Bot_mtbs = 1000; //mean time between scan messages
long Bot_lasttime; //last time messages' scan has been done
bool Start = false;
const int ledPin = 2;
int ledStatus = 0;
void handleNewMessages(int numNewMessages) {
Serial.println("handleNewMessages");
Serial.println(String(numNewMessages));
for (int i=0; i<numNewMessages; i++) {
String chat_id = String(bot.messages[i].chat_id);
String text = bot.messages[i].text;
String from_name = bot.messages[i].from_name;
if (from_name == "") from_name = "Guest";
if (text == "/ledon") {
digitalWrite(ledPin, HIGH); // turn the LED on (HIGH is the voltage level)
ledStatus = 1;
bot.sendMessage(chat_id, "Led is ON", "");
}
if (text == "/ledoff") {
ledStatus = 0;
digitalWrite(ledPin, LOW); // turn the LED off (LOW is the voltage level)
bot.sendMessage(chat_id, "Led is OFF", "");
}
if (text == "/status") {
if(ledStatus){
bot.sendMessage(chat_id, "Led is ON", "");
} else {
bot.sendMessage(chat_id, "Led is OFF", "");
}
}
if (text == "/start") {
String welcome = "Welcome to Universal Arduino Telegram Bot library, " + from_name + ".\n";
welcome += "This is Flash Led Bot example.\n\n";
welcome += "/ledon : to switch the Led ON\n";
welcome += "/ledoff : to switch the Led OFF\n";
welcome += "/status : Returns current status of LED\n";
bot.sendMessage(chat_id, welcome, "Markdown");
}
}
}
//***************************************************************************
#define WRITE_INTERVAL_MINITES 1
#define MAX_CONNECT 200 //reuse 2 times same clent
#define MAX_ERROR 100 //restart everything
const char *ssid = "sajjad";
const char *pass = "1q2w3e4r5t6y";
const char* host = "script.google.com";
const int httpsPort = 443;
int seconds = (WRITE_INTERVAL_MINITES * 60) - 20; // schedule first sending to TS in 20 sec after start
char *GScriptId = "AKfycbxRzkMt87iyiyU9EDY6REoRDpl5C9oHdjVyCSF6A4QN7jkNMGHD"; // Put your GScriptId here
// Access to Google Spreadsheet
String url = String("/macros/s/") + GScriptId + "/exec?";
HTTPSRedirect* client = nullptr;
float hum;
float tem;
void setup() {
Serial.begin(115200);
Serial.println();
delay(200);
Serial.println("Starting ...");
delay(200);
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
if (WiFi.status() != WL_CONNECTED) WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.println(".");
}
Serial.println("");
Serial.println("WiFi connected");
pinMode(ledPin, OUTPUT); // initialize digital ledPin as an output.
delay(10);
digitalWrite(ledPin, LOW); // initialize pin as off
}
void loop() {
//==============================From telegram=========================================
if (millis() > Bot_lasttime + Bot_mtbs) {
int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
while(numNewMessages) {
Serial.println("got response");
handleNewMessages(numNewMessages);
numNewMessages = bot.getUpdates(bot.last_message_received + 1);
}
Bot_lasttime = millis();
}
//***********************************************************************
static int error_count = 0;
static int connect_count = 0;
static bool flag = false;
if (!flag){
client = new HTTPSRedirect(httpsPort);
flag = true;
client->setPrintResponseBody(true); // or false and use String getResponseBody();
client->setContentTypeHeader("application/json");
if (!client->connected()) client->connect(host, httpsPort);
++connect_count;
}
// 30 sec after writing
if (client != nullptr){
if (!client->connected()){
client->connect(host, httpsPort);
}
}
else{
Serial.println("Error2 creating client object!");
error_count = -10;
}
Serial.println("=========================");
if (client->GET(url + String("readrow=last"), host)){
DynamicJsonBuffer jsonBuffer;
JsonObject& json = jsonBuffer.parseObject(client->getResponseBody());
//json.printTo(Serial);
String Timestamp = json["values"][0];
int MaxT0 = json["values"][1];
int minT0 = json["values"][2];
String Con0 = json["values"][3];
int Hu0 = json["values"][4];
String WS0 = json["values"][5];
String WD0 = json["values"][6];
int MaxT1 = json["values"][7];
int minT1 = json["values"][8];
String Con1 = json["values"][9];
int Hu1 = json["values"][10];
String WS1 = json["values"][11];
String WD1 = json["values"][12];
int MaxT2 = json["values"][13];
int minT2 = json["values"][14];
String Con2 = json["values"][15];
int Hu2 = json["values"][16];
String WS2 = json["values"][17];
String WD2 = json["values"][18];
int MaxT3 = json["values"][19];
int minT3 = json["values"][20];
String Con3 = json["values"][21];
int Hu3 = json["values"][22];
String WS3 = json["values"][23];
String WD3 = json["values"][24];
float TEMP = json["values"][25];
float Hu = json["values"][26];
Serial.println(Timestamp);
Serial.println(MaxT0);
Serial.println(minT0);
Serial.println(Con0);
Serial.println(Hu0);
Serial.println(WS0);
Serial.println(WD0);
Serial.println("");
Serial.println(MaxT1);
Serial.println(minT1);
Serial.println(Con1);
Serial.println(Hu1);
Serial.println(WS1);
Serial.println(WD1);
Serial.println("");
Serial.println(MaxT2);
Serial.println(minT2);
Serial.println(Con2);
Serial.println(Hu2);
Serial.println(WS2);
Serial.println(WD2);
Serial.println("");
Serial.println(MaxT3);
Serial.println(minT3);
Serial.println(Con3);
Serial.println(Hu3);
Serial.println(WS3);
Serial.println(WD3);
Serial.println("");
Serial.println(TEMP);
Serial.println(Hu);
++connect_count;
} else {
++error_count;
Serial.println("GET Last row val failed!");
seconds = 10; // stage again in 10 sec
}
if (error_count > MAX_ERROR){
Serial.println("Errors > MAX_ERROR...");
//delete client;
//client = nullptr;
//connect_count = -1;
//error_count = 0;
//flag = false;
ESP.restart();
}
if (connect_count > MAX_CONNECT){
Serial.println("Connects > MAX_CONNECT... rebuild");
connect_count = -1;
flag = false;
delete client;
//return; //where?
}
seconds++;
delay(1000); //1 sec loop
}
and this is my error when i want compile program: compile error
excuse me
I could not write because the character limit
this is my error when i want to compile:
Googlesheet_DHT_simple:88: error: conflicting declaration 'HTTPSRedirect* client'
HTTPSRedirect* client = nullptr;
^
Googlesheet_DHT_simple:13: error: 'client' has a previous declaration as 'WiFiClientSecure client'
WiFiClientSecure client;
^
C:\Users\hossein\Videos\Googlesheet_DHT_simple-master\Googlesheet_DHT_simple\Googlesheet_DHT_simple.ino: In function 'void loop()':
Googlesheet_DHT_simple:139: error: no match for 'operator=' (operand types are 'WiFiClientSecure' and 'HTTPSRedirect*')
client = new HTTPSRedirect(httpsPort);
^
C:\Users\hossein\Videos\Googlesheet_DHT_simple-master\Googlesheet_DHT_simple\Googlesheet_DHT_simple.ino:139:12: note: candidate is:
In file included from C:\Users\hossein\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0\libraries\ESP8266WiFi\src/ESP8266WiFi.h:41:0,
from C:\Users\hossein\Videos\Googlesheet_DHT_simple-master\Googlesheet_DHT_simple\Googlesheet_DHT_simple.ino:1:
C:\Users\hossein\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0\libraries\ESP8266WiFi\src/WiFiClientSecure.h:36:21: note: WiFiClientSecure& WiFiClientSecure::operator=(const WiFiClientSecure&)
WiFiClientSecure& operator=(const WiFiClientSecure&);
^
C:\Users\hossein\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0\libraries\ESP8266WiFi\src/WiFiClientSecure.h:36:21: note: no known conversion for argument 1 from 'HTTPSRedirect*' to 'const WiFiClientSecure&'
Googlesheet_DHT_simple:141: error: base operand of '->' has non-pointer type 'WiFiClientSecure'
client->setPrintResponseBody(true); // or false and use String getResponseBody();
^
Googlesheet_DHT_simple:142: error: base operand of '->' has non-pointer type 'WiFiClientSecure'
client->setContentTypeHeader("application/json");
^
Googlesheet_DHT_simple:143: error: base operand of '->' has non-pointer type 'WiFiClientSecure'
if (!client->connected()) client->connect(host, httpsPort);
^
Googlesheet_DHT_simple:143: error: base operand of '->' has non-pointer type 'WiFiClientSecure'
if (!client->connected()) client->connect(host, httpsPort);
^
Googlesheet_DHT_simple:150: error: no match for 'operator!=' (operand types are 'WiFiClientSecure' and 'std::nullptr_t')
if (client != nullptr){
^
C:\Users\hossein\Videos\Googlesheet_DHT_simple-master\Googlesheet_DHT_simple\Googlesheet_DHT_simple.ino:150:16: note: candidates are:
In file included from C:\Users\hossein\Documents\Arduino\libraries\ArduinoJson\src/ArduinoJson.hpp:13:0,
from C:\Users\hossein\Documents\Arduino\libraries\ArduinoJson\src/ArduinoJson.h:10,
from C:\Users\hossein\Videos\Googlesheet_DHT_simple-master\Googlesheet_DHT_simple\Googlesheet_DHT_simple.ino:2:
C:\Users\hossein\Documents\Arduino\libraries\ArduinoJson\src/ArduinoJson/JsonVariantComparisons.hpp:27:13: note: template<class TVariant, class TComparand> bool ArduinoJson::operator!=(const ArduinoJson::JsonVariantBase<T>&, TComparand)
inline bool operator!=(const JsonVariantBase<TVariant> &variant,
^
C:\Users\hossein\Documents\Arduino\libraries\ArduinoJson\src/ArduinoJson/JsonVariantComparisons.hpp:27:13: note: template argument deduction/substitution failed:
C:\Users\hossein\Videos\Googlesheet_DHT_simple-master\Googlesheet_DHT_simple\Googlesheet_DHT_simple.ino:150:19: note: 'WiFiClientSecure' is not derived from 'const ArduinoJson::JsonVariantBase<T>'
if (client != nullptr){
^
In file included from C:\Users\hossein\Documents\Arduino\libraries\ArduinoJson\src/ArduinoJson.hpp:13:0,
from C:\Users\hossein\Documents\Arduino\libraries\ArduinoJson\src/ArduinoJson.h:10,
from C:\Users\hossein\Videos\Googlesheet_DHT_simple-master\Googlesheet_DHT_simple\Googlesheet_DHT_simple.ino:2:
C:\Users\hossein\Documents\Arduino\libraries\ArduinoJson\src/ArduinoJson/JsonVariantComparisons.hpp:36:1: note: template<class TVariant, class TComparand> typename ArduinoJson::TypeTraits::EnableIf<(! ArduinoJson::TypeTraits::IsVariant<TComparand>::value), bool>::type ArduinoJson::operator!=(TComparand, const ArduinoJson::JsonVariantBase<T>&)
operator!=(TComparand comparand, const JsonVariantBase<TVariant> &variant) {
^
C:\Users\hossein\Documents\Arduino\libraries\ArduinoJson\src/ArduinoJson/JsonVariantComparisons.hpp:36:1: note: template argument deduction/substitution failed:
C:\Users\hossein\Videos\Googlesheet_DHT_simple-master\Googlesheet_DHT_simple\Googlesheet_DHT_simple.ino:150:19: note: mismatched types 'const ArduinoJson::JsonVariantBase<T>' and 'std::nullptr_t'
if (client != nullptr){
^
Googlesheet_DHT_simple:151: error: base operand of '->' has non-pointer type 'WiFiClientSecure'
if (!client->connected()){
^
Googlesheet_DHT_simple:152: error: base operand of '->' has non-pointer type 'WiFiClientSecure'
client->connect(host, httpsPort);
^
Googlesheet_DHT_simple:161: error: base operand of '->' has non-pointer type 'WiFiClientSecure'
if (client->GET(url + String("readrow=last"), host)){
^
Googlesheet_DHT_simple:163: error: base operand of '->' has non-pointer type 'WiFiClientSecure'
JsonObject& json = jsonBuffer.parseObject(client->getResponseBody());
^
Googlesheet_DHT_simple:254: error: type 'class WiFiClientSecure' argument given to 'delete', expected pointer
delete client;
^
exit status 1
conflicting declaration 'HTTPSRedirect* client'
Do you REALLY think that println() doesn't know how to print an int without your resource-wasting, ham-fisted "help"?
Get a clue.
delete test;
When you delete the memory used by the instance, and don't set the pointer back to NULL, what do you suppose is going to happen the next time you test that the pointer is not NULL? What is going to happen when you dereference the pointer?
You need to add more Serial.print() statements to the code to figure out exactly which part of the code is triggering the exception.
i now use only below project without universal telegram bot , and change all client to test and work corectly but when merge whit telegram bot i give error
This way, we learn whether you are connected, and, if not, as is probably the case, what host you are trying to connect to, on what port.
I'm curious as to why the HTTPSRedirect constructor takes a port number AND the connect() method takes a port number. One would think that the class would only need to be told once what the port number was.
So, the problem is that you have a test object that may, or may not, be valid (though odds are that it is), and that the object throws an exception in the connect() method.
Since HTTPSRedirect derives from WiFiClientSecure, I don't understand why you need a HTTPSRedirect instance AND a WiFiClientSecure instance in the same code. Just create a HTTPSRedirect instance, and it will be able to do everything that the WiFiClientSecure instance can do, plus more stuff.
this is answer of my friend:
It looks like you have two WiFiClientSecure instances at the same time: one for telegram bot, another one for google sheets. That is not going to work, as single WiFiClientSecure instance needs around 25kB of heap (that is in current git version, and even more in release 2.3.0), and the total amount of heap at the start of the program is ~40kB. You need to refactor your program so it only uses one WiFiClientSecure at a time.
but how i do this?
in below is my compile information:
Sketch uses 303141 bytes (29%) of program storage space. Maximum is 1044464 bytes.
Global variables use 41360 bytes (50%) of dynamic memory, leaving 40560 bytes for local variables. Maximum is 81920 bytes
Suppose that you have a friend that has a pickup truck. Suppose that you have another friend that has a car.
Can the friend with the pickup truck substitute for the friend with the car? Of course, he or she can.
Can the friend with the car substitute for the friend with the truck? Not if you need to haul a load of cinder blocks.
The HTTPSRequest class is like the friend with the truck - can do everything a truck can do and can do everything a car can do (except pass gas stations). The WiFiClientSecure class is like the friend with the car - can transport you but NOT the load of cinder blocks.
Since you have a friend with a truck, why do you need to catch a ride with the friend with the car, while the truck hauls the cinder blocks?
PaulS:
Suppose that you have a friend that has a pickup truck. Suppose that you have another friend that has a car.
Can the friend with the pickup truck substitute for the friend with the car? Of course, he or she can.
Can the friend with the car substitute for the friend with the truck? Not if you need to haul a load of cinder blocks.
The HTTPSRequest class is like the friend with the truck - can do everything a truck can do and can do everything a car can do (except pass gas stations). The WiFiClientSecure class is like the friend with the car - can transport you but NOT the load of cinder blocks.
Since you have a friend with a truck, why do you need to catch a ride with the friend with the car, while the truck hauls the cinder blocks?