Example sketches for the new Arduino IDE for ESP8266

Moderator: igrr

User avatar
By Samighi11
#29121
Here is what I'm trying to do. On my model railroad layout I have a yard for holding cars. I will have 4 or 5 tracks that will have track switches to select which set of tracks the train will enter/exit. For example if I want the train to enter/leave on track 1, I would enter 11 ( depending on how this gets coded) 11 would equal 1 for the entrance switch and the other 1 is for the exit switch, which would enable the train to enter on track 1 and leave on track 1. In a similar situation if I wanted to use track 3, I would enter 77 ( depending on how this gets coded) which would enable switches 1, 2 and 3 on both ends of the yard. I need to be able to change the switches so if a train pulls in to track 1 another train will be able to leave on track 3. So once I get this straightened out then I need to work on converting this over to use buttons on the screen or a box to enter the code to select the correct track combination needed.


I personally have a web server nearby or on the internet. I use (this gets into Web coding more) that server to send this server commands. that way my ESP8266 never has to be updated. The way it works is that on my PHP page on my web server (not ESP8266) using java script, I send the ESP8266 commands. This requires some understanding of how HTTP traffic travels. You can create a nice HTML/Jquery type page on your own server (say Mac, PC or internet hosted), and it will send commands to your local ESP8266. Again, this takes some trial and error as your ESP8266 could get a new IP Address.

(I just finished similar code with jQuery mobile a few days ago to operate an ESP8266, PWM to drive a self made Lego car, with lego motor)

Example

Code: Select all<TABLE BORDER=1 onclick="clickthis(event)">
<TR><TD COLSPAN=5>IP 10.0.57.103</TD></TR>
<TR>
<TD ID=ALED1>On</TD>
<TD ID=ALED2>Off</TD>......</TR></TABLE>


whenever anyone clicks, or touches (I use phone) the screen, an event is triggered.

Clickthis event does some magic on the screen (light up track 1,2,3 for example on your web page)

Code: Select allfunction clickthis(ev)
{

 elID = ev.target.id;
// alert(elID);
 el = document.getElementById(elID);
 strTxt = el.innerHTML
 if (strTxt == "On") { el.innerHTML = "Off"; el.class = "Off"; el.style.backgroundColor = "white"; el.style.Color = "black"; }
 else { el.innerHTML = "On"; el.class = "On"; el.style.backgroundColor = "Yellow"; el.style.Color = "Black"; }
  sentData();
// if (ev.target.id
}


and senddata sends the desired control (think FIRMATA) to the ESP8266. ESP is dumb and only does what it is told. You make your website user friendly. The only catch is that the ESP and your webpage have to be accessible at the same time from your computer/device. They don't have to be in any way connected. In my case, my Website is at http://someinternetsite.com/php/leds.php and my ESP is local at 10.0.57.xxx. This uses the browser to make the ESP8266 call (see XMLHTTP below)

Code: Select allfunction sentData()
{
 ledAA = 0;
 ledAB = 0;
 for (var i=10;i>0;i--) {
   txt = document.getElementById("ALED"+i).innerHTML;
    bit = 0;
    if (txt == "On") bit = 1;
    if (i >= 9)
    {
     ledAB += bit *  Math.pow(2 ,(i-9));
    }
    else
     ledAA += bit *  Math.pow(2,(i-1));
   
  }
 
  var xmlhttp;
   if (window.XMLHttpRequest)
     {// code for IE7+, Firefox, Chrome, Opera, Safari
     xmlhttp=new XMLHttpRequest();
     }
     var url = "http://10.0.57.103/ledtest?A=" + ledAA+ "&B=" + ledAB;

      xmlhttp.open("GET", url);
   xmlhttp.send("");
 
}


This might be a lot to absorb. It requires your intimate knowledge of JavaScript, Jquery, PHP, etc. Remember that your ESP could have many different IP Addresses, so you have to allow your webpage to somehow find it. I have not finished that part yet, but I may add an I2C OLED to the ESP so I know the IP address and that it is doing the right thing.

I will be happy to post all my code, but it is very tailored to me. If you take "senddata" part and just build a page, it is a good start. If you are able to build your website and post it, I can help finish it. It is pretty simple, just requires a lot of pre-planning. (I have seen model railroad shows, so I am aware of your situation roughly)

