Example sketches for the new Arduino IDE for ESP8266

Moderator: igrr

User avatar
By roccomuso
#24094
burkmurray wrote:Many thanks to @danbicks and @miker for this example!

I've been tearing my hair out trying to get the 1306 to work over I2C, and your code is elegant and a lot more compact than the Adfruit GFX library (no offense to Adafruit).

Now, time to tackle the MMA8451 accelerometer. I'll be sure to post my code when I get it working.

Enjoy your weekends, guys, and thanks again!

Burk


Any news on the accelerometer project? did you manage it? :D
User avatar
By 12d3
#24165 Hi,

I'm just getting into the ESP8266 ESP-012 and one of the first things I want to do is add a screen. I've ordered one of those small 1.3" OLED screens on ebay (I2C, driver SH1106).

I was wondering if anyone has a copy of the code I could use for this? I've tried to read through this thread, but I've got a bit lost with all the different amendments.

Thank you.
User avatar
By Electroguard
#25889 Just stumbled across your project, and was pleased to quickly get it going... an excellent project Dan, and thanks very much for sharing.

I have read all the posts - including mentions of veroboard and pcbs and display stability issues - so perhaps some readers might find this cheap ESP12 breakout board version a useful option, because it comes equiped with battery box and onboard regulator.

http://www.ebay.co.uk/itm/ESP8266-WIFI-Serial-Dev-Kit-Development-Board-Test-Wireless-Board-Full-IO-Leads-/381256220109?hash=item58c4a43dcd

It's only 3 quid from ebay, and offers a self-contained portable ESP-12 with all available GPIOs brought out on pins, plus even an integrated LDR on analog0.

The board already includes a tombstone link for shorting GPIO0 to ground for flashing, so it is quite straightford to replace the link with a handier plug-on momentary push-button for starting into flash mode, in which case the GPIO0 'flashing' button could then conveniently be used in 'running' mode as user input for selecting options etc if required (need to move OLED i2c Data from GPIO0 to something like GPIO15, of course).

The OLED is simplicity itself to connect direct to the boards pins with just 4 female to female jumpers, and after flashing, it is then ready to be taken anywhere.


Having suggested the item, I think I need to offer a little test program I wrote for it, because of some tips and gotchas that are pointed out in the comments..
Code: Select all/*
ESP8266-12E Dev Kit Breakout Board Tester
Created by Electroguard on 9th Aug 2015 as a personal convenience, and offered into the public domain for anyone to use.

Purpose: Exercises LDR and all LEDs to provide visual confirmation of everything working.

Operation: Steps sequentially through all available GPIOs to light their corresponding LEDs and verify correct pinouts (see notes below)
Each pass also steps through the RGB LED primary colours (first Red, then Green, then Blue)
LDR light-level adjusts sequencing speed (darker = slower) and RGB LED brightness after each pass (darker = dimmer)
The 'Nightrider' sequencing clearly shows the appropriate LED being controlled by the addressed GPIOs, irrespective of any mis-labelling:
 led1        power  (blue)
 led2        gpio2  (red)
 led3        gpio0  (red)
 led4        gpio5  (red)    (incorrectly labelled)
 led5        gpio4  (red)    (incorrectly labelled)
 led6        gpio14 (red)
 led7        gpio16 (red)
 RGB LED R   gpio15  red
 RGB LED G   gpio12  green
 RGB LED B   gpio13  blue
 
Known Board Problems:
 * TX and RX are wrongly labelled on the bottom 3-pin UART connector, the pin labelled RX is actually TX on the side connector, and vice-versa
 * GPIOs 4 and 5 are incorrectly labelled on current boards, which seems to be due to a more widespread problem of these pins being incorrectly
   tranposed on the ESP-12 modules themselves - if intending to use SPI, this causes the SPI pins to be wrong for those unaware of the problem.

Flashing Tips:
 * Module restarts after flashing, so remove tombstone link once flash mode is initiated to prevent restarting into flash mode again after
   flashing is complete, but also to prevent the programmed GPIO0 driven output from being shorted to ground.
   Such problems could be avoided by replacing the 'Program' (GPIO0) link with a plug-on momentary button, and also a similar Reset button.
   Pressing Reset while holding 'Program' button would initiate flash mode, then both could be released for the actual flashing.
 * Flashed programs may fail to start if UART's USB is unplugged, due to un-powered UART RX pin affecting ESPs startup mode, but the problem
   is easily solved by just disconnecting the un-powered UARTs RX connection, the flashed program should then run ok when the module is reset.
   If you just wish to test flashed program for correct operation, then the program should start ok if USART is left connected to USB.

Common Sense:
 * The pcb has integral 3.3v regulator.
 * It would be wise to solder a 10uF smoothing cap across the supply input if using a DC mains adapter (eg: 5v or 9v) instead of batteries.
 */

