Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- #include <AsyncTelegram2.h>
- #include <PubSubClient.h>
- // Timezone definition
- #define MYTZ "CET-1CEST,M3.5.0,M10.5.0/3"
- #include <time.h>
- // Client "secure" passed to Telegram library
- BearSSL::WiFiClientSecure tg_client;
- BearSSL::Session session;
- BearSSL::X509List certificate(telegram_cert);
- AsyncTelegram2 myBot(tg_client);
- // Client "unsecure" passed to MQTT library
- WiFiClient espClient;
- PubSubClient mqtt_client(espClient);
- const char* ssid = "xxxxxxxx"; // SSID WiFi network
- const char* pass = "xxxxxxxx"; // Password WiFi network
- const char* token = "xxxxxxxxx:AAFLd-B-xxxxxxxxxxxxxxxxxxxxxxxxxx";
- const char* mqtt_server = "broker.mqtt-dashboard.com";
- #define MSG_BUFFER_SIZE (50)
- char msg[MSG_BUFFER_SIZE];
- int value = 0;
- void callback(char* topic, byte* payload, unsigned int length) {
- Serial.print("Message arrived [");
- Serial.print(topic);
- Serial.print("] ");
- for (int i = 0; i < length; i++) {
- Serial.print((char)payload[i]);
- }
- Serial.println();
- }
- void reconnect() {
- // Loop until we're reconnected
- while (!mqtt_client.connected()) {
- Serial.print("Attempting MQTT connection...");
- // Create a random client ID
- String clientId = "ESP8266Client-";
- clientId += String(random(0xffff), HEX);
- // Attempt to connect
- if (mqtt_client.connect(clientId.c_str())) {
- Serial.println("connected");
- // Once connected, publish an announcement...
- mqtt_client.publish("outTopic", "hello world");
- // ... and resubscribe
- mqtt_client.subscribe("inTopic");
- } else {
- Serial.print("failed, rc=");
- Serial.print(mqtt_client.state());
- Serial.println(" try again in 5 seconds");
- // Wait 5 seconds before retrying
- delay(5000);
- }
- }
- }
- void setup() {
- pinMode(LED_BUILTIN, OUTPUT);
- Serial.begin(115200);
- // connects to the access point
- WiFi.mode(WIFI_STA);
- WiFi.begin(ssid, pass);
- delay(500);
- while (WiFi.status() != WL_CONNECTED) {
- Serial.print('.');
- delay(500);
- }
- // Sync time with NTP, to check properly Telegram certificate
- configTime(MYTZ, "time.google.com", "time.windows.com", "pool.ntp.org");
- //Set certficate, session and some other base client properies
- tg_client.setSession(&session);
- tg_client.setTrustAnchors(&certificate);
- tg_client.setBufferSizes(TCP_MSS, TCP_MSS);
- // Set the Telegram bot properties
- myBot.setUpdateTime(2000);
- myBot.setTelegramToken(token);
- // Check if all things are ok
- Serial.print("\nTest Telegram connection... ");
- myBot.begin() ? Serial.println("OK") : Serial.println("NOK");
- Serial.print("Bot name: @");
- Serial.println(myBot.getBotName());
- randomSeed(micros());
- mqtt_client.setServer(mqtt_server, 1883);
- mqtt_client.setCallback(callback);
- }
- void loop() {
- if (!mqtt_client.connected()) {
- reconnect();
- }
- mqtt_client.loop();
- static unsigned long lastMsg = 0;
- if ( millis() - lastMsg > 10000) {
- lastMsg = millis();
- ++value;
- snprintf (msg, MSG_BUFFER_SIZE, "hello world #%ld", value);
- Serial.print("Publish message: ");
- Serial.println(msg);
- mqtt_client.publish("outTopic", msg);
- }
- // LED blink
- static uint32_t ledTime = millis();
- if (millis() - ledTime > 200) {
- ledTime = millis();
- digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
- }
- // local variable to store telegram message data
- TBMessage msg;
- // if there is an incoming message...
- if (myBot.getNewMessage(msg)) {
- char message[256];
- snprintf(message, 256, "Message from @%s:\n%s", myBot.getBotName(), msg.text.c_str());
- Serial.println(message);
- // echo the received message to the user
- myBot.sendMessage(msg, msg.text);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement