LowPowerLab Forum

Software support => Coding questions => Topic started by: tunkmountainman on January 28, 2019, 06:54:13 PM

Title: Item not transmiting to the gateway
Post by: tunkmountainman on January 28, 2019, 06:54:13 PM
  I am having a bit of a problem with a sketch I borrowed from John at DIY playground. The node will be mounted in my wellhouse which is  bout 100 yds from my Gateway in the house.  It will monitor Temp, Voltage, pumpAmps, and pressure from a bubbler setup to check water in the well in feet. Testing in my workshop has revealed of the four sensors the pumpAmps is not transmitting to the gateway. I think the problem is with the "if" statements. I could use a bit of help from you experts out there as to a solution.

Code: [Select]
#include <RFM69.h>
#include <SPIFlash.h>
#include <RFM69_OTA.h>
#include <SPI.h>
#include <EmonLib.h>

EnergyMonitor emon1;

#define SERIAL_BAUD    19200

#define GATEWAYID   1    // this is the node ID of your gateway (which is probably tied to a Raspberry Pi)
#define NODEID      2   // must be unique to each sensor node
#define NETWORKID   100 // every node must match the same network ID to hear messages from each other and take actions directly
#define FREQUENCY       RF69_915MHZ //Match this with the version of your Moteino! (others: RF69_433MHZ, RF69_868MHZ)
#define ENCRYPTKEY      "0000000000000000" //has to be same 16 characters/bytes on all nodes, not more not less!
#define IS_RFM69HW      //uncomment only for RFM69HW! Leave out if you have RFM69W!

#ifdef SERIAL_EN
#define DEBUG(input)   {Serial.print(input); delay(1);}
#define DEBUGln(input) {Serial.println(input); delay(1);}
#else
#define DEBUG(input);
#define DEBUGln(input);
#endif


//*******define our pins
#define temp                A3 // temp in wellhouse
#define emon1_Vrms          A1 // wellhouse voltage
#define emon1_Irms          A0 // amperage in pump feed <pump on or off>
#define waterlevel          A2 // feet of water in the well

const int numReadings = 5;
int reading = 0;
int deltaReading = 0;
int readingTotal = 0;

bool currentpumpStatus = false;
bool pumpStatus = false;

char payload[] = "123 ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char item[5] = "";
int pump;
byte Status;

RFM69 radio;
SPIFlash flash(8, 0xEF30); //WINDBOND 4MBIT flash chip on CS pin D8 (default for Moteino)


void setup() {
  Serial.begin(SERIAL_BAUD);
  pinMode(temp, INPUT);
  pinMode(emon1.Irms, INPUT);
  pinMode(waterlevel, INPUT);
  pinMode(emon1.Vrms, INPUT);

  emon1.voltage(1, 234.26, 1.7);
  emon1.current(0, 111.1);

  radio.initialize(FREQUENCY, NODEID, NETWORKID);
#ifdef IS_RFM69HW
  radio.setHighPower(); //uncomment only for RFM69HW!
#endif
  radio.encrypt(ENCRYPTKEY);

  char buff[20];
  sprintf(buff, "WellhouseMote : %d Mhz...", FREQUENCY == RF69_433MHZ ? 433 : FREQUENCY == RF69_868MHZ ? 868 : 915);
  DEBUGln(buff);
  delay(500);

}

void loop() {
  // check for messages from the home automation gateway or other Moteino nodes
  if (radio.receiveDone()) {
    DEBUG("Msg received from sender ID "); DEBUG('['); DEBUG(radio.SENDERID); DEBUG("] ");
    for (byte i = 0; i < radio.DATALEN; i++)
      DEBUG((char)radio.DATA[i]);
    DEBUGln();

    // wireless programming token check
    // DO NOT REMOVE, or this sensor node will not be wirelessly programmable any more!
    CheckForWirelessHEX(radio, flash, true);

    //first send any ACK to request
    //DEBUG("   [RX_RSSI:");DEBUG(radio.RSSI);DEBUG("]");

    if (radio.ACKRequested())
    {
      radio.sendACK();
      DEBUGln("ACK sent.");
    }
  }

  {
    emon1.calcVI(20, 2000); //calculate all. No. of half wavelengths, time-out
    float Voltage = emon1.Vrms; //extract Vrms into a varible
    float Voltage2 = Voltage / 2;
    int pump = emon1.Irms; //extract Irms into a varible
    transmitStatus(9906, Voltage2);
    transmitStatus(9907, pump);
   
    delay(1000);
  }
  ///////See if the pump is running or not/////////////
if (pump > 8){
  currentpumpStatus = true;
  if (currentpumpStatus != pumpStatus){
    pumpStatus = true;
    Serial.println(pumpStatus);
    transmitStatus(9903, pumpStatus);  // we assigned '9903' to mean dryer sensor
  }
 }
 else if (pump <= 8) {
  currentpumpStatus = false;
  if (currentpumpStatus != pumpStatus){
    pumpStatus = false;
    transmitStatus(9903, pumpStatus);
  }
}
    ///////////////Water level in well///////////////
  {
    float raw = analogRead(A2);
    float new1 = raw - 123;
    float voltage = new1 / 1023.0 * 5.0;
    float pressure = 150 / 4.0 * voltage;
    float psi = (voltage - .5) / (4.5 - .5);
    float waterlevel = psi * 2.31;
    transmitStatus(9905, waterlevel);
    delay(1000 );
  }
    /////////////////Temperature//////////////
  {
    int reading = analogRead(A3);  //TMP36 sensor
    float voltage = reading * 3.3; // converting that reading to mV, for 3.3v arduino use 3.3
    voltage /= 1024.0;
    float C = (voltage - 0.5) * 100;
    int tempF = (C * 9.0 / 5.0) + 32.0;
    transmitStatus(9904, tempF); //dont forget to update metrics file on rpi0W
    delay(1000);
  }

}
void transmitStatus(int item, int status) {
  delay(50);
  sprintf(payload, "%d:%d", item, status);
  byte buffLen = strlen(payload);
  radio.sendWithRetry(GATEWAYID, payload, buffLen);
}