-->
Page 1 of 1

8266 Client / Server communication to perform action

PostPosted: Thu Feb 02, 2017 7:51 pm
by housecaz
Hello friends!

I just got an Arduino and an 8266 this week. I've managed to flash the 8266 and have some sample code running on it, but programming it feels way out of my league.

I'm looking for some code that will require 2 8266s:

- Server
One 8266 should create a server
This server will only connect to the network briefly and infrequently, but when it is on, it should respond to client request, or depending on ease, just be pingable. This unit will mostly be powered off, but when it is powered on, it should attempt to connect to the wireless network. This server will be assigned a static DHCP address by the router (based on MAC).

- Client
One 8266 should create a client

The client will loop trying to connect to the server via the wireless connection, and when it finds the server (via ping, or via a message that the two communicate) then the client performs an action (EG, light up an LED). If the ping fails (after lighting up from a success) then the light should turn off after X seconds.

How hard is this to program?

I've read some documents (https://cdn.sparkfun.com/assets/learn_t ... _v0.30.pdf) that show the available 8266 AT commands, but I have no real experience with programming. I've seen the WiFi101 boards such as the MKR1000, but I'm curious if this is possible with just the 8266 + Arduino Uno R3?

Thank you in any case!

Re: 8266 Client / Server communication to perform action

PostPosted: Thu Feb 02, 2017 11:26 pm
by housecaz
On a related note, I do have this code, but I'm getting an error:

WebServerLed:17: error: conflicting declaration 'SoftwareSerial Serial1'
SoftwareSerial Serial1(0, 1); // RX, TX
^
In file included from /home/donkey/.arduino15/packages/esp8266/hardware/esp8266/2.3.0/cores/esp8266/Arduino.h:245:0,
from sketch/WebServerLed.ino.cpp:1:
/home/donkey/.arduino15/packages/esp8266/hardware/esp8266/2.3.0/cores/esp8266/HardwareSerial.h:145:23: error: 'Serial1' has a previous declaration as 'HardwareSerial Serial1'
extern HardwareSerial Serial1;
^
exit status 1
conflicting declaration 'SoftwareSerial Serial1'


Code: Select all/*
 WiFiEsp example: WebServerLed
 
 A simple web server that lets you turn on and of an LED via a web page.
 This sketch will print the IP address of your ESP8266 module (once connected)
 to the Serial monitor. From there, you can open that address in a web browser
 to turn on and off the LED on pin 13.

 For more details see: http://yaab-arduino.blogspot.com/p/wifiesp.html
*/

#include "WiFiEsp.h"

// Emulate Serial1 on pins 6/7 if not present
#ifndef HAVE_HWSERIAL1
#include "SoftwareSerial.h"
SoftwareSerial Serial1(0, 1); // RX, TX
#endif

char ssid[] = "Twim";            // your network SSID (name)
char pass[] = "12345678";        // your network password
int status = WL_IDLE_STATUS;

int ledStatus = LOW;

WiFiEspServer server(80);

// use a ring buffer to increase speed and reduce memory allocation
RingBuffer buf(8);

void setup()
{
  Serial.begin(115200);   // initialize serial for debugging
  Serial1.begin(9600);    // initialize serial for ESP module
  WiFi.init(&Serial1);    // initialize ESP module

  // check for the presence of the shield
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    // don't continue
    while (true);
  }

  // attempt to connect to WiFi network
  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to WPA SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network
    status = WiFi.begin(ssid, pass);
  }

  Serial.println("You're connected to the network");
  printWifiStatus();
 
  // start the web server on port 80
  server.begin();
}


void loop()
{
  WiFiEspClient client = server.available();  // listen for incoming clients

  if (client) {                               // if you get a client,
    Serial.println("New client");             // print a message out the serial port
    buf.init();                               // initialize the circular buffer
    while (client.connected()) {              // loop while the client's connected
      if (client.available()) {               // if there's bytes to read from the client,
        char c = client.read();               // read a byte, then
        buf.push(c);                          // push it to the ring buffer

        // printing the stream to the serial monitor will slow down
        // the receiving of data from the ESP filling the serial buffer
        //Serial.write(c);
       
        // you got two newline characters in a row
        // that's the end of the HTTP request, so send a response
        if (buf.endsWith("\r\n\r\n")) {
          sendHttpResponse(client);
          break;
        }

        // Check to see if the client request was "GET /H" or "GET /L":
        if (buf.endsWith("GET /H")) {
          Serial.println("Turn led ON");
          ledStatus = HIGH;
          digitalWrite(13, HIGH);
        }
        else if (buf.endsWith("GET /L")) {
          Serial.println("Turn led OFF");
          ledStatus = LOW;
          digitalWrite(13, LOW);
        }
      }
    }
   
    // close the connection
    client.stop();
    Serial.println("Client disconnected");
  }
}


void sendHttpResponse(WiFiEspClient client)
{
  // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
  // and a content-type so the client knows what's coming, then a blank line:
  client.println("HTTP/1.1 200 OK");
  client.println("Content-type:text/html");
  client.println();
 
  // the content of the HTTP response follows the header:
  client.print("The LED is ");
  client.print(ledStatus);
  client.println("<br>");
  client.println("<br>");
 
  client.println("Click <a href=\"/H\">here</a> turn the LED on<br>");
  client.println("Click <a href=\"/L\">here</a> turn the LED off<br>");
 
  // The HTTP response ends with another blank line:
  client.println();
}

void printWifiStatus()
{
  // print the SSID of the network you're attached to
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print where to go in the browser
  Serial.println();
  Serial.print("To see this page in action, open a browser to http://");
  Serial.println(ip);
  Serial.println();
}