Example sketches for the new Arduino IDE for ESP8266

Moderator: igrr

User avatar
By johnbravo
#40458 I know this is not perfect, I just started working on it. I dont know if anyone has any interest in it, but i figured i would let everyone take a look. tell me what you think. And please keep in mind that this is a work in progress.

Just upload the sketch to the esp8266 and connect over serial from arduino to esp8266 and send the commands.

UPDATE New Commands Added!



Commands
This sets your ssid of the access point you want to connect to
SSID|linksys

This is the password to the access point
PASS|123456

The host you want to connect to
HOST|www.adafruit.com

The url for the web page you want to see
URL|/testwifi/index.html

Sends data to remote host
SEND|Somedata

Sets the port of the remote host
PORT|80

Request web page
GET|

connect to the access point
JOIN|





Code: Select all    #include <ESP8266WiFi.h>
   WiFiClient client;
   char ssid[56];
   char password[56];
    char host[56];
   char url[500];
   uint16 port;

void setup() {
   Serial.begin(115200);
   delay(100);
}
 
void Joinnetwork() {
   Serial.println();
   Serial.println();
   Serial.print("Connecting to ");
   Serial.println(ssid);
   
   WiFi.begin(ssid, password);
   
   while (WiFi.status() != WL_CONNECTED) {
      delay(500);
      Serial.print(".");
   }

   Serial.println("");
   Serial.println("WiFi connected");
   Serial.println("IP address: ");
   Serial.println(WiFi.localIP());

   GetPage();
}

void GetPage() {
   Serial.print("connecting to ");
   Serial.println(host);
 
   if (!client.connect(host , port)) {
      Serial.println("connection failed");
      return;
   }

   Serial.print("Requesting URL: ");
   Serial.println(url);
   
   client.print(String("GET ") + url + " HTTP/1.1\r\n" +
   "Host: " + host + "\r\n" +
   "Connection: close\r\n\r\n");
   
   delay(5000);
   
   while(client.available()){
      String line = client.readStringUntil('\r');
      Serial.print(line);
   }
   
   Serial.println();
   Serial.println("closing connection");
}



void loop() {
   String content = "";
   char character;
   
   while (Serial.available()){
      character = Serial.read();
      content.concat(character);
   }

   if (content != "") {
      CMD = stringOne.substring(0, 4);
      DATA = stringOne.substring(5);

      if (CMD == "SSID"){
         strcpy(ssid, DATA.c_str());
      }else if (CMD == "PASS"){
         strcpy(password, DATA.c_str());
      }else if (CMD == "HOST"){
         strcpy(host, DATA.c_str());
      }else if (CMD == "SEND"){
         client.print(DATA);
      }else if (CMD == "PORT"){
         port = DATA.toInt();
      }else if (CMD == "URL") {
         strcpy(url, DATA.c_str());
      }else if (CMD == "GET") {
         GetPage();
      }else if (CMD == "JOIN") {
         JoinAccessPoint();
      }
   }
   delay(100);
}
 
Last edited by johnbravo on Sun Feb 07, 2016 3:12 am, edited 3 times in total.