Main Content

Results for

Afternoon Everyone, Looking for some help with my Arduino/Thingspeak sketch.

To give you an idea I have a Lora Sensor sending data to a Lora/Uno receiver, the data I receive is two strings, one for DO and one for Temp, in the parsepacket section below I convert the string to floats and use these as my fields for Thingspeak.

Currently the code allows a connection to Thingspeak, it also correctly associates the DO and Temp float variables with corresponding graphs on the channel.

However after the initial WiFi connection all that happens is zero is written to both graphs and only once, another write will not happen untill I reboot the Arduino.

I'm pretty certain that the order of my Loop is wrong.

My expected results are to write Temp and DO to Thingspeak every 30 seconds, with actual values, not 0. Currently a write happens once and Temp and DO are Zero, despite the packets being received correctly with actual values.

If I can clarify anything or better explain then please feel free to ask, I'm fairly new to this.

TIA. AC

 if true
#include <ESP8266WiFi.h>
#include <ThingSpeak.h>
#include <LoRa.h>
#include <SoftwareSerial.h>
#define nss D10
#define rst D14
#define dio0 D2
//
WiFiClient client;
//
const char* ssid = "myssid";
const char* password = "mypassword";
//
char thingSpeakAddress[] = "api.thingspeak.com";
unsigned long channelID = mychannelid;
char* readAPIKey = "myapi";
char* writeAPIKey = "myapi";
unsigned long lastTime = 0;
unsigned long timerDelay = 30000;
unsigned int dataFieldOne = 1;
unsigned int dataFieldTwo = 2;
//
float DO;
float temp;
//
void setup() {
  WiFi.mode(WIFI_STA);
  ThingSpeak.begin(client);
  LoRa.setPins(nss, rst, dio0);
  Serial.begin(115200);
  while (!Serial);
  if (!LoRa.begin(868E6)) {//914E6
    Serial.println("Starting LoRa failed!");
    while (1);
  }
}
//
void loop() {
  int packetSize = LoRa.parsePacket();
  if (WiFi.status() != WL_CONNECTED) {
    Serial.print("Attempting to connect");
    while (WiFi.status() != WL_CONNECTED) {
      WiFi.begin(ssid, password);
      delay(5000);
      if (packetSize)
      {
        Serial.print("packetSize = ");
        Serial.println(packetSize);
        parseLoRaPacket();
      }
      ThingSpeak.setField(1, DO);
      ThingSpeak.setField(2, temp);
      int x = ThingSpeak.writeFields(channelID, writeAPIKey);
//
      if (x == 200) {
        Serial.println("channel update succesful.");
      }
      else {
        Serial.println("Problem updating channel.");
      }
    }
  }
}
//
void parseLoRaPacket() {
  String lora_data;   // String as local variable
//
    while (LoRa.available())
  {
    lora_data = LoRa.readString();
    Serial.print(lora_data);
//
    int strIndex = lora_data.indexOf("TEMP: ");
    if (strIndex > -1)
    {
      int startPos = strIndex + strlen("TEMP: ");
      temp = lora_data.substring(startPos).toFloat();
      Serial.println(temp);
    }
    strIndex = lora_data.indexOf("DO: ");
    if (strIndex > -1)
    {
      int startPos = strIndex + strlen("DO: ");
      DO = lora_data.substring(startPos).toFloat();
      Serial.println(DO);
    }
  }
}
  end

Hi. I am a begginer.

I did a sample "Calculate high and low temperatures" with matlab analysis. I want to write data in a specified field in the new channel. I do not know how to do that. I already replace the empty spaces with channel ID and Api key. Then, when i try to set the field that i want to, it returns an error.

Is it right doing like I mention here:

thingSpeakWrite(writeChannelID,'Fields',[2],maxTempF,'timestamp',timeMaxTemp,'WriteKey',writeAPIKey);