I'm trying to save an additional custom parameter to wifimanager which is the mqtt server address but all codes available in the library and all over the internet are for Arduinojson 5, I tried upgrading to Arduinojson 6 to the best of my ability. The code runs with no issues, however, when I restart the esp, it is gone. For somereason, it is not saved.
#include <FS.h> //this needs to be first, or it all crashes and burns...
#include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager
#include <ArduinoJson.h> //https://github.com/bblanchon/ArduinoJson
#define TRIGGER_PIN 16
char mqtt_server[40];
bool shouldSaveConfig = false;
void saveConfigCallback () { Serial.println("Should save config"); shouldSaveConfig = true; }
WiFiManager wifiManager;
WiFiManagerParameter custom_mqtt_server("server", "mqtt server", mqtt_server, 40);
void setup() {
Serial.begin(115200);
Serial.println("\n Starting");
pinMode(TRIGGER_PIN, INPUT);
//clean FS, for testing
//SPIFFS.format();
if (SPIFFS.begin()) {
Serial.println("** Mounting file system **");
if (SPIFFS.exists("/config.json")) {
//file exists, reading and loading
Serial.println("** Reading config file **");
File configFile = SPIFFS.open("/config.json", "r");
if (configFile) {
size_t size = configFile.size();
// Allocate a buffer to store contents of the file.
std::unique_ptr<char[]> buf(new char[size]);
configFile.readBytes(buf.get(), size);
DynamicJsonDocument doc(1024);
DeserializationError error = deserializeJson(doc, buf.get());
// Test if parsing succeeds.
if (error) {
Serial.print(F("deserializeJson() failed: "));
Serial.println(error.c_str());
return;
}
strcpy(mqtt_server, doc["mqtt_server"]); //get the mqtt_server <== you need one of these for each param
} else {
Serial.println("** Failed to load json config **");
}
configFile.close();
Serial.println("** Closed file **");
}
}
else {
Serial.println("** Failed to mount FS **");
}
wifiManager.setSaveConfigCallback(saveConfigCallback);
wifiManager.addParameter(&custom_mqtt_server);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
resetbtn();
}
Serial.println("connected...yeey :)");
//read updated parameters
strcpy(mqtt_server, custom_mqtt_server.getValue());
//save the custom parameters to FS
if (shouldSaveConfig) {
Serial.println("saving config");
DynamicJsonDocument doc(1024);
doc["mqtt_server"] = mqtt_server;
File configFile = SPIFFS.open("/config.json", "w");
if (!configFile) {
Serial.println("failed to open config file for writing");
}
serializeJson(doc, Serial);
serializeJson(doc, configFile);
configFile.close();
//end save
}
}
void loop() {
resetbtn();
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
resetbtn();
}
Serial.println("Connected");
wifiManager.process();
saveParamsCallback();
delay(3000);
}
void resetbtn() { if ( digitalRead(TRIGGER_PIN) == HIGH ) { wifiManager.startConfigPortal("Horray"); Serial.println("connected...yeey :)"); } }
void saveParamsCallback () {
Serial.println("Get Params:");
Serial.print(custom_mqtt_server.getID());
Serial.print(" : ");
Serial.println(custom_mqtt_server.getValue());
}
for same purpose instead of using: serializeJson(doc, configFile);
i'm using this function and for me work pretty well
// for writing
bool writeFile(const char * path, const char * message, unsigned int len){
File file = SPIFFS.open(path, FILE_WRITE);
if(!file){
return false;
}
if(!file.write((const uint8_t *)message, len)){
return false;
}
return true;
}
for reading i'm using this function
// for reading
int readFile(const char * path, char ** text){
// retval int - number of characters in the file
// in case of empty file 0
// in case of directory or not found -1
File file = SPIFFS.open(path);
if(!file || file.isDirectory()){
return -1;
}
int file_lenght = file.size();
*text = (char*) malloc(file_lenght*sizeof(char));
for(int i = 0; i < file_lenght; i++){
(*text)[i] = file.read();
}
return file_lenght;
}
you can use this function in this way:
#define WIFI_credential_filename "/config.json"
char * wifi_credentials;
int file_len = readFile(WIFI_credential_filename, &wifi_credentials);
// at this point "wifi_credentials" is filled with the content of
"/config.json" file
this is my implementation, surely it can be improved but I have tested it and it works
Related
I have this c program which used to search for the mp3 files of given directory and add the files data to linked list.i use SDL2 for audio handling. so much far i would be able to get the playing of mp3 file and what i'm facing here is that i don't know how to play the next song when the first song is played in the linked list.
Here is my code for the program.
#include <dirent.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "file_handle.h"
#include <SDL2/SDL.h>
#include <SDL2/SDL_mixer.h>
struct tags
{
const char *artist;
} tags;
Mix_Music *music = NULL;
bool loadmedia(char *filepath);
void play();
void init();
void load_file_directory(char *dir_path);
int main(int argc, char *argv[])
{
char *file_path;
if (argc <= 1)
{
fprintf(stderr, "no arguments provided!\n");
}
else
{
file_path = argv[2];
}
if (file_path)
{
load_file_directory(file_path);
}
init();
int count = 0;
for (struct song *node = head; node; node = node->next)
{
printf("%s\n", node->file_path);
}
for (struct song *node = head; node; node = node->next)
{
printf("%s\n", node->file_path);
printf("count is %d\n", count);
count++;
Mix_Music *song = Mix_LoadMUS(node->file_path);
if (Mix_PlayMusic(song, 1) != 0)
{
printf("something\n");
}
while (!SDL_QuitRequested())
SDL_Delay(250);
Mix_FreeMusic(song);
}
// char *file = argv[1];
// bool status = loadmedia(file);
// if(status){
// printf("%s - %s\n",Mix_GetMusicArtistTag(music),Mix_GetMusicTitleTag(music));
// if(Mix_PlayMusic(music,1)){
// printf("something wrong");
// Mix_FreeMusic(music);
// }
// }
// while(!SDL_QuitRequested())
// SDL_Delay(250);
}
bool loadmedia(char *file_path)
{
bool success = false;
if (file_path)
{
music = Mix_LoadMUS(file_path);
success = true;
}
else
{
printf("File Loaded Failed\n");
}
return success;
}
void init()
{
bool success;
if (SDL_Init(SDL_INIT_AUDIO) < 0)
{
printf("Couldn't initialize SDL: %s\n", SDL_GetError());
success = false;
}
if (Mix_Init(MIX_INIT_MP3) != MIX_INIT_MP3)
{
fprintf(stderr, "could not initialized mixer\n", Mix_GetError());
}
if (Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048) < 0)
{
printf("SDL_mixer could not be initialized %s\n", Mix_GetError());
success = false;
}
}
From several days I searched for this problem:
I want to do a connected device (IoT) using Watson IoT Platform and ESP32 (or similar). This device have some relays on board.
On Watson dashboard I created the device type, the physical/logical interface, I connected the ESP with the platform.
I created a custom action on the dashboard that has a param to identify the relay that I want to switch (switch2) and also a simple custom action (switch) without param.
The problem is that if I generate the action without the param (switch) I see the callback print, if I generate the action with a param (switch2) nothing happens.
I tried also to use the built-in Watson action "firmware update/download", if I use firmware download (that want some params such as uri, version, etc.) nothing happens, if I use firmware update (that don't want params), I see the callback of subscription.
Here you can see the code arduino like for the ESP
// --------------- HEADERS -------------------
#include <Arduino_JSON.h>
#include <EEPROM.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <WebServer.h>
#include <ESPmDNS.h>
#include <PubSubClient.h> //https://github.com/knolleary/pubsubclient/releases/tag/v2.3
WebServer server(80);
char wifi_ssid[100] = "xxxxxx";
char wifi_psw[200] = "xxxxxxx";
// ----- Watson IBM parameters
#define ORG "xxxx"
#define DEVICE_TYPE "Relay"
#define DEVICE_ID "xxxx"
#define TOKEN "xxxxxxxxxxxxxxxx"
char ibmServer[] = ORG ".messaging.internetofthings.ibmcloud.com";
char authMethod[] = "use-token-auth";
char token[] = TOKEN;
char clientId[] = "d:" ORG ":" DEVICE_TYPE ":" DEVICE_ID;
const char switchTopic[] = "iotdm-1/mgmt/custom/switch-actions-v1/switch2";
const char testTopic[] = "iotdm-1/mgmt/custom/switch-actions-v1/switch"; //"iot-2/cmd/+/fmt/+";
const char observeTopic[] = "iotdm-1/observe";
const char publishTopic[] = "iot-2/evt/status/fmt/json";
const char responseTopic[] = "iotdm-1/response";
const char deviceResponseTopic[] = "iotdevice-1/response";
const char manageTopic[] = "iotdevice-1/mgmt/manage";
const char updateTopic[] = "iotdm-1/mgmt/initiate/firmware/update";
void callback(char* topic, byte* payload, unsigned int payloadLength);
WiFiClient wifiClient;
PubSubClient client(ibmServer, 1883, callback, wifiClient);
bool outputEnable = false;
bool oldOutputEnable = false;
void setup() {
// put your setup code here, to run once:
//Init la porta seriale ed aspetta che si avii
Serial.begin(115200);
while(!Serial) {
delay(1);
}
setupWiFi();
}
void loop() {
// put your main code here, to run repeatedly:
if (!client.loop()) {
mqttConnect();
initManagedDevice();
}
delay(100);
}
// WiFi Settings
void setupWiFi() {
bool state = false;
Serial.println("---- Setup WiFi ----");
Serial.println(wifi_ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(wifi_ssid, wifi_psw);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
mqttConnect();
initManagedDevice();
publishStatus();
Serial.println("");
Serial.print("Connected to ");
Serial.println(wifi_ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
// IBM IoT
void mqttConnect() {
if (!!!client.connected()) {
Serial.print("Reconnecting MQTT client to "); Serial.println(ibmServer);
while (!!!client.connect(clientId, authMethod, token)) {
Serial.print(".");
delay(500);
}
Serial.println();
}
}
void initManagedDevice() {
if (client.subscribe("iotdm-1/response")) {
Serial.println("subscribe to responses OK");
} else {
Serial.println("subscribe to responses FAILED");
}
if (client.subscribe("iotdm-1/device/update")) {
Serial.println("subscribe to update OK");
} else {
Serial.println("subscribe to update FAILED");
}
if (client.subscribe(observeTopic)) {
Serial.println("subscribe to observe OK");
} else {
Serial.println("subscribe to observe FAILED");
}
if (client.subscribe(switchTopic)) {
Serial.println("subscribe to switch OK");
} else {
Serial.println("subscribe to switch FAILED");
}
if (client.subscribe(testTopic)) {
Serial.println("subscribe to Test OK");
} else {
Serial.println("subscribe to Test FAILED");
}
JSONVar root;
JSONVar d;
JSONVar supports;
supports["deviceActions"] = true;
supports["firmwareActions"] = true;
supports["switch-actions-v1"] = true;
d["supports"] = supports;
root["d"] = d;
char buff[300] = "";
JSON.stringify(root).toCharArray(buff, 300);
Serial.println("publishing device metadata:"); Serial.println(buff);
if (client.publish(manageTopic, buff)) {
Serial.println("device Publish ok");
} else {
Serial.print("device Publish failed:");
}
}
void callback(char* topic, byte* payload, unsigned int payloadLength) {
Serial.print("callback invoked for topic: "); Serial.println(topic);
if (strcmp (responseTopic, topic) == 0) {
return; // just print of response for now
}
if (strcmp (updateTopic, topic) == 0) {
handleUpdate(payload);
}
if (strcmp(switchTopic, topic) == 0) {
handleRemoteSwitch(payload);
}
if(strcmp(observeTopic, topic) == 0) {
handleObserve(payload);
}
}
void sendSuccessResponse(const char* reqId) {
JSONVar payload;
payload["rc"] = 200;
payload["reqId"] = reqId;
char buff[300] = "";
JSON.stringify(payload).toCharArray(buff, 300);
if (client.publish(deviceResponseTopic, buff)) {
Serial.println("Success sended");
} else {
Serial.print("Success failed:");
}
}
void publishStatus() {
String payload = "{\"relayStatus\":";
payload += outputEnable;
payload += "}";
Serial.print("Sending payload: "); Serial.println(payload);
if (client.publish(publishTopic, (char*) payload.c_str())) {
Serial.println("Publish OK");
} else {
Serial.println("Publish FAILED");
}
}
void handleUpdate(byte* payload) {
Serial.println("handle Update");
}
void handleObserve(byte* payload) {
JSONVar request = JSON.parse((char*)payload);
JSONVar d = request["d"];
JSONVar fields = d["fields"];
const char* field = fields[0]["field"];
if(strcmp(field, "mgmt.firmware") == 0) {
Serial.println("Upadete the firmware");
sendSuccessResponse(request["reqId"]);
} else {
Serial.println("Unmanaged observe");
Serial.println(request);
}
}
void handleRemoteSwitch(byte* payload) {
Serial.println("handle remote switching");
//invertedSwitch = !invertedSwitch;
outputEnable = !outputEnable;
JSONVar request = JSON.parse((char*)payload);
const char* id = request["reqId"];
sendSuccessResponse(id);
}
Thanks to everyone that wants to try to help me.
After several days I solved the issue, I post here de response for someone with the same problem:
The problem is the PubSubClient library, that accept only 128 bytes response message (including header). The Watson IBM produce longer response message than 128 bytes.
To change the buffer size for the response message, you need to modify the PubSubClient.h file at line
#define MQTT_MAX_PACKET_SIZE 128
And change the 128 byte with bigger number (eg. 1024).
I have a barcode code but need it to recognize the barcode so it can print to serial monitor. I have searched that I need to have it convert to int from char but have yet to find a way to do that if that is the answer.
For example I scan a barcode and if it is a certain number, then the serial monitor will print it.
#include <hid.h>
#include <hiduniversal.h>
#include <usbhub.h>
#include <Usb.h>
#include <hidboot.h>
#include <avr/pgmspace.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Bridge.h>
#include <FileIO.h>
#include <TM1637Display.h>
#define DEBUG true
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
const byte BarcodeLength = 11;
const int buzzer = 2;
const int CLK = 9; //Set the CLK pin connection to the display
const int DIO = 8; //Set the DIO pin connection to the display
const int DIO2 = 7; // Set 2nd DIO pin to second connection to display
const int CLK2 = 6; // Set the 2nd CLK pin to the second connection to display.
const int procTime = 5; // this is the time it takes for the process to finish countdown
int NumStep = 0; //Variable to interate
TM1637Display countdownDisplay(CLK, DIO); //set up the 4-Digit Display.
TM1637Display DelayedDisplay(CLK2, DIO2); //set up the 4-Digit Display.
class BarcodeReader : public KeyboardReportParser
{
USB Usb;
USBHub Hub;
HIDUniversal Hid;
HIDBoot<HID_PROTOCOL_KEYBOARD> Keyboard;
uint8_t buffer[15];
byte length;
bool valid;
void addCharacter(uint8_t c);
public:
BarcodeReader();
void begin();
void task();
const uint8_t* barcode();
void reset();
protected:
virtual void OnKeyDown(uint8_t mod, uint8_t key);
};
BarcodeReader::BarcodeReader()
: Usb()
, Hub(&Usb)
, Hid(&Usb)
, Keyboard(&Usb)
, length(0)
, valid(false)
{
}
void BarcodeReader::begin()
{
if (Usb.Init() == -1) {
Serial.println("OSC did not start.");
}
else
Serial.println("Barcode Ready");
Hid.SetReportParser(1, this);
}
void BarcodeReader::task()
{
Usb.Task();
}
void BarcodeReader::addCharacter(uint8_t c)
{
if (valid) {
// already have a barcode, new one will overwrite it
reset();
}
// could check for \n or \r if necessary, or even implement a timing
// mechanism for detecting end of barcode input, but for now we just
// assume fixed width field with no output separators
buffer[length++] = c;
//buffer[length] = '\0';
if (buffer[length] == '\0')
{
valid = true;
}
};
// return value no longer valid after another call to task()
const uint8_t* BarcodeReader::barcode()
{
return (valid) ? buffer : 0;
}
void BarcodeReader::reset()
{
length = 0;
valid = false;
}
void BarcodeReader::OnKeyDown(uint8_t mod, uint8_t key) {
uint8_t c = OemToAscii(mod, key);
if (c) addCharacter(c);
}
BarcodeReader barcodeReader;
void setup() {
countdownDisplay.showNumberDec(0);
DelayedDisplay.showNumberDec(0);
Bridge.begin();
Console.begin();
FileSystem.begin();
Serial.begin(9600);
if (DEBUG) Console.println("Done setup");
lcd.begin(16, 2);
pinMode(buzzer,OUTPUT);
//Serial.begin(115200);
lcd.print("Please Scan Your ID: ");
barcodeReader.begin();
countdownDisplay.setBrightness(0x0a); //set the diplay to maximum brightness
DelayedDisplay.setBrightness(0x0a);
}
void loop() {
//char *goodCards = { "1234567" };
//String dataString = "Testing, 1, 2, 3...";
File dataFile = FileSystem.open("/mnt/sda1/datalog.txt", FILE_APPEND);
// If the file opens successfully, write the string to it, close the file,
// and print the information to the Serial monitor.
barcodeReader.task();
const char* barcode = barcodeReader.barcode();
if (barcode || dataFile) {
lcd.print(barcode);
dataFile.print(barcode);
Serial.print(barcode);
barcodeReader.reset();
dataFile.close();
//If the barcode is the word 'Setup' then print setup to the screen
//search array of char conver strcmp(). You are sending 0101 not the actual value of 1234567
// collect all the input, convert it to an int
if(barcode == "0001") {
{
Serial.println(": SETUP");
}
}
}
//timer and barcode
//
// if(barcode == "1234567") {
// Serial.print(barcode);
// Serial.println(": SETUP");
//
//// analogWrite(A1,0);
//// analogWrite(A2,0);
// }
// if(barcode == "KAYAMAD") {
// Serial.print(barcode);
// Serial.println(": RUN");
//
//// analogWrite(A0,0);
//// analogWrite(A2,0);
// countdownDisplay.showNumberDec(0);
// DelayedDisplay.showNumberDec(0);
// for(int NumStep = procTime; NumStep > 0 ; NumStep--) //Interrate NumStep
// {
// countdownDisplay.showNumberDec(NumStep); //Display the Variable value;
// //DelayedDisplay.showNumberDec(NumStep); //Display the Variable value;
// delay(1000); //A second delay between steps.
// }
//
// for (int NumStep = 0; NumStep < 5 ; NumStep++) //Interrate NumStep
// {
// //countdownDisplay.showNumberDec(NumStep); //Display the Variable value;
// DelayedDisplay.showNumberDec(NumStep); //Display the Variable value;
// countdownDisplay.showNumberDec(0);
// delay(1000); //A second delay between steps.
//
// }
//
// }
//
// else if (barcode == "2234663") {
// Serial.print(barcode);
// Serial.println(": DELAY");
//
//// analogWrite(A0,0);
//// analogWrite(A1,0);
// countdownDisplay.showNumberDec(0);
// DelayedDisplay.showNumberDec(0);
//
//
// for (int NumStep = 0; NumStep < 5 ; NumStep++) //Interrate NumStep
// {
// //countdownDisplay.showNumberDec(NumStep); //Display the Variable value;
// DelayedDisplay.showNumberDec(NumStep); //Display the Variable value;
// countdownDisplay.showNumberDec(0);
// delay(1000); //A second delay between steps.
//
// }
//
//
// }
//
// If there is a problem opening the file, send an error to the Serial monitor
else {
if (DEBUG) Console.println("Error opening datalog.txt");
}
}
"Number" you get Computer sees as text, not number. We are sure that barcode will always be number. Conver char to int with atoi function.
Here's an example:
const char *bar = "123";
int bc = atoi(bar);
char IP[32] = "";
char PORT[4] = "0000";
int read_conf()
{
int i;
char line[25] = "";
FILE *fp;
char *str_ptr;
fp = fopen("client.conf","r");
for(i=1;(fgets(line,sizeof(line),fp));i++)
{
if(1==i)
{
str_ptr = strstr(line,"IP:");
if(str_ptr)
{
strcpy(IP,(str_ptr+3));
}
else
{
printf("Error in fetching IP \n");
exit(0);
}
}
else if(2==i)
{
str_ptr = strstr(line,"Port:");
if(str_ptr)
{
strcpy(PORT,(str_ptr+5));
}
else
{
printf("Error in fetching PORT \n");
exit(0);
}
}
}
return 1;
}
char *construct_url(int n,char * const argv[])
{
char * final_url;
int i,k=2;
int j = 0;
final_url = malloc(sizeof(char *)*300);
strcpy(final_url,"http://");
strcat(final_url,IP);
strcat(final_url,":");
strcat(final_url,PORT);
strcat(final_url,"/");
//printf("%s",final_url);
for(i=1;i<n,k>0;i++,k--)
{
strcat(final_url,argv[i]);
if(i==1)
{
strcat(final_url,"/");
}
else
{
strcat(final_url,"?");
}
}
return final_url;
}
In my above code it is adding a newline after IP and PORT value which is not correct URL construction. how do I avoid new line before concatenation.
client.conf consist
IP:10.12.130.216
Port:5200
Expected Result:
http://10.12.130.216:5200/
Getting Result:
http://10.12.130.216
:5200
/
read_conf can be written in simple as follows using fscanf.
char PORT[6] = "00000";//0-65535
int read_conf(void){
FILE *fp = fopen("client.conf","r");
if(1 != fscanf(fp, "IP:%31s\n", IP)){
printf("Error in fetching IP \n");
exit(0);
}
if(1 != fscanf(fp, "Port:%5s", PORT)){
printf("Error in fetching PORT \n");
exit(0);
}
fclose(fp);
return 1;
}
If you are sure that it is a new line, use:
strcat(final_url,IP);
strtok(final_url, "\n");
strcat(final_url,":");
strcat(final_url,PORT);
strtok(final_url, "\n");
strcat(final_url,"/");
or use strtock function separately for IP and PORT strings.
Im trying to learn glib/gtk. I wrote little code which prints files in directory and assigns "f" if they are normal files or "d" if they are directory. Problem is with if. It always gets false value and appends "f" to file name.
#include <glib.h>
#include <glib/gstdio.h>
#include <glib/gprintf.h>
int main()
{
GDir* home = NULL;
GError* error = NULL;
gchar* file = "a";
home = g_dir_open("/home/stamp", 0, &error);
while (file != NULL)
{
file = g_dir_read_name(home);
if (g_file_test(file, G_FILE_TEST_IS_DIR))
{
g_printf("%s: d\n", file);
} else {
g_printf("%s: f\n", file);
}
}
}
g_dir_read_name returns just the directory/file name. You need to build full path in order to test it using g_file_test. You can use g_build_filename for that.
int main()
{
GDir* home = NULL;
GError* error = NULL;
gchar* file = "a";
home = g_dir_open("/home/stamp", 0, &error);
while (file != NULL)
{
file = g_dir_read_name(home);
gchar* fileWithFullPath;
fileWithFullPath = g_build_filename("/home/stamp", file, (gchar*)NULL);
if (g_file_test(fileWithFullPath, G_FILE_TEST_IS_DIR))
{
g_printf("%s: d\n", file);
}
else
{
g_printf("%s: f\n", file);
}
g_free(fileWithFullPath);
}
g_dir_close( home );
}