Initial commit
This commit is contained in:
commit
557188bb38
4 changed files with 197 additions and 0 deletions
126
mqtt.c
Normal file
126
mqtt.c
Normal 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);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue