Thanks ScruffR
I have tested with that code and receive exactly what the esp32 is sending.
The result is the contents of the doc.
{“version”:“6.7”,“filename”:“1.ino.ttgo-lora32-v1.bin”,“v1”:3.299194,“status”:“connected”}
Now I want to be able to grab and store as a float v1: which is analogRead 3.2999194 and the status: connected
This is the part which I am having some trouble.
Here is my code.
#include <Arduino.h>
#include <ArduinoJson.h>
#include "Particle.h"
SYSTEM_MODE(MANUAL)
SYSTEM_THREAD(ENABLED);
DynamicJsonDocument doc(512);
#define ESP32_PORT Serial1 // Photon tx to esp32 rx2 GPIO16 and Photon rx to esp32 tx2 GPIO17
float val1 = 0.0;
float val2 = 0.0;
float val3 = 0.0;
String conn = "";
String incoming = ESP32_PORT.readString();
void setup() {
Serial.begin(115200);
ESP32_PORT.begin(115200);
WiFi.on();
WiFi.connect();
waitFor(WiFi.ready, 90000);
Particle.process();
Particle.connect();
System.enableUpdates();
}
void parseString(String str){
String temp = "";
int count = 0;
for(int i=0; i < str.length() ; i++){
if(str[i] == '\n'){
temp = "";
}else if(str[i] == ','){
float v = temp.toFloat();
if(count == 0){
val1 = v;
}
if(count == 1){
val2 = v;
}
if(count == 2){
val3 = v;
}
count++;
temp ="";
}else{
temp += str[i];
}
}
}
void print_a_value(String name , String val){
Serial.print(name);
Serial.print(" : ");
Serial.println(val);
}
void loop(){
static uint32_t ms500 = 0;
if (millis() - ms500 < 500) return;
ms500 = millis();
Particle.process();
if(ESP32_PORT.available() > 0){
String incoming = ESP32_PORT.readString();
deserializeJson(doc, incoming);
val1 = doc["v1"];
val2 = doc["v2"];
val3 = doc["v3"];
conn = (const char*)doc["status"];
// print_a_value("value 1" , String(val1));
// print_a_value("value 2" , String(val2));
// print_a_value("value 3" , String(val3));
// print_a_value("connection status" , conn);
// Serial.println("\r\n");
Serial.println(val1,2);
}
}
The outcome in particle serial monitor is 3.20
I can only get the number 3 and first decimal 2
I also want to find out how to store the status: which is either connected or not connected
Code on esp32(acting as a LoRa receiver which receives analogRead from esp32 transmitter via LoRa). So esp32 receiver is connected to Photon
#include <WiFi.h>
#include <WiFiClient.h>
#include <WebServer.h>
#include <Update.h>
#include <ArduinoJson.h>
#include <ESPmDNS.h>
#include <HTTPClient.h>
#include <WiFiManager.h> // https://github.com/tzapu/WiFiManager
//Libraries for LoRa
#include <SPI.h>
#include <LoRa.h>
#include "gpio_map.h"
DynamicJsonDocument doc(512);
#define HOST_NAME "esp2"
#include "ota.h"
#include "lora.h"
#include "http_firmware.h"
#define UPDATE_FREQUNCY 30000 // every 5 sec
#define SERIAL2 Serial2 // assume you have connected the particle board to esp32 serial port 2
unsigned long lora_timer = 0; // to store time
unsigned long update_timer = 0; // to store time
String analog_reads = ""; //we reserve string in memory to store analog read value
unsigned long last_update = 0; //to store last time transmitter had activity
String connection = "not connected";//string to store status
float value1 = 0.0;
float value2 = 0.0;
float value3 = 0.0;
void setup() {
//initialize Serial Monitor
Serial.begin(115200);
SERIAL2.begin(115200);
Serial.println("LoRa Receiver Test");
//SPI LoRa pins
SPI.begin(SCK, MISO, MOSI, SS);
//setup LoRa transceiver module
LoRa.setPins(SS, RST, DIO0);
if (!LoRa.begin(BAND)) {
Serial.println("Starting LoRa failed!");
//while (1);
}
Serial.println("LoRa Initializing OK!");
OTA_Init(); // instalize OTA setup
}
void loop() {
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// if((millis() - last_update) > 3000){ // if 3 seconds passed since last update
// connection = "not connected";
// SERIAL2.print(connection);
// }
if ((millis() - lora_timer) > 2000) { // every 2 seconds
lora_timer = millis(); // reset time
// connection = "not connected";
// SERIAL2.print(connection);
// Serial.println("THIS IS VERSION 7.9");
}
if ((millis() - update_timer) > UPDATE_FREQUNCY) { // every x seconds
check_for_new_update();
update_timer = millis(); // reset time
}
//try to parse packet
int packetSize = LoRa.parsePacket(); // packetSize will equal to more than 0 if we have packets stored in memory
if (packetSize > 0) { // if packet size more than 0
//received a packet
last_update = millis(); // update last activity time
connection = "connected"; // now we know that we have a connection
//read packet
while (LoRa.available() > 0 ) { // read all packets in memory until we have 0 packets
String incoming = LoRa.readString(); // read the packet
deserializeJson(doc, incoming);
value1 = doc["v1"];
// value2 = doc["v2"];
// value3 = doc["v3"];
doc["status"] = connection;
// analog_reads = String(value1) + " " + String(value2) + " " + String(value3);
analog_reads = String(value1);
// Serial.print("Analog Val : "); // print some text
// Serial.println(analog_reads); // print the value on serial
String outgoing = "";
serializeJson(doc, outgoing);
SERIAL2.print(outgoing); // send the value to particle board via serial 2 port
Serial.println(outgoing);
}
}
OTA_handler(); // handle web server events
}