mirror of https://github.com/jeelabs/esp-link.git
parent
a2bb08186f
commit
36303a2b67
@ -1,10 +1,14 @@ |
||||
#ifndef HTTPDESPFS_H |
||||
#define HTTPDESPFS_H |
||||
|
||||
#include <esp8266.h> |
||||
#include "espfs.h" |
||||
#include "espfsformat.h" |
||||
#include "cgi.h" |
||||
#include "httpd.h" |
||||
|
||||
int cgiEspFsHook(HttpdConnData *connData); |
||||
int ICACHE_FLASH_ATTR cgiEspFsTemplate(HttpdConnData *connData); |
||||
//int cgiEspFsTemplate(HttpdConnData *connData);
|
||||
//int ICACHE_FLASH_ATTR cgiEspFsHtml(HttpdConnData *connData);
|
||||
|
||||
#endif |
||||
|
@ -1,40 +1,20 @@ |
||||
#ifndef _USER_CONFIG_H_ |
||||
#define _USER_CONFIG_H_ |
||||
|
||||
/*DEFAULT CONFIGURATIONS*/ |
||||
|
||||
#define MQTT_HOST "mqtt.yourdomain.com" //or "mqtt.yourdomain.com"
|
||||
#define MQTT_PORT 1883 |
||||
#define MQTT_RECONNECT_TIMEOUT 5 // seconds
|
||||
#define MQTT_BUF_SIZE 1024 |
||||
#define MQTT_KEEPALIVE 120 /*second*/ |
||||
|
||||
#define MQTT_CLIENT_ID "H_%08X" //Cuidar para não colocar valores execendentes da ESTRUTURA SYSCFG
|
||||
#define MQTT_USER "DVES_USER" |
||||
#define MQTT_PASS "DVES_PASS" |
||||
|
||||
#define STA_SSID "TESTE" |
||||
#define STA_PASS "54545" |
||||
#define STA_TYPE AUTH_WPA2_PSK |
||||
|
||||
#define MQTT_RECONNECT_TIMEOUT 5 /*second*/ |
||||
|
||||
#define DEFAULT_SECURITY 0 |
||||
#define QUEUE_BUFFER_SIZE 2048 |
||||
|
||||
//#undef MCU_RESET_PIN
|
||||
//#undef MCU_ISP_PIN
|
||||
//#undef LED_CONN_PIN
|
||||
//#undef LED_SERIAL_PIN
|
||||
//
|
||||
//#define MCU_RESET_PIN 2
|
||||
//#define MCU_ISP_PIN -1
|
||||
//#define LED_CONN_PIN -1
|
||||
//#define LED_SERIAL_PIN -1
|
||||
#define MQTT_HOST "10.0.0.220" // "mqtt.yourdomain.com" or ip "10.0.0.1"
|
||||
#define MQTT_PORT 1883 |
||||
#define MQTT_SECURITY 0 |
||||
|
||||
//#define BAUD_RATE 9600
|
||||
//#define HOSTNAME "nodemcu\0 "
|
||||
#define MQTT_CLIENT_ID "esp-link" // ""
|
||||
#define MQTT_USER "" |
||||
#define MQTT_PASS "" |
||||
#define MQTT_KEEPALIVE 120 // seconds
|
||||
#define MQTT_CLSESSION true |
||||
|
||||
#define PROTOCOL_NAMEv31 /*MQTT version 3.1 compatible with Mosquitto v0.15*/ |
||||
//PROTOCOL_NAMEv311 /*MQTT version 3.11 compatible with https://eclipse.org/paho/clients/testing/*/
|
||||
#define PROTOCOL_NAMEv31 // MQTT version 3.1 compatible with Mosquitto v0.15
|
||||
//PROTOCOL_NAMEv311 // MQTT version 3.11 compatible with https://eclipse.org/paho/clients/testing/
|
||||
|
||||
#endif |
@ -1,140 +0,0 @@ |
||||
/*
|
||||
* Copyright (c) 2014, Tuan PM |
||||
* Email: tuanpm@live.com |
||||
* |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or without |
||||
* modification, are permitted provided that the following conditions |
||||
* are met: |
||||
* |
||||
* 1. Redistributions of source code must retain the above copyright |
||||
* notice, this list of conditions and the following disclaimer. |
||||
* 2. Redistributions in binary form must reproduce the above copyright |
||||
* notice, this list of conditions and the following disclaimer in the |
||||
* documentation and/or other materials provided with the distribution. |
||||
* 3. Neither the name of the copyright holder nor the names of its |
||||
* contributors may be used to endorse or promote products derived |
||||
* from this software without specific prior written permission. |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
||||
* POSSIBILITY OF SUCH DAMAGE. |
||||
* |
||||
*/ |
||||
#include "utils.h" |
||||
|
||||
uint8_t ICACHE_FLASH_ATTR
|
||||
UTILS_IsIPV4(int8_t* str) { |
||||
uint8_t segs = 0; /* Segment count. */ |
||||
uint8_t chcnt = 0; /* Character count within segment. */ |
||||
uint8_t accum = 0; /* Accumulator for segment. */ |
||||
/* Catch NULL pointer. */ |
||||
if (str == 0) |
||||
return 0; |
||||
/* Process every character in string. */ |
||||
|
||||
while (*str != '\0') { |
||||
/* Segment changeover. */ |
||||
|
||||
if (*str == '.') { |
||||
/* Must have some digits in segment. */ |
||||
if (chcnt == 0) |
||||
return 0; |
||||
/* Limit number of segments. */ |
||||
if (++segs == 4) |
||||
return 0; |
||||
/* Reset segment values and restart loop. */ |
||||
chcnt = accum = 0; |
||||
str++; |
||||
continue; |
||||
} |
||||
|
||||
/* Check numeric. */ |
||||
if ((*str < '0') || (*str > '9')) |
||||
return 0; |
||||
|
||||
/* Accumulate and check segment. */ |
||||
|
||||
if ((accum = accum * 10 + *str - '0') > 255) |
||||
return 0; |
||||
/* Advance other segment specific stuff and continue loop. */ |
||||
|
||||
chcnt++; |
||||
str++; |
||||
} |
||||
|
||||
/* Check enough segments and enough characters in last segment. */ |
||||
|
||||
if (segs != 3) |
||||
return 0; |
||||
if (chcnt == 0) |
||||
return 0; |
||||
/* Address okay. */ |
||||
|
||||
return 1; |
||||
} |
||||
|
||||
uint8_t ICACHE_FLASH_ATTR |
||||
UTILS_StrToIP(const int8_t* str, void* ip) { |
||||
|
||||
/* The count of the number of bytes processed. */ |
||||
int i; |
||||
/* A pointer to the next digit to process. */ |
||||
for (i = 0; i < 4; i++) { |
||||
/* The digit being processed. */ |
||||
char c; |
||||
/* The value of this byte. */ |
||||
int n = 0; |
||||
while (1) { |
||||
c = *(const char *)str; |
||||
(const char *)str++; |
||||
if (c >= '0' && c <= '9') { |
||||
n *= 10; |
||||
n += c - '0'; |
||||
} |
||||
/* We insist on stopping at "." if we are still parsing
|
||||
the first, second, or third numbers. If we have reached |
||||
the end of the numbers, we will allow any character. */ |
||||
else if ((i < 3 && c == '.') || i == 3) { |
||||
break; |
||||
} |
||||
else { |
||||
return 0; |
||||
} |
||||
} |
||||
if (n >= 256) { |
||||
return 0; |
||||
} |
||||
((uint8_t*)ip)[i] = n; |
||||
} |
||||
return 1; |
||||
} |
||||
|
||||
uint32_t ICACHE_FLASH_ATTR
|
||||
UTILS_Atoh(const int8_t* s) { |
||||
uint32_t value = 0, digit; |
||||
int8_t c; |
||||
|
||||
while ((c = *s++)) { |
||||
if ('0' <= c && c <= '9') |
||||
digit = c - '0'; |
||||
else if ('A' <= c && c <= 'F') |
||||
digit = c - 'A' + 10; |
||||
else if ('a' <= c && c <= 'f') |
||||
digit = c - 'a' + 10; |
||||
else break; |
||||
|
||||
value = (value << 4) | digit; |
||||
} |
||||
|
||||
return value; |
||||
} |
@ -1,9 +0,0 @@ |
||||
#ifndef _UTILS_H_ |
||||
#define _UTILS_H_ |
||||
|
||||
#include <esp8266.h> |
||||
|
||||
uint32_t ICACHE_FLASH_ATTR UTILS_Atoh(const int8_t* s); |
||||
uint8_t ICACHE_FLASH_ATTR UTILS_StrToIP(const int8_t* str, void* ip); |
||||
uint8_t ICACHE_FLASH_ATTR UTILS_IsIPV4(int8_t* str); |
||||
#endif |
@ -1,5 +1,70 @@ |
||||
#include <esp8266.h> |
||||
#include <mqtt.h> |
||||
#include <cgiwifi.h> |
||||
|
||||
MQTT_Client mqttClient; |
||||
|
||||
void ICACHE_FLASH_ATTR
|
||||
mqttConnectedCb(uint32_t *args) { |
||||
MQTT_Client* client = (MQTT_Client*)args; |
||||
MQTT_Publish(client, "announce/all", "Hello World!", 0, 0); |
||||
} |
||||
|
||||
void ICACHE_FLASH_ATTR
|
||||
mqttDisconnectedCb(uint32_t *args) { |
||||
// MQTT_Client* client = (MQTT_Client*)args;
|
||||
os_printf("MQTT Disconnected\n"); |
||||
} |
||||
|
||||
void ICACHE_FLASH_ATTR
|
||||
mqttTcpDisconnectedCb(uint32_t *args) { |
||||
// MQTT_Client* client = (MQTT_Client*)args;
|
||||
os_printf("MQTT TCP Disconnected\n"); |
||||
} |
||||
|
||||
void ICACHE_FLASH_ATTR
|
||||
mqttPublishedCb(uint32_t *args) { |
||||
// MQTT_Client* client = (MQTT_Client*)args;
|
||||
os_printf("MQTT Published\n"); |
||||
} |
||||
|
||||
void ICACHE_FLASH_ATTR
|
||||
mqttDataCb(uint32_t *args, const char* topic, uint32_t topic_len, const char *data, uint32_t data_len) { |
||||
char *topicBuf = (char*)os_zalloc(topic_len + 1); |
||||
char *dataBuf = (char*)os_zalloc(data_len + 1); |
||||
|
||||
// MQTT_Client* client = (MQTT_Client*)args;
|
||||
|
||||
os_memcpy(topicBuf, topic, topic_len); |
||||
topicBuf[topic_len] = 0; |
||||
|
||||
os_memcpy(dataBuf, data, data_len); |
||||
dataBuf[data_len] = 0; |
||||
|
||||
os_printf("Receive topic: %s, data: %s\n", topicBuf, dataBuf); |
||||
os_free(topicBuf); |
||||
os_free(dataBuf); |
||||
} |
||||
|
||||
void ICACHE_FLASH_ATTR
|
||||
wifiStateChangeCb(uint8_t status) |
||||
{ |
||||
if (status == wifiGotIP && mqttClient.connState != TCP_CONNECTING){ |
||||
MQTT_Connect(&mqttClient); |
||||
} |
||||
else if (status == wifiIsDisconnected && mqttClient.connState == TCP_CONNECTING){
|
||||
MQTT_Disconnect(&mqttClient); |
||||
} |
||||
} |
||||
|
||||
void init() { |
||||
|
||||
wifiAddStateChangeCb(wifiStateChangeCb); |
||||
MQTT_InitConnection(&mqttClient, MQTT_HOST, MQTT_PORT, MQTT_SECURITY); |
||||
MQTT_InitClient(&mqttClient, MQTT_CLIENT_ID, MQTT_USER, MQTT_PASS, MQTT_KEEPALIVE, MQTT_CLSESSION); |
||||
MQTT_InitLWT(&mqttClient, "/lwt", "offline", 0, 0); |
||||
MQTT_OnConnected(&mqttClient, mqttConnectedCb); |
||||
MQTT_OnDisconnected(&mqttClient, mqttDisconnectedCb); |
||||
MQTT_OnDisconnected(&mqttClient, mqttTcpDisconnectedCb); |
||||
MQTT_OnPublished(&mqttClient, mqttPublishedCb); |
||||
MQTT_OnData(&mqttClient, mqttDataCb); |
||||
} |
Loading…
Reference in new issue