Advertisement
Guest User

help eens bruv

a guest
Oct 14th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.51 KB | None | 0 0
  1. #include <FastLED.h>
  2. #include <LedControl.h>
  3. #define LED_PIN D4
  4. #define LED_COUNT 3
  5. #define SND_PIN D5
  6. #define BTN_PIN D6
  7.  
  8. #include <ESP8266WiFi.h>
  9. #include <WiFiClientSecure.h>
  10. #include <UniversalTelegramBot.h>
  11.  
  12. char ssid[] = "IOT";
  13. char password[] = "PASSWORD";
  14.  
  15. #define TELEGRAM_BOT_TOKEN "979907373:AAENTK2PQkMTGPp2o6MTEI8PsEdM7uRco_c"
  16. WiFiClientSecure client;
  17. UniversalTelegramBot bot(TELEGRAM_BOT_TOKEN, client);
  18.  
  19.  
  20. int delayBetweenChecks = 1000;
  21. unsigned long lastTimeChecked; //last time messages' scan has been done
  22.  
  23.  
  24. #define DIN_PIN D3
  25. #define CLK_PIN D1
  26. #define CS_PIN D2
  27.  
  28. int counter = 0;
  29. CRGB leds[LED_COUNT];
  30.  
  31. int maxInUse = 1; //change this variable to set how many MAX7219's you'll use
  32.  
  33. byte max7219_reg_noop = 0x00;
  34. byte max7219_reg_digit0 = 0x01;
  35. byte max7219_reg_digit1 = 0x02;
  36. byte max7219_reg_digit2 = 0x03;
  37. byte max7219_reg_digit3 = 0x04;
  38. byte max7219_reg_digit4 = 0x05;
  39. byte max7219_reg_digit5 = 0x06;
  40. byte max7219_reg_digit6 = 0x07;
  41. byte max7219_reg_digit7 = 0x08;
  42. byte max7219_reg_decodeMode = 0x09;
  43. byte max7219_reg_intensity = 0x0a;
  44. byte max7219_reg_scanLimit = 0x0b;
  45. byte max7219_reg_shutdown = 0x0c;
  46. byte max7219_reg_displayTest = 0x0f;
  47.  
  48.  
  49. LedControl display = LedControl(DIN_PIN, CLK_PIN, CS_PIN);
  50.  
  51. void setup() {
  52. Serial.begin(115200);
  53. Serial.println("");
  54. pinMode(SND_PIN, OUTPUT);
  55.  
  56. pinMode(DIN_PIN, OUTPUT);
  57. pinMode(CLK_PIN, OUTPUT);
  58. pinMode(CS_PIN, OUTPUT);
  59.  
  60. initMax7219();
  61.  
  62. FastLED.addLeds<NEOPIXEL, LED_PIN>(leds, LED_COUNT);
  63. leds[0] = CRGB::Red;
  64. leds[1] = CRGB::Red;
  65. leds[2] = CRGB::Blue;
  66. FastLED.show();
  67.  
  68. tone (SND_PIN, 800);
  69. delay (500);
  70. noTone (SND_PIN);
  71.  
  72. pinMode(BTN_PIN, INPUT_PULLUP);
  73. drawNumber (0);
  74.  
  75. // Set WiFi to station mode and disconnect from an AP if it was Previously connected
  76. WiFi.mode(WIFI_STA);
  77. WiFi.disconnect();
  78. delay(100);
  79. pinMode(LED_PIN, OUTPUT);
  80.  
  81. // attempt to connect to Wifi network:
  82. Serial.print("Connecting Wifi: ");
  83. Serial.println(ssid);
  84. WiFi.begin(ssid, password);
  85.  
  86. while (WiFi.status() != WL_CONNECTED) {
  87. Serial.print(".");
  88. delay(500);
  89. }
  90.  
  91. Serial.println("");
  92. Serial.println("WiFi connected");
  93. Serial.print("IP address: ");
  94. Serial.println(WiFi.localIP());
  95.  
  96. // Only required on 2.5 Beta
  97. client.setInsecure();
  98. bot.longPoll = 60;
  99. }
  100.  
  101. void handleNewMessages(int numNewMessages) {
  102.  
  103. for (int i = 0; i < numNewMessages; i++) {
  104. String chat_id = String(bot.messages[i].chat_id);
  105. String text = bot.messages[i].text;
  106.  
  107. String from_name = bot.messages[i].from_name;
  108. if (from_name == "") from_name = "Gast";
  109.  
  110. Serial.print("De volgende knop is ingedrukt: ");
  111. Serial.println(text);
  112.  
  113. if (text == F("/on")) {
  114. digitalWrite(LED_PIN, HIGH);
  115. tone (SND_PIN, 800);
  116. }
  117. if (text == F("/off")) {
  118. digitalWrite(LED_PIN, LOW);
  119. tone (SND_PIN, 0);
  120. }
  121.  
  122.  
  123. // Start de bot met /start
  124. if (text == "/start") {
  125. String welcome = "Welkom bij jouw alarmsysteem, " + from_name + ".\n";
  126. welcome += "/on : om het alarm aan te zetten\n";
  127. welcome += "/off : om het alarm uit te zetten\n";
  128. welcome += "/status : Geeft de huidige status van het alarm\n";
  129. bot.sendMessage(chat_id, welcome, "Markdown");
  130. }
  131. }
  132. }
  133.  
  134.  
  135. void loop() {
  136. if (digitalRead(BTN_PIN)== LOW){
  137. leds[0] = CRGB::Green;
  138. leds[1] = CRGB::Green;
  139. leds[2] = CRGB::Green;
  140. FastLED.show();
  141. counter++;
  142. drawNumber(counter);
  143.  
  144. }else{
  145. leds[0] = CRGB::Black;
  146. leds[1] = CRGB::Black;
  147. leds[2] = CRGB::Black;
  148. FastLED.show();
  149.  
  150. }
  151.  
  152. }
  153. void initMax7219()
  154. {
  155. //initiation of the max 7219
  156. maxSingle(max7219_reg_scanLimit, 0x07);
  157. maxSingle(max7219_reg_decodeMode, 0x00); // using an led matrix (not digits)
  158. maxSingle(max7219_reg_shutdown, 0x01); // not in shutdown mode
  159. maxSingle(max7219_reg_displayTest, 0x00); // no display test
  160. for (int e = 1; e <= 8; e++) maxSingle(e,0); // empty registers, turn all LEDs off
  161. maxSingle(max7219_reg_intensity, 0x0f & 0x0f); // the first 0x0f is the value you can set
  162. }
  163.  
  164. void putByte(byte data)
  165. {
  166. byte i = 8;
  167. byte mask;
  168. while(i > 0)
  169. {
  170. mask = 0x01 << (i - 1); // get bitmask
  171. digitalWrite(CLK_PIN, LOW); // tick
  172. if (data & mask) digitalWrite(DIN_PIN, HIGH);
  173. else digitalWrite(DIN_PIN, LOW);
  174. digitalWrite(CLK_PIN, HIGH); // tock
  175. --i; // move to lesser bit
  176. }
  177. }
  178.  
  179. void maxSingle( byte reg, byte col)
  180. {
  181. digitalWrite(CS_PIN, LOW); // begin
  182. putByte(reg); // specify register
  183. putByte(col); // put data
  184. digitalWrite(CS_PIN, HIGH);
  185. }
  186.  
  187.  
  188. void displayViaArray(byte array[]) {
  189. for(int i = 0; i < 8; i++) {
  190. maxSingle(i + 1, array[i]);
  191. }
  192. }
  193.  
  194. byte numbers[][10] = // These numbers are 4x6 pixels
  195. { {2, 5, 5, 5, 5, 2}, // 0
  196. {2, 6, 2, 2, 2, 2}, // 1
  197. {6, 1, 3, 6, 4, 7}, // 2
  198. {7, 1, 2, 1, 1, 6}, // 3
  199. {1, 5, 5, 7, 1, 1}, // 4
  200. {7, 4, 6, 1, 1, 6}, // 5
  201. {2, 4, 6, 5, 5, 2}, // 6
  202. {7, 1, 1, 2, 2, 2}, // 7
  203. {7, 5, 7, 5, 5, 7}, // 8
  204. {2, 5, 5, 3, 1, 2}
  205. }; // 9
  206.  
  207. void drawNumber(int number)
  208. {
  209.  
  210. int unit = number % 10; // Get the lowest digit
  211. int decimal = (number / 10) % 10; // Get the second digit
  212. byte image[8]; // Reserve memory for the image
  213. for (int i = 0; i < 6; i++) // The numbers are only 6 rows tall
  214. {
  215. image[i] = numbers[unit][i]; // Just copy the low digit
  216. if (number > 9) // Only draw the high digit if appropriate
  217. { // Do some bit combining to not overwrite the other digit
  218. image[i] |= numbers[decimal][i] << 4;
  219. }
  220. }
  221. image[6] = number > 900 ? 128 : 0; // Empty line, or one dot to indicate overflow
  222. image[7] = 255 >> max(0, 8 - (number / 100)); // every dot represents 100
  223. drawImage(image);
  224. }
  225.  
  226. void drawImage(byte * image)
  227. {
  228. for (byte row = 0; row < 8; row++) maxSingle(row + 1, image[row]);
  229. }
  230.  
  231. void clearImage()
  232. {
  233. for (byte row = 0; row < 8; row++) maxSingle(row + 1, 0);
  234. }
  235.  
  236. if (millis() > lastTimeChecked + delayBetweenChecks) {
  237. // getUpdates returns 1 if there is a new message from Telegram
  238. int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
  239. if (numNewMessages) {
  240. Serial.println("got response");
  241. handleNewMessages(numNewMessages);
  242. }
  243. lastTimeChecked = millis();
  244. }
  245. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement