Close

First almost useful ESP8266 Testapplication

A project log for Alarmierungsuhr

This is a Clock Build which will switch to a Counter if a alert is received.

stefan-xpStefan-Xp 11/23/2016 at 21:111 Comment

Greatly Inspired by #ESP8266 Retro Browser I have rewritten my sketch a bit :)

/*
  Software serial With ESP 8266 Test
  Tested with Arduino Uno
  Autor: Stefan-Xp @ HackADay.io
  
  +++ ESP8266 ++
  D10 --> ESP8266 TX
  D11 --> ESP8266 RX
  
  +++ Input +++
  A0 --> Button --> Ground
  
  +++ Output +++
  // Status LEDs
  tbd
 */
 
#include <SoftwareSerial.h>
// Access Point
#define SSID        "tbd"
#define PASS        "0123456789" // My luggage has the same combination!

// General Defines
#define TIMEOUT     5000 // ~ms
#define SERVER_IDENTIFIER "useYourOwn;-)"

// I/O Defines
#define SWITCH_PIN A0

SoftwareSerial mySerial(10, 11); // RX, TX  ; SoftwareSerial for ESP8266
char charbuffer[255];            // Characterbuffer for incoming Data
int inCount;                     // Count of incoming Data Bytes

boolean sendHTTPRequest(String Parameter);  // Function for sendingHTTPRequest

void setup()  
{
  // Open serial communications and wait for port to open:
  Serial.begin(19200);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

  // set the data rate for the SoftwareSerial port for ESP8266
  mySerial.begin(9600); // Perhapps you need to Adjust the Baudrate at the ESP8266!
  
  Serial.print("Ready for commands!\r\n");
  
  // This is one Input Pin
  pinMode(SWITCH_PIN, INPUT_PULLUP);
}

void loop() // run over and over
{
   byte inputChar, rndCount;
   String OutPutParameter;
   
   int iCount;
   
   /* Maybe there should be regular checks if the ESP8266 is still 
      connected or something...
   */

   // This reads the Data as long as there are some available
   while (mySerial.available()>0)
   {
      inputChar = mySerial.read();
      charbuffer[inCount] = inputChar;    

      // If there is a new line, there shall also be a new row
      if(inputChar == '\n')
      {
        inCount++;
        charbuffer[inCount] = '\r';
      }
      
      inCount++;
      
      // Print if buffer is full
      if(inCount > 254)
      {
        Serial.print(charbuffer);
        inCount = 0;
        
        while (inCount > 0)
        {
          // clear buffer
          inCount--;
          charbuffer[inCount] = 0;
        }
      }
      
   }  
  
  // If there are some Datas in the Buffer, print them now.
  if(inCount > 0)
  {
    Serial.print(charbuffer);
    inCount = 0;
    
    while (inCount > 0)
    {
      // clear buffer
      inCount--;
      charbuffer[inCount] = 0;
    }
  }
  
  // If there are commands via Serial...
  if (Serial.available())
    switch(Serial.read())

    {
    case '?':
       Serial.print("Request Status ... ");
       mySerial.print("AT+CIPSTATUS\r\n");
       break;
       
  
    case 's': // Send Testdata
        OutPutParameter = "/ESP_Test/Test.php?info=RandomNumber%20";
        OutPutParameter += random(1000);
        OutPutParameter += "<br>";
        
       sendHTTPRequest(OutPutParameter);
   
    }
 
   // If Button is pressed, send on release..
   if(digitalRead(SWITCH_PIN) == LOW)
   {
       while(digitalRead(SWITCH_PIN) == LOW)
         rndCount++;
       
        OutPutParameter = "/ESP_Test/Test.php?info=RandomNumber%20by%20Button:%20";
        OutPutParameter += rndCount;
        OutPutParameter += "<br>";
        
        sendHTTPRequest(OutPutParameter);
   }
}

