Merge branch 'master' into open-libmain

This commit is contained in:
Angus Gratton 2016-06-30 09:09:27 +10:00
commit d8bcb5d702
65 changed files with 3272 additions and 739 deletions

View file

@ -0,0 +1,4 @@
PROGRAM=dht_sensor
EXTRA_COMPONENTS = extras/dht
include ../../common.mk

View file

@ -0,0 +1,46 @@
/*
*
* This sample code is in the public domain.
*/
#include "espressif/esp_common.h"
#include "esp/uart.h"
#include "FreeRTOS.h"
#include "task.h"
#include "dht.h"
#include "esp8266.h"
/* An example using the ubiquitous DHT** humidity sensors
* to read and print a new temperature and humidity measurement
* from a sensor attached to GPIO pin 4.
*/
int const dht_gpio = 4;
void dhtMeasurementTask(void *pvParameters)
{
int8_t temperature = 0;
int8_t humidity = 0;
// DHT sensors that come mounted on a PCB generally have
// pull-up resistors on the data pin. It is recommended
// to provide an external pull-up resistor otherwise...
gpio_set_pullup(dht_gpio, false, false);
while(1) {
if (dht_fetch_data(dht_gpio, &humidity, &temperature)) {
printf("Humidity: %i%% Temp: %iC\n", humidity, temperature);
} else {
printf("Could not read data from sensor...");
}
// Three second delay...
vTaskDelay(3000 / portTICK_RATE_MS);
}
}
void user_init(void)
{
uart_set_baud(0, 115200);
xTaskCreate(dhtMeasurementTask, (signed char *)"dhtMeasurementTask", 256, NULL, 2, NULL);
}

View file

@ -13,9 +13,9 @@
#define TESTSTRING "O hai there! %d %d %d"
const char *dramtest = TESTSTRING;
const __attribute__((section(".iram1.notrodata"))) char iramtest[] = TESTSTRING;
const __attribute__((section(".text.notrodata"))) char iromtest[] = TESTSTRING;
const RAM char dramtest[] = TESTSTRING;
const char *iromtest = TESTSTRING;
const IRAM_DATA char iramtest[] = TESTSTRING;
static inline uint32_t get_ccount (void)
{

View file

@ -0,0 +1,4 @@
# Simple makefile for simple example
PROGRAM=json_jsmn_simple
EXTRA_COMPONENTS = extras/jsmn
include ../../common.mk

View file

@ -0,0 +1,112 @@
/**
* Copyright (c) 2010 Serge A. Zaitsev and
* 2016 Drasko DRASKOVIC <drasko.draskovic@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "espressif/esp_common.h"
#include "esp/uart.h"
#include "FreeRTOS.h"
#include "task.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "jsmn.h"
/**
* A small example of jsmn parsing when JSON structure is known and number of
* tokens is predictable.
*/
const char *JSON_STRING =
"{\"user\": \"johndoe\", \"admin\": false, \"uid\": 1000,\n "
"\"groups\": [\"users\", \"wheel\", \"audio\", \"video\"]}";
static int jsoneq(const char *json, jsmntok_t *tok, const char *s) {
if (tok->type == JSMN_STRING && (int) strlen(s) == tok->end - tok->start &&
strncmp(json + tok->start, s, tok->end - tok->start) == 0) {
return 0;
}
return -1;
}
void json_test(void *pvParameters)
{
int i;
int r;
jsmn_parser p;
jsmntok_t t[128]; /* We expect no more than 128 tokens */
jsmn_init(&p);
r = jsmn_parse(&p, JSON_STRING, strlen(JSON_STRING), t, sizeof(t)/sizeof(t[0]));
if (r < 0) {
printf("Failed to parse JSON: %d\n", r);
}
/* Assume the top-level element is an object */
if (r < 1 || t[0].type != JSMN_OBJECT) {
printf("Object expected\n");
}
/* Loop over all keys of the root object */
for (i = 1; i < r; i++) {
if (jsoneq(JSON_STRING, &t[i], "user") == 0) {
/* We may use strndup() to fetch string value */
printf("- User: %.*s\n", t[i+1].end-t[i+1].start,
JSON_STRING + t[i+1].start);
i++;
} else if (jsoneq(JSON_STRING, &t[i], "admin") == 0) {
/* We may additionally check if the value is either "true" or "false" */
printf("- Admin: %.*s\n", t[i+1].end-t[i+1].start,
JSON_STRING + t[i+1].start);
i++;
} else if (jsoneq(JSON_STRING, &t[i], "uid") == 0) {
/* We may want to do strtol() here to get numeric value */
printf("- UID: %.*s\n", t[i+1].end-t[i+1].start,
JSON_STRING + t[i+1].start);
i++;
} else if (jsoneq(JSON_STRING, &t[i], "groups") == 0) {
int j;
printf("- Groups:\n");
if (t[i+1].type != JSMN_ARRAY) {
continue; /* We expect groups to be an array of strings */
}
for (j = 0; j < t[i+1].size; j++) {
jsmntok_t *g = &t[i+j+2];
printf(" * %.*s\n", g->end - g->start, JSON_STRING + g->start);
}
i += t[i+1].size + 1;
} else {
printf("Unexpected key: %.*s\n", t[i].end-t[i].start,
JSON_STRING + t[i].start);
}
}
while (1)
;
}
void user_init(void)
{
uart_set_baud(0, 115200);
printf("SDK version:%s\n", sdk_system_get_sdk_version());
xTaskCreate(json_test, (signed char *)"jsont", 1024, NULL, 2, NULL);
}

View file

@ -157,6 +157,7 @@ static void mqtt_task(void *pvParameters)
break;
}
printf("Connection dropped, request restart\n\r");
DisconnectNetwork(&network);
taskYIELD();
}
}

View file

@ -1,5 +1,4 @@
PROGRAM=ota_basic
OTA=1
EXTRA_COMPONENTS=extras/rboot-ota
EXTRA_COMPONENTS=extras/rboot-ota extras/mbedtls
include ../../common.mk

View file

@ -1,23 +1,121 @@
/* A very simple OTA example
*
* Binds a TCP socket, reads an image from it over TFTP and then flashes live.
* Tries to run both a TFTP client and a TFTP server simultaneously, either will accept a TTP firmware and update it.
*
* Not a realistic OTA setup, this needs adapting (choose either client or server) before you'd want to use it.
*
* For more information about esp-open-rtos OTA see https://github.com/SuperHouse/esp-open-rtos/wiki/OTA-Update-Configuration
*
* NOT SUITABLE TO PUT ON THE INTERNET OR INTO A PRODUCTION ENVIRONMENT!!!!
*/
#include <string.h>
#include "espressif/esp_common.h"
#include "esp/uart.h"
#include "FreeRTOS.h"
#include "task.h"
#include "esp8266.h"
#include "ssid_config.h"
#include "mbedtls/sha256.h"
#include "ota-tftp.h"
#include "rboot-integration.h"
#include "rboot.h"
#include "rboot-api.h"
/* TFTP client will request this image filenames from this server */
#define TFTP_IMAGE_SERVER "192.168.1.23"
#define TFTP_IMAGE_FILENAME1 "firmware1.bin"
#define TFTP_IMAGE_FILENAME2 "firmware2.bin"
/* Output of the command 'sha256sum firmware1.bin' */
static const char *FIRMWARE1_SHA256 = "88199daff8b9e76975f685ec7f95bc1df3c61bd942a33a54a40707d2a41e5488";
/* Example function to TFTP download a firmware file and verify its SHA256 before
booting into it.
*/
static void tftpclient_download_and_verify_file1(int slot, rboot_config *conf)
{
printf("Downloading %s to slot %d...\n", TFTP_IMAGE_FILENAME1, slot);
int res = ota_tftp_download(TFTP_IMAGE_SERVER, TFTP_PORT+1, TFTP_IMAGE_FILENAME1, 1000, slot, NULL);
printf("ota_tftp_download %s result %d\n", TFTP_IMAGE_FILENAME1, res);
if (res != 0) {
return;
}
printf("Looks valid, calculating SHA256...\n");
uint32_t length;
bool valid = rboot_verify_image(conf->roms[slot], &length, NULL);
static mbedtls_sha256_context ctx;
mbedtls_sha256_init(&ctx);
mbedtls_sha256_starts(&ctx, 0);
valid = valid && rboot_digest_image(conf->roms[slot], length, (rboot_digest_update_fn)mbedtls_sha256_update, &ctx);
static uint8_t hash_result[32];
mbedtls_sha256_finish(&ctx, hash_result);
mbedtls_sha256_free(&ctx);
if(!valid)
{
printf("Not valid after all :(\n");
return;
}
printf("Image SHA256 = ");
valid = true;
for(int i = 0; i < sizeof(hash_result); i++) {
char hexbuf[3];
snprintf(hexbuf, 3, "%02x", hash_result[i]);
printf(hexbuf);
if(strncmp(hexbuf, FIRMWARE1_SHA256+i*2, 2))
valid = false;
}
printf("\n");
if(!valid) {
printf("Downloaded image SHA256 didn't match expected '%s'\n", FIRMWARE1_SHA256);
return;
}
printf("SHA256 Matches. Rebooting into slot %d...\n", slot);
rboot_set_current_rom(slot);
sdk_system_restart();
}
/* Much simpler function that just downloads a file via TFTP into an rboot slot.
(
*/
static void tftpclient_download_file2(int slot)
{
printf("Downloading %s to slot %d...\n", TFTP_IMAGE_FILENAME2, slot);
int res = ota_tftp_download(TFTP_IMAGE_SERVER, TFTP_PORT+1, TFTP_IMAGE_FILENAME2, 1000, slot, NULL);
printf("ota_tftp_download %s result %d\n", TFTP_IMAGE_FILENAME2, res);
}
void tftp_client_task(void *pvParameters)
{
printf("TFTP client task starting...\n");
rboot_config conf;
conf = rboot_get_config();
int slot = (conf.current_rom + 1) % conf.count;
printf("Image will be saved in OTA slot %d.\n", slot);
if(slot == conf.current_rom) {
printf("FATAL ERROR: Only one OTA slot is configured!\n");
while(1) {}
}
/* Alternate between trying two different filenames. Probalby want to change this if making a practical
example!
Note: example will reboot into FILENAME1 if it is successfully downloaded, but FILENAME2 is ignored.
*/
while(1) {
tftpclient_download_and_verify_file1(slot, &conf);
vTaskDelay(5000 / portTICK_RATE_MS);
tftpclient_download_file2(slot);
vTaskDelay(5000 / portTICK_RATE_MS);
}
}
void user_init(void)
{
uart_set_baud(0, 115200);
@ -38,5 +136,7 @@ void user_init(void)
sdk_wifi_set_opmode(STATION_MODE);
sdk_wifi_station_set_config(&config);
printf("Starting TFTP server...");
ota_tftp_init_server(TFTP_PORT);
xTaskCreate(&tftp_client_task, (signed char *)"tftp_client", 2048, NULL, 2, NULL);
}

6
examples/sntp/Makefile Normal file
View file

@ -0,0 +1,6 @@
# Makefile for the sntp_example program
PROGRAM=sntp_example
EXTRA_COMPONENTS = extras/sntp
include ../../common.mk

View file

@ -0,0 +1,79 @@
/*
* Test code for SNTP on esp-open-rtos.
*
* Jesus Alonso (doragasu)
*/
#include <espressif/esp_common.h>
#include <esp/uart.h>
#include <string.h>
#include <stdio.h>
#include <FreeRTOS.h>
#include <task.h>
#include <lwip/err.h>
#include <lwip/sockets.h>
#include <lwip/sys.h>
#include <lwip/netdb.h>
#include <lwip/dns.h>
#include <ssid_config.h>
/* Add extras/sntp component to makefile for this include to work */
#include <sntp.h>
#include <time.h>
#define SNTP_SERVERS "0.pool.ntp.org", "1.pool.ntp.org", \
"2.pool.ntp.org", "3.pool.ntp.org"
#define vTaskDelayMs(ms) vTaskDelay((ms)/portTICK_RATE_MS)
#define UNUSED_ARG(x) (void)x
void sntp_tsk(void *pvParameters)
{
char *servers[] = {SNTP_SERVERS};
UNUSED_ARG(pvParameters);
/* Wait until we have joined AP and are assigned an IP */
while (sdk_wifi_station_get_connect_status() != STATION_GOT_IP) {
vTaskDelayMs(100);
}
/* Start SNTP */
printf("Starting SNTP... ");
/* SNTP will request an update each 5 minutes */
sntp_set_update_delay(5*60000);
/* Set GMT+1 zone, daylight savings off */
const struct timezone tz = {1*60, 0};
/* SNTP initialization */
sntp_initialize(&tz);
/* Servers must be configured right after initialization */
sntp_set_servers(servers, sizeof(servers) / sizeof(char*));
printf("DONE!\n");
/* Print date and time each 5 seconds */
while(1) {
vTaskDelayMs(5000);
time_t ts = time(NULL);
printf("TIME: %s", ctime(&ts));
}
}
void user_init(void)
{
uart_set_baud(0, 115200);
printf("SDK version:%s\n", sdk_system_get_sdk_version());
struct sdk_station_config config = {
.ssid = WIFI_SSID,
.password = WIFI_PASS,
};
/* required to call wifi_set_opmode before station_set_config */
sdk_wifi_set_opmode(STATION_MODE);
sdk_wifi_station_set_config(&config);
xTaskCreate(sntp_tsk, (signed char *)"SNTP", 1024, NULL, 1, NULL);
}