Your new topic does not fit any of the above??? Check first. Then post here. Thanks.

Moderator: igrr

User avatar
By Electric Devil
#59876 HI All

I wrote a bit of code for UART - WiFi UDP bridge with settings stored in EEPROM memory of ESP-01
For flashing i use ESP8266Flasher.exe (NODE) - start address 0x00000, baudrate 230400, flash size 1M/80Mhz, SPI DIO

and i still get problem with self resseting ESP
in my mind - i do something wrong with flashing, can yu give me ideas how to flash.

Code: Select all#include <EEPROM.h>
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>

String ssid;
byte ssid_len;
String pass;
byte pass_len;
unsigned int local_Port;
unsigned int remote_Port;
char packetBuffer[UDP_TX_PACKET_MAX_SIZE];          // buffer to hold incoming packet
String inputString = "";         // a string to hold incoming data
volatile bool stringComplete = false;  // whether the string is complete
volatile byte attempt = 30;
bool wifi_en = true;
String ACK_string;
bool ack = false;
long chip_id;
WiFiUDP Udp;

void setup() {
  inputString.reserve(200);
  Serial.begin(115200);
  EEPROM.begin(512);
  delay(10);

  ssid_len = EEPROM.read(0);
  for (byte i = 1; i < ssid_len + 1; i++) {
    ssid += char(EEPROM.read(i));
  }

  pass_len = EEPROM.read(33);
  for (byte i = 34; i < pass_len + 34; i++) {
    pass += char(EEPROM.read(i));
  }

  local_Port = ((EEPROM.read(67) * 10000) + (EEPROM.read(68) * 100) + EEPROM.read(69));
  remote_Port = ((EEPROM.read(70) * 10000) + (EEPROM.read(71) * 100) + EEPROM.read(72));

  WiFi.begin(ssid.c_str(), pass.c_str());

  while (WiFi.status() != WL_CONNECTED && attempt > 0) {
    delay(500);
    Serial.print(".");
    attempt--;
  }

  if (WiFi.status() != WL_CONNECTED) {
    wifi_en = false;
    Serial.println("WiFi error");
  }

  if (WiFi.status() == WL_CONNECTED) {
    Udp.begin(local_Port);
    Serial.println("");
    Serial.println("WiFi connected");
  }

  Serial.println(ssid);
  Serial.println(pass);

}

void loop() {
  serialEvent();
  parse_serial();
  if (WiFi.status() != WL_CONNECTED && wifi_en == true) {
    WiFi.begin(ssid.c_str(), pass.c_str());
    Udp.begin(local_Port);
  }
}

void serialEvent() {
  while (Serial.available()) {
    char inChar = (char)Serial.read();
    inputString += inChar;
    if (inChar == '\n') {
      stringComplete = true;
    }
  }
}

