Initial commit

This commit is contained in:
A.M. Rowsell 2018-08-13 23:34:17 -04:00
commit 557188bb38
4 changed files with 197 additions and 0 deletions

20
Makefile Normal file
View file

@ -0,0 +1,20 @@
P=mqtt
CC=xtensa-lx106-elf-gcc
LDLIBS=-nostdlib -ggdb -Wl,-Map=output.map -Wl,--start-group -lm -lc -lhal -lpp -llwip -lphy -lnet80211 -lwpa -lat -lwpa2 -lmain -Wl,--end-group -lgcc
CFLAGS= -I. -mlongcalls -std=gnu11
LDFLAGS=-Teagle.app.v6.ld
OBJ=mqtt.o
DEPS=mqtt.h
$(P)-0x00000.bin: $(P)
esptool.py elf2image $^
$(P): $(OBJ)
%.o: %.c $(DEPS)
flash: $(P)-0x00000.bin
esptool.py --port /dev/feather1 write_flash 0 $(P)-0x00000.bin 0x10000 $(P)-0x10000.bin
clean:
rm -f $(P) $(P).o $(P)-0x00000.bin $(P)-0x10000.bin

126
mqtt.c Normal file
View file

@ -0,0 +1,126 @@
#include <stdint.h>
#include "user_interface.h"
#include "ets_sys.h"
#include "osapi.h"
#include "mem.h"
#include "gpio.h"
#include "espconn.h"
#include "os_type.h"
#include "mqtt.h"
/* Functions we will need to implement:
* Send -- will handle all sending of all packets
* Connect -- set up TCP connection and parameters
* Publish -- send message to server
* Subscribe -- we probably won't need this
* We just want to connect, and publish info. We don't care about
* security or QoS in this basic implementation.
*/
static os_timer_t oneTimer;
LOCAL void ICACHE_FLASH_ATTR data_sent_callback(void *arg) {
os_printf("Data sent!\n");
}
LOCAL void ICACHE_FLASH_ATTR data_recv_callback(void *arg, char *pdata, unsigned short len) {
// deal with received data
os_printf("Received data of length %d -- %s \r\n", len, pdata);
}
LOCAL void ICACHE_FLASH_ATTR connected_callback(void *arg) {
struct espconn *pConn = arg;
os_printf("Connected callback\n");
espconn_regist_recvcb(pConn, (espconn_recv_callback)data_recv_callback);
espconn_regist_sentcb(pConn, (espconn_sent_callback)data_sent_callback);
// enable keepalive
espconn_set_opt(pConn, ESPCONN_KEEPALIVE);
char *pbuf = (char *)os_zalloc(2 * 1024);
uint8_t getReq[] = "GET / HTTP/1.1\r\n\r\n";
os_sprintf(pbuf, getReq);
espconn_send(pConn, pbuf, os_strlen(pbuf));
os_free(pbuf);
}
LOCAL void ICACHE_FLASH_ATTR reconnected_callback(void *arg, sint8 err) {
os_printf("Reconnected?\n");
os_printf("Error code: %d\n", err);
}
LOCAL void ICACHE_FLASH_ATTR disconnected_callback(void *arg) {
os_printf("Disconnected\n");
}
LOCAL uint8_t ICACHE_FLASH_ATTR tcpConnect(void *arg) {
mqtt_session_t *session = arg;
struct ip_info ipConfig;
LOCAL struct espconn conn;
LOCAL struct _esp_tcp tcp_s;
if (wifi_station_get_connect_status() == STATION_GOT_IP && ipConfig.ip.addr != 0) {
os_printf("Everything looks good!\n");
}
os_printf("Entered tcpConnect\n");
wifi_get_ip_info(STATION_IF, &ipConfig);
// set up basic TCP connection parameters
os_printf("about to set up TCP params\n");
conn.proto.tcp = &tcp_s;
conn.type = ESPCONN_TCP;
conn.proto.tcp->local_port = espconn_port();
conn.proto.tcp->remote_port = 80;
conn.state = ESPCONN_NONE;
os_memcpy(conn.proto.tcp->remote_ip, session->ip, 4);
os_printf("About to register callbacks\n");
// register callbacks
espconn_regist_connectcb(&conn, (espconn_connect_callback)connected_callback);
espconn_regist_reconcb(&conn, (espconn_reconnect_callback)reconnected_callback);
espconn_regist_disconcb(&conn, (espconn_connect_callback)disconnected_callback);
os_printf("About to connect\n");
//make the connection
if(espconn_connect(&conn) == 0) {
os_printf("Connection successful\n");
} else {
os_printf("Connection error\n");
}
session->activeConnection = &conn;
os_printf("About to return\n");
return 0;
}
LOCAL uint8_t ICACHE_FLASH_ATTR mqtt_connect(mqtt_session_t *session) {
}
LOCAL uint8_t ICACHE_FLASH_ATTR mqtt_send(mqtt_session_t *session, mqtt_message_t *message) {
}
void ICACHE_FLASH_ATTR user_init() {
uint8_t wifiStatus;
LOCAL mqtt_session_t globalSession;
LOCAL mqtt_session_t *pGlobalSession = &globalSession;
char ssid[32] = "Kwangmyong";
char passkey[64] = "vqmfg55020";
struct station_config stationConf;
gpio_init(); // init gpio so we can use the LED
wifi_status_led_install(0, PERIPHS_IO_MUX_GPIO0_U, FUNC_GPIO0); // set GPIO0 as status LED
stationConf.bssid_set = 0;
os_memcpy(&stationConf.ssid, ssid, 32); // copy the ssid and passkey into the station_config struct
os_memcpy(&stationConf.password, passkey, 64);
wifi_set_opmode_current(0x01); //station mode
wifi_station_set_config_current(&stationConf); // tell it about our config, this auto-connects us as well
// prepare the TCP/MQTT connection stuff
// test server is at 51.15.65.206
char testUser[] = "MrAureliusR";
char testPass[] = "test";
char testTopic[] = "input";
pGlobalSession->port = 80; // port 80 just for testing
const char esp_tcp_server_ip[4] = {51, 15, 65, 206}; // remote IP of TCP server
os_memcpy(pGlobalSession->ip, esp_tcp_server_ip, 4);
pGlobalSession->username = &testUser[0];
pGlobalSession->password = &testPass[0];
pGlobalSession->topic_name = &testTopic[0];
os_timer_setfn(&oneTimer, (os_timer_func_t *)tcpConnect, pGlobalSession);
os_timer_arm(&oneTimer, 15000, 0);
}

51
mqtt.h Normal file
View file

@ -0,0 +1,51 @@
#include "user_interface.h"
#include "ets_sys.h"
#include "osapi.h"
#include "espconn.h"
#include "os_type.h"
typedef enum mqtt_message_enum
{
MQTT_MSG_TYPE_CONNECT = 1,
MQTT_MSG_TYPE_CONNACK = 2,
MQTT_MSG_TYPE_PUBLISH = 3,
MQTT_MSG_TYPE_PUBACK = 4,
MQTT_MSG_TYPE_PUBREC = 5,
MQTT_MSG_TYPE_PUBREL = 6,
MQTT_MSG_TYPE_PUBCOMP = 7,
MQTT_MSG_TYPE_SUBSCRIBE = 8,
MQTT_MSG_TYPE_SUBACK = 9,
MQTT_MSG_TYPE_UNSUBSCRIBE = 10,
MQTT_MSG_TYPE_UNSUBACK = 11,
MQTT_MSG_TYPE_PINGREQ = 12,
MQTT_MSG_TYPE_PINGRESP = 13,
MQTT_MSG_TYPE_DISCONNECT = 14
} mqtt_message_type;
typedef struct {
uint8_t *message;
uint16_t length;
mqtt_message_type type;
} mqtt_message_t;
typedef struct {
uint8_t ip[4];
uint32_t port;
uint32_t localPort;
uint8_t *client_id;
uint8_t *topic_name;
uint8_t qos_level;
uint8_t *username;
uint8_t *password;
char valid_connection;
struct espconn *activeConnection;
} mqtt_session_t;
LOCAL uint8_t ICACHE_FLASH_ATTR tcpConnect(void *arg);
LOCAL void ICACHE_FLASH_ATTR disconnected_callback(void *arg);
LOCAL void ICACHE_FLASH_ATTR reconnected_callback(void *arg, sint8 err);
LOCAL void ICACHE_FLASH_ATTR connected_callback(void *arg);
LOCAL void ICACHE_FLASH_ATTR data_recv_callback(void *arg, char *pdata, unsigned short len);
LOCAL void ICACHE_FLASH_ATTR data_sent_callback(void *arg);
LOCAL uint8_t ICACHE_FLASH_ATTR mqtt_send(mqtt_session_t *session, mqtt_message_t *message);
LOCAL uint8_t ICACHE_FLASH_ATTR mqtt_connect(mqtt_session_t *session);

0
user_config.h Normal file
View file