Hi guys/girls,
I have the following challenge to overcome, where I don't know the correct solution for it.
The hardware are two Arduino uno's that is used as Client and Server both have a Dragino Lora shield attached to it. The communication from server to client and from client to server is working
Only on the server side there is also a click shield(MIKROE) attached to it that works on SPI that works fine with the sample code from MikroE if the Lora shield is detached.
I know that it is not possible to use the same output for chip select on SPI.
So I used Pin 10 for Lora Shield and Pin 4 for the Click Shield to convert the distance to 4-20mA
In my believe the Library from Radiohead that is used for the lora shield
should deactivate chip select pin. after it is used by it.
This is my code that I use.
#include <SPI.h>
#include <RH_RF95.h>
#include <RHSPIDriver.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
//#include <MemoryFree.h>
//#include <pgmStrToRAM.h>const int multiplier = 1000;
const float minSensorValue = 28.0;
const float maxSensorValue = 450.0;
const float minCurrent = 4 * multiplier;
const float maxCurrent = 20 * multiplier;
const float minScaledCurrent = 800; //Not sure where this value came from
const float maxScaledCurrent = 4095; //four MSB of the MCP4921 are control bits, leaving 12 for data
const unsigned long period = 499;
unsigned long startMillis;
unsigned long currentMillis;
float percentage;
float voltage;
float sensorValue;
int min_width = 6;
int num_digits_after_decimal = 2;
char databufSensor[10];
char databufBatvolt[10];
char databufPercentage[10];
float mAScaledF;
int mAScaled;
int mAScaledInt;
float mA;
byte mAScaled_Hi;
byte mAScaled_Lo;//Clickboard communication
#define MOSI 11 //
#define CLK 13 //
#define Clickboard 4 // click SHIELD SLot 1 = D10, Slot 2 = D9 (D10 is used by the Lora Shield changed to D4)//// Oled display connections
//// On an arduino UNO: A4(SDA), A5(SCL)
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels
#define OLED_RESET 5 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
//
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);//CS Lora shield Interupt pin D2
RH_RF95 rf95(10,2);void setup()
{
Serial.begin(9600);
while(!Serial);// Serial.println(getPSTR("Old way to force String to Flash")); // forced to be compiled into and read
// Serial.println(F("New way to force String to Flash")); // forced to be compiled into and read
// Serial.println(F("Free RAM = ")); //F function does the same and is now a built in library, in IDE > 1.0.0
// Serial.println(freeMemory()); // print how much RAM is available in bytes.pinMode (Clickboard, OUTPUT); pinMode (MOSI, OUTPUT); pinMode (CLK, OUTPUT);
//Initialize Slave select pin 10 and interupt pin 2
if (!rf95.init())
Serial.println("init failed");
rf95.setFrequency(868.0);
//rf95.setTxPower(23, false);//SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));}
startMillis = millis();
}void loop()
{
currentMillis = millis();
if (currentMillis - startMillis >= period) //test whether the period has elapsed
{
digitalWrite(Clickboard,HIGH);uint8_t total[3]; total[0] = 'C'; total[1] = '1'; total[2] = '\0'; rf95.send(total, 3); rf95.waitPacketSent(); //Recieve Package //Now wait for a reply uint8_t buf[RH_RF95_MAX_MESSAGE_LEN]; uint8_t len = sizeof(buf); if (rf95.waitAvailableTimeout(499)) { if (rf95.recv(buf, &len)) { size_t bufflen = sizeof(buf); char str[bufflen]; memcpy( str, buf, bufflen ); str[bufflen]; // 'str' is now a string float recievedData[2]; //Seperate recieved data char* command = strtok(str, ";"); for (int i = 0; i <= 1; i++) { recievedData[i] = atof(command); command = strtok(0, ";"); } sensorValue = recievedData[0]; voltage = recievedData[1]; percentage = ((recievedData[1]-3)/0.7)*100; } else { Serial.println("recv failed"); } } //Convert C string to float dtostrf(sensorValue,min_width,num_digits_after_decimal,databufSensor); dtostrf(voltage,min_width,num_digits_after_decimal,databufBatvolt); dtostrf(percentage,min_width,num_digits_after_decimal,databufPercentage); //The chip select pin for the Dragino shield will be handeld by the library. mA = (((sensorValue - minSensorValue) / (maxSensorValue - minSensorValue)) * (maxCurrent - minCurrent) + minCurrent); mAScaledF = (((mA - minCurrent) / (maxCurrent - minCurrent)) * (maxScaledCurrent - minScaledCurrent) + minScaledCurrent); mAScaled = (int) mAScaledF; mAScaled_Hi = (mAScaled >> 8) &0x0F; mAScaled_Hi |= 0x30; mAScaledInt = (mAScaled_Hi << 8); mAScaled_Lo = mAScaled; mAScaledInt |= mAScaled_Lo; // The standard Arduino SPI library cannot handle data larger than 8 bits // so we cannot make use of the SPI.transfer() function to send our packet. // Instead, we have to bit bang the value in using shiftOut(). // Enable the slave device digitalWrite(CLK,LOW); digitalWrite(MOSI,LOW); digitalWrite(Clickboard,LOW); //Serial.println(mAScaledInt,BIN); Serial.println(digitalRead(Clickboard)); Serial.println(digitalRead(10)); // Do this for MSBFIRST serial // shift out highbyte shiftOut(MOSI, CLK, MSBFIRST, (mAScaledInt >> 8)); // shift out lowbyte shiftOut(MOSI, CLK, MSBFIRST, mAScaledInt); // Disable the slave device digitalWrite(Clickboard,HIGH); //Write to Oled Display I2C communication display.clearDisplay(); display.setTextSize(1); // Normal 1:1 pixel scale display.setTextColor(SSD1306_WHITE); // Draw white text display.setCursor(35,0); strcat(databufBatvolt,"V"); display.println(databufBatvolt); display.setCursor(85,0); strcat(databufPercentage,"%"); display.println(databufPercentage); display.setTextSize(2);// Draw 2X-scale text display.setCursor(25,14); strcat(databufSensor,"cm"); display.println(databufSensor); display.setTextSize(1);// Draw 2X-scale text display.setCursor(0,0); display.println((float)mA / multiplier); display.setCursor(26,0); display.println("mA"); display.display(); startMillis = currentMillis; } }
What am I doing wrong because it is not sending to the Click board(4-20mA T Click).
I can not control the Click Board 4-20mA to send the mili amperage.