void parse_serial(void) {
  if (stringComplete) {

    if (inputString.startsWith("SET_LOCAL_PORT=")) {
      inputString.remove(0, 15);
      local_Port = inputString.toInt();
      Udp.begin(local_Port);
      Serial.print("Local Port: ");
      Serial.println(local_Port);
      EEPROM.write(67, 0);
      EEPROM.write(68, 0);
      EEPROM.write(69, 0);
      EEPROM.write(67, local_Port / 10000);
      EEPROM.write(68, (local_Port % 10000) / 100);
      EEPROM.write(69, local_Port % 100);
      EEPROM.commit();
      inputString = "";
      stringComplete = false;
    }
    if (inputString.startsWith("SET_REMOTE_PORT=")) {
      inputString.remove(0, 16);
      remote_Port = inputString.toInt();
      Serial.print("Remote Port: ");
      Serial.println(remote_Port);
      EEPROM.write(70, 0);
      EEPROM.write(71, 0);
      EEPROM.write(72, 0);
      EEPROM.write(70, remote_Port / 10000);
      EEPROM.write(71, (remote_Port % 10000) / 100);
      EEPROM.write(72, remote_Port % 100);
      EEPROM.commit();
      inputString = "";
      stringComplete = false;
    }
    if (inputString.startsWith("SET_SSID=")) {
      inputString.remove(0, 9);
      if (inputString.length() > 32) inputString.remove(32);
      char ssid_tmp[inputString.length()];
      inputString.toCharArray(ssid_tmp, inputString.length());
      Serial.print("New SSID: ");
      Serial.println(inputString);
      EEPROM.write(0, inputString.length());
      for (byte i = 1; i < 33; i++) {
        EEPROM.write(i, 0);
        delay(10);
      }
      for (byte i = 1; i < inputString.length() + 1; i++) {
        EEPROM.write(i, ssid_tmp[i - 1]);
        delay(10);
      }
      EEPROM.commit();
      inputString = "";
      stringComplete = false;
    }
   
    if (inputString.startsWith("SET_PASS=")) {
      inputString.remove(0, 9);
      if (inputString.length() > 32) inputString.remove(32);
      char pass_tmp[inputString.length()];
      inputString.toCharArray(pass_tmp, inputString.length());
      Serial.print("New Pass: ");
      Serial.println(inputString);
      EEPROM.write(33, inputString.length());
      for (byte i = 34; i < 66; i++) {
        EEPROM.write(i, 0);
        delay(10);
      }
      for (byte i = 34; i < inputString.length() + 34; i++) {
        EEPROM.write(i, pass_tmp[i - 34]);
        delay(10);
      }
      EEPROM.commit();
      inputString = "";
      stringComplete = false;
    }

    if (inputString.startsWith("MODULE_HELP")) {
      Serial.println("*************************************************************************");
      Serial.println("* SET_LOCAL_PORT=12345 - (unsigned int)                                 *");
      Serial.println("* SET_REMOTE_PORT=12345 - (unsigned int)                                *");
      Serial.println("* SET_SSID=mywifissid - (mix alphanumeric lowercase UPPERCASE, max. 32) *");
      Serial.println("* SET_PASS=mywifipass - (mix alphanumeric lowercase UPPERCASE, max. 32) *");
      Serial.println("* MODULE_HELP - (this help)                                             *");
      Serial.println("*************************************************************************");
      inputString = "";
      stringComplete = false;
    }

    IPAddress ip = WiFi.localIP();
    ip[3] = 255;
    inputString.replace('\n', ' ');
    inputString.trim();
    Udp.beginPacket(ip, remote_Port);
    Udp.print(inputString);
    Udp.endPacket();
    inputString = "";
    stringComplete = false;
  }

  int packetSize = Udp.parsePacket();
  if (packetSize) {
    Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
    Serial.println(packetBuffer);
    if (ack) {
      ACK_string += "ACK+";
      ACK_string += packetBuffer;
      IPAddress ip = WiFi.localIP();
      ip[3] = 255;
      Udp.beginPacket(ip, remote_Port);
      Udp.print(ACK_string);
      Udp.endPacket();
      ACK_string = "";
    }
    memset(packetBuffer, 0, sizeof(packetBuffer));
  }
}

void EEPROMWritelong(int address, long value) {
  byte four = (value & 0xFF);
  byte three = ((value >> 8) & 0xFF);
  byte two = ((value >> 16) & 0xFF);
  byte one = ((value >> 24) & 0xFF);

  EEPROM.write(address, four);
  EEPROM.write(address + 1, three);
  EEPROM.write(address + 2, two);
  EEPROM.write(address + 3, one);
}

long EEPROMReadlong(long address) {
  long four = EEPROM.read(address);
  long three = EEPROM.read(address + 1);
  long two = EEPROM.read(address + 2);
  long one = EEPROM.read(address + 3);
  return ((four << 0) & 0xFF) + ((three << 8) & 0xFFFF) + ((two << 16) & 0xFFFFFF) + ((one << 24) & 0xFFFFFFFF);
}




below i attached my compiled code (arduino 1.6.8 / esp 2.3.0 / generic esp module )
You do not have the required permissions to view the files attached to this post.