int timer = 500;               // Initial sequencing delay - gets modified by LDR light level.
int ledPins[] = {
  2, 0, 4, 5, 14, 16           // Array of LED indicators and their associated GPIOs
 };                            // Note: postions of GPIOs 4 and 5 have been reversed in the array sequence to correct pin labelling errors

int pinCount = 6;              // The number of available GPIOs
int rgbPins[] = {15, 12, 13};  // The GPIOs for RGB LEDs - Red, Green, Blue
int rgbCount = 3;
int rgbPin = -1;
int brightness = 200;          // Initial RGB LED brightness - gets modified by LDR light level

void setup() {
  Serial.begin(9600);          // Initialize serial communication to display LDR value in serial monitor
  for (int thisPin = 0; thisPin < pinCount; thisPin++) {  // Initialise GPIOs and turn off LEDs
    pinMode(ledPins[thisPin], OUTPUT);
    digitalWrite(ledPins[thisPin], HIGH);
  }
  for (rgbPin = 0; rgbPin < rgbCount; rgbPin++) {
    pinMode(rgbPins[rgbPin], OUTPUT);
    analogWrite(rgbPins[rgbPin], 0);
  }
}

void loop() {
  int sensorValue = analogRead(A0);                       // Read LDR value on analog pin 0
  Serial.println(sensorValue);                            // Print LDR value to serial monitor
  timer = map(sensorValue, 100, 400, 10, 1000);           // Adjust sequencing timer according to LDR value
  brightness = map(sensorValue, 50, 450, 255, 2);         // Use LDR value to adjust RGB LED brightness level
  for (int thisPin = 0; thisPin < pinCount; thisPin++) { // Sequence up through GPIOs and their corresponding LEDs
    digitalWrite(ledPins[thisPin], LOW);                 
    sensorValue = analogRead(A0);                         // Update LDR value during sequencing
    timer = map(sensorValue, 50, 450, 10, 1000);          // Use LDR value to adjust sequencing timer value
    delay(timer);
    digitalWrite(ledPins[thisPin], HIGH);
  }

  analogWrite(rgbPins[rgbPin], 0);                        // Turn off RGB LED old colour and select next RGB colour
  rgbPin++;
  if (rgbPin > rgbCount-1) rgbPin = 0;
  analogWrite(rgbPins[rgbPin], brightness);               // Turn on RGB LEDs next colour

  for (int thisPin=pinCount-2; thisPin >= 1; thisPin--) { // Sequence back down through GPIOs and their corresponding LEDs
    digitalWrite(ledPins[thisPin], LOW);
    sensorValue = analogRead(A0);                         // Update LDR value during sequencing
    timer = map(sensorValue, 50, 450, 10, 1000);          // Use LDR value to adjust sequencing timer value
    delay(timer);
    digitalWrite(ledPins[thisPin], HIGH);
  }
}
User avatar
By danbicks
#25912 Electroguard,

A pleasure buddy. This little cheap board is great, I must admit I have a couple of these just for quick rapid Proto-type. On the 2 pin header for GPI/O 0 I have soldered a push switch just for programming purposes, this board serves really well prior to designing a product solution PCB.

Glad you like'd the example.

Dans