Splitting library into own file. Adding onewire.
The mqtt.c file was getting pretty big, so the example code is now in main.c. Also, we need a onewire library, so I've started to write on in onewire.c. We will need to make some changes to the Makefile as well, but I will need to consult a couple of resources for that. Signed-off-by: A.M. Rowsell <amrowsell@frozenelectronics.ca>
This commit is contained in:
parent
c8bff5cace
commit
70cf55efdf
6 changed files with 185 additions and 111 deletions
102
main.c
Normal file
102
main.c
Normal file
|
@ -0,0 +1,102 @@
|
|||
#include "espconn.h"
|
||||
#include "main.h"
|
||||
#include "mqtt.h"
|
||||
#include "onewire.h"
|
||||
|
||||
|
||||
LOCAL void ICACHE_FLASH_ATTR con(void *arg) {
|
||||
os_printf("Entered con!\n");
|
||||
mqtt_session_t *pSession = (mqtt_session_t *)arg;
|
||||
mqtt_send(pSession, NULL, 0, MQTT_MSG_TYPE_CONNECT);
|
||||
|
||||
os_timer_disarm(&oneTimer);
|
||||
os_timer_setfn(&oneTimer, (os_timer_func_t *)sub, arg);
|
||||
os_timer_arm(&oneTimer, 200, 0);
|
||||
}
|
||||
|
||||
LOCAL void ICACHE_FLASH_ATTR pub(void *arg) {
|
||||
os_printf("Entered pub!\n");
|
||||
mqtt_session_t *pSession = (mqtt_session_t *)arg;
|
||||
uint8_t data[] = "2.718281828459045";
|
||||
uint32_t dataLen = os_strlen(data);
|
||||
mqtt_send(pSession, &data[0], dataLen, MQTT_MSG_TYPE_PUBLISH);
|
||||
os_timer_disarm(&oneTimer);
|
||||
os_timer_setfn(&oneTimer, (os_timer_func_t *)ping, arg);
|
||||
os_timer_arm(&oneTimer, 200, 0);
|
||||
}
|
||||
|
||||
LOCAL void ICACHE_FLASH_ATTR ping(void *arg) {
|
||||
os_printf("Entered ping!\n");
|
||||
mqtt_session_t *pSession = (mqtt_session_t *)arg;
|
||||
mqtt_send(pSession, NULL, 0, MQTT_MSG_TYPE_PINGREQ);
|
||||
os_timer_disarm(&oneTimer);
|
||||
os_timer_setfn(&oneTimer, (os_timer_func_t *)sub, arg);
|
||||
os_timer_arm(&oneTimer, 200, 0);
|
||||
}
|
||||
|
||||
LOCAL void ICACHE_FLASH_ATTR sub(void *arg) {
|
||||
os_printf("Entered sub!\n");
|
||||
mqtt_session_t *pSession = (mqtt_session_t *)arg;
|
||||
mqtt_send(pSession, NULL, 0, MQTT_MSG_TYPE_SUBSCRIBE);
|
||||
|
||||
}
|
||||
|
||||
LOCAL void ICACHE_FLASH_ATTR discon(void *arg) {
|
||||
os_printf("Entered discon!\n");
|
||||
mqtt_session_t *pSession = (mqtt_session_t *)arg;
|
||||
mqtt_send(pSession, NULL, 0, MQTT_MSG_TYPE_DISCONNECT);
|
||||
os_timer_disarm(&oneTimer);
|
||||
}
|
||||
|
||||
void ICACHE_FLASH_ATTR user_init() {
|
||||
uint8_t wifiStatus;
|
||||
LOCAL mqtt_session_t globalSession;
|
||||
LOCAL mqtt_session_t *pGlobalSession = &globalSession;
|
||||
char ssid[32] = "yourwifissid";
|
||||
char passkey[64] = "yourwifipassword";
|
||||
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
|
||||
// Adafruit IO is at 52.5.238.97
|
||||
|
||||
static const char testUser[11] = { 0x4d, 0x72, 0x41, 0x75, 0x72, 0x65, 0x6c, 0x69, 0x75, 0x73, 0x52 };
|
||||
static const uint8_t testUser_len = 11;
|
||||
static const char testPass[32] = { 0x63, 0x61, 0x62, 0x31, 0x39, 0x36, 0x36, 0x34, 0x31, 0x66, 0x33, 0x63, 0x34, 0x34, 0x36, 0x32, 0x61, 0x35, 0x30, 0x39, 0x64, 0x62, 0x64, 0x31, 0x34, 0x61, 0x30, 0x61, 0x66, 0x36, 0x64, 0x32 };
|
||||
static const uint8_t testPass_len = 32;
|
||||
//static const char testPass[4] = { 0x74, 0x65, 0x73, 0x74 };
|
||||
//static const uint8_t testPass_len = 4;
|
||||
static const char testTopic[37] = { 0x4d, 0x72, 0x41, 0x75, 0x72, 0x65, 0x6c, 0x69, 0x75, 0x73, 0x52, 0x2f, 0x66, 0x65, 0x65, 0x64, 0x73, 0x2f, 0x62, 0x65, 0x64, 0x72, 0x6f, 0x6f, 0x6d, 0x2e, 0x62, 0x65, 0x64, 0x72, 0x6f, 0x6f, 0x6d, 0x74, 0x65, 0x6d, 0x70 };
|
||||
static const uint8_t testTopic_len = 37;
|
||||
//static const char testTopic[4] = { 0x74, 0x65, 0x73, 0x74 };
|
||||
//static const uint8_t testTopic_len = 4;
|
||||
static const char clientID[5] = { 0x46, 0x5a, 0x5a, 0x4e, 0x30 };
|
||||
static const uint8_t clientID_len = 5;
|
||||
pGlobalSession->port = 1883; // mqtt port
|
||||
static const char esp_tcp_server_ip[4] = {52, 5, 238, 97};
|
||||
os_memcpy(pGlobalSession->ip, esp_tcp_server_ip, 4);
|
||||
pGlobalSession->username_len = testUser_len;
|
||||
pGlobalSession->username = os_zalloc(sizeof(uint8_t) * pGlobalSession->username_len);
|
||||
os_memcpy(pGlobalSession->username, testUser, pGlobalSession->username_len);
|
||||
pGlobalSession->password_len = testPass_len;
|
||||
pGlobalSession->password = os_zalloc(sizeof(uint8_t) * pGlobalSession->password_len);
|
||||
os_memcpy(pGlobalSession->password, testPass, pGlobalSession->password_len);
|
||||
pGlobalSession->topic_name_len = testTopic_len;
|
||||
pGlobalSession->topic_name = os_zalloc(sizeof(uint8_t) * pGlobalSession->topic_name_len);
|
||||
os_memcpy(pGlobalSession->topic_name, testTopic, pGlobalSession->topic_name_len);
|
||||
pGlobalSession->client_id_len = clientID_len;
|
||||
pGlobalSession->client_id = os_zalloc(sizeof(uint8_t) * pGlobalSession->client_id_len);
|
||||
os_memcpy(pGlobalSession->client_id, clientID, pGlobalSession->client_id_len);
|
||||
|
||||
os_timer_setfn(&oneTimer, (os_timer_func_t *)tcpConnect, pGlobalSession);
|
||||
os_timer_arm(&oneTimer, 15000, 0);
|
||||
os_timer_setfn(&testTimer, (os_timer_func_t *)con, pGlobalSession);
|
||||
os_timer_arm(&testTimer, 16000, 0);
|
||||
}
|
||||
#endif
|
10
main.h
Normal file
10
main.h
Normal file
|
@ -0,0 +1,10 @@
|
|||
#include "mqtt.h"
|
||||
#include "onewire.h"
|
||||
|
||||
LOCAL void ICACHE_FLASH_ATTR con(void *arg);
|
||||
LOCAL void ICACHE_FLASH_ATTR pub(void *arg);
|
||||
LOCAL void ICACHE_FLASH_ATTR sub(void *arg);
|
||||
LOCAL void ICACHE_FLASH_ATTR ping(void *arg);
|
||||
LOCAL void ICACHE_FLASH_ATTR discon(void *arg);
|
||||
|
||||
void ICACHE_FLASH_ATTR user_init();
|
112
mqtt.c
112
mqtt.c
|
@ -11,6 +11,7 @@
|
|||
#include "espconn.h"
|
||||
#include "os_type.h"
|
||||
#include "mqtt.h"
|
||||
#include "onewire.h"
|
||||
|
||||
/* Functions we will need to implement:
|
||||
* Send -- will handle all sending of all packets
|
||||
|
@ -355,114 +356,3 @@ LOCAL uint8_t ICACHE_FLASH_ATTR mqtt_send(mqtt_session_t *session, uint8_t *data
|
|||
os_printf("No wifi! Narf!\n");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*********************************************/
|
||||
/* EVERYTHING FROM HERE DOWN IS SIMPLY */
|
||||
/* SIMULATING WHAT A USER WOULD DO WITH THIS */
|
||||
/* API. IT'S NOT PART OF THE LIBRARY. */
|
||||
/* IT'S JUST HERE FOR TESTING. */
|
||||
/*********************************************/
|
||||
#ifdef DEBUG
|
||||
LOCAL void ICACHE_FLASH_ATTR con(void *arg);
|
||||
LOCAL void ICACHE_FLASH_ATTR pub(void *arg);
|
||||
LOCAL void ICACHE_FLASH_ATTR sub(void *arg);
|
||||
LOCAL void ICACHE_FLASH_ATTR ping(void *arg);
|
||||
LOCAL void ICACHE_FLASH_ATTR discon(void *arg);
|
||||
|
||||
LOCAL void ICACHE_FLASH_ATTR con(void *arg) {
|
||||
os_printf("Entered con!\n");
|
||||
mqtt_session_t *pSession = (mqtt_session_t *)arg;
|
||||
mqtt_send(pSession, NULL, 0, MQTT_MSG_TYPE_CONNECT);
|
||||
|
||||
os_timer_disarm(&oneTimer);
|
||||
os_timer_setfn(&oneTimer, (os_timer_func_t *)sub, arg);
|
||||
os_timer_arm(&oneTimer, 200, 0);
|
||||
}
|
||||
|
||||
LOCAL void ICACHE_FLASH_ATTR pub(void *arg) {
|
||||
os_printf("Entered pub!\n");
|
||||
mqtt_session_t *pSession = (mqtt_session_t *)arg;
|
||||
uint8_t data[] = "2.718281828459045";
|
||||
uint32_t dataLen = os_strlen(data);
|
||||
mqtt_send(pSession, &data[0], dataLen, MQTT_MSG_TYPE_PUBLISH);
|
||||
os_timer_disarm(&oneTimer);
|
||||
os_timer_setfn(&oneTimer, (os_timer_func_t *)ping, arg);
|
||||
os_timer_arm(&oneTimer, 200, 0);
|
||||
}
|
||||
|
||||
LOCAL void ICACHE_FLASH_ATTR ping(void *arg) {
|
||||
os_printf("Entered ping!\n");
|
||||
mqtt_session_t *pSession = (mqtt_session_t *)arg;
|
||||
mqtt_send(pSession, NULL, 0, MQTT_MSG_TYPE_PINGREQ);
|
||||
os_timer_disarm(&oneTimer);
|
||||
os_timer_setfn(&oneTimer, (os_timer_func_t *)sub, arg);
|
||||
os_timer_arm(&oneTimer, 200, 0);
|
||||
}
|
||||
|
||||
LOCAL void ICACHE_FLASH_ATTR sub(void *arg) {
|
||||
os_printf("Entered sub!\n");
|
||||
mqtt_session_t *pSession = (mqtt_session_t *)arg;
|
||||
mqtt_send(pSession, NULL, 0, MQTT_MSG_TYPE_SUBSCRIBE);
|
||||
|
||||
}
|
||||
|
||||
LOCAL void ICACHE_FLASH_ATTR discon(void *arg) {
|
||||
os_printf("Entered discon!\n");
|
||||
mqtt_session_t *pSession = (mqtt_session_t *)arg;
|
||||
mqtt_send(pSession, NULL, 0, MQTT_MSG_TYPE_DISCONNECT);
|
||||
os_timer_disarm(&oneTimer);
|
||||
}
|
||||
|
||||
void ICACHE_FLASH_ATTR user_init() {
|
||||
uint8_t wifiStatus;
|
||||
LOCAL mqtt_session_t globalSession;
|
||||
LOCAL mqtt_session_t *pGlobalSession = &globalSession;
|
||||
char ssid[32] = "yourwifissid";
|
||||
char passkey[64] = "yourwifipassword";
|
||||
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
|
||||
// Adafruit IO is at 52.5.238.97
|
||||
|
||||
static const char testUser[11] = { 0x4d, 0x72, 0x41, 0x75, 0x72, 0x65, 0x6c, 0x69, 0x75, 0x73, 0x52 };
|
||||
static const uint8_t testUser_len = 11;
|
||||
static const char testPass[32] = { 0x63, 0x61, 0x62, 0x31, 0x39, 0x36, 0x36, 0x34, 0x31, 0x66, 0x33, 0x63, 0x34, 0x34, 0x36, 0x32, 0x61, 0x35, 0x30, 0x39, 0x64, 0x62, 0x64, 0x31, 0x34, 0x61, 0x30, 0x61, 0x66, 0x36, 0x64, 0x32 };
|
||||
static const uint8_t testPass_len = 32;
|
||||
//static const char testPass[4] = { 0x74, 0x65, 0x73, 0x74 };
|
||||
//static const uint8_t testPass_len = 4;
|
||||
static const char testTopic[37] = { 0x4d, 0x72, 0x41, 0x75, 0x72, 0x65, 0x6c, 0x69, 0x75, 0x73, 0x52, 0x2f, 0x66, 0x65, 0x65, 0x64, 0x73, 0x2f, 0x62, 0x65, 0x64, 0x72, 0x6f, 0x6f, 0x6d, 0x2e, 0x62, 0x65, 0x64, 0x72, 0x6f, 0x6f, 0x6d, 0x74, 0x65, 0x6d, 0x70 };
|
||||
static const uint8_t testTopic_len = 37;
|
||||
//static const char testTopic[4] = { 0x74, 0x65, 0x73, 0x74 };
|
||||
//static const uint8_t testTopic_len = 4;
|
||||
static const char clientID[5] = { 0x46, 0x5a, 0x5a, 0x4e, 0x30 };
|
||||
static const uint8_t clientID_len = 5;
|
||||
pGlobalSession->port = 1883; // mqtt port
|
||||
static const char esp_tcp_server_ip[4] = {52, 5, 238, 97};
|
||||
os_memcpy(pGlobalSession->ip, esp_tcp_server_ip, 4);
|
||||
pGlobalSession->username_len = testUser_len;
|
||||
pGlobalSession->username = os_zalloc(sizeof(uint8_t) * pGlobalSession->username_len);
|
||||
os_memcpy(pGlobalSession->username, testUser, pGlobalSession->username_len);
|
||||
pGlobalSession->password_len = testPass_len;
|
||||
pGlobalSession->password = os_zalloc(sizeof(uint8_t) * pGlobalSession->password_len);
|
||||
os_memcpy(pGlobalSession->password, testPass, pGlobalSession->password_len);
|
||||
pGlobalSession->topic_name_len = testTopic_len;
|
||||
pGlobalSession->topic_name = os_zalloc(sizeof(uint8_t) * pGlobalSession->topic_name_len);
|
||||
os_memcpy(pGlobalSession->topic_name, testTopic, pGlobalSession->topic_name_len);
|
||||
pGlobalSession->client_id_len = clientID_len;
|
||||
pGlobalSession->client_id = os_zalloc(sizeof(uint8_t) * pGlobalSession->client_id_len);
|
||||
os_memcpy(pGlobalSession->client_id, clientID, pGlobalSession->client_id_len);
|
||||
|
||||
os_timer_setfn(&oneTimer, (os_timer_func_t *)tcpConnect, pGlobalSession);
|
||||
os_timer_arm(&oneTimer, 15000, 0);
|
||||
os_timer_setfn(&testTimer, (os_timer_func_t *)con, pGlobalSession);
|
||||
os_timer_arm(&testTimer, 16000, 0);
|
||||
}
|
||||
#endif
|
||||
|
|
1
mqtt.h
1
mqtt.h
|
@ -82,6 +82,7 @@ typedef struct {
|
|||
uint32_t password_len; /**< The length of the password string */
|
||||
char validConnection; /**< Boolean value which indicates whether or not the TCP connection is established, set in tcpConnect() */
|
||||
struct espconn *activeConnection; /**< A pointer to the espconn structure containing details of the TCP connection */
|
||||
// Add pointers to user callback functions
|
||||
} mqtt_session_t;
|
||||
|
||||
/**
|
||||
|
|
38
onewire.c
Normal file
38
onewire.c
Normal file
|
@ -0,0 +1,38 @@
|
|||
#include <stdint.h>
|
||||
#include "gpio.h"
|
||||
#include "hw_timer.c"
|
||||
#include "onewire.h"
|
||||
|
||||
LOCAL void timerISR(void) {
|
||||
return;
|
||||
}
|
||||
|
||||
LOCAL void reset(oneWire_t *owDev) {
|
||||
// reset the ow line, check for presence?
|
||||
}
|
||||
|
||||
LOCAL void ICACHE_FLASH_ATTR transact(oneWire_t *owDev, ds18b20_cmds cmd) {
|
||||
switch(cmd) {
|
||||
case READ_ROM:
|
||||
break;
|
||||
case MATCH_ROM:
|
||||
break;
|
||||
case SEARCH_ROM:
|
||||
break;
|
||||
case ALARM_SEARCH:
|
||||
break;
|
||||
case SKIP_ROM:
|
||||
break;
|
||||
case CONVERT_T:
|
||||
break;
|
||||
case SCRATCH_READ:
|
||||
break;
|
||||
case SCRATCH_WRITE:
|
||||
break;
|
||||
case SCRATCH_COPY:
|
||||
break;
|
||||
case E2_RECALL:
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
33
onewire.h
Normal file
33
onewire.h
Normal file
|
@ -0,0 +1,33 @@
|
|||
#include <stdint.h>
|
||||
#include "hw_timer.c"
|
||||
|
||||
/**
|
||||
* @struct oneWire_t
|
||||
* Structure that holds all the information about onewire connections, including the 64-bit ROM address for each device and some buffer memory to read out the scratchpad
|
||||
*/
|
||||
typedef struct {
|
||||
uint8_t address[8];
|
||||
uint8_t scratchpad[20];
|
||||
} oneWire_t;
|
||||
|
||||
/**
|
||||
* @enum ds18b20_cmds
|
||||
* This enum simply gives easy to read names to all the various one-wire commands that the DS18B20 can accept
|
||||
*/
|
||||
enum ds18b20_cmds {
|
||||
READ_ROM = 0x33,
|
||||
MATCH_ROM = 0x55,
|
||||
SEARCH_ROM = 0xF0,
|
||||
ALARM_SEARCH = 0xEC,
|
||||
SKIP_ROM = 0xCC,
|
||||
CONVERT_T = 0x44,
|
||||
SCRATCH_READ = 0xBE,
|
||||
SCRATCH_WRITE = 0x4E,
|
||||
SCRATCH_COPY = 0x48,
|
||||
E2_RECALL = 0xB8
|
||||
};
|
||||
|
||||
|
||||
LOCAL void timerISR(void);
|
||||
LOCAL void ICACHE_FLASH_ATTR reset(oneWire_t *owDev);
|
||||
LOCAL void ICACHE_FLASH_ATTR transact(oneWire_t *owDev, ds18b20_cmds cmd);
|
Loading…
Add table
Add a link
Reference in a new issue