CWT-THC-S connection failure with heltec LoRa and thingspeak

I am trying to use an industrial soil temperature, humidity and conductivity sensor together with a LoRa heltec. one LoRa works as a transmitter and will transmit the sensor data to another LoRa that will work as a receiver and will receive the data and send it to thingspeak. For some reason I'm only getting random data and the receiver is sending blank data to thingspeak. Below are the codes:

Sender

#include <stdlib.h>
#include <HardwareSerial.h>
#include "heltec.h"

#define BAND 915E6
#define RE 14
#define DE 13

const byte inq[] = {0x01, 0x03, 0x00, 0x00, 0x00, 0x03, 0x05, 0xCB};
byte values[13];

HardwareSerial mod(2); // RX=16 , TX =17

String rssi = "RSSI --";
String packsize = "--";
String packet ;

void setup() {

  Serial.begin(115200);
  mod.begin(4800, SERIAL_8N1, 16, 17);
  pinMode(RE,OUTPUT);
  pinMode(DE,OUTPUT);
  
  Heltec.begin(true,true,true,true,true);

  Heltec.display->init();
  Heltec.display->flipScreenVertically();
  Heltec.display->setFont(ArialMT_Plain_10);
  delay(1500);
  Heltec.display->clear();
  Heltec.display->drawString(0,0,"Heltec.Lora OK!");
  Heltec.display->display();
  delay(1000);
}

void loop() {

  double val1, val2, val3;
  Hum();
  val1 = double((values[3]*256) + values[4]) / 1000;
  val2 = double((values[5]*256) + values[6]) / 10;
  val3 = double((values[7]*256) + values[8]) / 1000;

  char val1string[5];
  dtostrf(val1,3,1,val1string);

  char val2String[5];
  dtostrf(val2,3,1,val2String);

  char val3String[5];
  dtostrf(val3,3,1,val3String);

  String strUmidade (val1string);
  String strTemperatura (val2String);
  String strCondutividade (val3String);

  Serial.print("Sinal de umidade: ");
  Serial.println(val1, 6);
  Serial.print("Temperatura: ");
  Serial.print(val2, 2);
  Serial.println(" ºC");
  Serial.print("Condutividade: ");
  Serial.print(val3, 6);
  Serial.println(" ds/m");
  Serial.println();

  Heltec.display->clear();
  Heltec.display->setTextAlignment(TEXT_ALIGN_LEFT);
  Heltec.display->setFont(ArialMT_Plain_10);

  Heltec.display->drawString(0,0,"Enviando Dados:");
  Heltec.display->setFont(ArialMT_Plain_10);
  Heltec.display->drawString(0,20,"Temp: ");
  Heltec.display->drawString(35,20,strTemperatura+" °C");
  Heltec.display->drawString(0,35,"Umid: ");
  Heltec.display->drawString(35,35,strUmidade+" %");
  Heltec.display->drawString(0,50,"Cond: ");
  Heltec.display->drawString(35,50,strCondutividade+" ds/m");
  Heltec.display->display();

  LoRa.beginPacket();
  LoRa.setTxPower(14,RF_PACONFIG_PASELECT_PABOOST);
  LoRa.print(val1);
  LoRa.print(val2);
  LoRa.print(val3);
  LoRa.endPacket();
  
  digitalWrite(LED, HIGH);
  delay(1000);
  digitalWrite(LED, LOW);
  delay(1000);
}

void Hum(){  
  digitalWrite(DE, HIGH);
  digitalWrite(RE, HIGH);
  delay(10);
  
  if (mod.write(inq, sizeof(inq)) == 8) {
    digitalWrite(DE, LOW);
    digitalWrite(RE, LOW);
    for(byte i = 0; i < 13; i++) {
      values[i] = mod.read();
      Serial.print(values[i], HEX);
      Serial.print(" ");
    }
    Serial.println();       
  }
}

Receiver

#include "heltec.h"
#include <WiFi.h>

const char* ssid = "Fofinho"; // Change this to your WiFi SSID
const char* password = "Aaik1987"; // Change this to your WiFi password
String apiKey ="VS9PCKIKLMJP95S7";
const char* server = "api.thingspeak.com";

#define BAND 915E6
String rssi = "RSSI --";
String packsize = "--";
String packet ;

String umidade;
String temperatura;
String condutividade;

WiFiClient client;

void setup() {
  
 Serial.begin(115200);
 
 Heltec.begin(true,true,true,true,true);

 WiFi.begin(ssid, password);             //Start wifi connection
 Serial.print("Conectando...");
 while (WiFi.status() != WL_CONNECTED) { //Check for the connection
  delay(500);
  Serial.print(".");
 }  
 Serial.print("Conectado com o IP: ");
 Serial.println(WiFi.localIP());
 
 Heltec.display->init();
 Heltec.display->flipScreenVertically();
 Heltec.display->setFont(ArialMT_Plain_10);
 delay(1500);
 Heltec.display->clear();
 Heltec.display->drawString(0,0, "Lora OK! Conectado WIFI");
  Heltec.display->drawString(0,10,"Esperando dados...");
 Heltec.display->display();
 delay(1000);
 //LoRa.onReceive(cbk);
 LoRa.receive(); 
}

void loop() {
  
  int packetSize = LoRa.parsePacket();
  if(packetSize) { cbk(packetSize); }

  if (client.connect(server,80)) { // "184.106.153.149" or api.thingspeak.com
    String postStr = apiKey;
    postStr +="&field1=";
    postStr += String(temperatura);
    postStr +="&field2=";
    postStr += String(umidade);
    postStr +="&field3=";
    postStr += String(condutividade);
    postStr += "\r\n\r\n";

    client.print("POST /update HTTP/1.1\n");
    client.print("Host: api.thingspeak.com\n");
    client.print("Connection: close\n");
    client.print("X-THINGSPEAKAPIKEY: "+apiKey+"\n");
    client.print("Content-Type: application/x-www-form-urlencoded\n");
    client.print("Content-Length: ");
    client.print(postStr.length());
    client.print("\n\n");
    client.print(postStr);

    Serial.println("% send to Thingspeak");
  }
  
  Serial.println("Waiting…");
  // thingspeak needs minimum 15 sec delay between updates
  delay(15000);
}

void LoRaData(){
  
  Heltec.display->clear();
  Heltec.display->setTextAlignment(TEXT_ALIGN_LEFT);
  Heltec.display->setFont(ArialMT_Plain_10);
  Heltec.display->drawString(0,0,"Dados Recebidos:");
  Heltec.display->setFont(ArialMT_Plain_10);
  Heltec.display->drawStringMaxWidth(0,20,128,"Temp: " + temperatura + " °C");
  Heltec.display->drawStringMaxWidth(0,35,128,"Umid: " + umidade + " %");
  Heltec.display->drawStringMaxWidth(0,50,128,"Cond: " + condutividade + " ds/m");
  Heltec.display->display();
}

void cbk(int packetsize) {
  
  packet ="";
  packsize = String(packetsize, DEC);
  for (int i = 0; i < packetsize; i++) { 
    char c = (char)LoRa.read();
    packet += c;
    }
  //rssi = "RSSI" + String(LoRa.packetRssi(), DEC);
// Separar os valores de umidade e temperatura
  temperatura = packet.substring(0, 4);
  umidade = packet.substring(5, 9);  
  condutividade = packet.substring(10, 14); 

  LoRaData();
}

From the sensor or the Lora receiver?

Probably from the sensor because the receiver doesn't even receive this random data that it shows on the transmitter display and on the monitor serial.

In the serial monitor the hexadecimal data appears FF FF FF FF FF FF FF FF, there is probably something wrong in the code that I am not able to find out which part is the error.

Then use serial monitor and a Serial.print(sensorvalue) to find that out.

I'm using it on the transmitter and only random data appears.


Please use reply #5 and respond to it.

have you run the Heltec example programs
File>Examples>Heltec ESP32 DEV-Boards>LoRa>LoRaReceiver and LoRaSender?

You do need to go back to basics here.

Try example sketches which are plain transmit and receive functions, this will show if the basic LoRa communications is working.

It's working, I was testing it with a DHT22 and it worked perfectly, both the communication between the transmitter and the LoRa receiver and the sending of data to thingspeak

Yes, works perfectly

I will try it today

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.