I was trying to connect cytron uno , cytron LoRa RFM shield and espresso litev2.0 together and upload data to the thingspeak, the data will be received from another LoRa node.
the LoRa RFM shield is attach on the cytron uno and the espresso lite v2.0 is connected like the attachment below
here is my code:
- CODE: SELECT_ALL_CODE
#include <CytronWiFiShield.h>
#include <CytronWiFiClient.h>
#include <SoftwareSerial.h>
#include <ThingSpeak.h>
#include <WiFi.h>
#include <SPI.h>
#include <RH_RF95.h>
const char *ssid = "alexoi ASUS";
const char *pass = "ooi888168";
char thingSpeakAddress[] = "api.thingspeak.com";
String APIKey = "WW85UNEM7BW0FAM3";
const int updateThingSpeakInterval;
ESP8266Client client;
WiFiServer server(80);
long lastConnectionTime = 0;
boolean lastConnected = false;
#define RFM95_CS 10
#define RFM95_RST 7
#define RFM95_INT 2
#define RF95_FREQ 915.0
RH_RF95 rf95(RFM95_CS, RFM95_INT);
byte sendLen;
char buffer[50];
SoftwareSerial esp(8, 9); // RX, TX
void setup() {
pinMode(RFM95_RST, OUTPUT);
digitalWrite(RFM95_RST, HIGH);
while (!Serial);
Serial.begin(9600);
delay(100);
Serial.println("Arduino LoRa Receiver!");
// manual reset
digitalWrite(RFM95_RST, LOW);
delay(10);
digitalWrite(RFM95_RST, HIGH);
delay(10);
while (!rf95.init()) {
Serial.println("LoRa radio init failed");
while (1);
}
Serial.println("LoRa radio init OK!");
// Defaults after init are 434.0MHz, modulation GFSK_Rb250Fd250, +13dbM
if (!rf95.setFrequency(RF95_FREQ)) {
Serial.println("setFrequency failed");
while (1);
}
Serial.print("Set Freq to: "); Serial.println(RF95_FREQ);
// Defaults after init are 434.0MHz, 13dBm, Bw = 125 kHz, Cr = 4/5, Sf = 128chips/symbol, CRC on
// The default transmitter power is 13dBm, using PA_BOOST.
// If you are using RFM95/96/97/98 modules which uses the PA_BOOST transmitter pin, then
// you can set transmitter powers from 5 to 23 dBm:
rf95.setTxPower(23, false);
esp.begin(9600);
if(!wifi.begin(8, 9))
{
esp.println(F("Error talking to shield"));
while(1);
}
esp.println(F("Start wifi connection"));
if(!wifi.connectAP(ssid, pass))
{
esp.println(F("Error connecting to WiFi"));
while(1);
}
esp.print(F("Connected to "));esp.println(wifi.SSID());
esp.println(F("IP address: "));
esp.println(wifi.localIP());
ThingSpeak.begin(client);
delay(1000);
}
void loop() {
if (rf95.available())
{
uint8_t buf[RH_RF95_MAX_MESSAGE_LEN];
uint8_t len = sizeof(buf);
if (rf95.recv(buf, &len))
{
RH_RF95::printBuffer("Received in HEX: ", buf, len);
Serial.println("Received in DEC: ");
int val = (int)strtol(buf,NULL,16);
Serial.println(val);
if (client.available()) {
char c = client.read();
Serial.print(c);
}
// Disconnect from ThingSpeak
if (!client.connected() && lastConnected) {
Serial.println("...disconnected");
Serial.println();
client.stop();
}
// Update ThingSpeak
if (!client.connected() && (millis() - lastConnectionTime > updateThingSpeakInterval)) {
updateThingSpeak("field1=" + val );
Serial.println(val);
}
lastConnected = client.connected();
}
else
{
Serial.println("Receive failed");
}
}
}
void updateThingSpeak(String tsData) {
if (client.connect(thingSpeakAddress, 80)) {
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(tsData.length());
client.print("\n\n");
client.print(tsData);
lastConnectionTime = millis();
if (client.connected()) {
Serial.println("Connecting to ThingSpeak...");
Serial.println();
}
}
}
i use software serial to connect to the espresso lite v2.0.
the code doesn't work out, is my code wrong already?
can i use serial connection for both LoRa RFM shield and espresso lite v2.0?
hope anyone know can help me out
thanks!