Arduino UART ESP01 , json parse 4with 433 MHZ receiver and transmitter - json

I am using 2 Arduino, 1 Esp01,433 MHz 1 receiver and 1 433 MHz transmitter. The transmtiter sending uint_8 "85648217". When i write below the code.I can receive the messages and i can get response from api.
#include <ArduinoJson.h>
#include <RH_ASK.h>
#include <SPI.h>
#define RX 0
#define TX 1
using namespace std;
unsigned char rxBuf[512];
RH_ASK rf_driver;
void setup() {
rf_driver.init();
Serial.begin(9600);
}
void loop() {
{
uint8_t buf[8];
uint8_t buflen = sizeof(buf);
if (rf_driver.recv(buf, &buflen))
{
Serial.println("https://example.com/example/example/"+(String((char*)buf))+";headers\n");
//(String((char*)buf) is number from transmitter
String message = Serial.readString();
const size_t capacity = JSON_OBJECT_SIZE(1) + 20;
DynamicJsonDocument doc(capacity);
// DeserializationError error = deserializeJson(doc, message);
//if (error) {
// Serial.print(F("deserializeJson() failed: "));
//Serial.println(error.f_str());
// return;
// }
Serial.println(doc["attribute"].as<const char*>());
Serial.println(message);
}
}
}
When i write code like this.I can't receive message.I am receving "07413943⸮" or "07413943⸮ " .I don't know why is this happening.Does anyone know how can i fix this?
#include <ArduinoJson.h>
#include <RH_ASK.h>
#include <SPI.h>
#define RX 0
#define TX 1
using namespace std;
unsigned char rxBuf[512];
RH_ASK rf_driver;
void setup() {
rf_driver.init();
Serial.begin(9600);
}
void loop() {
{
uint8_t buf[8];
uint8_t buflen = sizeof(buf);
if (rf_driver.recv(buf, &buflen))
{
Serial.println("https://example.com/example/example/"+(String((char*)buf))+";headers\n");
//(String((char*)buf) is number from transmitter
String message = Serial.readString();
const size_t capacity = JSON_OBJECT_SIZE(1) + 20;
DynamicJsonDocument doc(capacity);
DeserializationError error = deserializeJson(doc, message);
if (error) {
Serial.print(F("deserializeJson() failed: "));
Serial.println(error.f_str());
return;
}
Serial.println(doc["attribute"].as<const char*>());
Serial.println(message);
}
}
}

I found the solution.It seems like i need to convert string this (String((char*)buf)) but not like this.Here is how i solved.
uint8_t buflen = sizeof(buf)
if (rf_driver.recv(buf, &buflen))
{
rf_driver.printBuffer("Got:",buf,buflen);
String rcv;
for (int i=0; i<buflen;i++){
rcv+=(char)buf[i];
}
Serial.print("example.com/example"+rcv+";header");
String input = Serial.readString();
StaticJsonDocument<32> doc;
DeserializationError error = deserializeJson(doc,input);
if (error) {
Serial.print(F("deserializeJson() failed: "));
Serial.println(error.f_str());
return;
}
float attribute= doc["attribute"]; // 428.5
Serial.println(float(attribute));
Serial.println(input);

Related

How can I fix my serial communication from Arduio Uno to ESP8266?

I'm testing the Serial Communication with my Arduino and NodeMCU, and how I can send one data to another. Here is the Code.
For the Arduino Uno
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); //RX, TX
float i = 10;
void setup() {
Serial.begin(9600);
mySerial.begin(9600);
}
void loop() {
Serial.print("Number ");
Serial.print(i);
Serial.println("");
mySerial.write(i);
delay(1000);
}
And here is for my ESP8266 NodeMCU
#include <SoftwareSerial.h>
SoftwareSerial myNode(D1, D2); //RX, TX
float git;
void setup() {
Serial.begin(115200);
myNode.begin(9600);
}
void loop() {
git = Serial.read();
Serial.print("The Number is ");
Serial.print(git);
Serial.println("");
delay(1000);
}
The result should be that in the Serial Monitor it should say "The Number is 10". But instead it says -1.00 enter image description here
Am I doing something wrong?
Ah, I finally figured it out. Apparently I need to initialize the NodeMCU Pin's as so:
#include <SoftwareSerial.h>
static const uint8_t D1 = 5;
static const uint8_t D2 = 4;
SoftwareSerial myNode(D1, D2); //RX, TX
float git;
void setup() {
Serial.begin(115200);
myNode.begin(9600);
}
void loop() {
git = Serial.read();
Serial.print("The Number is ");
Serial.print(git);
Serial.println("");
delay(1000);
}
Try
git = myNode.read();
instead of
git = Serial.read();
And check myNode.available() before reading

Blynk Button is not getting actuated

I, a complete beginner, was doing a project with ESP8266 where I interfaced with a relay and two sensors. The sensors are working fine but the relay is not getting actuated by the Blynk button when I press it. Below is the code where I got proper output for the sensors but not the relay.
I get all the values of sensors in the Blynk app but not the relay actuation where I connected it to a motor and in the D2 pin. Thanks in advance.:)
#define BLYNK_PRINT Serial
#define BLYNK_TEMPLATE_ID "my_template"
#define BLYNK_DEVICE_NAME "my_device name"
#define BLYNK_AUTH_TOKEN "Auth token"
#include <SPI.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <DHT.h>
#define BLYNK_PRINT Serial
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS D2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
char auth[] = "Authtoken";
char ssid[] = "my_ssid";
char pass[] = "my_pass";
#define sensorPin D3
int sensorState = 0;
int lastState = 0;
#define DHTPIN 2
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer;
void sendSensor()
{
float h = dht.readHumidity();
float t = dht.readTemperature();
Blynk.virtualWrite(V5, h); //hum
Blynk.virtualWrite(V6, t); //temp
}
void setup()
{
pinMode(D2,OUTPUT); //these two lines are the one which use for actuating the relay
digitalWrite(D2, HIGH);//
Blynk.begin(auth, ssid, pass);
pinMode(sensorPin, INPUT);
dht.begin();
timer.setInterval(1000L, sendSensor);
Blynk.begin(auth, ssid, pass);
sensors.begin();
}
int sensor = 0;
void sendTemps()
{
sensor = analogRead(A0);
sensors.requestTemperatures();
float temp = sensors.getTempCByIndex(0);
Blynk.virtualWrite(V1, temp);
Blynk.virtualWrite(V2, sensor);
delay(1000);
}
void loop()
{
Blynk.run();
timer.run();
sendTemps();
sensorState = digitalRead(sensorPin);
if (sensorState == 1 && lastState == 0) {
lastState = 1;
delay(1000);
}
else if (sensorState == 1 && lastState == 1) {
delay(1000);
}
else {
lastState = 0;
delay(1000);
}
delay(100);
}

ESP8266 restart after the second request to the server (Exception 9)

I am new to this community, and I look forward to your help with this issue.
I have communication between two ESP8266 modules. One of these as a Server/Access-Point and the other as a Client. The configuration and connection between these two modules are satisfactory, and part of the communication as well. However, after the Client makes the first request (Begin and GET), and the server responds, for the second Client/Server request, the Client restarts with the next message or exception.
Exception (9):
epc1=0x40207204 epc2=0x00000000 epc3=0x00000000 excvaddr=0x0036006a depc=0x00000000
That is, the first request to the server's root is answered, but the second to /data causes the mentioned restart on the Client.
After reviewing the stack exception decoder, I find that the code (9) means 9: LoadStoreAlignmentCause Load or store to an unaligned address.
Then, I've tried different solutions for this issue
erase all flash content
http.end()
change the macro delay() for delayMicroseconds()
and a few more
But nothing works yet.
Please, help me with this issue. The codes of the server and Client are:
The client:
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#define LED_PIN 2
const char* ssid = "Mi_Servidor";
const char* password = "";
const char* host = "http://192.168.4.1";
int counter=1, httpCode=0;
HTTPClient http;
void peticion (int httpCode){
String mensaje;
if(httpCode == 200) {
mensaje = http.getString();
Serial.println(mensaje);
}
else {
Serial.print("[HTTP] GET... failed, no connection or no HTTP server\n");
}
}
void setup() {
pinMode(LED_PIN, OUTPUT);
Serial.begin(115200);
delay(10);
Serial.print("Conectando a ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("WiFi conectada");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void loop(){
counter++;
int modulo=counter%2;
if(WiFi.status() != WL_CONNECTED){
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
}
else{
Serial.print("[HTTP] begin...\n");
if(modulo==0) http.begin("192.168.4.1", 80); //HTTp
else http.begin("192.168.4.1", 80, "/data"); //HTTp
httpCode = http.GET();
Serial.print("httpCode: "); //JJ
Serial.println(httpCode); //JJ
peticion(httpCode);
delay(20);
}
Serial.println("End Loop!");
}
And the server:
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#define LED_PIN 2
void handle_datos();
const char WiFiClave[] = ""; //Sin clave
const char AP_Nombre[] = "Mi_Servidor";
int val;
String MiMessage = "Message to transmit:\n\r";
ESP8266WebServer server(80);
void handle_data() {
server.send(200, "text/plain", MiMessage);
delay(1000);
}
void handle_root() {
server.send(200, "text/plain", "Mi_root");
delay(1000);
}
void setup() {
pinMode(LED_PIN, OUTPUT);
Serial.begin(115200);
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW);
WiFi.mode(WIFI_AP);
WiFi.softAP(AP_Nombre, WiFiClave);
server.on("/data", handle_data);
server.on("/", handle_root);
server.begin();
// for (int i = 0; i <= 3999; i++) {
// MiMessage.concat(String(i));
// MiMessage.concat(";");
// }
Serial.println();
Serial.println(MiMessage);
Serial.println("\nServidor ready... ");
}
void loop() {
server.handleClient();
delay(100);
}
Thanks in advance

Setting variables from json into a struct have weird results

I have something in my code output I don't understand.
My guess is the issue is due to [me of course] ... the usage of both "struct"+"arduinoJson" to store configuration values.
I have made this simple code that only connect to Wifi;
either using hardcoded settings
loadConfigHarcoded(); // Serial print of values: OK, and "WiFi Success!"
either using deserializeJson
loadConfigFromJson(); // Serial print of values: OK, but "WiFi Connect Failed!"
Is there anything I missed ?
The full code :
#include <ESP8266WiFi.h>
#include <ArduinoJson.h>
const char* ssid = "my_ssid";
const char* password = "my_password";
String confJson = "{\"ssid\": \""+String(ssid)+"\", \"password\": \""+String(password)+"\"}";
struct sConfig {
const char* ssid;
const char* password;
const char* host;
};
sConfig config;
void loadConfigHarcoded() {
config.ssid = ssid;
config.password = password;
}
void loadConfigFromJson() {
DynamicJsonDocument docConfig(1024);
DeserializationError err = deserializeJson(docConfig, confJson);
if (!err) {
config.ssid = docConfig["ssid"];
config.password = docConfig["password"];
}
}
void setup() {
Serial.begin(115200);
Serial.println();
loadConfigHarcoded(); // Serial print of values are OK ; and "WiFi Success!"
//loadConfigFromJson(); // Serial print of values are OK ; but "WiFi Connect Failed!"
Serial.print("SSID=");
Serial.println(config.ssid);
Serial.print("PSWD=");
Serial.println(config.password);
WiFi.mode(WIFI_STA);
WiFi.begin(config.ssid, config.password);
if (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.println("WiFi Connect Failed!");
} else {
Serial.println("WiFi Success!");
}
}
void loop() {
}

How to send and parse JSON data through NRF24L01 sensor?

TRANSMITTER.ino (COM8 port)
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include<ArduinoJson.h>
RF24 radio(7, 8); // CE, CSN
const byte addresses[6] = {"00001"};
void setup() {
radio.begin();
radio.openWritingPipe(addresses);
radio.setPALevel(RF24_PA_MIN);
radio.stopListening();
}
void loop() {
DynamicJsonBuffer jBuffer;
JsonObject& root = jBuffer.createObject();
root["North"] = "true";
root["South"] = "false";
root["East"] = "true";
root["West"] = "true";
radio.write(&root, sizeof(root));
delay(1000);
}
RECEIVER.ino (COM9 port)
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include<ArduinoJson.h>
RF24 radio(7, 8); // CE, CSN
const byte addresses[6] = {"00001"};
void setup() {
Serial.begin(9600);
radio.begin();
radio.openReadingPipe(0, addresses);
radio.setPALevel(RF24_PA_MIN);
radio.startListening();
}
void loop() {
if ( radio.available()) {
unsigned char data[1024];
radio.read(&data, sizeof(data));
StaticJsonBuffer<1024> jsonBuffer;
JsonObject& toor = jsonBuffer.parseObject(data);
String n = toor["North"];
String s = toor["South"];
String e = toor["East"];
String w = toor["West"];
Serial.println(n);
Serial.println(s);
Serial.println(e);
Serial.println(w);
delay(1000);
}
}
I am trying to transmit a JSON data over NRF24L01, and printing it to the serial monitor, but i dont see any output in my serial monitor(COM9 serial monitor). what is the mistake am i doing here?
I am using arduino JSON 5.13.5 version