Skip to content

Instantly share code, notes, and snippets.

@dwblair
Created September 25, 2020 20:23
Show Gist options
  • Save dwblair/895fdbe3cbb5f5f1c43025230a8a5f10 to your computer and use it in GitHub Desktop.
Save dwblair/895fdbe3cbb5f5f1c43025230a8a5f10 to your computer and use it in GitHub Desktop.
#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
#include <SPI.h>
#include <RH_RF95.h>
#define RFM95_CS 18
#define RFM95_RST 14
#define RFM95_INT 26
// Change to 434.0 or other frequency, must match RX's freq!
#define RF95_FREQ 915.0
// Singleton instance of the radio driver
RH_RF95 rf95(RFM95_CS, RFM95_INT);
const char* ssid = "Temagami-EXT_EXT";
const char* password = "888888pab70";
const char* serverName = "https://edgecollective.farmos.net/farm/sensor/listener/47803f44fef9cf01489d033d2eef4424?private_key=9f6a52fb667566b828069794274fad22";
// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastTime = 0;
// Timer set to 10 minutes (600000)
//unsigned long timerDelay = 600000;
// Set timer to 90 seconds (5000)
unsigned long timerDelay = 90000;
StaticJsonDocument<200> doc;
int postcount = 0;
void setup() {
Serial.begin(115200);
while (!rf95.init()) {
Serial.println("LoRa radio init failed");
Serial.println("Uncomment '#define SERIAL_DEBUG' in RH_RF95.cpp for detailed debug info");
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);
rf95.setTxPower(23, false);
WiFi.begin(ssid, password);
Serial.println("Connecting");
while(WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to WiFi network with IP Address: ");
Serial.println(WiFi.localIP());
Serial.println("Timer set to 5 seconds (timerDelay variable), it will take 5 seconds before publishing the first reading.");
}
void loop() {
if (rf95.available())
{
// Should be a message for us now
uint8_t buf[RH_RF95_MAX_MESSAGE_LEN];
uint8_t len = sizeof(buf);
if (rf95.recv(buf, &len))
{
RH_RF95::printBuffer("Received: ", buf, len);
Serial.print("Got: ");
Serial.println((char*)buf);
Serial.print("RSSI: ");
Serial.println(rf95.lastRssi(), DEC);
doc["temp"] = 23.5;
doc["humidity"] = 50.2;
Serial.print( (timerDelay - (millis() - lastTime))/1000.);
Serial.println(" sec remain til next POST");
if ( ( (millis() - lastTime) > timerDelay) || (postcount == 0)) {
if(WiFi.status()== WL_CONNECTED){
HTTPClient http;
// Your Domain name with URL path or IP address with path
http.begin(serverName);
// Specify content-type header
//http.addHeader("Content-Type", "application/x-www-form-urlencoded");
// Data to send with HTTP POST
//String httpRequestData = "api_key=tPmAT5Ab3j7F9&sensor=BME280&value1=24.25&value2=49.54&value3=1005.14";
// Send HTTP POST request
//int httpResponseCode = http.POST(httpRequestData);
// If you need an HTTP request with a content type: application/json, use the following:
http.addHeader("Content-Type", "application/json");
//int httpResponseCode = http.POST("{\"api_key\":\"tPmAT5Ab3j7F9\",\"sensor\":\"BME280\",\"value1\":\"24.25\",\"value2\":\"49.54\",\"value3\":\"1005.14\"}");
String output;
serializeJson(doc, output);
int httpResponseCode = http.POST(output);
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
postcount=postcount+1;
// Free resources
http.end();
}
else {
Serial.println("WiFi Disconnected");
}
lastTime = millis();
}
// Send a reply
uint8_t data[] = "And hello back to you";
rf95.send(data, sizeof(data));
rf95.waitPacketSent();
Serial.println("Sent a reply");
}
else
{
Serial.println("Receive failed");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment