Example sketches for the new Arduino IDE for ESP8266

Moderator: igrr

User avatar
By Samighi11
#29107
if you have A0, A1, A2, SDA, SCL and RESET set, and the chip is powered
Code: Select all VCC - Source, VDD ground,
the I2C scanner sketch should pick it up. That is where you would start. Until I2C scanner shows you the address, none of your sketches will work.


Sorry, VSS (not VCC) is ground, VDD is Positive

Vcc Vdd = Positive supply voltage
Vee, Vss = Negative supply, ground
User avatar
By Samighi11
#29113
johntech2014 - Ok I 'm asking for help on this one again.



For a sanity check, I tested this myself. A0, A1, A2 are to GND (0x20 by default), RESET to 3.3, VDD to 3.3, VSS to GND, SCL, SDA to GPIOs on ESP8266. My main issue which I keep forgetting about is the the FTDI doesn't generate enough power, I always have to use an external 3.3V source. That is 50% of my errors.

Here is the code for I2C scanner.

http://playground.arduino.cc/Main/I2cScanner?action=sourceblock&num=1

change the following line

Wire.begin();

to

Code: Select allWire.begin(4,5);
OR
Code: Select allWire.begin(5,4);

or any GPIO pin you are using. I used 4 and 5 just to stay close to the Arduino pin numbering.

the scanner should return

Scanning...
I2C device found at address 0x20 !
done
User avatar
By johntech2014
#29118
Samighi11 wrote:
johntech2014 - Ok I 'm asking for help on this one again.

Here is the code for I2C scanner.

http://playground.arduino.cc/Main/I2cScanner?action=sourceblock&num=1

Scanning...
I2C device found at address 0x20 !
done


Samighi11

Let me stop you for a second..... I have loaded your sketch, ran it and it works fine. I can bring up the web page and type http://192.168.XXX.XXX/ledtest?A=5&B=5 and it turns on the correct LED's as it should. I just had to change
Code: Select all Wire.begin(4,5) to Wire.begin(0,2)
and it works fine on the ESP8266-01.

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.

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 do appreciate both of your guys information and help on this matter. I'm still learning....
Thank you

John
User avatar
By Samighi11
#29120
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.

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