sa
User avatar
By martinayotte
#29124
johntech2014 wrote:In talking to martinayotte on another post , he brought up the idea of using AdaFruit's library where there is this code
Code: Select allvoid Adafruit_MCP23017::writeGPIOAB(uint16_t ba)
that you enter the hex number representing the LEDs you want on or off. In looking at AdaFruits library, they are basically doing the same thing you are doing only in a library. I have been searching the internet trying to find a sample sketch on using the code above. So far I have not found anything. Using their library to turn on /off 1 pin is similar to using a regular Arduino command. Since I'm not having much success with the above code I may revert back to yours and start cutting out all the extra stuff to where I have the bear minimum of code and then start trying to create buttons instead of typing the commands on the address line. I'm sorry if I lead you to believe I didn't get it working.

Which piece of code do you need ?
Because AdaFruits library is pretty straight forward. You have writeGPIOAB() to output many pins at the same time, but you also have digitalWrite() for single pin "à la Arduino".
Is it the "uint16_t" that confuse you ? It simply that MCP23017 has 16 GPIO pins.
User avatar
By johntech2014
#29143
martinayotte wrote:
johntech2014 wrote:In talking to martinayotte on another post , he brought up the idea of using AdaFruit's library where there is this
.......
bear minimum of code and then start trying to create buttons instead of typing the commands on the address line. I'm sorry if I lead you to believe I didn't get it working.

Which piece of code do you need ?
Because AdaFruits library is pretty straight forward. You have writeGPIOAB() to output many pins at the same time, but you also have digitalWrite() for single pin "à la Arduino".
Is it the "uint16_t" that confuse you ? It simply that MCP23017 has 16 GPIO pins.


I totally understand the Arduino way . No problems there. It's when you posted the void Adafruit_MCP23017::writeGPIOAB(uint16_t ba) that threw me. I understand entering Hex numbers to turn on the different output pins. I just didn't know what code I needed to put in a sketch to make this all happen. Then I asked the same questions over at AdaFruit they gave me the same answer as you. void Adafruit_MCP23017::writeGPIOAB(uint16_t ba). I even started looking at the library itself to see if I could figure it out. I was getting nowhere fast so I went back to look at the sketch from Samighi1, which at least I could see how that worked. So after several days of searching what this all meant, the AdaFruit guy came back this morning and said "This is all you need"
Code: Select allAdafruit_MCP23017 mcp;
 
void setup() { 
  mcp.begin();      // use default address 0
  for ( int i=0 ; i < 16 ; i++ ) {
    mcp.pinMode( i, OUTPUT );
  }
 mcp.writeGPIOAB( 0xf00f ); 
}

void loop () {
}


I can't believe after all that searching .... it was that simple!!!!! I will try this sketch this afternoon after work and see it in action. If that's all it takes I will be grateful the search is over.
User avatar
By johntech2014
#29199 Well the code from the last post "wasn't all I needed" so I had to fill in what was missing. Here is the entire sketch using the AdaFruit library showing both options for turning on the output pins either 1 at a time or in in groups . I have this running on an ESP8266-01 board. It turns on GPA0-GPA7 pins and GPB0 pin with the HEX # 0x01ff using
Code: Select allmcp.writeGPIOAB( 0x01ff );


Then it flashes GPA0 using
Code: Select allvoid loop() {
 delay(100);
 mcp.digitalWrite(0, HIGH);
 delay(100);
 mcp.digitalWrite(0, LOW);
}


Here is the entire sketch.
Code: Select all#include <Wire.h>
#include "Adafruit_MCP23017.h"

// Basic pin reading and pullup test for the MCP23017 I/O expander
// public domain!
// Connect pin #12 of the expander to Analog 5 (i2c clock)
// Connect pin #13 of the expander to Analog 4 (i2c data)
//***** Use pins 0 and 2 if using a ESP8266-01*****
// Connect pins #15, 16 and 17 of the expander to ground (address selection)
// Connect pin #9 of the expander to 5V (power)
// Connect pin #10 of the expander to ground (common ground)
// Connect pin #18 through a ~10kohm resistor to 5V (reset pin, active low)

// Output #0 is on pin 21 so connect an LED or whatever from that to ground

Adafruit_MCP23017 mcp;

void setup() {
 Wire.begin(0, 2);
 mcp.begin(); // use default address 0

 for ( int i = 0 ; i < 16 ; i++ ) { // set all pins to output
 mcp.pinMode( i, OUTPUT );
 }
 mcp.writeGPIOAB( 0x01ff ); // turn on corresponding pins to HEX value
}
// flip the pin #0 up and down

void loop() {
 delay(100);
 mcp.digitalWrite(0, HIGH);
 delay(100);
 mcp.digitalWrite(0, LOW);
}