Post topics, source code that relate to the Arduino Platform

User avatar
By Kalix
#6734 I just got your sketch working on my Arduino Pro Mini 3v.
Also I can confirm esp.println -> esp.print is needed to get a correct reply.

On my Pro Mini I switched the two serial connections because the software serial did not seem to be keeping up with the esp8266 chatter. This makes uploading a new sketch a bit less ideal because I have to disconnect the esp serial wires before uploading it.
User avatar
By Dir
#6829 And my version of sketch.
Working Ok on Arduino Pro Mini.
Lot of debug messages..

Code: Select all/*

Wiring Setup (Arduino Pro Mini):
- Pins 11 and 12 to ESP8266 (running at baudrate of 9600) connected via SoftwareSerial
- 3.3v and GND to ESP8266 (DO NOT USE 5v!!!!!)
- Pins 2-10 are setup to be toggled high/low (e.g. relay switches, LEDs, etc.)
- Pins 0 and 1 are left untouched for USB/Serial debugging (will print out its IP address here)
- Pin 13 connect to RST of ESP8266. It's  being used for hardware ESP8266 reset.
How To Use:
- Fill in the Strings below to set your WiFi network details for connection
- Load onto Arduino
- Remove all power from Arduino to properly reset ESP8266
- Return power to Arduino, wait for the LED on pin 13 to illuminate in a solid manner
- Send HTTP requests to the IP address of your Arduino as follows:
  - http://x.x.x.x/CMD=1111E
  - Translation: set the first four pins (2-5) to HIGH
  - Maximum command length: 9 (e.g. 001100110)
- If using a browser or cURL request, the data returned will be a JSON object reporting
  the current HIGH/LOW status of all 9 controllable pins
*/


#include <SoftwareSerial.h>
#define BUFFER_SIZE 512
#define GET_SIZE 64
#define dbg Serial  // USB local debug
String MSSID= "rat_test";
String PASS="";
SoftwareSerial esp(12,11);

String serverPort = "80";

char buffer[BUFFER_SIZE]; // Don't touch
char get_s[GET_SIZE];
char OKrn[] = "OK\r\n"; // Don't touch


bool ExecCmd(String cmd, int timeout, char* term=OKrn,char* term1=OKrn) {
  unsigned long t=millis();
  dbg.println("ExecCmd:" + cmd);
  esp.println(cmd);
 
  bool found=false;
  int i=0;
  int len=strlen(term);
  int len1=strlen(term1);
  while(millis()<t+timeout) {
    if(esp.available()) {
      buffer[i++]=esp.read();
     
      if(i>=len) {
        if(strncmp(buffer+i-len, term, len)==0 || strncmp(buffer+i-len1, term1, len1)==0) {
          found=true;
          break;
        }
      }
    }
  }
  buffer[i]=0;
  dbg.print(">>");
  dbg.print(buffer);
  dbg.println("<<");
  return found;
}


// ##################
// ## Setup & Loop ##
// ##################
void setup() {
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(10,OUTPUT);
  pinMode(13, OUTPUT); // For debugging purposes w/o USB
  //reset esp
  digitalWrite(13,LOW);
  delay(400);
  digitalWrite(13,HIGH);
  delay(400);
 
  esp.begin(9600);
  dbg.begin(9600);
  dbg.println("DEBUG: Running Setup");
  // Reset ESP, Test, Configure, Connect, Start Server
  ExecCmd("AT+RST",6000,"ready"); // Reset
  ExecCmd("AT",2000); // Test
  ExecCmd("AT+CWMODE=1",2000); // Set to client mode
  ExecCmd("AT+CWJAP=\"" + MSSID + "\",\"" + PASS + "\"",6000); // Join AP
  ExecCmd("AT+CIPMUX=1",2000); // Allow multiple connections
  ExecCmd("AT+CIPSERVER=1,"+serverPort,2000); // Start server on port
  ExecCmd("AT+CIFSR",1000);
  dbg.println("DEBUG: Setup complete\n\n");
 
}



void loop() {
  //updateLights(currentCommand);
  // BEGIN - Borrowed Code
  int ch_id, packet_len, lssid = 0, lpass = 0,i=0;
  char *pb; 
  char cmd[32];
  String s;
 
  if (esp.find("+IPD,") ){
   dbg.println("Incoming message");
   dbg.print("Get data length:");
   //get data length
   i=0;
   while (esp.available()) {
     buffer[i++]=esp.read();
     if (buffer[i-1]==':') break;
   }
   buffer[i]=0;
   sscanf(buffer, "%d,%d", &ch_id, &packet_len);
   dbg.println(packet_len);
   
   if (packet_len>0) {
    //Got data
    dbg.print("Got Data:");
    i=0;
    while (esp.available() && i<packet_len) buffer[i++]=esp.read();
    buffer[i]=0;
    dbg.println(buffer);
    if (strncmp(buffer, "CMD=", 4) == 0) {   //Direct TCP command CMD=11E via NC for example
     dbg.print("Got CMD:");
     s=String(buffer+4);
     dbg.println(s);
     updateLights(s);
     serveReply(ch_id,s); 
    };
     if (strncmp(buffer,"GET /CMD=",9) == 0) { //WEB command http://x.x.x.x/CMD=111E
        dbg.print("Got WEB CMD:");
        s=String(buffer+9);
        s.remove(s.indexOf('E')+1);
        dbg.println(s);
        updateLights(s);
        serveReply(ch_id,s);
     };
   }
  }
  // END - Borrowed Code
}
// ##################
// ## Setup & Loop ##
// ##################



void serveReply(int ch_id,String received){
 
  unsigned long t=millis();
  String reply = "HTTP/1.1 200 OK {"+received+"}";
  dbg.println("Sending back a response");
  //ExecCmd("AT+CIPSTATUS="+String(ch_id),2000);
  ExecCmd("AT+CIPSEND="+String(ch_id)+","+String(reply.length()+2),2000,">"); // change 18 to reply length
  ExecCmd(reply,2000,"Unlink");
  dbg.print("ServerReply:");
  dbg.println(millis()-t);
}


void updateLights(String inbound){
  dbg.println("UpdateLights:"+inbound);
  for(int x = 0; x < inbound.length()-1 && x<8; x++){
    int powerValue = inbound[x] - '0';
    if (powerValue == 1){
      //ledOn(x);
      digitalWrite(x+2, HIGH);
    }
    if (powerValue == 0){
      //ledOff(x);
      digitalWrite(x+2, LOW);
    }
  }
}

User avatar
By maptor
#6850 Thanks, Dir! I committed your improvements to my original code and gave you a bit of credit in the notes. ;) Glad to see that you've managed to get it running on a Pro.