Skip to content

Commit 444d813

Browse files
authored
Merge pull request #62 from PRO2XY/esp32_port
Esp32 port
2 parents 87a417d + 551271e commit 444d813

File tree

10 files changed

+403
-15
lines changed

10 files changed

+403
-15
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ env:
2020
- SCRIPT=platformioSingle EXAMPLE_NAME=PhotoFromFileID EXAMPLE_FOLDER=/SendPhoto/ BOARDTYPE=ESP8266 BOARD=d1_mini
2121
- SCRIPT=platformioSingle EXAMPLE_NAME=Location EXAMPLE_FOLDER=/ BOARDTYPE=ESP8266 BOARD=d1_mini
2222
- SCRIPT=platformioSingle EXAMPLE_NAME=ChannelPost EXAMPLE_FOLDER=/ BOARDTYPE=ESP8266 BOARD=d1_mini
23+
- SCRIPT=platformio PLATFORMIO_CI_SRC=src EXAMPLE_NAME=AdvancedLED_platformio EXAMPLE_FOLDER=/ BOARDTYPE=ESP32 BOARD=esp32dev
2324

2425

2526
install:
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.pioenvs
2+
.piolibdeps
3+
.clang_complete
4+
.gcc-flags.json
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Continuous Integration (CI) is the practice, in software
2+
# engineering, of merging all developer working copies with a shared mainline
3+
# several times a day < http://docs.platformio.org/page/ci/index.html >
4+
#
5+
# Documentation:
6+
#
7+
# * Travis CI Embedded Builds with PlatformIO
8+
# < https://docs.travis-ci.com/user/integration/platformio/ >
9+
#
10+
# * PlatformIO integration with Travis CI
11+
# < http://docs.platformio.org/page/ci/travis.html >
12+
#
13+
# * User Guide for `platformio ci` command
14+
# < http://docs.platformio.org/page/userguide/cmd_ci.html >
15+
#
16+
#
17+
# Please choice one of the following templates (proposed below) and uncomment
18+
# it (remove "# " before each line) or use own configuration according to the
19+
# Travis CI documentation (see above).
20+
#
21+
22+
23+
#
24+
# Template #1: General project. Test it using existing `platformio.ini`.
25+
#
26+
27+
# language: python
28+
# python:
29+
# - "2.7"
30+
#
31+
# install:
32+
# - pip install -U platformio
33+
#
34+
# script:
35+
# - platformio run
36+
37+
38+
#
39+
# Template #2: The project is intended to by used as a library with examples
40+
#
41+
42+
# language: python
43+
# python:
44+
# - "2.7"
45+
#
46+
# env:
47+
# - PLATFORMIO_CI_SRC=path/to/test/file.c
48+
# - PLATFORMIO_CI_SRC=examples/file.ino
49+
# - PLATFORMIO_CI_SRC=path/to/test/directory
50+
#
51+
# install:
52+
# - pip install -U platformio
53+
#
54+
# script:
55+
# - platformio ci --lib="." --board=ID_1 --board=ID_2 --board=ID_N
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
## ESP32 - AdvancedLED PlatformIO
2+
PlatformIO example for controlling an LED from a Telegram Bot.
3+
This example sets up an ESP32 to be controlled through a Telegram Bot.
4+
As of January 3rd, 2018, the platform (in platformio.ini) must be set to espressif32_stage.
5+
6+
#### Pre-requisites
7+
* You must have a Telegram Bot for this example to work. To make one,
8+
1. Open Telegram (on mobile, web, or desktop)
9+
2. Start a chat with BotFather (@BotFather)
10+
3. Send `/start` to BotFather, followed by `/newbot`
11+
4. Send a friendly name for your bot (this isn't the username of bot)
12+
5. Type in and send the username for your bot (ending in bot)
13+
6. Copy the token provided by BotFather and paste it at BOTtoken below
14+
* Modify `ssid` and `password` to match your WiFi network settings
15+
* Modify `BOTtoken` with the correct bot token acquired from Botfather
16+
* Modify `ledPin` to match the GPIO pin on which the LED on your board is connected
17+
18+
#### Available commands for the bot:
19+
* /start - Displays an intro message in chat and provides a custom keyboard with available commands
20+
* /options - Displays a custom keyboard with available commands
21+
* /ledon - Turns on the LED specified by `ledPin`
22+
* /ledoff - Turns off the LED specified by `ledPin`
23+
* /blink - Starts blinking the LED specified by `ledPin`
24+
* /PWM - Provides an inline keyboard for setting a PWM value for the LED specified by `ledPin`
25+
26+
#### Notes
27+
* Clicking on a button on an inline keyboard sends back a message of type="callback_query".
28+
* Blink is implemented using low frequency PWM, as fetching message updates can take enough time to cause a noticeable lag in blinking period.
29+
* LED implementation is based on inverse polarity of LED.
30+
* Telegram Bot API documentation available at https://core.telegram.org/bots/api
31+
32+
## Credit
33+
Application originally written by [Giancarlo Bacchio]([email protected]) for [ESP8266-TelegramBot library](https://github.com/Gianbacchio/ESP8266-TelegramBot)
34+
Adapted by [Brian Lough](https://github.com/witnessmenow) for UniversalTelegramBot Library
35+
Adapted for ESP32, and modified for advance features by [Pranav Sharma](https://github.com/pro2xy)
36+
37+
## License
38+
39+
You may copy, distribute and modify the software provided that modifications are described and licensed for free under [LGPL-3](http://www.gnu.org/licenses/lgpl-3.0.html). Derivatives works (including modifications or anything statically linked to the library) can only be redistributed under [LGPL-3](http://www.gnu.org/licenses/lgpl-3.0.html), but applications that use the library don't have to be.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
; PlatformIO Project Configuration File
2+
;
3+
; Build options: build flags, source filter
4+
; Upload options: custom upload port, speed and extra flags
5+
; Library options: dependencies, extra library storages
6+
; Advanced options: extra scripting
7+
;
8+
; Please visit documentation for the other options and examples
9+
; http://docs.platformio.org/page/projectconf.html
10+
[common]
11+
lib_deps_external = ArduinoJson
12+
13+
[env:esp32dev]
14+
platform = espressif32_stage
15+
board = esp32dev
16+
framework = arduino
17+
lib_deps = ${common.lib_deps_external}
Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
/*******************************************************************
2+
Example for controlling an LED from a Telegram Bot
3+
4+
You must have a Telegram Bot for this example to work. To make one,
5+
1. Open Telegram (on mobile, web, or desktop)
6+
2. Start a chat with BotFather (@BotFather)
7+
3. Send /start to BotFather, followed by /newbot
8+
4. Send a friendly name for your bot (this isn't the username of bot)
9+
5. Type in and send the username for your bot (ending in bot)
10+
6. Copy the token provided by BotFather and paste it at BOTtoken below
11+
12+
Telegram Bot API documentation available at https://core.telegram.org/bots/api
13+
14+
Note: As of 3rd Jan. 2018, it is necessary to use espressif32_stage
15+
platform for PlatformIO
16+
17+
written by Giacarlo Bacchio (Gianbacchio on Github)
18+
adapted by Brian Lough ( witnessmenow ) for UniversalTelegramBot library
19+
adapted by Pranav Sharma ( PRO2XY ) for ESP32 on PlatformIO
20+
Library related discussions on https://t.me/arduino_telegram_library
21+
22+
*******************************************************************/
23+
#include <Arduino.h>
24+
#include <WiFi.h>
25+
#include <WiFiClientSecure.h>
26+
#include <UniversalTelegramBot.h>
27+
28+
29+
// WiFi parameters
30+
const char *ssid = "SSID"; // your network SSID (name)
31+
const char *password = "PASS"; // your network key
32+
33+
// Bot parameters
34+
#define BOTtoken "xxxxxxxxxx:xxxxxxxxxxxxxxxxxxxxxxxxxx" // your Bot Token (Get from Botfather)
35+
int Bot_mtbs = 1000; //mean time between scan messages
36+
long Bot_lasttime; //last time messages' scan has been done
37+
38+
// LED parameters
39+
const int ledPin = 16; // Internal Red LED on Karyni (2 on NodeMCU-32S)
40+
enum ledModes {ON = 0, OFF, BLINK, PWM}; // define modes of operation for LED (inverse polarity)
41+
enum ledModes ledmode = OFF; // Start with off
42+
43+
WiFiClientSecure client;
44+
UniversalTelegramBot bot(BOTtoken, client);
45+
46+
// Function prototypes (PlatformIO doesn't make these for you automatically)
47+
void handleNewMessages(int numNewMessages); // parses new messages and sends them to msgInterpretation
48+
void msgInterpretation(String from_name, String text, String chat_id, String message_type);
49+
50+
51+
void setup() {
52+
Serial.begin(115200);
53+
//bot._debug=true; // uncomment to see debug messages from bot library
54+
55+
// attempt to connect to Wifi network:
56+
Serial.print("Connecting Wifi: ");
57+
Serial.println(ssid);
58+
// Start WiFi in Station mode
59+
WiFi.mode(WIFI_STA);
60+
WiFi.begin(ssid, password);
61+
62+
// Wait for WiFi to connect
63+
while (WiFi.status() != WL_CONNECTED) {
64+
Serial.print('.');
65+
delay(500);
66+
}
67+
68+
Serial.println("\r\nConnected!");
69+
Serial.print("IP address: ");
70+
Serial.println(WiFi.localIP());
71+
72+
pinMode(ledPin, OUTPUT); // initialize ledPin as an output.
73+
digitalWrite(ledPin, HIGH); // initialize pin as high (LED Off)
74+
}
75+
76+
void loop() {
77+
// Every "Bot_mtbs" the bot checks if any messages have arrived
78+
if (millis() > Bot_lasttime + Bot_mtbs) {
79+
Bot_lasttime = millis();
80+
Serial.print(F("Checking for messages.. "));
81+
int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
82+
Serial.print(numNewMessages); Serial.println(" new messages");
83+
84+
if(numNewMessages > 0){
85+
handleNewMessages(numNewMessages);
86+
}
87+
}
88+
}
89+
90+
// Parse new messages and send them for interpretation
91+
void handleNewMessages(int numNewMessages) {
92+
// Extract info from the message
93+
for (int i = 0; i < numNewMessages; i++) {
94+
Serial.print(F("Handling message ")); Serial.println(i+1);
95+
String chat_id = String(bot.messages[i].chat_id);
96+
String text = bot.messages[i].text;
97+
98+
String from_name = bot.messages[i].from_name;
99+
if (from_name == "") from_name = "Guest";
100+
// Call the function for understand the message
101+
msgInterpretation(from_name, text, chat_id, bot.messages[i].type);
102+
}
103+
}
104+
105+
void msgInterpretation(String from_name, String text, String chat_id, String message_type) {
106+
Serial.print(F("Interpreting message: ")); Serial.println(text);
107+
Serial.print(F("Type: ")); Serial.println(message_type);
108+
Serial.print(F("From: ")); Serial.println(from_name);
109+
110+
if (text == "/start") { // First interaction of user
111+
String welcome = "Hi " + from_name + "!\n";
112+
welcome += "I am your Telegram Bot running on ESP32.\n\n";
113+
welcome += "Select one of the /options below!\n\n";
114+
String keyboardJson = "[[\"/ledon\", \"/ledoff\"],[\"/blink\", \"/PWM\"],[\"/status\"]]";
115+
bot.sendMessageWithReplyKeyboard(chat_id, welcome, "Markdown", keyboardJson, true);
116+
}
117+
118+
if (text == "/options") { // List out the custom keyboard
119+
String keyboardJson = "[[\"/ledon\", \"/ledoff\"],[\"/blink\", \"/PWM\"],[\"/status\"]]";
120+
bot.sendMessageWithReplyKeyboard(chat_id, "Choose from one of the following options", "", keyboardJson, true);
121+
}
122+
123+
if (text == "/status") { // Report present ledmode to user
124+
String response = "LED is ";
125+
switch (ledmode){
126+
case ON:
127+
response += "on";
128+
break;
129+
case OFF:
130+
response += "off";
131+
break;
132+
case BLINK:
133+
response += "blinking";
134+
break;
135+
case PWM:
136+
response += "PWMing";
137+
break;
138+
}
139+
bot.sendMessage(chat_id, response, "");
140+
}
141+
142+
if (text == "/ledon") {
143+
ledmode = ON;
144+
ledcDetachPin(ledPin); // detach pin from ledc
145+
digitalWrite(ledPin, LOW); // turn the LED on (drive pin LOW)
146+
Serial.println(F("Turning LED on"));
147+
bot.sendMessage(chat_id, "Turning LED on", "");
148+
}
149+
150+
if (text == "/ledoff") {
151+
ledmode = OFF;
152+
ledcDetachPin(ledPin); // detach pin from ledc
153+
digitalWrite(ledPin, HIGH); // turn the LED off (drive pin HIGH)
154+
Serial.println(F("Turning LED off"));
155+
bot.sendMessage(chat_id, "Turning LED off", "");
156+
}
157+
158+
if (text == "/blink") {
159+
ledmode = BLINK;
160+
// We use ledc for blinking by setting PWM at a low frequency
161+
ledcSetup(0, 1, 8); // Channel, Freq., Resolution
162+
ledcAttachPin(ledPin, 0); // Pin, Channel
163+
ledcWrite(0, 192); // Channel, Duty (stay off for 90% time) (remember inverse polarity!)
164+
Serial.println(F("Blink set"));
165+
bot.sendMessage(chat_id, "Blink set", "");
166+
}
167+
168+
if (text == "/PWM") { // Send an inline keyboard for seleting PWM values (percentage)
169+
// For inline keyboard markup, see https://core.telegram.org/bots/api#inlinekeyboardmarkup
170+
String keyboardJson = "[[{ \"text\" : \"0\", \"callback_data\" : \"0\" }],";
171+
keyboardJson += "[{ \"text\" : \"10\", \"callback_data\" : \"10\" },";
172+
keyboardJson += "{ \"text\" : \"20\", \"callback_data\" : \"20\" },";
173+
keyboardJson += "{ \"text\" : \"30\", \"callback_data\" : \"30\" }],";
174+
keyboardJson += "[{ \"text\" : \"40\", \"callback_data\" : \"40\" },";
175+
keyboardJson += "{ \"text\" : \"50\", \"callback_data\" : \"50\" },";
176+
keyboardJson += "{ \"text\" : \"60\", \"callback_data\" : \"60\" }],";
177+
keyboardJson += "[{ \"text\" : \"70\", \"callback_data\" : \"70\" },";
178+
keyboardJson += "{ \"text\" : \"80\", \"callback_data\" : \"80\" },";
179+
keyboardJson += "{ \"text\" : \"90\", \"callback_data\" : \"90\" }],";
180+
keyboardJson += "[{ \"text\" : \"100\", \"callback_data\" : \"100\" }]]";
181+
Serial.println(F("Sending PWM keyboard"));
182+
bot.sendMessageWithInlineKeyboard(chat_id, "Set PWM level", "", keyboardJson);
183+
}
184+
185+
if(message_type=="callback_query") { // Received when user taps a button on inline keyboard
186+
// In our case, callback_query is only received for PWM values. In other cases you may
187+
// want to append an identifier to the values sent in 'callback_data' (e.g. 'duty=10')
188+
// and then check for it here using text.startsWith("duty=") or something similar.
189+
ledmode = PWM; // set proper LED mode
190+
uint8_t duty = text.toInt(); // Convert value to int
191+
duty = duty*2.55; // For duty between 0 - 100%, actual duty would be between 0 - 255
192+
193+
// We use ledc for PWM
194+
ledcSetup(0, 5000, 8); // Channel, Freq., Resolution
195+
ledcAttachPin(ledPin, 0); // Pin, Channel
196+
ledcWrite(0, 255-duty); // Channel, Duty (255 - x to inverse polarity)
197+
Serial.println(F("PWM set"));
198+
String message = "PWM set with duty ";
199+
message += String(duty);
200+
bot.sendMessage(chat_id, message, "");
201+
}
202+
}

platformio.ini

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,9 @@ platform = espressif8266
1515
board = d1_mini
1616
framework = arduino
1717
lib_deps = WifiManager, ${common.lib_deps_external}
18+
19+
[env:esp32dev]
20+
platform = espressif32_stage
21+
board = esp32dev
22+
framework = arduino
23+
lib_deps = WifiManager, ${common.lib_deps_external}

scripts/travis/platformio.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/sh -eux
2+
3+
platformio ci $PWD/examples/$BOARDTYPE$EXAMPLE_FOLDER$EXAMPLE_NAME/ -b $BOARD --lib="."

0 commit comments

Comments
 (0)