So, In the last few weeks’s I have been converting my Large ESP8266 project to multiple .h Module files so I can carry functions over from project to project and at the same time have them centrally managed to update code / fix bugs so they carry over to all of my projects.

One important detail on how I do this is to be able to trigger function code from within the Main project as needed so I don’t have to handle user specific events within the Header file where it does not belong. So, I do this by using TypeDef and declaring a Prototype for my Callback function. In this case,

typedef void (*DHTFunctionCallback)(struct sensor_GlobalReading * MyReading);
DHTFunctionCallback DHTCallBack;

Then set the callback function,

init_ThermostatSettings(&myDHTFunctionCallback);

Set the callback in the Header file

void ICACHE_FLASH_ATTR init_ThermostatSettings(void * LDHTCallBack(struct sensor_GlobalReading MyReading))
{
if (LDHTCallBack != NULL)
{
    DHTCallBack = LDHTCallBack;
}
}

Then in my Header file I then check to see if the value is set != NULL. If so then I call it.

DHTCallBack(&GlobalReading);

The Main Function will get triggered and I can then access the variables stored in the STACK/HEAP depending on how I pass it to the caller from the callee.

void ICACHE_FLASH_ATTR myDHTFunctionCallback(struct sensor_GlobalReading * MyReading)
{
    os_printf("[%s]\r\n", __func__);
}

One thing to note for the LOVE OF ORANGE JUICE WATCH OUT FOR USE AFTER FREE ISSUES. If your doing this from an ASync call just be aware to Lock the thread/variables so you don’t crash or use Data that get’s falsely injected!

With this method you can just Include the Handler, Call the Coordinating Init Function and pass the &Address of a function matching the prototype and your off to the races!

Full Header and Main.C example
DHT.H
#ifndef dhtthermastat
#define dhtthermastat

#include "../library/common.h"
#include "../library/station.h"
#define ThermMemorySpaceSector 0x50
#define ThermMemorySpace 0x50000
#define DHT_MAXCOUNT 32000
#define DHT_NO_PIN -1
#define DHT_PACKET_SIZE 5

///NOTE: The call back points to a Global Structure named GlobalReading, This structure can / may change by the time user code executes. It may be best to copy the variable staticly.

typedef void (*DHTFunctionCallback)(struct sensor_GlobalReading * MyReading);
DHTFunctionCallback DHTCallBack;

int DHT_PIN = 0;

LOCAL struct espconn TCPOutGoingespconn; //This sends outgoing get request
static volatile os_timer_t CheckIPAndSend_timer;

enum sensor_type MySensor;
enum sensor_type
{
    MySensor_DHT11,MySensor_DHT22
};

struct __attribute__((__packed__)) ThermostatStruct{
    int programmed;
    enum sensor_type ThermostatType;
    int GPIO;
    int PowerMethod;
    BOOL PollingEnabled;
    int Polling;
    BOOL SendOnInit;
    BOOL SendOnInitOnlyIf;
    int tempatureDegrees;
    BOOL classification;
    BOOL DeepSleepAfterSend;
    int DeepSleepTime;
    BOOL Enabled;
    char Reserved[14]; //This fixes the 4 Byte Allignment in SPI Flash Read, 44bytes
};

struct ThermostatStruct ThermostatConfig;

struct sensor_GlobalReading{
    float temperature;
    float humidity;
    const char* source;
    uint8_t sensor_id[16];
    BOOL success;
};


void ICACHE_FLASH_ATTR DHT(void);
//struct sensor_GlobalReading * ICACHE_FLASH_ATTR readDHT(int force);
void DHTInit(enum sensor_type LMySensor, uint32_t GPIOPin, bool enablepolling, uint32_t polltime);

static inline float scale_humidity(int *data) {
  if (MySensor == MySensor_DHT11) {
    return data[0];
  } else {
    float humidity = data[0] * 256 + data[1];
    return humidity /= 10;
  }
}

static inline float scale_temperature(int *data) 
{
    if (MySensor == MySensor_DHT11) 
    {
        os_printf("MySensor DHT11");
        return data[2];
    } 
    else 
    {
        os_printf("MySensor DHT22");
        float temperature = data[2] & 0x7f;
        temperature *= 256;
        temperature += data[3];
        temperature /= 10;
        if (data[2] & 0x80)
        temperature *= -1;
        return temperature;
    }
}

struct sensor_GlobalReading GlobalReading = {
    .source = "DHT11", .success = 0
};

void ICACHE_FLASH_ATTR renderHTTPThermostatrequest(char * Request)
{
    decodeHTML(Request);
    if (os_strlen(Request) > 127)
    {
        os_printf("Returning - GET VAR IS TOO BIG: %s\r\n", Request);
        return;
    }

    char HTTPGETSPLIT[128];
    os_memset(HTTPGETSPLIT, 0, 128);
    os_memcpy(&HTTPGETSPLIT, Request, os_strlen(Request));
    os_printf("Before Split: %s\r\n", HTTPGETSPLIT);
    char * Key = strstr(HTTPGETSPLIT, "=");
    if(Key == NULL)
    {
        os_printf("Returning - Not a valid Split: %s\r\n", HTTPGETSPLIT);
        return;
    }
    *Key = 0x0;
    char * Value = Key + 1;
    Key = HTTPGETSPLIT;

    os_printf("Key: %s\r\nValue: %s\r\n", Key, Value);

    os_printf("renderHTTPThermostatrequest Key: %s\r\nValue: %s\r\n", Key, Value);
    if (strcmp(Key, "Thermostat") == 0)
    {
        //os_strcpy(newconfig.ssid, Value);
        if (strcmp(Value, "DHT11") == 0)
        {
            os_printf("Setting to DHT11");
            ThermostatConfig.ThermostatType = MySensor_DHT11;
        }
        else
        {
            os_printf("Setting to DHT22");
            ThermostatConfig.ThermostatType = MySensor_DHT22;
        }
        os_printf("Thermastat Set to: ", Value);
        return;
    }
    else if (strcmp(Key, "GPIO") == 0)
    {
        ThermostatConfig.GPIO = atoi(Value);
        os_printf("GPIO set to %s - %d", Value, atoi(Value));
    }
    else if (strcmp(Key, "PowerMethod") == 0)
    {
        ThermostatConfig.PowerMethod = atoi(Value);
    }
    else if (strcmp(Key, "pollingEnabled") == 0)
    {
        if (strcmp(Value, "true") == 0)
        {
            ThermostatConfig.PollingEnabled = true;
        }
        else
        {
            ThermostatConfig.PollingEnabled = false;
        }
    }
    else if (strcmp(Key, "POLLING") == 0)
    {
        ThermostatConfig.Polling = atoi(Value);
        os_printf("Polling set to %s - %d", Value, atoi(Value));
    }
    else if (strcmp(Key, "SendOnInit") == 0)
    {
        if (strcmp(Value, "true") == 0)
        {
            ThermostatConfig.SendOnInit = true;
        }
        else
        {
            ThermostatConfig.SendOnInit = false;
        }
    }
    else if (strcmp(Key, "SendOnInitOnlyIf") == 0)
    {
        if (strcmp(Value, "true") == 0)
        {
            ThermostatConfig.SendOnInitOnlyIf = true;
        }
        else
        {
            ThermostatConfig.SendOnInitOnlyIf = false;
        }
    }
    else if (strcmp(Key, "tempatureDegrees") == 0)
    {
        ThermostatConfig.tempatureDegrees = atoi(Value);
    }
    else if (strcmp(Key, "classification") == 0)
    {
        if (strcmp(Value, "warmer") == 0)
        {
            ThermostatConfig.classification = true;
        }
        else if (strcmp(Value, "colder") == 0)
        {
            ThermostatConfig.classification = false;
        }
    }
    else if (strcmp(Key, "DeepSleepAfterPoll") == 0)
    {
        if (strcmp(Value, "true") == 0)
        {
            ThermostatConfig.DeepSleepAfterSend = true;
        }
        else if (strcmp(Value, "false") == 0)
        {
            ThermostatConfig.DeepSleepAfterSend = false;
        }
    }
    else if (strcmp(Key, "DeepSleepTime") == 0)
    {
        ThermostatConfig.DeepSleepTime = atoi(Value);
    }
    else if (strcmp(Key, "enabled") == 0)
    {
        if (strcmp(Value, "true") == 0)
        {
            ThermostatConfig.Enabled = true;
        }
        else
        {
            ThermostatConfig.Enabled = false;
        }

        //os_printf("ssid: \"%s\"\npw: \"%s\"\nProgrammed: %d\r\n",newconfig.ssid,newconfig.password,newconfig.programmed);
        ThermostatConfig.programmed = 1;
        //os_printf("Writing ThermConfig at %02x\r\n", (WifiMemorySpace+sizeof(APFlashStationConf)+sizeof(newconfig)));
        spi_flash_erase_sector(ThermMemorySpaceSector);
        SpiFlashOpResult writeResult = spi_flash_write(ThermMemorySpace, (uint32 *)&ThermostatConfig, sizeof(ThermostatConfig));
        if (writeResult == SPI_FLASH_RESULT_OK)
        {
            /*
            ThermostatConfig.programmed = 0;
            SpiFlashOpResult ReadResult = spi_flash_read(0x50000,(uint32 *)&ThermostatConfig,sizeof(ThermostatConfig));     
            if (ReadResult == SPI_FLASH_RESULT_OK)
            {
                os_printf("Therm SPI Read SPI_FLASH_RESULT_OK\r\n");
                if(ThermostatConfig.programmed == 1)
                {
                    os_printf("\r\nSaved ThermostatConfig Information Found!!\r\nThermostatType: \"%d\"\nGPIO: \"%d\"\r\n", ThermostatConfig.ThermostatType, ThermostatConfig.GPIO);
                    if(ThermostatConfig.Enabled == true)
                    {
                        os_printf("Thermostat Enabled - Starting Polling at %d\r\n", ThermostatConfig.Polling);
                        DHTInit(MySensor_DHT11, ThermostatConfig.PollingEnabled, ThermostatConfig.Polling * 1000); //1000 = 1 Sec
                    }
                }
            }
            */

            os_printf("ThermSettings SavedSystem is rebooting...");     
            //os_delay_us(100000);
            os_printf("now...");        
            system_restart();
        }
        else if(writeResult == SPI_FLASH_RESULT_ERR)
        {
            os_printf("ThermSettings SPI_FLASH_RESULT_ERR...");
        }
        else if(writeResult == SPI_FLASH_RESULT_TIMEOUT)
        {
            os_printf("ThermSettings SPI_FLASH_RESULT_TIMEOUT...");
        }
        return;

    }
    else /* default: */
    {
        os_printf("No valid request Handler for - %s\r\n", Request); //Output last variable
        /*
        int i;
        for(i = 0; i < 10; i++) {
                os_printf("0x%02X ", Request[i]);
        }
        */
    }
}

LOCAL bool ICACHE_FLASH_ATTR pollDHTCb()
{
    //Something is going on with Powermethod on GPIO15?
    os_printf("pollDHTCb()");
    if (ThermostatConfig.PowerMethod > 0)
    {
        os_printf("Turning on power to the DHT - Pin%d\r\n", ThermostatConfig.PowerMethod);
        PIN_FUNC_SELECT(g_pin_muxes[ThermostatConfig.PowerMethod], g_pin_funcs[ThermostatConfig.PowerMethod]);
        gpio_output_set((1 << ThermostatConfig.PowerMethod), 0, 0, 0); //Turn on power to the DHT
    }
    if(ThermostatConfig.programmed != 1)
    {
        os_printf("pollDHTCb, not programmed\r\n");
        return false;
    }
    int counter = 0;
    int laststate = 1;
    int i = 0;
    int bits_in = 0;
    int data[100];
    data[0] = data[1] = data[2] = data[3] = data[4] = 0;
    //disable interrupts, start of critical section
    //os_intr_lock();
    wdt_feed();
    // Wake up device, 250ms of high
    GPIO_OUTPUT_SET(DHT_PIN, 1);
    delay_ms(250);
    // Hold low for 20ms
    GPIO_OUTPUT_SET(DHT_PIN, 0);
    delay_ms(20);
    // High for 40us
    GPIO_OUTPUT_SET(DHT_PIN, 1);
    os_delay_us(40);
    GPIO_DIS_OUTPUT(DHT_PIN);
    i=0;
    while (GPIO_INPUT_GET(DHT_PIN) == 1) 
    {
        os_delay_us(3);
        if (i >= DHT_MAXCOUNT) 
        {
            goto fail;
        }
        i++;
    }
    i=0;
    while (GPIO_INPUT_GET(DHT_PIN) == 0) 
    {
        os_delay_us(3);
        if (i >= DHT_MAXCOUNT) 
        {
            goto fail;
        }
        i++;
    }

    counter = 0;
    while (counter < 40)
    {
        i=0;
        while (GPIO_INPUT_GET(DHT_PIN) == 1) 
        {
            os_delay_us(3);
            if (i >= DHT_MAXCOUNT) 
            {
                goto fail;
            }
            i++;
        }

        i=0;
        while (GPIO_INPUT_GET(DHT_PIN) == 0) 
        {
            os_delay_us(3);
            if (i >= DHT_MAXCOUNT) 
            {
                goto fail;
            }
            i++;
        }

        os_delay_us(47);
        data[bits_in / 8] <<= 1;
        if (GPIO_INPUT_GET(DHT_PIN) == 1)
        {
            data[bits_in / 8] |= 1;
            //os_printf("\r\nONE %d", counter);
        }
        else
        {
            //os_printf("\r\nZERO %d", counter);
        }
        bits_in++;
        counter++;
    }

    //Re-enable interrupts, end of critical section
    //os_intr_unlock();
    if (ThermostatConfig.PowerMethod > 0)
    {
        os_printf("Turning off power to DHT\r\n");
        gpio_output_set(0, (1 << ThermostatConfig.PowerMethod), 0, 0); //Turn off power to the DHT
    }

    if (bits_in < 40) {
        os_printf("Got too few bits: %d should be at least 40\r\n", bits_in);
        goto fail;
    }


    int checksum = (data[0] + data[1] + data[2] + data[3]) & 0xFF;

    //os_printf("DHT: %02x %02x %02x %02x [%02x] CS: %02x", data[0], data[1],data[2],data[3],data[4],checksum);

    if (data[4] != checksum) 
    {
        os_printf("Checksum was incorrect after %d bits. Expected %d but got %d\r\n", bits_in, data[4], checksum);
        goto fail;
    }

    GlobalReading.temperature = scale_temperature(data);
    GlobalReading.humidity = scale_humidity(data);
    os_printf("Temp =  %d *F, Hum = %d %%\n", (int)(GlobalReading.temperature * 1.8 + 32), (int)(GlobalReading.humidity));
    GlobalReading.success = 1;
    DHTCallBack(&GlobalReading); //Invoke Callback
    return true;

    fail:
        os_printf("Failed to get GlobalReading, dying\n");
        GlobalReading.success = 0;
        if (ThermostatConfig.PowerMethod > 0)
        {
            os_printf("Turning off power to DHT\r\n");
            gpio_output_set(0, (1 << ThermostatConfig.PowerMethod), 0, 0); //Turn off power to the DHT
        }
        return false;
}

LOCAL bool ICACHE_FLASH_ATTR pollDHT()
{
    if(ThermostatConfig.programmed != 1)
    {
        os_printf("Polling DHT, not programmed\r\n");
        return false;
    }
    os_printf("Polling DHT\r\n");
    if (pollDHTCb())  //Get Tempature from DHT
    {
        int TempInF = (int)(GlobalReading.temperature * 1.8 + 32);
        if (ThermostatConfig.SendOnInitOnlyIf)
        {
            if(ThermostatConfig.classification)
            {
                if (TempInF > ThermostatConfig.tempatureDegrees) //Warmer
                {
                    os_printf("Warmer: TempInF > ThermostatConfig.tempatureDegrees\r\n");
                    //Turn On LED
                    DHTCallBack(&GlobalReading);
                    PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO2_U, FUNC_GPIO2);
                    gpio_output_set(0, (1 << 2), 0, 0);
                }
                else
                {
                    //Turn OFF LED
                    PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO2_U, FUNC_GPIO2);
                    gpio_output_set((1 << 2), 0, 0, 0);
                }
            }
            else if(ThermostatConfig.classification) //Colder
            {
                if (TempInF < ThermostatConfig.tempatureDegrees)
                {
                    os_printf("Colder: TempInF < ThermostatConfig.tempatureDegrees\r\n");
                    //Turn On LED
                    DHTCallBack(&GlobalReading);
                    PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO2_U, FUNC_GPIO2);
                    gpio_output_set(0, (1 << 2), 0, 0);
                }
                else
                {
                    //Turn OFF LED
                    PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO2_U, FUNC_GPIO2);
                    gpio_output_set((1 << 2), 0, 0, 0);
                }
            }
        }
        else
        {
            os_printf("Sending on Poll\r\n");
        }
    }
}

void DHTInit(enum sensor_type LMySensor, uint32_t GPIOPin, bool enablepolling, uint32_t polltime) 
{
    os_printf("[%s]\r\n", __func__);
    MySensor = LMySensor;
    //PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO0_U, FUNC_GPIO0);
    //PIN_PULLUP_EN(PERIPHS_IO_MUX_GPIO0_U);
    PIN_FUNC_SELECT(g_pin_muxes[GPIOPin], g_pin_funcs[GPIOPin]);
    PIN_PULLUP_EN(g_pin_muxes[GPIOPin]);
    DHT_PIN = GPIOPin;
    if (enablepolling)
    {
        os_printf("DHT Setup for type %d, poll interval of %d using GPIO %d, Starting Timer...\r\n", LMySensor, (int)polltime, GPIOPin);
        static ETSTimer dhtTimer;
        os_timer_setfn(&dhtTimer, pollDHT, NULL);
        os_timer_arm(&dhtTimer, polltime, 1);
    }
    else
    {
        os_printf("DHT Setup for type %d using GPIO %d\n", LMySensor, GPIOPin);
    }

}


char * DOMAIN = "controllingTheInter.net";
bool sentAlert=false;

//char requestText[128];
//  int TempInF = (int)(GlobalReading.temperature * 1.8 + 32);
//  int humidity = (int)GlobalReading.humidity;
//  os_sprintf(requestText,"GET /esp8266.php?ssid=myssid&IP=MyIp&ID=MyId /HTTP/1.1\r\nUser-Agent: F%dH%d\r\nHost: %s\r\nAccept: */*\r\n\r\n", TempInF, humidity, DOMAIN);
//  os_printf("Sending: %s", requestText);
//  espconn_send(conn,requestText,128);
//  sentAlert=true;

//Add code to support new sockets sending data.
void ICACHE_FLASH_ATTR LoadStationAndSendData(struct sensor_GlobalReading MyReading)
{
    init_station(NULL, NULL, NULL);     
    //LoadAccessPoint();
    //os_timer_setfn(&CheckIPAndSend_timer, (os_timer_func_t *)network_check_ip_send_therm, (void *)&MyReading);
    //os_timer_arm(&CheckIPAndSend_timer, 1000, 1);
}

SpiFlashOpResult ICACHE_FLASH_ATTR Set_ThermostatSettings(enum sensor_type LThermostatType, int LGPIO, int LPowerMethod, bool LPollingEnabled, int LPolling, bool LSendOnInit, bool LSendOnInitOnlyIf, int LtempatureDegrees, char * LClassification, bool LDeepSleepAfterSend, int LDeepSleepTime, int LEnabled, char * LReserved)
{
    os_printf("[%s]\r\n", __func__);
    ThermostatConfig.ThermostatType = LThermostatType;
    DHT_PIN = LGPIO;
    ThermostatConfig.GPIO = LGPIO;
    ThermostatConfig.PowerMethod = LPowerMethod;
    ThermostatConfig.PollingEnabled = LPollingEnabled;
    ThermostatConfig.Polling = LPolling;
    ThermostatConfig.SendOnInit = LSendOnInit;
    ThermostatConfig.SendOnInitOnlyIf = LSendOnInitOnlyIf;
    ThermostatConfig.tempatureDegrees = LtempatureDegrees;
    if (LClassification != NULL)
    {
        if (strcmp(LClassification, "warmer") == 0)
        {
            ThermostatConfig.classification = true;
        }
        else if (strcmp(LClassification, "colder") == 0)
        {
            ThermostatConfig.classification = false;
        }
    }
    ThermostatConfig.DeepSleepAfterSend = LDeepSleepAfterSend;
    ThermostatConfig.DeepSleepTime = LDeepSleepTime;
    ThermostatConfig.Enabled = LEnabled;
    ThermostatConfig.programmed = 1;
    //os_printf("Writing ThermConfig at %02x\r\n", (WifiMemorySpace+sizeof(APFlashStationConf)+sizeof(newconfig)));
    spi_flash_erase_sector(ThermMemorySpaceSector);
    SpiFlashOpResult writeResult = spi_flash_write(ThermMemorySpace, (uint32 *)&ThermostatConfig, sizeof(ThermostatConfig));
    if (writeResult == SPI_FLASH_RESULT_OK)
    {
        //os_printf("ThermSettings SavedSystem is rebooting...");       
        //os_delay_us(100000);
        //os_printf("now...");      
        //system_restart();
    }
    else if(writeResult == SPI_FLASH_RESULT_ERR)
    {
        os_printf("ThermSettings SPI_FLASH_RESULT_ERR...");
    }
    else if(writeResult == SPI_FLASH_RESULT_TIMEOUT)
    {
        os_printf("ThermSettings SPI_FLASH_RESULT_TIMEOUT...");
    }
    return writeResult;
}

void ICACHE_FLASH_ATTR init_ThermostatSettings(void * LDHTCallBack(struct sensor_GlobalReading MyReading))
{
    os_printf("[%s]\r\n", __func__);
    //os_printf("GlobalReading ThermConfig at %02x\r\n", (WifiMemorySpace+sizeof(FlashStationConf)+sizeof(APFlashStationConf)));
    //SpiFlashOpResult ReadResult = spi_flash_read(WifiMemorySpace+sizeof(FlashStationConf)+sizeof(APFlashStationConf),(uint32 *)&ThermostatConfig,sizeof(ThermostatConfig));
    SpiFlashOpResult ReadResult = spi_flash_read(ThermMemorySpace,(uint32 *)&ThermostatConfig,sizeof(ThermostatConfig));        
    if (ReadResult == SPI_FLASH_RESULT_OK)
    {
        os_printf("Therm SPI Read SPI_FLASH_RESULT_OK\r\n");
        if(ThermostatConfig.programmed == 1)
        {
            os_printf("Saved ThermostatConfig Information Found!!\r\nThermostatType: \"%d\"\nGPIO: \"%d\"\r\nTempature: \"%d\"\r\n", ThermostatConfig.ThermostatType, ThermostatConfig.GPIO, ThermostatConfig.tempatureDegrees);
            if(ThermostatConfig.Enabled == true)
            {
                if (LDHTCallBack != NULL)
                {
                    DHTCallBack = LDHTCallBack;
                }
                os_printf("Thermostat Enabled - Starting Polling at %d\r\n", ThermostatConfig.Polling);
                DHTInit(ThermostatConfig.ThermostatType, ThermostatConfig.GPIO, ThermostatConfig.PollingEnabled, ThermostatConfig.Polling * 1000 * 60); //1000 = 1 Sec

                if (ThermostatConfig.SendOnInit)
                {
                    if (pollDHTCb()) //This gets the raw value
                    {//This part may be able to be turned into a function
                        int TempInF = (int)(GlobalReading.temperature * 1.8 + 32);
                        if (ThermostatConfig.SendOnInitOnlyIf)
                        {
                            if(ThermostatConfig.classification)
                            {
                                if (TempInF > ThermostatConfig.tempatureDegrees) //Warmer
                                {
                                    os_printf("Warmer: TempInF > ThermostatConfig.tempatureDegrees\r\n");
                                    //wifi_set_opmode(STATION_MODE);
                                    //LoadStation();
                                    DHTCallBack(&GlobalReading);
                                    //Turn On LED
                                    PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO2_U, FUNC_GPIO2);
                                    gpio_output_set(0, (1 << 2), 0, 0);
                                }
                            }
                            else if(ThermostatConfig.classification) //Colder
                            {
                                if (TempInF < ThermostatConfig.tempatureDegrees)
                                {
                                    os_printf("Colder: TempInF < ThermostatConfig.tempatureDegrees\r\n");
                                    DHTCallBack(&GlobalReading);
                                    //Turn On LED
                                    PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO2_U, FUNC_GPIO2);
                                    gpio_output_set(0, (1 << 2), 0, 0);
                                    //wifi_set_opmode(STATION_MODE);
                                    //LoadStation();    
                                }
                            }
                        }
                        else
                        {
                            //wifi_set_opmode(STATION_MODE);
                            //LoadStation();
                        }
                        if(ThermostatConfig.DeepSleepAfterSend && false)
                        {
                                //SET_PERI_REG_MASK(UART_CONF0(0),  UART_TXFIFO_RST);//RESET    FIFO
                                //CLEAR_PERI_REG_MASK(UART_CONF0(0),    UART_TXFIFO_RST);
                                os_printf("Going to sleep for %d\r\n", ThermostatConfig.DeepSleepTime);
                                deep_sleep_set_option(2);
                                system_deep_sleep(ThermostatConfig.DeepSleepTime * 60000 * 1000); //UC to Minutes
                        }
                    }
                }
            }
        }
        else
        {
            os_printf("\r\nNo ThermostatConfig Information Found!!\r\n");
        }
    }
    else if(ReadResult == SPI_FLASH_RESULT_ERR)
    {
        os_printf("SPI_FLASH_RESULT_ERR");
    }
    else if(ReadResult == SPI_FLASH_RESULT_TIMEOUT)
    {
        os_printf("SPI_FLASH_RESULT_TIMEOUT");
    }

    //os_printf("SizeOf(ThermostatConfig) = %d\r\n", sizeof(ThermostatConfig));
}
#endif

Main.C

//https://www.mikrocontroller.net/attachment/263828/The-ESP8266-Book-August-2015.pdf
#include "ets_sys.h"
#include "osapi.h"
#include "gpio.h"
#include "os_type.h"
#include "ip_addr.h"
#include "mem.h"
#include "user_interface.h"
#include "lwip/stats.h"
#include "espconn.h"

#include "c_types.h" ////ONE WIRE

#include "../library/uart.h" //Copy these from your Driver Lib to your local folder
#include "../library/gpio16.h" //Copy these from your Driver Lib to your local folder

#include "../library/common.h"
#include "../library/dht.h"
#include "../library/station.h"
#include "../library/sockets.h"
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////

#define user_procTaskPrio        0
#define user_procTaskQueueLen    1
os_event_t user_procTaskQueue[user_procTaskQueueLen];

void ICACHE_FLASH_ATTR StationConnected()
{
    os_printf("[%s]\r\n", __func__);
    //Creates Timer Socket
    uint8 FreeSocket = GetFreeSocket();
    os_strcpy(MySendDataStruct[FreeSocket].Name, "MyThermSocket");
    MySendDataStruct[FreeSocket].Domain = DOMAIN;
    os_sprintf(MySendDataStruct[FreeSocket].DataToSend, "GET /esp8266.php?ssid=myssid&IP=MyIp2&ID=MyId2 /HTTP/1.1\r\nUser-Agent: SomeAgent\r\nHost: %s\r\nAccept: */*\r\n\r\n", MySendDataStruct[FreeSocket].Domain);
    StartSendingSocketTimer(FreeSocket);
}

void ICACHE_FLASH_ATTR myDHTFunctionCallback(struct sensor_GlobalReading * MyReading)
{
    os_printf("[%s]\r\n", __func__);
    //init_station("Thermastat.local", true, &StationConnected);
    //init_sockets(NULL, NULL, NULL, NULL); //Do not override Socket.H callbacks
}

void ICACHE_FLASH_ATTR sdk_init_done_cb(void) 
{ 
    os_printf("[%s] initializing ESP8266!\r\n", __func__);

    SpiFlashOpResult ReadResult = spi_flash_read(ThermMemorySpace,(uint32 *)&ThermostatConfig,sizeof(ThermostatConfig));        
    if (ReadResult != SPI_FLASH_RESULT_OK || ThermostatConfig.programmed != 1 || true) //If not set, Set it..
    {
        os_printf("Setting's not found, Calling Set_ThermostatSettings");
        //NOTE: Don't use GPIO16/D0, This is reserved.
        //DHT22 GPIO=GPIO0/D3 Power=GPIO15/D8 PollingEnabled Interval=1Min SendOnInit=true SendIfOnlyIf=false tempatureDegrees=NULL classification DeepSleepAfterSend DeepSleepTime Enabled Reserved
        SpiFlashOpResult writeResult = Set_ThermostatSettings(MySensor_DHT22, 0, 15, true, 1, true, false, NULL, NULL, false, NULL, true, NULL);
        //DHT22 GPIO=GPIO0/D3 Power=GPIO4/D2 PollingEnabled Interval=1Min SendOnInit=true SendIfOnlyIf=false tempatureDegrees=NULL classification=warmer/colder DeepSleepAfterSend DeepSleepTime Enabled Reserved
        //Set_ThermostatSettings(MySensor_DHT22, 0, 4, true, 1, true, false, NULL, "warmer", false, 5, true, NULL);
        if (writeResult == SPI_FLASH_RESULT_OK)
        {
            init_ThermostatSettings(&myDHTFunctionCallback);
        }
    }
    else
    {
        init_ThermostatSettings(&myDHTFunctionCallback);
    }

}

void ICACHE_FLASH_ATTR user_init()
{

    //void  uart0_tx_buffer(uint8   *buf,   uint16  len)
    uart_div_modify(0, UART_CLK_FREQ / 115200);
    wifi_set_opmode(0);
    wifi_set_sleep_type( NONE_SLEEP_T );

    ETS_GPIO_INTR_DISABLE();// Disable gpio interrupts
    gpio_init();

    //Start os task
    system_init_done_cb(sdk_init_done_cb);
    //system_os_task(loop, user_procTaskPrio, user_procTaskQueue, user_procTaskQueueLen); //Task to Signal for later
}

Leave a Reply

Your email address will not be published. Required fields are marked *

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax