Post topics, source code that relate to the Arduino Platform

User avatar
By eyalsch
#5353 I'm trying to move from ethernet shield to ESP8266.
I'm looking for an example of controlling a LED throw HTML page something equal to this example from the ethernet shield:

Code: Select all#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 1, 150 }; // ip in lan
byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(80); //server port

String readString;
int ledPin = 7;


void setup(){

    pinMode(ledPin, OUTPUT); //pin selected to control
    // Test LED
    digitalWrite(ledPin, HIGH); // set pin high
    delay(500);
    digitalWrite(ledPin, LOW); // set pin low
   
    //start Ethernet
    Ethernet.begin(mac, ip, gateway, subnet);
    server.begin();
}

void loop(){
    // Create a client connection
    EthernetClient client = server.available();
    if (client) {
        while (client.connected()) {
            if (client.available()) {
                char c = client.read();

                //read char by char HTTP request
                if (readString.length() < 100) {

                    //store characters to string
                    readString += c;
                }

                //if HTTP request has ended
                if (c == '\n') {
                    client.println("HTTP/1.1 200 OK"); //send new page
                    client.println("Content-Type: text/html");
                    client.println();

                    client.println("<HTML>");
                    client.println("<HEAD>");
                    client.println("<TITLE>Home Automation - Testing a LED</TITLE>");
                    client.println("</HEAD>");
                    client.println("<BODY>");
                    client.println("<H1 style=\"color:blue;\">Home Automation - Testing a LED</H1>");
                    client.println("<hr>");
                    client.println("<br>");

                    client.println("<H2><a href=\"/?lighton\"\">Turn On Light</a><br></H2>");
                    client.println("<H2><a href=\"/?lightoff\"\">Turn Off Light</a><br></H2>");

                    client.println("</BODY>");
                    client.println("</HTML>");

                    delay(1);
                    //stopping client
                    client.stop();

                    // control arduino pin
                    if(readString.indexOf("?lighton") >0) //checks for on
                    {
                        digitalWrite(ledPin, HIGH); // set pin high
                    }
                    else{
                        if(readString.indexOf("?lightoff") >0) //checks for off
                        {
                            digitalWrite(ledPin, LOW); // set pin low
                        }
                    }
                    //clearing string for next read
                    readString="";

                }
            }
        }
    }
}


THANKS.
User avatar
By EasyIoT
#5380 Hi, if you are using ESP8266 EasyIoT Arduino library https://github.com/iot-playground/Arduino/tree/master/Esp8266EasyIoT, this is very simple. Actual web page is on EasyIoT server with nice controls and javascript, but you can do many more things like data loging and secure ssl conenction. Use ESP8299 V0.9.9.2 firmware.

Here is code example:

#include <Esp8266EasyIoT.h>
#include <SoftwareSerial.h>

Esp8266EasyIoT esp;

SoftwareSerial serialEsp(10, 11);



#define RELAY_1 13 // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
#define RELAY_ON 1 // GPIO value to write to turn on attached relay
#define RELAY_OFF 0 // GPIO value to write to turn off attached relay


void setup()
{
serialEsp.begin(9600);
Serial.begin(115200);

Serial.println("EasyIoTEsp init");

esp.begin(incomingMessage, 3, &serialEsp, &Serial);
//esp.begin(incomingMessage, 3, &serialEsp);

pinMode(13, OUTPUT);

esp.present(1, S_DIGITAL_OUTPUT);
}

void loop()
{
esp.process();
}

void incomingMessage(const Esp8266EasyIoTMsg &message) {
// We only expect one type of message from controller. But we better check anyway.

Serial.println("New message");
if (message.type==S_DIGITAL_OUTPUT) {
// Change relay state
digitalWrite(message.sensor-1+RELAY_1, message.getBool()?RELAY_ON:RELAY_OFF);

Serial.print("Incoming change for sensor:");
Serial.print(message.sensor);
Serial.print(", New status: ");
Serial.println(message.getBool());
}
}
User avatar
By ohgary
#6000 When I run this code I get an error on the CIPSTART.. Looks like there is a missing quote on the port number. I checked the config file and it is quoted properly.
know issues?


AT+CIPSTART="TCP","192.168.1.185",9999"
User avatar
By EasyIoT
#6102
ohgary wrote:When I run this code I get an error on the CIPSTART.. Looks like there is a missing quote on the port number. I checked the config file and it is quoted properly.
know issues?


AT+CIPSTART="TCP","192.168.1.185",9999"


Try with version on GitHub https://github.com/iot-playground/Arduino/blob/master/Esp8266EasyIoT/examples/esp8266_relay/esp8266_relay.ino

Complete tutorial is here http://iot-playground.com/2-uncategorised/16-esp8266-wifi-relay-switch