Post topics, source code that relate to the Arduino Platform

User avatar
By chinaski
#84856 Hi.

I'm trying to build up a pseudo-framework for my esp8266 projects. The idea is to have the code that my devices share abstracted from the particulars of that device. Functional programming oriented.
i.e. multiWifi setup, MQTT, mDNS, power managment, etc.

I've already coded in plain C the basic structure but after compiling and uploading successfully, the esp goes crazy, never fully booting and in a strange loop.

Any recommendations on how to organise the code? Or something specially wrong with these files.
Thanks in advance.

Here are the files so far:

a_definitions.ino
Code: Select all//general imports
#include <ESP8266WiFi.h>        // Include the Wi-Fi library
#include <ESP8266WiFiMulti.h>   // Include the Wi-Fi-Multi library
#include <ESP8266mDNS.h>        // Include the mDNS library

ESP8266WiFiMulti wifiMulti;     // Create an instance of the ESP8266WiFiMulti class, called 'wifiMulti'

// Create a client ID
String deviceId = "8266-"+String(ESP.getChipId(), HEX);

typedef struct Credential {
    const char* ssid;
    const char* password;
} Credential;

Credential credentials[] = {
    {"ssid1", "password1"},
    {"ssid2", "password2"}
};


b_functions.ino
Code: Select all//Space for definition of general functions

void fullSleep(int time) {

}

void push() {

}

void pull() {

}

bool loadWifiCredentials() {
  int i = 0;
  int quantity = sizeof(credentials);

  for (i = 0; i < quantity; ++i) {
    wifiMulti.addAP(credentials[i].ssid,credentials[i].password);
  }

  return true;

  // for (i=0,i<knownApsQuantity,i++) {
  //   wifiMulti.addAP(wifiSsid[i],wifiPassword[i]);
  // }
}

bool connectWifi() {
  if (loadWifiCredentials()) {
    Serial.println("Connecting ...");
    int i = 0;
    while (wifiMulti.run() != WL_CONNECTED) { // Wait for the Wi-Fi to connect: scan for Wi-Fi networks, and connect to the strongest of the networks above
      delay(1000);
      Serial.print(++i); Serial.print(' ');
    }
    return true;
  } else {
    return false;
  }
}

bool publishDns() {
  if (!MDNS.begin(deviceId)) {             // Start the mDNS responder for esp8266.local
    return false;
  }
  return true;

}


z_base.ino
Code: Select all//Particular includes
//#include <library1>

void setup() {
  Serial.begin(115200);
  delay(1);
  Serial.println("Device " + deviceId + " is waking up.");

  if (connectWifi()) {
    Serial.println('\n');
    Serial.print("Connected to ");
    Serial.println(WiFi.SSID());              // Tell us what network we're connected to
    Serial.print("IP address:\t");
    Serial.println(WiFi.localIP());           // Send the IP address of the ESP8266 to the compute
  }

  if (publishDns()) {
    Serial.println("mDNS started.");
  } else {
    Serial.println("mDNS failed.");
  }
}

void loop() {
  Serial.println("loop");
  delay(10000);
}