Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <FastLED.h>
- #include <LedControl.h>
- #define LED_PIN D4
- #define LED_COUNT 3
- #define SND_PIN D5
- #define BTN_PIN D6
- #include <ESP8266WiFi.h>
- #include <WiFiClientSecure.h>
- #include <UniversalTelegramBot.h>
- char ssid[] = "IOT";
- char password[] = "PASSWORD";
- #define TELEGRAM_BOT_TOKEN "979907373:AAENTK2PQkMTGPp2o6MTEI8PsEdM7uRco_c"
- WiFiClientSecure client;
- UniversalTelegramBot bot(TELEGRAM_BOT_TOKEN, client);
- int delayBetweenChecks = 1000;
- unsigned long lastTimeChecked; //last time messages' scan has been done
- #define DIN_PIN D3
- #define CLK_PIN D1
- #define CS_PIN D2
- int counter = 0;
- CRGB leds[LED_COUNT];
- int maxInUse = 1; //change this variable to set how many MAX7219's you'll use
- byte max7219_reg_noop = 0x00;
- byte max7219_reg_digit0 = 0x01;
- byte max7219_reg_digit1 = 0x02;
- byte max7219_reg_digit2 = 0x03;
- byte max7219_reg_digit3 = 0x04;
- byte max7219_reg_digit4 = 0x05;
- byte max7219_reg_digit5 = 0x06;
- byte max7219_reg_digit6 = 0x07;
- byte max7219_reg_digit7 = 0x08;
- byte max7219_reg_decodeMode = 0x09;
- byte max7219_reg_intensity = 0x0a;
- byte max7219_reg_scanLimit = 0x0b;
- byte max7219_reg_shutdown = 0x0c;
- byte max7219_reg_displayTest = 0x0f;
- LedControl display = LedControl(DIN_PIN, CLK_PIN, CS_PIN);
- void setup() {
- Serial.begin(115200);
- Serial.println("");
- pinMode(SND_PIN, OUTPUT);
- pinMode(DIN_PIN, OUTPUT);
- pinMode(CLK_PIN, OUTPUT);
- pinMode(CS_PIN, OUTPUT);
- initMax7219();
- FastLED.addLeds<NEOPIXEL, LED_PIN>(leds, LED_COUNT);
- leds[0] = CRGB::Red;
- leds[1] = CRGB::Red;
- leds[2] = CRGB::Blue;
- FastLED.show();
- tone (SND_PIN, 800);
- delay (500);
- noTone (SND_PIN);
- pinMode(BTN_PIN, INPUT_PULLUP);
- drawNumber (0);
- // Set WiFi to station mode and disconnect from an AP if it was Previously connected
- WiFi.mode(WIFI_STA);
- WiFi.disconnect();
- delay(100);
- pinMode(LED_PIN, OUTPUT);
- // attempt to connect to Wifi network:
- Serial.print("Connecting Wifi: ");
- Serial.println(ssid);
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- Serial.print(".");
- delay(500);
- }
- Serial.println("");
- Serial.println("WiFi connected");
- Serial.print("IP address: ");
- Serial.println(WiFi.localIP());
- // Only required on 2.5 Beta
- client.setInsecure();
- bot.longPoll = 60;
- }
- void handleNewMessages(int 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 = "Gast";
- Serial.print("De volgende knop is ingedrukt: ");
- Serial.println(text);
- if (text == F("/on")) {
- digitalWrite(LED_PIN, HIGH);
- tone (SND_PIN, 800);
- }
- if (text == F("/off")) {
- digitalWrite(LED_PIN, LOW);
- tone (SND_PIN, 0);
- }
- // Start de bot met /start
- if (text == "/start") {
- String welcome = "Welkom bij jouw alarmsysteem, " + from_name + ".\n";
- welcome += "/on : om het alarm aan te zetten\n";
- welcome += "/off : om het alarm uit te zetten\n";
- welcome += "/status : Geeft de huidige status van het alarm\n";
- bot.sendMessage(chat_id, welcome, "Markdown");
- }
- }
- }
- void loop() {
- if (digitalRead(BTN_PIN)== LOW){
- leds[0] = CRGB::Green;
- leds[1] = CRGB::Green;
- leds[2] = CRGB::Green;
- FastLED.show();
- counter++;
- drawNumber(counter);
- }else{
- leds[0] = CRGB::Black;
- leds[1] = CRGB::Black;
- leds[2] = CRGB::Black;
- FastLED.show();
- }
- }
- void initMax7219()
- {
- //initiation of the max 7219
- maxSingle(max7219_reg_scanLimit, 0x07);
- maxSingle(max7219_reg_decodeMode, 0x00); // using an led matrix (not digits)
- maxSingle(max7219_reg_shutdown, 0x01); // not in shutdown mode
- maxSingle(max7219_reg_displayTest, 0x00); // no display test
- for (int e = 1; e <= 8; e++) maxSingle(e,0); // empty registers, turn all LEDs off
- maxSingle(max7219_reg_intensity, 0x0f & 0x0f); // the first 0x0f is the value you can set
- }
- void putByte(byte data)
- {
- byte i = 8;
- byte mask;
- while(i > 0)
- {
- mask = 0x01 << (i - 1); // get bitmask
- digitalWrite(CLK_PIN, LOW); // tick
- if (data & mask) digitalWrite(DIN_PIN, HIGH);
- else digitalWrite(DIN_PIN, LOW);
- digitalWrite(CLK_PIN, HIGH); // tock
- --i; // move to lesser bit
- }
- }
- void maxSingle( byte reg, byte col)
- {
- digitalWrite(CS_PIN, LOW); // begin
- putByte(reg); // specify register
- putByte(col); // put data
- digitalWrite(CS_PIN, HIGH);
- }
- void displayViaArray(byte array[]) {
- for(int i = 0; i < 8; i++) {
- maxSingle(i + 1, array[i]);
- }
- }
- byte numbers[][10] = // These numbers are 4x6 pixels
- { {2, 5, 5, 5, 5, 2}, // 0
- {2, 6, 2, 2, 2, 2}, // 1
- {6, 1, 3, 6, 4, 7}, // 2
- {7, 1, 2, 1, 1, 6}, // 3
- {1, 5, 5, 7, 1, 1}, // 4
- {7, 4, 6, 1, 1, 6}, // 5
- {2, 4, 6, 5, 5, 2}, // 6
- {7, 1, 1, 2, 2, 2}, // 7
- {7, 5, 7, 5, 5, 7}, // 8
- {2, 5, 5, 3, 1, 2}
- }; // 9
- void drawNumber(int number)
- {
- int unit = number % 10; // Get the lowest digit
- int decimal = (number / 10) % 10; // Get the second digit
- byte image[8]; // Reserve memory for the image
- for (int i = 0; i < 6; i++) // The numbers are only 6 rows tall
- {
- image[i] = numbers[unit][i]; // Just copy the low digit
- if (number > 9) // Only draw the high digit if appropriate
- { // Do some bit combining to not overwrite the other digit
- image[i] |= numbers[decimal][i] << 4;
- }
- }
- image[6] = number > 900 ? 128 : 0; // Empty line, or one dot to indicate overflow
- image[7] = 255 >> max(0, 8 - (number / 100)); // every dot represents 100
- drawImage(image);
- }
- void drawImage(byte * image)
- {
- for (byte row = 0; row < 8; row++) maxSingle(row + 1, image[row]);
- }
- void clearImage()
- {
- for (byte row = 0; row < 8; row++) maxSingle(row + 1, 0);
- }
- if (millis() > lastTimeChecked + delayBetweenChecks) {
- // getUpdates returns 1 if there is a new message from Telegram
- int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
- if (numNewMessages) {
- Serial.println("got response");
- handleNewMessages(numNewMessages);
- }
- lastTimeChecked = millis();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement