├── .gitignore ├── ESPea_Examples.cpp ├── ESPea_Examples.h ├── README.md ├── examples ├── 01.Basics │ ├── Blink │ │ └── Blink.ino │ └── BlinkWithoutDelay │ │ └── BlinkWithoutDelay.ino ├── 03.Shields │ ├── shield-lora │ │ ├── readme.md │ │ └── shield-lora.ino │ ├── shield-micro-sd │ │ ├── readme.md │ │ └── shield-micro-sd.ino │ └── shield-oled │ │ ├── readme.md │ │ └── shield-oled.ino └── 04.Network │ ├── http-lock │ ├── http-lock.ino │ ├── readme.md │ └── sample-config.json │ └── mqtt-client │ ├── mqtt-client.ino │ └── readme.md └── library.properties /.gitignore: -------------------------------------------------------------------------------- 1 | *.swp 2 | .DS_Store 3 | -------------------------------------------------------------------------------- /ESPea_Examples.cpp: -------------------------------------------------------------------------------- 1 | // ESPea_Examples.cpp 2 | -------------------------------------------------------------------------------- /ESPea_Examples.h: -------------------------------------------------------------------------------- 1 | // ESPea_Examples.h 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## ESPea-Examples 2 | 3 | Examples for ESPea and ESPea32 4 | -------------------------------------------------------------------------------- /examples/01.Basics/Blink/Blink.ino: -------------------------------------------------------------------------------- 1 | /* 2 | ESP8266 Blink by Simon Peter 3 | Blink the blue LED on the ESP-01 module 4 | This example code is in the public domain 5 | 6 | The blue LED on the ESP-01 module is connected to GPIO1 7 | (which is also the TXD pin; so we cannot use Serial.print() at the same time) 8 | 9 | Note that this sketch uses LED_BUILTIN to find the pin with the internal LED 10 | */ 11 | 12 | void setup() { 13 | pinMode(LED_BUILTIN, OUTPUT); // Initialize the LED_BUILTIN pin as an output 14 | } 15 | 16 | // the loop function runs over and over again forever 17 | void loop() { 18 | digitalWrite(LED_BUILTIN, LOW); // Turn the LED on (Note that LOW is the voltage level 19 | // but actually the LED is on; this is because 20 | // it is acive low on the ESP-01) 21 | delay(1000); // Wait for a second 22 | digitalWrite(LED_BUILTIN, HIGH); // Turn the LED off by making the voltage HIGH 23 | delay(2000); // Wait for two seconds (to demonstrate the active low LED) 24 | } 25 | -------------------------------------------------------------------------------- /examples/01.Basics/BlinkWithoutDelay/BlinkWithoutDelay.ino: -------------------------------------------------------------------------------- 1 | /* 2 | ESP8266 BlinkWithoutDelay by Simon Peter 3 | Blink the blue LED on the ESP-01 module 4 | Based on the Arduino Blink without Delay example 5 | This example code is in the public domain 6 | 7 | The blue LED on the ESP-01 module is connected to GPIO1 8 | (which is also the TXD pin; so we cannot use Serial.print() at the same time) 9 | 10 | Note that this sketch uses LED_BUILTIN to find the pin with the internal LED 11 | */ 12 | 13 | int ledState = LOW; 14 | 15 | unsigned long previousMillis = 0; 16 | const long interval = 1000; 17 | 18 | void setup() { 19 | pinMode(LED_BUILTIN, OUTPUT); 20 | } 21 | 22 | void loop() 23 | { 24 | unsigned long currentMillis = millis(); 25 | if(currentMillis - previousMillis >= interval) { 26 | previousMillis = currentMillis; 27 | if (ledState == LOW) 28 | ledState = HIGH; // Note that this switches the LED *off* 29 | else 30 | ledState = LOW; // Note that this switches the LED *on* 31 | digitalWrite(LED_BUILTIN, ledState); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /examples/03.Shields/shield-lora/readme.md: -------------------------------------------------------------------------------- 1 | ## RAW Example For ESPea Lora Shield ## 2 | 3 | For the shield, the config for pins 4 | 5 | ``` 6 | // Example with NO DIO pin connected 7 | const lmic_pinmap lmic_pins = { 8 | .nss = 16, 9 | .rxtx = LMIC_UNUSED_PIN, 10 | .rst = LMIC_UNUSED_PIN, 11 | .dio = {LMIC_UNUSED_PIN, LMIC_UNUSED_PIN, LMIC_UNUSED_PIN}, 12 | }; 13 | ``` 14 | 15 | ### Installing ### 16 | 17 | * Install library arduino-lmic by hallard 18 | * Install it using the Arduino Library manager ("Sketch" -> "Include Library" -> "Manage Libraries..."), or 19 | * Download a zipfile from [github](https://github.com/hallard/arduino-lmic) using the "Download ZIP" button and install it using the IDE ("Sketch" -> "Include Library" -> "Add .ZIP Library..." 20 | * Clone this git repository into your sketchbook/libraries folder. 21 | * Run the sktech 22 | -------------------------------------------------------------------------------- /examples/03.Shields/shield-lora/shield-lora.ino: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2015 Matthijs Kooijman 3 | * 4 | * Permission is hereby granted, free of charge, to anyone 5 | * obtaining a copy of this document and accompanying files, 6 | * to do whatever they want with them without any restriction, 7 | * including, but not limited to, copying, modification and redistribution. 8 | * NO WARRANTY OF ANY KIND IS PROVIDED. 9 | * 10 | * This example transmits data on hardcoded channel and receives data 11 | * when not transmitting. Running this sketch on two nodes should allow 12 | * them to communicate. 13 | *******************************************************************************/ 14 | 15 | #include 16 | #include 17 | #include 18 | 19 | #if !defined(DISABLE_INVERT_IQ_ON_RX) 20 | #error This example requires DISABLE_INVERT_IQ_ON_RX to be set. Update \ 21 | config.h in the lmic library to set it. 22 | #endif 23 | 24 | // How often to send a packet. Note that this sketch bypasses the normal 25 | // LMIC duty cycle limiting, so when you change anything in this sketch 26 | // (payload length, frequency, spreading factor), be sure to check if 27 | // this interval should not also be increased. 28 | // See this spreadsheet for an easy airtime and duty cycle calculator: 29 | // https://docs.google.com/spreadsheets/d/1voGAtQAjC1qBmaVuP1ApNKs1ekgUjavHuVQIXyYSvNc 30 | #define TX_INTERVAL 2000 31 | 32 | // Pin mapping 33 | const lmic_pinmap lmic_pins = { 34 | .nss = 16, 35 | .rxtx = LMIC_UNUSED_PIN, 36 | .rst = LMIC_UNUSED_PIN, 37 | .dio = {LMIC_UNUSED_PIN, LMIC_UNUSED_PIN, LMIC_UNUSED_PIN}, 38 | }; 39 | 40 | // These callbacks are only used in over-the-air activation, so they are 41 | // left empty here (we cannot leave them out completely unless 42 | // DISABLE_JOIN is set in config.h, otherwise the linker will complain). 43 | void os_getArtEui (u1_t* buf) { } 44 | void os_getDevEui (u1_t* buf) { } 45 | void os_getDevKey (u1_t* buf) { } 46 | 47 | void onEvent (ev_t ev) { 48 | } 49 | 50 | osjob_t txjob; 51 | osjob_t timeoutjob; 52 | static void tx_func (osjob_t* job); 53 | 54 | // Transmit the given string and call the given function afterwards 55 | void tx(const char *str, osjobcb_t func) { 56 | os_radio(RADIO_RST); // Stop RX first 57 | delay(1); // Wait a bit, without this os_radio below asserts, apparently because the state hasn't changed yet 58 | LMIC.dataLen = 0; 59 | while (*str) 60 | LMIC.frame[LMIC.dataLen++] = *str++; 61 | LMIC.osjob.func = func; 62 | os_radio(RADIO_TX); 63 | Serial.println("TX"); 64 | } 65 | 66 | // Enable rx mode and call func when a packet is received 67 | void rx(osjobcb_t func) { 68 | LMIC.osjob.func = func; 69 | LMIC.rxtime = os_getTime(); // RX _now_ 70 | // Enable "continuous" RX (e.g. without a timeout, still stops after 71 | // receiving a packet) 72 | os_radio(RADIO_RXON); 73 | Serial.println("RX"); 74 | } 75 | 76 | static void rxtimeout_func(osjob_t *job) { 77 | digitalWrite(LED_BUILTIN, LOW); // off 78 | } 79 | 80 | static void rx_func (osjob_t* job) { 81 | // Blink once to confirm reception and then keep the led on 82 | digitalWrite(LED_BUILTIN, LOW); // off 83 | delay(10); 84 | digitalWrite(LED_BUILTIN, HIGH); // on 85 | 86 | // Timeout RX (i.e. update led status) after 3 periods without RX 87 | os_setTimedCallback(&timeoutjob, os_getTime() + ms2osticks(3*TX_INTERVAL), rxtimeout_func); 88 | 89 | // Reschedule TX so that it should not collide with the other side's 90 | // next TX 91 | os_setTimedCallback(&txjob, os_getTime() + ms2osticks(TX_INTERVAL/2), tx_func); 92 | 93 | Serial.print("Got "); 94 | Serial.print(LMIC.dataLen); 95 | Serial.println(" bytes"); 96 | Serial.write(LMIC.frame, LMIC.dataLen); 97 | Serial.println(); 98 | 99 | // Restart RX 100 | rx(rx_func); 101 | } 102 | 103 | static void txdone_func (osjob_t* job) { 104 | rx(rx_func); 105 | } 106 | 107 | // log text to USART and toggle LED 108 | static void tx_func (osjob_t* job) { 109 | // say hello 110 | tx("Hello, world!", txdone_func); 111 | // reschedule job every TX_INTERVAL (plus a bit of random to prevent 112 | // systematic collisions), unless packets are received, then rx_func 113 | // will reschedule at half this time. 114 | os_setTimedCallback(job, os_getTime() + ms2osticks(TX_INTERVAL + random(500)), tx_func); 115 | } 116 | 117 | // application entry point 118 | void setup() { 119 | Serial.begin(115200); 120 | Serial.println("Starting"); 121 | #ifdef VCC_ENABLE 122 | // For Pinoccio Scout boards 123 | pinMode(VCC_ENABLE, OUTPUT); 124 | digitalWrite(VCC_ENABLE, HIGH); 125 | delay(1000); 126 | #endif 127 | 128 | pinMode(LED_BUILTIN, OUTPUT); 129 | 130 | // initialize runtime env 131 | os_init(); 132 | 133 | // Set up these settings once, and use them for both TX and RX 134 | 135 | #if defined(CFG_eu868) 136 | // Use a frequency in the g3 which allows 10% duty cycling. 137 | LMIC.freq = 869525000; 138 | #elif defined(CFG_us915) 139 | LMIC.freq = 902300000; 140 | #endif 141 | 142 | // Maximum TX power 143 | LMIC.txpow = 27; 144 | // Use a medium spread factor. This can be increased up to SF12 for 145 | // better range, but then the interval should be (significantly) 146 | // lowered to comply with duty cycle limits as well. 147 | LMIC.datarate = DR_SF9; 148 | // This sets CR 4/5, BW125 (except for DR_SF7B, which uses BW250) 149 | LMIC.rps = updr2rps(LMIC.datarate); 150 | 151 | Serial.println("Started"); 152 | Serial.flush(); 153 | 154 | // setup initial job 155 | os_setCallback(&txjob, tx_func); 156 | } 157 | 158 | void loop() { 159 | // execute scheduled jobs and events 160 | os_runloop_once(); 161 | } 162 | -------------------------------------------------------------------------------- /examples/03.Shields/shield-micro-sd/readme.md: -------------------------------------------------------------------------------- 1 | 2 | ## How To Start 3 | 4 | * Open Arduino IDE 5 | * Choose board type "NodeMCU 1.0" 6 | * Upload the sketch 7 | 8 | -------------------------------------------------------------------------------- /examples/03.Shields/shield-micro-sd/shield-micro-sd.ino: -------------------------------------------------------------------------------- 1 | /* 2 | SD card test 3 | 4 | This example shows how use the utility libraries on which the' 5 | SD library is based in order to get info about your SD card. 6 | Very useful for testing a card when you're not sure whether its working or not. 7 | 8 | * The ESPea Micro SD Shield uses: 9 | * D5, D6, D7, D8, 3V3 and G 10 | * 11 | * The shield uses SPI bus pins: 12 | * D5 = CLK 13 | * D6 = MISO 14 | * D7 = MOSI 15 | * D8 = CS 16 | 17 | created 28 Mar 2011 18 | by Limor Fried 19 | modified 9 Apr 2012 20 | by Tom Igoe 21 | */ 22 | // include the SD library: 23 | #include 24 | #include 25 | 26 | // set up variables using the SD utility library functions: 27 | Sd2Card card; 28 | SdVolume volume; 29 | SdFile root; 30 | 31 | // change this to match your SD shield or module; 32 | // Arduino Ethernet shield: pin 4 33 | // Adafruit SD shields and modules: pin 10 34 | // Sparkfun SD shield: pin 8 35 | // ESPea SD shield: D8 36 | const int chipSelect = D8; 37 | 38 | void setup() 39 | { 40 | // Open serial communications and wait for port to open: 41 | Serial.begin(9600); 42 | while (!Serial) { 43 | ; // wait for serial port to connect. Needed for Leonardo only 44 | } 45 | 46 | 47 | Serial.print("\nInitializing SD card..."); 48 | 49 | // we'll use the initialization code from the utility libraries 50 | // since we're just testing if the card is working! 51 | if (!card.init(SPI_HALF_SPEED, chipSelect)) { 52 | Serial.println("initialization failed. Things to check:"); 53 | Serial.println("* is a card inserted?"); 54 | Serial.println("* is your wiring correct?"); 55 | Serial.println("* did you change the chipSelect pin to match your shield or module?"); 56 | return; 57 | } else { 58 | Serial.println("Wiring is correct and a card is present."); 59 | } 60 | 61 | // print the type of card 62 | Serial.print("\nCard type: "); 63 | switch (card.type()) { 64 | case SD_CARD_TYPE_SD1: 65 | Serial.println("SD1"); 66 | break; 67 | case SD_CARD_TYPE_SD2: 68 | Serial.println("SD2"); 69 | break; 70 | case SD_CARD_TYPE_SDHC: 71 | Serial.println("SDHC"); 72 | break; 73 | default: 74 | Serial.println("Unknown"); 75 | } 76 | 77 | // Now we will try to open the 'volume'/'partition' - it should be FAT16 or FAT32 78 | if (!volume.init(card)) { 79 | Serial.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card"); 80 | return; 81 | } 82 | 83 | 84 | // print the type and size of the first FAT-type volume 85 | uint32_t volumesize; 86 | Serial.print("\nVolume type is FAT"); 87 | Serial.println(volume.fatType(), DEC); 88 | Serial.println(); 89 | 90 | volumesize = volume.blocksPerCluster(); // clusters are collections of blocks 91 | volumesize *= volume.clusterCount(); // we'll have a lot of clusters 92 | volumesize *= 512; // SD card blocks are always 512 bytes 93 | Serial.print("Volume size (bytes): "); 94 | Serial.println(volumesize); 95 | Serial.print("Volume size (Kbytes): "); 96 | volumesize /= 1024; 97 | Serial.println(volumesize); 98 | Serial.print("Volume size (Mbytes): "); 99 | volumesize /= 1024; 100 | Serial.println(volumesize); 101 | 102 | 103 | Serial.println("\nFiles found on the card (name, date and size in bytes): "); 104 | root.openRoot(volume); 105 | 106 | // list all files in the card with date and size 107 | root.ls(LS_R | LS_DATE | LS_SIZE); 108 | } 109 | 110 | 111 | void loop(void) { 112 | 113 | } 114 | -------------------------------------------------------------------------------- /examples/03.Shields/shield-oled/readme.md: -------------------------------------------------------------------------------- 1 | 2 | ## How To Start 3 | 4 | * Open Arduino IDE 5 | * Install library "Adafruit_SSD1306" with "Sketch -> Included Library -> Manage Libraries" 6 | * Open the directory "Sketchbook location", find the directory for Adafruit_SSD1306 7 | * Open file Adafruit_SSD1306.h, uncomment the line ```#define SSD1306_128_64``` and comment the line ```#define SSD1306_128_32``` 8 | * Upload the sketch 9 | 10 | -------------------------------------------------------------------------------- /examples/03.Shields/shield-oled/shield-oled.ino: -------------------------------------------------------------------------------- 1 | /********************************************************************* 2 | This is an example for our Monochrome OLEDs based on SSD1306 drivers 3 | 4 | Pick one up today in the adafruit shop! 5 | ------> http://www.adafruit.com/category/63_98 6 | 7 | This example is for a 128x64 size display using I2C to communicate 8 | 3 pins are required to interface (2 I2C and one reset) 9 | 10 | Adafruit invests time and resources providing this open source code, 11 | please support Adafruit and open-source hardware by purchasing 12 | products from Adafruit! 13 | 14 | Written by Limor Fried/Ladyada for Adafruit Industries. 15 | BSD license, check license.txt for more information 16 | All text above, and the splash screen must be included in any redistribution 17 | *********************************************************************/ 18 | 19 | #include 20 | #include 21 | #include 22 | #include 23 | 24 | #define OLED_RESET 0 25 | Adafruit_SSD1306 display(OLED_RESET); 26 | 27 | #define NUMFLAKES 10 28 | #define XPOS 0 29 | #define YPOS 1 30 | #define DELTAY 2 31 | 32 | 33 | #define LOGO16_GLCD_HEIGHT 16 34 | #define LOGO16_GLCD_WIDTH 16 35 | static const unsigned char PROGMEM logo16_glcd_bmp[] = 36 | { B00000000, B11000000, 37 | B00000001, B11000000, 38 | B00000001, B11000000, 39 | B00000011, B11100000, 40 | B11110011, B11100000, 41 | B11111110, B11111000, 42 | B01111110, B11111111, 43 | B00110011, B10011111, 44 | B00011111, B11111100, 45 | B00001101, B01110000, 46 | B00011011, B10100000, 47 | B00111111, B11100000, 48 | B00111111, B11110000, 49 | B01111100, B11110000, 50 | B01110000, B01110000, 51 | B00000000, B00110000 }; 52 | 53 | #if (SSD1306_LCDHEIGHT != 64) 54 | #error("Height incorrect, please fix Adafruit_SSD1306.h!"); 55 | #endif 56 | 57 | void setup() { 58 | Serial.begin(9600); 59 | 60 | // by default, we'll generate the high voltage from the 3.3v line internally! (neat!) 61 | display.begin(SSD1306_SWITCHCAPVCC, 0x3D); // initialize with the I2C addr 0x3D (for the 128x64) 62 | // init done 63 | 64 | // Show image buffer on the display hardware. 65 | // Since the buffer is intialized with an Adafruit splashscreen 66 | // internally, this will display the splashscreen. 67 | display.display(); 68 | delay(2000); 69 | 70 | // Clear the buffer. 71 | display.clearDisplay(); 72 | 73 | // draw a single pixel 74 | display.drawPixel(10, 10, WHITE); 75 | // Show the display buffer on the hardware. 76 | // NOTE: You _must_ call display after making any drawing commands 77 | // to make them visible on the display hardware! 78 | display.display(); 79 | delay(2000); 80 | display.clearDisplay(); 81 | 82 | // draw many lines 83 | testdrawline(); 84 | display.display(); 85 | delay(2000); 86 | display.clearDisplay(); 87 | 88 | // draw rectangles 89 | testdrawrect(); 90 | display.display(); 91 | delay(2000); 92 | display.clearDisplay(); 93 | 94 | // draw multiple rectangles 95 | testfillrect(); 96 | display.display(); 97 | delay(2000); 98 | display.clearDisplay(); 99 | 100 | // draw mulitple circles 101 | testdrawcircle(); 102 | display.display(); 103 | delay(2000); 104 | display.clearDisplay(); 105 | 106 | // draw a white circle, 10 pixel radius 107 | display.fillCircle(display.width()/2, display.height()/2, 10, WHITE); 108 | display.display(); 109 | delay(2000); 110 | display.clearDisplay(); 111 | 112 | testdrawroundrect(); 113 | delay(2000); 114 | display.clearDisplay(); 115 | 116 | testfillroundrect(); 117 | delay(2000); 118 | display.clearDisplay(); 119 | 120 | testdrawtriangle(); 121 | delay(2000); 122 | display.clearDisplay(); 123 | 124 | testfilltriangle(); 125 | delay(2000); 126 | display.clearDisplay(); 127 | 128 | // draw the first ~12 characters in the font 129 | testdrawchar(); 130 | display.display(); 131 | delay(2000); 132 | display.clearDisplay(); 133 | 134 | // draw scrolling text 135 | testscrolltext(); 136 | delay(2000); 137 | display.clearDisplay(); 138 | 139 | // text display tests 140 | display.setTextSize(1); 141 | display.setTextColor(WHITE); 142 | display.setCursor(0,0); 143 | display.println("Hello, world!"); 144 | display.setTextColor(BLACK, WHITE); // 'inverted' text 145 | display.println(3.141592); 146 | display.setTextSize(2); 147 | display.setTextColor(WHITE); 148 | display.print("0x"); display.println(0xDEADBEEF, HEX); 149 | display.display(); 150 | delay(2000); 151 | display.clearDisplay(); 152 | 153 | // miniature bitmap display 154 | display.drawBitmap(30, 16, logo16_glcd_bmp, 16, 16, 1); 155 | display.display(); 156 | delay(1); 157 | 158 | // invert the display 159 | display.invertDisplay(true); 160 | delay(1000); 161 | display.invertDisplay(false); 162 | delay(1000); 163 | display.clearDisplay(); 164 | 165 | // draw a bitmap icon and 'animate' movement 166 | testdrawbitmap(logo16_glcd_bmp, LOGO16_GLCD_HEIGHT, LOGO16_GLCD_WIDTH); 167 | } 168 | 169 | 170 | void loop() { 171 | 172 | } 173 | 174 | 175 | void testdrawbitmap(const uint8_t *bitmap, uint8_t w, uint8_t h) { 176 | uint8_t icons[NUMFLAKES][3]; 177 | 178 | // initialize 179 | for (uint8_t f=0; f< NUMFLAKES; f++) { 180 | icons[f][XPOS] = random(display.width()); 181 | icons[f][YPOS] = 0; 182 | icons[f][DELTAY] = random(5) + 1; 183 | 184 | Serial.print("x: "); 185 | Serial.print(icons[f][XPOS], DEC); 186 | Serial.print(" y: "); 187 | Serial.print(icons[f][YPOS], DEC); 188 | Serial.print(" dy: "); 189 | Serial.println(icons[f][DELTAY], DEC); 190 | } 191 | 192 | while (1) { 193 | // draw each icon 194 | for (uint8_t f=0; f< NUMFLAKES; f++) { 195 | display.drawBitmap(icons[f][XPOS], icons[f][YPOS], bitmap, w, h, WHITE); 196 | } 197 | display.display(); 198 | delay(200); 199 | 200 | // then erase it + move it 201 | for (uint8_t f=0; f< NUMFLAKES; f++) { 202 | display.drawBitmap(icons[f][XPOS], icons[f][YPOS], bitmap, w, h, BLACK); 203 | // move it 204 | icons[f][YPOS] += icons[f][DELTAY]; 205 | // if its gone, reinit 206 | if (icons[f][YPOS] > display.height()) { 207 | icons[f][XPOS] = random(display.width()); 208 | icons[f][YPOS] = 0; 209 | icons[f][DELTAY] = random(5) + 1; 210 | } 211 | } 212 | } 213 | } 214 | 215 | 216 | void testdrawchar(void) { 217 | display.setTextSize(1); 218 | display.setTextColor(WHITE); 219 | display.setCursor(0,0); 220 | 221 | for (uint8_t i=0; i < 168; i++) { 222 | if (i == '\n') continue; 223 | display.write(i); 224 | if ((i > 0) && (i % 21 == 0)) 225 | display.println(); 226 | } 227 | display.display(); 228 | delay(1); 229 | } 230 | 231 | void testdrawcircle(void) { 232 | for (int16_t i=0; i0; i-=5) { 263 | display.fillTriangle(display.width()/2, display.height()/2-i, 264 | display.width()/2-i, display.height()/2+i, 265 | display.width()/2+i, display.height()/2+i, WHITE); 266 | if (color == WHITE) color = BLACK; 267 | else color = WHITE; 268 | display.display(); 269 | delay(1); 270 | } 271 | } 272 | 273 | void testdrawroundrect(void) { 274 | for (int16_t i=0; i=0; i-=4) { 320 | display.drawLine(0, display.height()-1, display.width()-1, i, WHITE); 321 | display.display(); 322 | delay(1); 323 | } 324 | delay(250); 325 | 326 | display.clearDisplay(); 327 | for (int16_t i=display.width()-1; i>=0; i-=4) { 328 | display.drawLine(display.width()-1, display.height()-1, i, 0, WHITE); 329 | display.display(); 330 | delay(1); 331 | } 332 | for (int16_t i=display.height()-1; i>=0; i-=4) { 333 | display.drawLine(display.width()-1, display.height()-1, 0, i, WHITE); 334 | display.display(); 335 | delay(1); 336 | } 337 | delay(250); 338 | 339 | display.clearDisplay(); 340 | for (int16_t i=0; i 8 | #include 9 | #include 10 | #include 11 | 12 | #define RELAY_PIN 5 // wire D1 to relay 13 | 14 | const char* ssid = "YOUR-SSID"; 15 | const char* password = "YOUR-PASSWORD"; 16 | const long lockTimeout = 3000; 17 | 18 | ESP8266WebServer server(80); 19 | long unlockedTime = 0; 20 | 21 | void handleQuery() { 22 | char rsp[255]; 23 | sprintf(rsp, "{\"state\":\"%s\",\"statusCode\":200,\"battery\":100}", digitalRead(RELAY_PIN) ? "unlocked" : "locked"); 24 | 25 | server.send(200, "text/plain", rsp); 26 | } 27 | 28 | void handleLock() { 29 | String state = server.arg("state"); 30 | if (state == "locked") { 31 | digitalWrite(RELAY_PIN, 0); 32 | } else { 33 | // There's a bug for ESP8266 Arduino core version 2.3.0 that it can't get param state 34 | digitalWrite(RELAY_PIN, 1); 35 | // Lock again after 3 seconds 36 | unlockedTime = millis(); 37 | } 38 | server.send(200, "text/plain", "{\"battery\":100,\"statusCode\":200}"); 39 | } 40 | 41 | void setup() { 42 | Serial.begin(115200); 43 | pinMode(RELAY_PIN, OUTPUT); 44 | WiFi.mode(WIFI_STA); 45 | WiFi.begin(ssid, password); 46 | 47 | Serial.println(""); 48 | while (WiFi.status() != WL_CONNECTED) { 49 | delay(500); 50 | Serial.print("."); 51 | } 52 | 53 | Serial.println(""); 54 | Serial.print("Connected to "); 55 | Serial.println(ssid); 56 | Serial.print("IP address: "); 57 | Serial.println(WiFi.localIP()); 58 | 59 | server.on("/", HTTP_GET, handleQuery); // Query lock status 60 | server.on("/", HTTP_POST, handleLock); // Set lock status 61 | server.begin(); 62 | 63 | } 64 | 65 | void loop() { 66 | server.handleClient(); 67 | 68 | // Restore lock status 69 | if ((unlockedTime > 0) && (millis() - unlockedTime > lockTimeout)) { 70 | unlockedTime = 0; 71 | digitalWrite(RELAY_PIN, 0); 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /examples/04.Network/http-lock/readme.md: -------------------------------------------------------------------------------- 1 | Make a Siri HomeKit enabled lock with [ESPea](https://blog.aprbrother.com/product/espea) 2 | 3 | See the [instructables](http://www.instructables.com/id/Siri-HomeKit-Controlled-ESP8266-Lock/) for more information 4 | -------------------------------------------------------------------------------- /examples/04.Network/http-lock/sample-config.json: -------------------------------------------------------------------------------- 1 | { 2 | "bridge": { 3 | "name": "Homebridge", 4 | "username": "CC:22:3D:E3:CE:30", 5 | "port": 51826, 6 | "pin": "031-45-154" 7 | }, 8 | 9 | "description": "This is an example configuration file with one fake accessory and one fake platform. You can use this as a template for creating your own configuration file containing devices you actually own.", 10 | 11 | "accessories": [ 12 | { 13 | "accessory": "Httplock", 14 | "name": "Front Door", 15 | "url": "http://192.168.3.14/", 16 | "ssl-root-ca-cert": "/tmp/x.cert", 17 | "lock-id": "1", 18 | "username": "test", 19 | "password": "test" 20 | } 21 | ] 22 | } 23 | -------------------------------------------------------------------------------- /examples/04.Network/mqtt-client/mqtt-client.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Basic ESP8266 MQTT example 3 | 4 | This sketch demonstrates the capabilities of the pubsub library in combination 5 | with the ESP8266 board/library. 6 | 7 | It connects to an MQTT server then: 8 | - subscribes to the topic "/beacons", printing out any messages 9 | it receives. NB - it assumes the received payloads are strings not binary 10 | 11 | It will reconnect to the server if the connection is lost using a blocking 12 | reconnect function. 13 | 14 | To install the ESP8266 board, (using Arduino 1.6.4+): 15 | - Add the following 3rd party board manager under "File -> Preferences -> Additional Boards Manager URLs": 16 | http://arduino.esp8266.com/stable/package_esp8266com_index.json 17 | - Open the "Tools -> Board -> Board Manager" and click install for the ESP8266" 18 | - Select your ESP8266 in "Tools -> Board" 19 | 20 | */ 21 | 22 | #include 23 | #include 24 | 25 | // Update these with values suitable for your network. 26 | 27 | #define BUFFER_SIZE 1000 28 | 29 | const char* ssid = "YOUR-SSID"; 30 | const char* password = "YOUR-PASS"; 31 | const char* mqtt_server = "mqtt.bconimg.com"; 32 | 33 | WiFiClient espClient; 34 | PubSubClient client(espClient); 35 | 36 | void callback(const MQTT::Publish& pub) { 37 | Serial.print("Message arrived ["); 38 | Serial.print(pub.topic()); 39 | Serial.print(" => "); 40 | if (pub.has_stream()) { 41 | uint8_t buf[BUFFER_SIZE]; 42 | int read; 43 | while (read = pub.payload_stream()->read(buf, BUFFER_SIZE)) { 44 | Serial.write(buf, read); 45 | } 46 | pub.payload_stream()->stop(); 47 | Serial.println(""); 48 | } else 49 | Serial.println(pub.payload_string()); 50 | } 51 | 52 | void setup() { 53 | pinMode(BUILTIN_LED, OUTPUT); // Initialize the BUILTIN_LED pin as an output 54 | Serial.begin(115200); 55 | setup_wifi(); 56 | client.set_server(mqtt_server, 1883); 57 | client.set_callback(callback); 58 | } 59 | 60 | void setup_wifi() { 61 | 62 | delay(10); 63 | // We start by connecting to a WiFi network 64 | Serial.println(); 65 | Serial.print("Connecting to "); 66 | Serial.println(ssid); 67 | 68 | WiFi.begin(ssid, password); 69 | 70 | while (WiFi.status() != WL_CONNECTED) { 71 | delay(500); 72 | Serial.print("."); 73 | } 74 | 75 | Serial.println(""); 76 | Serial.println("WiFi connected"); 77 | Serial.println("IP address: "); 78 | Serial.println(WiFi.localIP()); 79 | } 80 | 81 | void reconnect() { 82 | // Loop until we're reconnected 83 | while (!client.connected()) { 84 | Serial.print("Attempting MQTT connection..."); 85 | // Attempt to connect 86 | if (client.connect("ESP8266Client")) { 87 | Serial.println("connected"); 88 | // Once connected, publish an announcement... 89 | client.publish("outTopic", "hello world"); 90 | // ... and resubscribe 91 | client.subscribe("/beacons"); 92 | } else { 93 | Serial.println(" try again in 5 seconds"); 94 | // Wait 5 seconds before retrying 95 | delay(5000); 96 | } 97 | } 98 | } 99 | 100 | void loop() { 101 | if (!client.connected()) { 102 | reconnect(); 103 | } 104 | client.loop(); 105 | } 106 | -------------------------------------------------------------------------------- /examples/04.Network/mqtt-client/readme.md: -------------------------------------------------------------------------------- 1 | ## Description 2 | 3 | The example shows how to coneect MQTT broker and subscribe a topic. It can also work with our [WiFi iBeacon Receiver](https://blog.aprbrother.com/product/wifi-ibeacon-receiver) 4 | 5 | ## How To Start 6 | 7 | * Open Arduino IDE 8 | * Install library [PubSubClient](https://github.com/Imroy/pubsubclient). 9 | * Download [zip file](https://github.com/Imroy/pubsubclient/archive/master.zip) 10 | * Install the library "Sketch -> Include Library -> Add .ZIP library" 11 | * Change the SSID and password to yours 12 | * Upload the sketch 13 | * Open serial monitor (baud rate: 115200) and see messages from MQTT broker 14 | -------------------------------------------------------------------------------- /library.properties: -------------------------------------------------------------------------------- 1 | name=ESPea Examples 2 | version=1.0.1 3 | author=aprbrother.com 4 | maintainer=AprBrother 5 | sentence=Examples for the ESPea. 6 | paragraph=Examples for ESPea 7 | category=Other 8 | url=https://github.com/AprilBrother/ESPea-Examples 9 | architectures=* 10 | --------------------------------------------------------------------------------