// This Function waits as long as the desired String is received
boolean waitForString(String myString)
{
 int TimeOut = 0;
 byte checkNumber = 0;
 Serial.print("Waiting for String " + myString + " ... ");
 
 // Do this until TimeOut
 while (TimeOut < TIMEOUT)
    {
      if(mySerial.available()>0)
      {
        // Check every Character of myString
        if(mySerial.read() == myString[checkNumber])
        {
          checkNumber++;
          if(checkNumber >= myString.length())
          {
            // Pass - ALL Characters were found
            return true;
          }
        }  
      }
      else
      {  
        delay(1);
        TimeOut++;
      }
    } 
    
    return false;
}

// This function sends a HTTP Request with specific parameters
boolean sendHTTPRequest(String Parameter)
{
    Serial.println("Sending HTTP Request...");
    
    String cmd, cmd2;
    
    // Contact the Server
    cmd = "AT+CIPSTART=\"TCP\",\"";
    cmd += SERVER_IDENTIFIER;
    cmd += "\",80\r\n";
    mySerial.print(cmd);
    
    delay(100);
    // Check if "ok"
    if(waitForString("ok") == false)
      return false;
    else
      Serial.println("pass");
    
    // Build Command to be sent to the Server  
    cmd = "GET ";
    cmd += Parameter + " HTTP/1.1\r\nHost: ";
    cmd += SERVER_IDENTIFIER;
    cmd += ":80\r\n\r\n";
    
    // Tell the ESP8266 to listen to Data
    cmd2 = "AT+CIPSENDBUF="; 
    cmd2 += cmd.length() ;
    cmd2 += "\r\n";
    mySerial.print(cmd2);
    
    // Now wait for >
    if(waitForString(">") == false)
      return false;
    else
      Serial.println("pass");
      
    // Send Request
    mySerial.print(cmd);
    
    // Maybe a check should be here to check if the Data was transfered sucessfully.
}

// Connect to the specified wireless network.
// To do: Implement a StatusLED for Connection State.
// To do: Test This.
boolean connectWiFi()
{
  Serial.print("WifiNetwork Connect ... ");
  
  String cmd = "AT+CWJAP=\""; 
  cmd += SSID; 
  cmd += "\",\""; 
  cmd += PASS; 
  cmd += "\"";
  
  // Check if "ok" 
  if(waitForString("ok") == false)
  {
    Serial.println(" fail");
    return false;
  }
  else
  {
    Serial.println(" pass");
  }
  
  return true;
}

The Code is a bit bigger now... i think nothing for the 1k Contest :-P

And here is the Code in the PHP Page:

<?php
$inhalt = $_GET['info'] . "\n\r";

$handle = fopen ("Result.html", a); // Attach
fwrite ($handle, $inhalt);
fclose ($handle);

echo "Write: " . $inhalt . "Success"
?>

And also the Result.htm:

RandomNumber 807<br>

RandomNumber 249<br>

RandomNumber 807<br>

RandomNumber by Button: 208<br>

RandomNumber by Button: 73<br>

RandomNumber by Button: 2<br>

RandomNumber 807<br>

RandomNumber 807<br>

RandomNumber by Button: 99<br>

RandomNumber 807<br>

RandomNumber 249<br>

RandomNumber 73<br>

RandomNumber 807<br>

RandomNumber 249<br>
Did you notice, that you get the same Random Numbers every Time the Arduino reboots? :-D

The next steps are:

- Adding at least 2 Status LEDs (Connection State, Request State)

- Exchanging Softwareserial for Serial

- Add the SoftwareSerial for the BOSS Swissphone Pager

- Check the Webinterface of http://www.Divera247.com

- Develop an Algorithm to Get a Useful AlertMessage.

- Solder together an Hardware incarnation of this Device ;-)

I hope you enjoyed this writeup and comments are always apreciated!

Discussions

Stefan-Xp wrote 11/23/2016 at 21:12 point

Ahh and also need to lock the Request in case it is not completed...

  Are you sure? yes | no