Hi everyone: I have written this code taken by an existing one. I gives the "make BOARD=nucleo-f401re flash term " command in order to compile and flash on my Board but, at the end , it does not enter the shell. I mean that I expect (at the end of compiling and execution) the pyterm prompt in order to issue the commands . But nothing . It seems to stop to the execution of the last line of code .: shell_run(shell_commands, line_buf, SHELL_DEFAULT_BUFSIZE); What is the problem ? Thank you for your help.
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <time.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "thread.h"
#include "shell.h"
#include "shell_commands.h"
#include "periph/adc.h"
#include "periph/gpio.h"
#include "analog_util.h"
#include "xtimer.h"
#include "periph/timer.h"
#include "msg.h"
#include "net/emcute.h"
#include "net/ipv6/addr.h"
#define ADC_IN_USE ADC_LINE(0)
#define ADC_IN_USE1 ADC_LINE(1)
#define GPIO_IN_USE GPIO_PIN(0,10)
#define ADC_RES ADC_RES_12BIT
#define ADC_RES1 ADC_RES_12BIT
#define RES ADC_RES_10BIT
#define DELAY (100LU * US_PER_MS) /* 1000 ms */
#define NUMOFSUBS (16U)
#define TOPIC_MAXLEN (64U)
#define _IPV6_DEFAULT_PREFIX_LEN (64U)
#ifndef EMCUTE_ID
#define EMCUTE_ID ("gertrud")
#endif
#define EMCUTE_PORT (1883U)
#define EMCUTE_PRIO (THREAD_PRIORITY_MAIN - 1)
static char stack[THREAD_STACKSIZE_DEFAULT];
static msg_t queue[8];
/*netif_add("4","fec0:affe::99");*/
// struct that contains sensors
typedef struct sensors{
int moisture;
int level;
}t_sensors;
static void *emcute_thread(void *arg)
{
(void)arg;
emcute_run(EMCUTE_PORT, EMCUTE_ID);
return NULL; /* should never be reached */
}
// function that disconnects from the mqttsn gateway
static int discon(void){
int res = emcute_discon();
if (res == EMCUTE_NOGW) {
puts("error: not connected to any broker");
return 1;
}
else if (res != EMCUTE_OK) {
puts("error: unable to disconnect");
return 1;
}
puts("Disconnect successful");
return 0;
}
// function that publish messages to the topic
// it takes as input the topic, the message to send and
// the value of qos
static int pub(char* topic, char* data, int qos){
emcute_topic_t t;
unsigned flags = EMCUTE_QOS_0;
switch (qos) {
case 1:
flags |= EMCUTE_QOS_1;
break;
case 2:
flags |= EMCUTE_QOS_2;
break;
default:
flags |= EMCUTE_QOS_0;
break;
}
/* step 1: get topic id */
t.name = topic;
if (emcute_reg(&t) != EMCUTE_OK) {
puts("error: unable to obtain topic ID");
return 1;
}
/* step 2: publish data */
if (emcute_pub(&t, data, strlen(data), flags) != EMCUTE_OK) {
printf("error: unable to publish data to topic '%s [%i]'\n",
t.name, (int)t.id);
return 1;
}
printf("published %s on topic %s\n", data, topic);
return 0;
}
// function that connects to the mqtt gateway
// it takes as input the ip address and the port
static int con(char* addr, int port){
sock_udp_ep_t gw = { .family = AF_INET6, .port = EMCUTE_PORT };
gw.port = port;
/* parse address */
if (ipv6_addr_from_str((ipv6_addr_t *)&gw.addr.ipv6, addr) == NULL) {
printf("error parsing IPv6 address\n");
return 1;
}
if (emcute_con(&gw, true, NULL, NULL, 0, 0) != EMCUTE_OK) {
printf("error: unable to connect to [%s]:%i\n", addr, port);
return 1;
}
printf("Successfully connected to gateway at [%s]:%i\n", addr, port);
return 0;
}
// function that read sensor values
static void read_sensors_values(t_sensors* sensors){
int sample = 0;
int sample1 = 0;
sample = adc_sample(ADC_IN_USE, ADC_RES);
sample1 = adc_sample(ADC_IN_USE1, ADC_RES1);
sensors->moisture = adc_util_map(sample, ADC_RES, 10, 100);
sensors->level = adc_util_map(sample1, ADC_RES1, 10, 100);
/* xtimer_periodic_wakeup(&last, DELAY); */
}
// new shell command: start the station
// the function takes in input ip address and port of the gateway,
// and the id of the specified station
// every five seconds it generates new sensor values and publish them to
// "localgateway_to_awsiot" + station id
static int cmd_start(int argc, char **argv){
if (argc < 4) {
printf("usage: %s <address> <port> <id>\n", argv[0]);
return 1;
}
// sensors struct
t_sensors sensors;
// name of the topic
char topic[32];
sprintf(topic,"localgateway_to_awsiot%d", atoi(argv[3]));
// json that it will published
char json[128];
/* Sample continously the ADC line */
while (1) {
xtimer_ticks32_t last = xtimer_now();
// it tries to connect to the gateway
if (con(argv[1], atoi(argv[2]))) {
continue;
}
// takes the current date and time
char datetime[20];
time_t current;
time(¤t);
struct tm* t = localtime(¤t);
int c = strftime(datetime, sizeof(datetime), "%Y-%m-%d %T", t);
if(c == 0) {
printf("Error! Invalid format\n");
return 0;
}
// updates sensor values
read_sensors_values(&sensors);
// fills the json document
sprintf(json, "{\"id\": \"%d\", \"datetime\": \"%s\", \"moisture\": "
"\"%d\", \"level\": \"%d\"}",
atoi(argv[3]), datetime, sensors.moisture, sensors.level);
// publish to the topic
pub(topic, json, 0);
xtimer_periodic_wakeup(&last, DELAY);
// it disconnects from the gateway
discon();
// it sleeps for five seconds
xtimer_sleep(5);
}
return 0;
}
static const shell_command_t shell_commands[] = {
{"start", "Start the station", cmd_start},
{NULL, NULL, NULL}};
int main(void)
{
printf("\r\nMQTT-SN application\r\n");
printf("\r\nType 'help' to get started. Have a look at the README.md for more information.\r\n");
/* initialize the ADC line */
if (adc_init(ADC_IN_USE) < 0) {
printf ("\r\nInitialization of ADC_LINE(%u) failed\r\n", ADC_IN_USE);
return 1;
}
else {
printf ("\r\nSuccessfully initialized ADC_LINE(%u)\r\n", ADC_IN_USE);
}
if (adc_init(ADC_IN_USE1) < 0) {
printf("\r\nInitialization of ADC_LINE(%u) failed\r\n", ADC_IN_USE1);
return 1;
}
else {
printf("\r\nSuccessfully initialized ADC_LINE(%u)\r\n", ADC_IN_USE1);
}
fflush(stdout);
puts("prova\n");
/* the main thread needs a msg queue to be able to run `ping6`*/
msg_init_queue(queue, ARRAY_SIZE(queue));
puts("prova\n");
/* start the emcute thread */
thread_create(stack, sizeof(stack), EMCUTE_PRIO, 0,
emcute_thread, NULL, "emcute");
puts("prova\n");
/* start shell */
char line_buf[SHELL_DEFAULT_BUFSIZE];
puts("prova\n");
shell_run(shell_commands, line_buf, SHELL_DEFAULT_BUFSIZE);
puts("provax\n");
/* should be never reached */
return 0;