Merge branch 'feature/better_crash_dumps' into open-libmain
This commit is contained in:
commit
5fa17990dd
62 changed files with 5163 additions and 1355 deletions
|
|
@ -1,5 +1,4 @@
|
|||
# Simple makefile for simple example
|
||||
PROGRAM=cpp_01_tasks
|
||||
OTA=0
|
||||
EXTRA_COMPONENTS=extras/cpp_support
|
||||
include ../../common.mk
|
||||
|
|
|
|||
|
|
@ -19,15 +19,14 @@
|
|||
|
||||
// DS18B20 driver
|
||||
#include "ds18b20/ds18b20.h"
|
||||
// Onewire init
|
||||
#include "onewire/onewire.h"
|
||||
|
||||
void broadcast_temperature(void *pvParameters)
|
||||
{
|
||||
|
||||
uint8_t amount = 0;
|
||||
uint8_t sensors = 2;
|
||||
ds_sensor_t t[sensors];
|
||||
uint8_t sensors = 1;
|
||||
ds18b20_addr_t addrs[sensors];
|
||||
float results[sensors];
|
||||
|
||||
// Use GPIO 13 as one wire pin.
|
||||
uint8_t GPIO_FOR_ONE_WIRE = 13;
|
||||
|
|
@ -36,8 +35,6 @@ void broadcast_temperature(void *pvParameters)
|
|||
|
||||
// Broadcaster part
|
||||
err_t err;
|
||||
// Initialize one wire bus.
|
||||
onewire_init(GPIO_FOR_ONE_WIRE);
|
||||
|
||||
while(1) {
|
||||
|
||||
|
|
@ -66,18 +63,17 @@ void broadcast_temperature(void *pvParameters)
|
|||
|
||||
for(;;) {
|
||||
// Search all DS18B20, return its amount and feed 't' structure with result data.
|
||||
amount = ds18b20_read_all(GPIO_FOR_ONE_WIRE, t);
|
||||
amount = ds18b20_scan_devices(GPIO_FOR_ONE_WIRE, addrs, sensors);
|
||||
|
||||
if (amount < sensors){
|
||||
printf("Something is wrong, I expect to see %d sensors \nbut just %d was detected!\n", sensors, amount);
|
||||
}
|
||||
|
||||
for (int i = 0; i < amount; ++i)
|
||||
ds18b20_measure_and_read_multi(GPIO_FOR_ONE_WIRE, addrs, sensors, results);
|
||||
for (int i = 0; i < sensors; ++i)
|
||||
{
|
||||
int intpart = (int)t[i].value;
|
||||
int fraction = (int)((t[i].value - intpart) * 100);
|
||||
// Multiple "" here is just to satisfy compiler and don`t raise 'hex escape sequence out of range' warning.
|
||||
sprintf(msg, "Sensor %d report: %d.%02d ""\xC2""\xB0""C\n",t[i].id, intpart, fraction);
|
||||
// ("\xC2\xB0" is the degree character (U+00B0) in UTF-8)
|
||||
sprintf(msg, "Sensor %08x%08x reports: %f \xC2\xB0""C\n", (uint32_t)(addrs[i] >> 32), (uint32_t)addrs[i], results[i]);
|
||||
printf("%s", msg);
|
||||
|
||||
struct netbuf* buf = netbuf_new();
|
||||
|
|
|
|||
|
|
@ -1,59 +1,78 @@
|
|||
/* ds18b20 - Retrieves temperature from ds18b20 sensors and print it out.
|
||||
/* ds18b20_onewire.c - Retrieves readings from one or more DS18B20 temperature
|
||||
* sensors, and prints the results to stdout.
|
||||
*
|
||||
* This sample code is in the public domain.,
|
||||
*/
|
||||
#include "espressif/esp_common.h"
|
||||
#include "esp/uart.h"
|
||||
|
||||
#include "FreeRTOS.h"
|
||||
#include "task.h"
|
||||
#include "timers.h"
|
||||
#include "queue.h"
|
||||
#include "esp/uart.h"
|
||||
|
||||
// DS18B20 driver
|
||||
#include "ds18b20/ds18b20.h"
|
||||
// Onewire init
|
||||
#include "onewire/onewire.h"
|
||||
|
||||
void print_temperature(void *pvParameters)
|
||||
{
|
||||
int delay = 500;
|
||||
uint8_t amount = 0;
|
||||
// Declare amount of sensors
|
||||
uint8_t sensors = 2;
|
||||
ds_sensor_t t[sensors];
|
||||
|
||||
// Use GPIO 13 as one wire pin.
|
||||
uint8_t GPIO_FOR_ONE_WIRE = 13;
|
||||
|
||||
onewire_init(GPIO_FOR_ONE_WIRE);
|
||||
#define SENSOR_GPIO 13
|
||||
#define MAX_SENSORS 8
|
||||
#define RESCAN_INTERVAL 8
|
||||
#define LOOP_DELAY_MS 250
|
||||
|
||||
void print_temperature(void *pvParameters) {
|
||||
ds18b20_addr_t addrs[MAX_SENSORS];
|
||||
float temps[MAX_SENSORS];
|
||||
int sensor_count;
|
||||
|
||||
// There is no special initialization required before using the ds18b20
|
||||
// routines. However, we make sure that the internal pull-up resistor is
|
||||
// enabled on the GPIO pin so that one can connect up a sensor without
|
||||
// needing an external pull-up (Note: The internal (~47k) pull-ups of the
|
||||
// ESP8266 do appear to work, at least for simple setups (one or two sensors
|
||||
// connected with short leads), but do not technically meet the pull-up
|
||||
// requirements from the DS18B20 datasheet and may not always be reliable.
|
||||
// For a real application, a proper 4.7k external pull-up resistor is
|
||||
// recommended instead!)
|
||||
|
||||
gpio_set_pullup(SENSOR_GPIO, true, true);
|
||||
|
||||
while(1) {
|
||||
// Search all DS18B20, return its amount and feed 't' structure with result data.
|
||||
amount = ds18b20_read_all(GPIO_FOR_ONE_WIRE, t);
|
||||
// Every RESCAN_INTERVAL samples, check to see if the sensors connected
|
||||
// to our bus have changed.
|
||||
sensor_count = ds18b20_scan_devices(SENSOR_GPIO, addrs, MAX_SENSORS);
|
||||
|
||||
if (amount < sensors){
|
||||
printf("Something is wrong, I expect to see %d sensors \nbut just %d was detected!\n", sensors, amount);
|
||||
}
|
||||
if (sensor_count < 1) {
|
||||
printf("\nNo sensors detected!\n");
|
||||
} else {
|
||||
printf("\n%d sensors detected:\n", sensor_count);
|
||||
// If there were more sensors found than we have space to handle,
|
||||
// just report the first MAX_SENSORS..
|
||||
if (sensor_count > MAX_SENSORS) sensor_count = MAX_SENSORS;
|
||||
|
||||
for (int i = 0; i < amount; ++i)
|
||||
{
|
||||
int intpart = (int)t[i].value;
|
||||
int fraction = (int)((t[i].value - intpart) * 100);
|
||||
// Multiple "" here is just to satisfy compiler and don`t raise 'hex escape sequence out of range' warning.
|
||||
printf("Sensor %d report: %d.%02d ""\xC2""\xB0""C\n",t[i].id, intpart, fraction);
|
||||
// Do a number of temperature samples, and print the results.
|
||||
for (int i = 0; i < RESCAN_INTERVAL; i++) {
|
||||
ds18b20_measure_and_read_multi(SENSOR_GPIO, addrs, sensor_count, temps);
|
||||
for (int j = 0; j < sensor_count; j++) {
|
||||
// The DS18B20 address is a 64-bit integer, but newlib-nano
|
||||
// printf does not support printing 64-bit values, so we
|
||||
// split it up into two 32-bit integers and print them
|
||||
// back-to-back to make it look like one big hex number.
|
||||
uint32_t addr0 = addrs[j] >> 32;
|
||||
uint32_t addr1 = addrs[j];
|
||||
float temp_c = temps[j];
|
||||
float temp_f = (temp_c * 1.8) + 32;
|
||||
printf(" Sensor %08x%08x reports %f deg C (%f deg F)\n", addr0, addr1, temp_c, temp_f);
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
// Wait for a little bit between each sample (note that the
|
||||
// ds18b20_measure_and_read_multi operation already takes at
|
||||
// least 750ms to run, so this is on top of that delay).
|
||||
vTaskDelay(LOOP_DELAY_MS / portTICK_RATE_MS);
|
||||
}
|
||||
}
|
||||
printf("\n");
|
||||
vTaskDelay(delay / portTICK_RATE_MS);
|
||||
}
|
||||
}
|
||||
|
||||
void user_init(void)
|
||||
{
|
||||
void user_init(void) {
|
||||
uart_set_baud(0, 115200);
|
||||
|
||||
printf("SDK version:%s\n", sdk_system_get_sdk_version());
|
||||
|
||||
xTaskCreate(&print_temperature, (signed char *)"print_temperature", 256, NULL, 2, NULL);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -305,7 +305,7 @@ static void test_system_interaction()
|
|||
}
|
||||
uint32_t ticks = xTaskGetTickCount() - start;
|
||||
printf("Timer interaction test PASSED after %dms.\n", ticks*portTICK_RATE_MS);
|
||||
while(1) {}
|
||||
abort();
|
||||
}
|
||||
|
||||
/* The following "sanity tests" are designed to try to execute every code path
|
||||
|
|
|
|||
|
|
@ -1,43 +1,44 @@
|
|||
/* This is the root certificate for the CA trust chain of
|
||||
/* This is the CA certificate for the CA trust chain of
|
||||
www.howsmyssl.com in PEM format, as dumped via:
|
||||
|
||||
openssl s_client -showcerts -connect www.howsmyssl.com:443 </dev/null
|
||||
|
||||
The root cert is the last cert in the chain output by the server.
|
||||
The CA cert is the last cert in the chain output by the server.
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
/*
|
||||
1 s:/C=US/O=Let's Encrypt/CN=Let's Encrypt Authority X3
|
||||
i:/O=Digital Signature Trust Co./CN=DST Root CA X3
|
||||
*/
|
||||
const char *server_root_cert = "-----BEGIN CERTIFICATE-----\r\n"
|
||||
"MIIFNzCCBB+gAwIBAgISAfl7TPw6Mf/F6VCBc5eaHA7kMA0GCSqGSIb3DQEBCwUA\r\n"
|
||||
"MEoxCzAJBgNVBAYTAlVTMRYwFAYDVQQKEw1MZXQncyBFbmNyeXB0MSMwIQYDVQQD\r\n"
|
||||
"ExpMZXQncyBFbmNyeXB0IEF1dGhvcml0eSBYMTAeFw0xNTEyMzAwNzQ0MDBaFw0x\r\n"
|
||||
"NjAzMjkwNzQ0MDBaMBwxGjAYBgNVBAMTEXd3dy5ob3dzbXlzc2wuY29tMIIBIjAN\r\n"
|
||||
"BgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEArKF7WzSrDPinQhd9mVfoW5u46/TC\r\n"
|
||||
"fbYKR3MEryetUSeQuwuXFj2xO6+a/JQ99UC3Eq+9s8uFdx1zFFS6qfW+HXBwGWVf\r\n"
|
||||
"ajKEyIcXUJZCRn7aWpTWZq6cuv4bZSv1QklGViQs8UCZifcN0A/mYrH7zHG2WDn2\r\n"
|
||||
"fxNgA+nJBqbZPr1gP9hqGFCnX+dPR5WxtC9+Dv9Sx+wiOWVz/obVTCygdqqcpa5I\r\n"
|
||||
"3/U9REVgO2VfT+xMty6NZTMCjTJ+GXZuB/BrMe9+ZmgWk0grJyqdrCxOCyK6B4g+\r\n"
|
||||
"Fvs8WFRNTHdQnP3/NT5hPtreZ3nuY2YY7RbGFwUaBcvJwbbIqalpiQ1X7QIDAQAB\r\n"
|
||||
"o4ICQzCCAj8wDgYDVR0PAQH/BAQDAgWgMB0GA1UdJQQWMBQGCCsGAQUFBwMBBggr\r\n"
|
||||
"BgEFBQcDAjAMBg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AMCAYYwfwYIKwYBBQUHAQEEczBxMDIG\r\n"
|
||||
"CCsGAQUFBzABhiZodHRwOi8vaXNyZy50cnVzdGlkLm9jc3AuaWRlbnRydXN0LmNv\r\n"
|
||||
"bTA7BggrBgEFBQcwAoYvaHR0cDovL2FwcHMuaWRlbnRydXN0LmNvbS9yb290cy9k\r\n"
|
||||
"c3Ryb290Y2F4My5wN2MwHwYDVR0jBBgwFoAUxKexpHsscfrb4UuQdf/EFWCFiRAw\r\n"
|
||||
"VAYDVR0gBE0wSzAIBgZngQwBAgEwPwYLKwYBBAGC3xMBAQEwMDAuBggrBgEFBQcC\r\n"
|
||||
"ARYiaHR0cDovL2Nwcy5yb290LXgxLmxldHNlbmNyeXB0Lm9yZzA8BgNVHR8ENTAz\r\n"
|
||||
"MDGgL6AthitodHRwOi8vY3JsLmlkZW50cnVzdC5jb20vRFNUUk9PVENBWDNDUkwu\r\n"
|
||||
"Y3JsMB0GA1UdDgQWBBSoSmpjBH3duubRObemRWXv86jsoTANBgkqhkiG9w0BAQsF\r\n"
|
||||
"AAOCAQEA3TPXEfNjWDjdGBX7CVW+dla5cEilaUcne8IkCJLxWh9KEik3JHRRHGJo\r\n"
|
||||
"uM2VcGfl96S8TihRzZvoroed6ti6WqEBmtzw3Wodatg+VyOeph4EYpr/1wXKtx8/\r\n"
|
||||
"wApIvJSwtmVi4MFU5aMqrSDE6ea73Mj2tcMyo5jMd6jmeWUHK8so/joWUoHOUgwu\r\n"
|
||||
"X4Po1QYz+3dszkDqMp4fklxBwXRsW10KXzPMTZ+sOPAveyxindmjkW8lGy+QsRlG\r\n"
|
||||
"PfZ+G6Z6h7mjem0Y+iWlkYcV4PIWL1iwBi8saCbGS5jN2p8M+X+Q7UNKEkROb3N6\r\n"
|
||||
"KOqkqm57TH2H3eDJAkSnh6/DNFu0Qg==\r\n"
|
||||
"-----END CERTIFICATE-----\r\n";
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -41,11 +41,11 @@
|
|||
#include "mbedtls/error.h"
|
||||
#include "mbedtls/certs.h"
|
||||
|
||||
#define WEB_SERVER "howsmyssl.com"
|
||||
#define WEB_SERVER "www.howsmyssl.com"
|
||||
#define WEB_PORT "443"
|
||||
#define WEB_URL "https://www.howsmyssl.com/a/check"
|
||||
|
||||
#define GET_REQUEST "GET "WEB_URL" HTTP/1.1\n\n"
|
||||
#define GET_REQUEST "GET "WEB_URL" HTTP/1.1\nHost: "WEB_SERVER"\n\n"
|
||||
|
||||
/* Root cert for howsmyssl.com, stored in cert.c */
|
||||
extern const char *server_root_cert;
|
||||
|
|
@ -115,7 +115,7 @@ void http_get_task(void *pvParameters)
|
|||
strlen(pers))) != 0)
|
||||
{
|
||||
printf(" failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret);
|
||||
while(1) {} /* todo: replace with abort() */
|
||||
abort();
|
||||
}
|
||||
|
||||
printf(" ok\n");
|
||||
|
|
@ -129,7 +129,7 @@ void http_get_task(void *pvParameters)
|
|||
if(ret < 0)
|
||||
{
|
||||
printf(" failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n", -ret);
|
||||
while(1) {} /* todo: replace with abort() */
|
||||
abort();
|
||||
}
|
||||
|
||||
printf(" ok (%d skipped)\n", ret);
|
||||
|
|
@ -138,7 +138,7 @@ void http_get_task(void *pvParameters)
|
|||
if((ret = mbedtls_ssl_set_hostname(&ssl, WEB_SERVER)) != 0)
|
||||
{
|
||||
printf(" failed\n ! mbedtls_ssl_set_hostname returned %d\n\n", ret);
|
||||
while(1) {} /* todo: replace with abort() */
|
||||
abort();
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
|
|||
3
examples/mqtt_client/Makefile
Normal file
3
examples/mqtt_client/Makefile
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
PROGRAM=mqtt_client
|
||||
EXTRA_COMPONENTS = extras/paho_mqtt_c
|
||||
include ../../common.mk
|
||||
221
examples/mqtt_client/mqtt_client.c
Normal file
221
examples/mqtt_client/mqtt_client.c
Normal file
|
|
@ -0,0 +1,221 @@
|
|||
#include "espressif/esp_common.h"
|
||||
#include "esp/uart.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include <FreeRTOS.h>
|
||||
#include <task.h>
|
||||
#include <ssid_config.h>
|
||||
|
||||
#include <espressif/esp_sta.h>
|
||||
#include <espressif/esp_wifi.h>
|
||||
|
||||
#include <paho_mqtt_c/MQTTESP8266.h>
|
||||
#include <paho_mqtt_c/MQTTClient.h>
|
||||
|
||||
#include <semphr.h>
|
||||
|
||||
|
||||
/* You can use http://test.mosquitto.org/ to test mqtt_client instead
|
||||
* of setting up your own MQTT server */
|
||||
#define MQTT_HOST ("test.mosquitto.org")
|
||||
#define MQTT_PORT 1883
|
||||
|
||||
#define MQTT_USER NULL
|
||||
#define MQTT_PASS NULL
|
||||
|
||||
xSemaphoreHandle wifi_alive;
|
||||
xQueueHandle publish_queue;
|
||||
#define PUB_MSG_LEN 16
|
||||
|
||||
static void beat_task(void *pvParameters)
|
||||
{
|
||||
portTickType xLastWakeTime = xTaskGetTickCount();
|
||||
char msg[PUB_MSG_LEN];
|
||||
int count = 0;
|
||||
|
||||
while (1) {
|
||||
vTaskDelayUntil(&xLastWakeTime, 10000 / portTICK_RATE_MS);
|
||||
printf("beat\r\n");
|
||||
snprintf(msg, PUB_MSG_LEN, "Beat %d\r\n", count++);
|
||||
if (xQueueSend(publish_queue, (void *)msg, 0) == pdFALSE) {
|
||||
printf("Publish queue overflow.\r\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void topic_received(MessageData *md)
|
||||
{
|
||||
int i;
|
||||
MQTTMessage *message = md->message;
|
||||
printf("Received: ");
|
||||
for( i = 0; i < md->topic->lenstring.len; ++i)
|
||||
printf("%c", md->topic->lenstring.data[ i ]);
|
||||
|
||||
printf(" = ");
|
||||
for( i = 0; i < (int)message->payloadlen; ++i)
|
||||
printf("%c", ((char *)(message->payload))[i]);
|
||||
|
||||
printf("\r\n");
|
||||
}
|
||||
|
||||
static const char * get_my_id(void)
|
||||
{
|
||||
// Use MAC address for Station as unique ID
|
||||
static char my_id[13];
|
||||
static bool my_id_done = false;
|
||||
int8_t i;
|
||||
uint8_t x;
|
||||
if (my_id_done)
|
||||
return my_id;
|
||||
if (!sdk_wifi_get_macaddr(STATION_IF, (uint8_t *)my_id))
|
||||
return NULL;
|
||||
for (i = 5; i >= 0; --i)
|
||||
{
|
||||
x = my_id[i] & 0x0F;
|
||||
if (x > 9) x += 7;
|
||||
my_id[i * 2 + 1] = x + '0';
|
||||
x = my_id[i] >> 4;
|
||||
if (x > 9) x += 7;
|
||||
my_id[i * 2] = x + '0';
|
||||
}
|
||||
my_id[12] = '\0';
|
||||
my_id_done = true;
|
||||
return my_id;
|
||||
}
|
||||
|
||||
static void mqtt_task(void *pvParameters)
|
||||
{
|
||||
int ret = 0;
|
||||
struct Network network;
|
||||
MQTTClient client = DefaultClient;
|
||||
char mqtt_client_id[20];
|
||||
uint8_t mqtt_buf[100];
|
||||
uint8_t mqtt_readbuf[100];
|
||||
MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
|
||||
|
||||
NewNetwork( &network );
|
||||
memset(mqtt_client_id, 0, sizeof(mqtt_client_id));
|
||||
strcpy(mqtt_client_id, "ESP-");
|
||||
strcat(mqtt_client_id, get_my_id());
|
||||
|
||||
while(1) {
|
||||
xSemaphoreTake(wifi_alive, portMAX_DELAY);
|
||||
printf("%s: started\n\r", __func__);
|
||||
printf("%s: (Re)connecting to MQTT server %s ... ",__func__,
|
||||
MQTT_HOST);
|
||||
ret = ConnectNetwork(&network, MQTT_HOST, MQTT_PORT);
|
||||
if( ret ){
|
||||
printf("error: %d\n\r", ret);
|
||||
taskYIELD();
|
||||
continue;
|
||||
}
|
||||
printf("done\n\r");
|
||||
NewMQTTClient(&client, &network, 5000, mqtt_buf, 100,
|
||||
mqtt_readbuf, 100);
|
||||
|
||||
data.willFlag = 0;
|
||||
data.MQTTVersion = 3;
|
||||
data.clientID.cstring = mqtt_client_id;
|
||||
data.username.cstring = MQTT_USER;
|
||||
data.password.cstring = MQTT_PASS;
|
||||
data.keepAliveInterval = 10;
|
||||
data.cleansession = 0;
|
||||
printf("Send MQTT connect ... ");
|
||||
ret = MQTTConnect(&client, &data);
|
||||
if(ret){
|
||||
printf("error: %d\n\r", ret);
|
||||
DisconnectNetwork(&network);
|
||||
taskYIELD();
|
||||
continue;
|
||||
}
|
||||
printf("done\r\n");
|
||||
MQTTSubscribe(&client, "/esptopic", QOS1, topic_received);
|
||||
xQueueReset(publish_queue);
|
||||
|
||||
while(1){
|
||||
|
||||
char msg[PUB_MSG_LEN - 1] = "\0";
|
||||
while(xQueueReceive(publish_queue, (void *)msg, 0) ==
|
||||
pdTRUE){
|
||||
printf("got message to publish\r\n");
|
||||
MQTTMessage message;
|
||||
message.payload = msg;
|
||||
message.payloadlen = PUB_MSG_LEN;
|
||||
message.dup = 0;
|
||||
message.qos = QOS1;
|
||||
message.retained = 0;
|
||||
ret = MQTTPublish(&client, "/beat", &message);
|
||||
if (ret != SUCCESS ){
|
||||
printf("error while publishing message: %d\n", ret );
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
ret = MQTTYield(&client, 1000);
|
||||
if (ret == DISCONNECTED)
|
||||
break;
|
||||
}
|
||||
printf("Connection dropped, request restart\n\r");
|
||||
taskYIELD();
|
||||
}
|
||||
}
|
||||
|
||||
static void wifi_task(void *pvParameters)
|
||||
{
|
||||
uint8_t status = 0;
|
||||
uint8_t retries = 30;
|
||||
struct sdk_station_config config = {
|
||||
.ssid = WIFI_SSID,
|
||||
.password = WIFI_PASS,
|
||||
};
|
||||
|
||||
printf("WiFi: connecting to WiFi\n\r");
|
||||
sdk_wifi_set_opmode(STATION_MODE);
|
||||
sdk_wifi_station_set_config(&config);
|
||||
|
||||
while(1)
|
||||
{
|
||||
while ((status != STATION_GOT_IP) && (retries)){
|
||||
status = sdk_wifi_station_get_connect_status();
|
||||
printf("%s: status = %d\n\r", __func__, status );
|
||||
if( status == STATION_WRONG_PASSWORD ){
|
||||
printf("WiFi: wrong password\n\r");
|
||||
break;
|
||||
} else if( status == STATION_NO_AP_FOUND ) {
|
||||
printf("WiFi: AP not found\n\r");
|
||||
break;
|
||||
} else if( status == STATION_CONNECT_FAIL ) {
|
||||
printf("WiFi: connection failed\r\n");
|
||||
break;
|
||||
}
|
||||
vTaskDelay( 1000 / portTICK_RATE_MS );
|
||||
--retries;
|
||||
}
|
||||
if (status == STATION_GOT_IP) {
|
||||
printf("WiFi: Connected\n\r");
|
||||
xSemaphoreGive( wifi_alive );
|
||||
taskYIELD();
|
||||
}
|
||||
|
||||
while ((status = sdk_wifi_station_get_connect_status()) == STATION_GOT_IP) {
|
||||
xSemaphoreGive( wifi_alive );
|
||||
taskYIELD();
|
||||
}
|
||||
printf("WiFi: disconnected\n\r");
|
||||
sdk_wifi_station_disconnect();
|
||||
vTaskDelay( 1000 / portTICK_RATE_MS );
|
||||
}
|
||||
}
|
||||
|
||||
void user_init(void)
|
||||
{
|
||||
uart_set_baud(0, 115200);
|
||||
printf("SDK version:%s\n", sdk_system_get_sdk_version());
|
||||
|
||||
vSemaphoreCreateBinary(wifi_alive);
|
||||
publish_queue = xQueueCreate(3, PUB_MSG_LEN);
|
||||
xTaskCreate(&wifi_task, (int8_t *)"wifi_task", 256, NULL, 2, NULL);
|
||||
xTaskCreate(&beat_task, (int8_t *)"beat_task", 256, NULL, 3, NULL);
|
||||
xTaskCreate(&mqtt_task, (int8_t *)"mqtt_task", 1024, NULL, 4, NULL);
|
||||
}
|
||||
|
|
@ -14,13 +14,15 @@
|
|||
#include "ssid_config.h"
|
||||
|
||||
#include "ota-tftp.h"
|
||||
#include "rboot-ota.h"
|
||||
#include "rboot-integration.h"
|
||||
#include "rboot.h"
|
||||
#include "rboot-api.h"
|
||||
|
||||
void user_init(void)
|
||||
{
|
||||
uart_set_baud(0, 115200);
|
||||
|
||||
rboot_config_t conf = rboot_get_config();
|
||||
rboot_config conf = rboot_get_config();
|
||||
printf("\r\n\r\nOTA Basic demo.\r\nCurrently running on flash slot %d / %d.\r\n\r\n",
|
||||
conf.current_rom, conf.count);
|
||||
|
||||
|
|
|
|||
|
|
@ -85,7 +85,7 @@ void tls_server_task(void *pvParameters)
|
|||
strlen(pers))) != 0)
|
||||
{
|
||||
printf(" failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret);
|
||||
while(1) {} /* todo: replace with abort() */
|
||||
abort();
|
||||
}
|
||||
|
||||
printf(" ok\n");
|
||||
|
|
@ -99,7 +99,7 @@ void tls_server_task(void *pvParameters)
|
|||
if(ret < 0)
|
||||
{
|
||||
printf(" failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n", -ret);
|
||||
while(1) {} /* todo: replace with abort() */
|
||||
abort();
|
||||
}
|
||||
|
||||
printf(" ok (%d skipped)\n", ret);
|
||||
|
|
@ -109,7 +109,7 @@ void tls_server_task(void *pvParameters)
|
|||
if(ret != 0)
|
||||
{
|
||||
printf(" failed\n ! mbedtls_pk_parse_key returned - 0x%x\n\n", -ret);
|
||||
while(1) { } /*todo: replace with abort() */
|
||||
abort();
|
||||
}
|
||||
|
||||
printf(" ok\n");
|
||||
|
|
@ -134,7 +134,7 @@ void tls_server_task(void *pvParameters)
|
|||
if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &srvcert, &pkey ) ) != 0 )
|
||||
{
|
||||
printf( " failed\n ! mbedtls_ssl_conf_own_cert returned %d\n\n", ret );
|
||||
while(1) { }
|
||||
abort();
|
||||
}
|
||||
|
||||
mbedtls_ssl_conf_rng(&conf, mbedtls_ctr_drbg_random, &ctr_drbg);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue