generated from LailaTheElf/rp2040_c
Compare commits
3 Commits
6a8cab04bf
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
8f18c13ab0
|
|||
|
b6cc668e2b
|
|||
|
34049c9e97
|
@@ -16,6 +16,16 @@ target_include_directories(wiznet_dhcp PUBLIC ./ioLibrary_Driver/Internet/DHCP)
|
||||
add_library(wiznet_mqtt
|
||||
ioLibrary_Driver/Internet/MQTT/MQTTClient.c
|
||||
ioLibrary_Driver/Internet/MQTT/mqtt_interface.c
|
||||
ioLibrary_Driver/Internet/MQTT/MQTTPacket/src/MQTTConnectClient.c
|
||||
ioLibrary_Driver/Internet/MQTT/MQTTPacket/src/MQTTConnectServer.c
|
||||
ioLibrary_Driver/Internet/MQTT/MQTTPacket/src/MQTTDeserializePublish.c
|
||||
ioLibrary_Driver/Internet/MQTT/MQTTPacket/src/MQTTFormat.c
|
||||
ioLibrary_Driver/Internet/MQTT/MQTTPacket/src/MQTTPacket.c
|
||||
ioLibrary_Driver/Internet/MQTT/MQTTPacket/src/MQTTSerializePublish.c
|
||||
ioLibrary_Driver/Internet/MQTT/MQTTPacket/src/MQTTSubscribeClient.c
|
||||
ioLibrary_Driver/Internet/MQTT/MQTTPacket/src/MQTTSubscribeServer.c
|
||||
ioLibrary_Driver/Internet/MQTT/MQTTPacket/src/MQTTUnsubscribeClient.c
|
||||
ioLibrary_Driver/Internet/MQTT/MQTTPacket/src/MQTTUnsubscribeServer.c
|
||||
)
|
||||
target_link_libraries(wiznet_mqtt w5500_driver)
|
||||
target_include_directories(wiznet_mqtt PUBLIC ./ioLibrary_Driver/Internet/DHCP)
|
||||
target_include_directories(wiznet_mqtt PUBLIC ./ioLibrary_Driver/Internet/MQTT)
|
||||
|
||||
15
src/config.h
15
src/config.h
@@ -6,15 +6,26 @@
|
||||
#define PIN_RGB_G 16
|
||||
#define PIN_RGB_B 25
|
||||
|
||||
#define SPI_DRIVER spi0
|
||||
#define SPI_BOUD 1E6
|
||||
#define PIN_SPI_CLK 2
|
||||
#define PIN_SPI_MISO 4
|
||||
#define PIN_SPI_MOSI 3
|
||||
#define PIN_MAX31856_CS 1
|
||||
#define PIN_DISPLAY_CS 0
|
||||
#define PIN_DISPLAY_DC 7
|
||||
#define PIN_DISPLAY_RST 6
|
||||
#define PIN_RST 6
|
||||
|
||||
#define PIN_W5500_CS 29
|
||||
#define W5500_SPI_DRIVER spi0
|
||||
#define W5500_SPI_DRIVER SPI_DRIVER
|
||||
|
||||
#define DHCP_SOCKET_ID 1
|
||||
|
||||
#define MQTT_SOCKET_ID 2
|
||||
#define MQTT_BROKER_IP {10, 1, 2, 2} // broker.hivemq.com
|
||||
#define MQTT_BROKER_PORT 1883
|
||||
#define MQTT_CLIENT_ID "ELFTemp"
|
||||
#define MQTT_USERNAME "tempsens"
|
||||
#define MQTT_PASSWORD ""
|
||||
|
||||
#endif
|
||||
|
||||
137
src/main.cpp
137
src/main.cpp
@@ -5,14 +5,18 @@
|
||||
#include <Adafruit_SSD1306.h>
|
||||
#include <Adafruit_SPIDevice.h>
|
||||
#include <Adafruit_ILI9341.h>
|
||||
#include <dhcp.h>
|
||||
#include <MQTTClient.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "config.h"
|
||||
#include "max31856.h"
|
||||
#include "w5500_rp.h"
|
||||
#include "wizchip_conf.h"
|
||||
|
||||
max31856_t temp[2];
|
||||
Adafruit_SPIDevice spidev(PIN_DISPLAY_CS, PIN_SPI_CLK, PIN_SPI_MISO, PIN_SPI_MOSI);
|
||||
Adafruit_SSD1306 display(128, 64, &spidev, PIN_DISPLAY_DC, PIN_DISPLAY_RST, PIN_DISPLAY_CS);
|
||||
Adafruit_SSD1306 display(128, 64, &spidev, PIN_DISPLAY_DC, PIN_RST, PIN_DISPLAY_CS);
|
||||
|
||||
void init_rbg()
|
||||
{
|
||||
@@ -42,16 +46,32 @@ void rgb_rainbow()
|
||||
|
||||
int main() {
|
||||
stdio_init_all();
|
||||
gpio_init(PIN_RST);
|
||||
gpio_set_dir(PIN_RST, GPIO_OUT);
|
||||
gpio_put(PIN_RST, 1);
|
||||
init_rbg();
|
||||
rgb_rainbow();
|
||||
gpio_put(PIN_RST, 0);
|
||||
|
||||
gpio_set_function(PIN_SPI_CLK, GPIO_FUNC_SPI);
|
||||
gpio_set_function(PIN_SPI_MOSI, GPIO_FUNC_SPI);
|
||||
gpio_set_function(PIN_SPI_MISO, GPIO_FUNC_SPI);
|
||||
spi_init(SPI_DRIVER, SPI_BOUD);
|
||||
spi_set_format(SPI_DRIVER, 8, SPI_CPOL_0, SPI_CPHA_0, SPI_MSB_FIRST);
|
||||
display.begin();
|
||||
display.fillScreen(SSD1306_BLACK);
|
||||
display.setTextColor(SSD1306_WHITE);
|
||||
display.setTextSize(2); //12x16
|
||||
display.setCursor(64-18, 32-8);
|
||||
display.print("hoi");
|
||||
display.display();
|
||||
|
||||
// init temp sensors
|
||||
spi_set_format(SPI_MAX31856, SPI_MAX31856_BLOCK_SIZE, SPI_MAX31856_SPO, SPI_MAX31856_SPH, SPI_MAX31856_BIT_ORDER);
|
||||
temp[0].pin_cs = PIN_MAX31856_CS;
|
||||
max31856_init(&(temp[0]));
|
||||
//temp[1].pin_cs = 5;
|
||||
//max31856_init(&(temp[1]));
|
||||
gpio_set_function(PIN_SPI_CLK, GPIO_FUNC_SPI);
|
||||
gpio_set_function(PIN_SPI_MOSI, GPIO_FUNC_SPI);
|
||||
gpio_set_function(PIN_SPI_MISO, GPIO_FUNC_SPI);
|
||||
// config temp sensor 0
|
||||
max31856_write8(&(temp[0]), MAX31856_REG_CR0, MAX31856_REG_CR0_CMODE | /*MAX31856_REG_CR0_OCFAULT |*/ MAX31856_REG_CR0_50HZ);
|
||||
max31856_write8(&(temp[0]), MAX31856_REG_CR1, MAX31856_REG_CR1_AVGSEL_4 | MAX31856_REG_CR1_TC_TYPE_K);
|
||||
@@ -61,41 +81,124 @@ int main() {
|
||||
|
||||
// init ethernet
|
||||
w5500_rp_init();
|
||||
wiz_NetInfo netinfo = {
|
||||
.mac={'E', 'L', 'F', 177, 31, 252},
|
||||
.ip={192, 254, 18, 91},
|
||||
.sn={255, 255, 0, 0}, // subnet mask
|
||||
.gw={0, 0, 0, 0},
|
||||
.dns={0, 0, 0, 0},
|
||||
.dhcp=NETINFO_DHCP
|
||||
};
|
||||
wizchip_setnetinfo(&netinfo);
|
||||
w5500_rp_dhcp();
|
||||
w5500_rp_mqtt_start();
|
||||
|
||||
display.begin();
|
||||
display.fillScreen(SSD1306_BLACK);
|
||||
display.setTextColor(SSD1306_WHITE);
|
||||
display.setTextSize(2);
|
||||
display.display();
|
||||
absolute_time_t time_read_temp = make_timeout_time_ms(0);
|
||||
absolute_time_t time_read_ip = make_timeout_time_ms(10);
|
||||
absolute_time_t time_update_display = make_timeout_time_ms(20);
|
||||
absolute_time_t time_second = make_timeout_time_ms(0);
|
||||
absolute_time_t time_msecond = make_timeout_time_ms(0);
|
||||
absolute_time_t time_mqtt = make_timeout_time_ms(0);
|
||||
|
||||
rgb_rainbow();
|
||||
char cj_str[8] = {0, };
|
||||
char tc_str[8] = {0, };
|
||||
char ip[16] = {0, };
|
||||
char mqtt_status[20] = {0, };
|
||||
|
||||
while (true)
|
||||
{
|
||||
char cj_str[8];
|
||||
char tc_str[8];
|
||||
int64_t time = INT64_MIN;
|
||||
int64_t time_max = INT64_MIN;
|
||||
|
||||
time = absolute_time_diff_us(time_read_temp, get_absolute_time());
|
||||
time_max = MAX(time, time_max);
|
||||
if (time >= 0) {
|
||||
time_read_temp = delayed_by_ms(time_read_temp, 300);
|
||||
spi_set_format(SPI_MAX31856, SPI_MAX31856_BLOCK_SIZE, SPI_MAX31856_SPO, SPI_MAX31856_SPH, SPI_MAX31856_BIT_ORDER);
|
||||
sleep_ms(5);
|
||||
max31856_temp_t cj = max31856_get_cj_temp_c(&temp[0]);
|
||||
snprintf(&cj_str[0], 8, "%i.%02uC", cj.i, cj.f);
|
||||
max31856_temp_t tc = max31856_get_tc_temp_c(&temp[0]);
|
||||
snprintf(&tc_str[0], 8, "%i.%02uC", tc.i, tc.f);
|
||||
|
||||
printf("CJ temp: %s\n", &cj_str[0]);
|
||||
printf("TC temp: %s\n", &tc_str[0]);
|
||||
MQTTMessage msg = {
|
||||
.qos = QOS2,
|
||||
.retained = 0,
|
||||
.dup = 0,
|
||||
.id = 0,
|
||||
.payload = &tc_str[0],
|
||||
.payloadlen = strlen(&tc_str[0])
|
||||
};
|
||||
w5500_rp_mqtt_publish("/kees/tempprobe/tc", &msg);
|
||||
msg.payload = &cj_str[0];
|
||||
msg.payloadlen = strlen(&cj_str[0]);
|
||||
w5500_rp_mqtt_publish("/kees/tempprobe/cj", &msg);
|
||||
|
||||
// printf("CJ temp: %s\n", &cj_str[0]);
|
||||
// printf("TC temp: %s\n", &tc_str[0]);
|
||||
}
|
||||
|
||||
time = absolute_time_diff_us(time_read_temp, get_absolute_time());
|
||||
time_max = MAX(time, time_max);
|
||||
if (time >= 0) {
|
||||
time_read_temp = delayed_by_ms(time_read_temp, 300);
|
||||
netinfo = {
|
||||
.mac={255, 255, 255, 255, 255, 255},
|
||||
.ip={255, 255, 255, 255},
|
||||
.sn={255, 255, 255, 255}, // subnet mask
|
||||
.gw={255, 255, 255, 255},
|
||||
.dns={255, 255, 255, 255},
|
||||
.dhcp=NETINFO_STATIC
|
||||
};
|
||||
wizchip_getnetinfo(&netinfo);
|
||||
// w5500_rp_print_netinfo(netinfo);
|
||||
snprintf(&ip[0], 16, "%u.%u.%u.%u", netinfo.ip[0], netinfo.ip[1], netinfo.ip[2], netinfo.ip[3]);
|
||||
}
|
||||
|
||||
time = absolute_time_diff_us(time_update_display, get_absolute_time());
|
||||
time_max = MAX(time, time_max);
|
||||
if (time >= 0) {
|
||||
time_update_display = delayed_by_ms(time_update_display, 300);
|
||||
spi_set_format(spi0, 8, SPI_CPOL_0, SPI_CPHA_0, SPI_MSB_FIRST);
|
||||
sleep_ms(5);
|
||||
display.fillScreen(SSD1306_BLACK);
|
||||
display.setCursor(0, 0);
|
||||
display.setTextSize(1);
|
||||
display.println(&ip[0]);
|
||||
display.setTextSize(2);
|
||||
display.print("CJ ");
|
||||
display.println(&cj_str[0]);
|
||||
display.print("TC ");
|
||||
display.println(&tc_str[0]);
|
||||
display.setTextSize(1);
|
||||
display.println(&mqtt_status[0]);
|
||||
display.display();
|
||||
}
|
||||
|
||||
sleep_ms(200);
|
||||
DHCP_run();
|
||||
time = absolute_time_diff_us(time_second, get_absolute_time());
|
||||
time_max = MAX(time, time_max);
|
||||
if (time >= 0) {
|
||||
time_second = delayed_by_ms(time_second, 1000);
|
||||
DHCP_time_handler();
|
||||
}
|
||||
|
||||
time = absolute_time_diff_us(time_msecond, get_absolute_time());
|
||||
time_max = MAX(time, time_max);
|
||||
if (time >= 0) {
|
||||
time_msecond = delayed_by_ms(time_msecond, 1);
|
||||
MilliTimer_Handler();
|
||||
}
|
||||
|
||||
time = absolute_time_diff_us(time_mqtt, get_absolute_time());
|
||||
time_max = MAX(time, time_max);
|
||||
if (time >= 0) {
|
||||
time_mqtt = delayed_by_ms(time_mqtt, 100);
|
||||
w5500_rp_mqtt_run();
|
||||
snprintf(&mqtt_status[0], 20, "MQTT: %s", w5500_rp_mqtt_get_status());
|
||||
}
|
||||
|
||||
if (time_max > 100) {
|
||||
printf("%llius\n", time_max);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -13,8 +13,8 @@ void max31856_init(max31856_t *max)
|
||||
gpio_put(max->pin_cs, 1);
|
||||
|
||||
// init SPI
|
||||
spi_init(SPI_MAX31856, SPI_MAX31856_BOUD);
|
||||
spi_set_format(SPI_MAX31856, SPI_MAX31856_BLOCK_SIZE, SPI_MAX31856_SPO, SPI_MAX31856_SPH, SPI_MAX31856_BIT_ORDER);
|
||||
// spi_init(SPI_MAX31856, SPI_MAX31856_BOUD);
|
||||
// spi_set_format(SPI_MAX31856, SPI_MAX31856_BLOCK_SIZE, SPI_MAX31856_SPO, SPI_MAX31856_SPH, SPI_MAX31856_BIT_ORDER);
|
||||
|
||||
max->initilzed = true;
|
||||
}
|
||||
|
||||
105
src/w5500_rp.cpp
105
src/w5500_rp.cpp
@@ -1,9 +1,14 @@
|
||||
|
||||
#include <hardware/timer.h>
|
||||
#include <pico/time.h>
|
||||
#include <stdio.h>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <wizchip_conf.h>
|
||||
#include "hardware/spi.h"
|
||||
#include <pico/stdlib.h>
|
||||
#include <wizchip_conf.h>
|
||||
#include <socket.h>
|
||||
#include <dhcp.h>
|
||||
#include <MQTTClient.h>
|
||||
|
||||
#include "config.h"
|
||||
#include "w5500_rp.h"
|
||||
@@ -26,6 +31,7 @@ void w5500_rp_spi_read_burst(uint8_t *buffer, uint16_t size) {
|
||||
}
|
||||
|
||||
void w5500_rp_spi_cs_select() {
|
||||
spi_set_format(W5500_SPI_DRIVER, 8, SPI_CPOL_0, SPI_CPHA_0, SPI_MSB_FIRST);
|
||||
gpio_put(PIN_W5500_CS, 0);
|
||||
}
|
||||
void w5500_rp_spi_cs_deselect() {
|
||||
@@ -33,8 +39,103 @@ void w5500_rp_spi_cs_deselect() {
|
||||
}
|
||||
|
||||
void w5500_rp_init() {
|
||||
gpio_init(PIN_W5500_CS);
|
||||
gpio_set_dir(PIN_W5500_CS, GPIO_OUT);
|
||||
|
||||
reg_wizchip_spi_cbfunc(&w5500_rp_spi_read_byte, &w5500_rp_spi_write_byte);
|
||||
reg_wizchip_spiburst_cbfunc(&w5500_rp_spi_read_burst, &w5500_rp_spi_write_burst);
|
||||
reg_wizchip_cs_cbfunc(w5500_rp_spi_cs_select, w5500_rp_spi_cs_deselect);
|
||||
reg_wizchip_cris_cbfunc(NULL, NULL);
|
||||
}
|
||||
|
||||
void w5500_rp_print_netinfo(wiz_NetInfo netinfo) {
|
||||
printf("IP: %3u.%3u.%3u.%3u\n", netinfo.ip[0], netinfo.ip[1], netinfo.ip[2], netinfo.ip[3]);
|
||||
printf("mask: %3u.%3u.%3u.%3u\n", netinfo.sn[0], netinfo.sn[1], netinfo.sn[2], netinfo.sn[3]);
|
||||
printf("gate: %3u.%3u.%3u.%3u\n", netinfo.gw[0], netinfo.gw[1], netinfo.gw[2], netinfo.gw[3]);
|
||||
printf("dns: %3u.%3u.%3u.%3u\n", netinfo.dns[0], netinfo.dns[1], netinfo.dns[2], netinfo.dns[3]);
|
||||
printf("mac: %02x:%02x:%02x:%02x:%02x:%02x\n", netinfo.mac[0], netinfo.mac[1], netinfo.mac[2], netinfo.mac[3], netinfo.mac[4], netinfo.mac[5]);
|
||||
}
|
||||
|
||||
bool dhcp_assigned = false;
|
||||
uint8_t dhcp_buffer[1024];
|
||||
|
||||
void w5500_rp_dhcp_assigned() {
|
||||
dhcp_assigned = true;
|
||||
}
|
||||
|
||||
void w5500_rp_dhcp() {
|
||||
DHCP_init(DHCP_SOCKET_ID, &dhcp_buffer[0]);
|
||||
reg_dhcp_cbfunc(NULL, NULL, NULL);
|
||||
}
|
||||
|
||||
|
||||
enum mqtt_state {
|
||||
MQTT_INACTIVE,
|
||||
MQTT_CONNECTING,
|
||||
MQTT_CONNECTED,
|
||||
} mqtt_state = MQTT_INACTIVE;
|
||||
char mqtt_state_str_inactive[] = "inactive";
|
||||
char mqtt_state_str_connecting[] = "connecting";
|
||||
char mqtt_state_str_connected[] = "connected";
|
||||
char mqtt_state_str_invalid[] = "invlid";
|
||||
MQTTClient mqtt;
|
||||
Network mqtt_net;
|
||||
unsigned char mqtt_buff_send[1024];
|
||||
unsigned char mqtt_buff_read[1024];
|
||||
|
||||
void w5500_rp_mqtt_start() {
|
||||
mqtt_state = MQTT_CONNECTING;
|
||||
}
|
||||
void w5500_rp_mqtt_init() {
|
||||
mqtt_state = MQTT_CONNECTING;
|
||||
NewNetwork(&mqtt_net, MQTT_SOCKET_ID);
|
||||
unsigned char mqtt_broker_ip[] = MQTT_BROKER_IP;
|
||||
int err = ConnectNetwork(&mqtt_net, &mqtt_broker_ip[0], MQTT_BROKER_PORT);
|
||||
if (err == SOCK_ERROR) {
|
||||
return;
|
||||
}
|
||||
MQTTClientInit(&mqtt, &mqtt_net, 300, &mqtt_buff_send[0], 1024, &mqtt_buff_read[0], 1024);
|
||||
|
||||
|
||||
MQTTPacket_connectData mqtt_conndata = MQTTPacket_connectData_initializer;
|
||||
mqtt_conndata.willFlag = 0;
|
||||
mqtt_conndata.MQTTVersion = 3;
|
||||
mqtt_conndata.clientID.cstring = MQTT_CLIENT_ID;
|
||||
mqtt_conndata.username.cstring = MQTT_USERNAME;
|
||||
mqtt_conndata.password.cstring = MQTT_PASSWORD;
|
||||
mqtt_conndata.keepAliveInterval = 60;
|
||||
mqtt_conndata.cleansession = 1;
|
||||
if (MQTTConnect(&mqtt, &mqtt_conndata) == 0) {
|
||||
mqtt_state = MQTT_CONNECTED;
|
||||
}
|
||||
}
|
||||
|
||||
void w5500_rp_mqtt_run() {
|
||||
switch (mqtt_state) {
|
||||
case MQTT_INACTIVE:
|
||||
return;
|
||||
case MQTT_CONNECTING:
|
||||
w5500_rp_mqtt_init();
|
||||
break;
|
||||
case MQTT_CONNECTED:
|
||||
MQTTYield(&mqtt, 5);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
char* w5500_rp_mqtt_get_status() {
|
||||
switch (mqtt_state) {
|
||||
case MQTT_INACTIVE:
|
||||
return &mqtt_state_str_inactive[0];
|
||||
case MQTT_CONNECTING:
|
||||
return &mqtt_state_str_connecting[0];
|
||||
case MQTT_CONNECTED:
|
||||
return &mqtt_state_str_connected[0];
|
||||
}
|
||||
return &mqtt_state_str_invalid[0];
|
||||
}
|
||||
|
||||
void w5500_rp_mqtt_publish(const char *topic, MQTTMessage *msg) {
|
||||
MQTTPublish(&mqtt, topic, msg);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,8 +1,16 @@
|
||||
#ifndef W5500_RP_H
|
||||
#define W5500_RP_H
|
||||
|
||||
#include "hardware/spi.h"
|
||||
#include <wizchip_conf.h>
|
||||
#include <MQTTClient.h>
|
||||
|
||||
void w5500_rp_init();
|
||||
void w5500_rp_print_netinfo(wiz_NetInfo netinfo);
|
||||
void w5500_rp_dhcp();
|
||||
|
||||
void w5500_rp_mqtt_run();
|
||||
char* w5500_rp_mqtt_get_status();
|
||||
void w5500_rp_mqtt_start();
|
||||
void w5500_rp_mqtt_publish(const char *topic, MQTTMessage *msg);
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user