ciao
Sto cercando di realizzare una comunicazione tra 2 HC12 per trasmettere e ricevere dati di temperatura e umidità
Alla fine ci sarà un master con display e micro SD e 5 slave
I parametri sono gli stessi per entrambi i moduli
Riesco a trasmettere e ricevere il numero che ho attribuito al modulo (02) e solo talvolta i dati letti dal sensore
Grazie per qualsiasi suggerimento
Master
///////////////////////////////////////////////datalogger
/* Do not power over USB. Per datasheet,
power HC12 with a supply of at least 100 mA with
a 22 uF - 1000 uF reservoir capacitor.
Upload code to two Arduinos connected to two computers.
Transceivers must be at least several meters apart to work.
*/
//sezione oled
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
// The pins for I2C are defined by the Wire-library.
// On an arduino UNO: A4(SDA), A5(SCL)
#define OLED_RESET 4 // 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);
//fine sezione oled
#include <SoftwareSerial.h>
const byte HC12RxdPin = 2; // "RXD" Pin on HC12
const byte HC12TxdPin = 3; // "TXD" Pin on HC12
const byte HC12SetPin = 6; // "SET" Pin on HC12
SoftwareSerial HC12(HC12TxdPin, HC12RxdPin); // RX nano to HC-12 TX Pin, TX nano to HC-12 RX Pin
byte incomingByte;
String readBuffer = "";
boolean HC12End = false;
void setup()
{
Serial.begin(9600); // Open serial port to computer
///////////////////////////////////////////////////////////////////////sezione oled start
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS))
{
Serial.println(F("SSD1306 allocation failed"));
for (;;); // Don't proceed, loop forever
}
display.clearDisplay();
display.setTextColor(SSD1306_WHITE); // Draw white text
display.setTextSize(1);
display.setCursor(20, 0);
display.print("STYRABAG AG");
// Show initial display buffer contents on the screen --
// the library initializes this with an Adafruit splash screen.
display.display();
delay(2000); // Pause for 2 seconds
display.clearDisplay();
/////////////////////////////////////////////////////////////////////////sezione oled fine
///////////////////////////////////////////////////////////sezione HC12 start
HC12.begin(9600); // Open serial port to HC12
Serial.println("HC12 ready:");
pinMode(HC12SetPin, OUTPUT);
digitalWrite(HC12SetPin, HIGH);
///////////////////////////////////////////////////////////sezione HC12 fine
delay(500);
}
void loop()
{
HC12.print("02"); // Send code to HC-12 remote sensor unit
delay(500);
HC12.print('\n'); // Send code to HC-12 remote sensor unit
delay(500);
Serial.println("waiting for data");
delay(500);
while (HC12.available()) // If HC-12 has data
{
incomingByte = HC12.read(); // Store each icoming byte from HC-12
readBuffer += char(incomingByte); // Add each byte to ReadBuffer string variable
Serial.print(readBuffer); // Send the data to Serial monitor
if (readBuffer == "00")
{
HC12End = true;
Serial.println("HC12End"); // Send the data to Serial monitor
}
display.clearDisplay();
}
///////////////////////////////////////////////////////////////////////sezione oled start
display.setTextColor(SSD1306_WHITE); // Draw white text
display.setTextSize(1);
display.setCursor(20, 0);
display.println("STYRABAG AG");
display.setTextSize(2); // Normal 1:1 pixel scale
display.print(readBuffer);
display.display(); // Show initial display buffer contents on the screen
/////////////////////////////////////////////////////////////////////sezione oled fine
readBuffer = "";
// delay(2000); // Pause for 2 seconds readBuffer = "";
}
Slave
///////////////////////////////////////////////datalogger
/* HC12 Send/Receive Example Program 1
By Mark J. Hughes
for AllAboutCircuits.com
Connect HC12 "RXD" pin to Arduino Digital Pin 4
Connect HC12 "TXD" pin to Arduino Digital Pin 5
Connect HC12 "Set" pin to Arduino Digital Pin 6
Do not power over USB. Per datasheet,
power HC12 with a supply of at least 100 mA with
a 22 uF - 1000 uF reservoir capacitor.
Upload code to two Arduinos connected to two computers.
Transceivers must be at least several meters apart to work.
*/
//htu21d start
#include <Wire.h>
#include "Adafruit_HTU21DF.h"
// Connect Vin to 3-5VDC
// Connect GND to ground
// Connect SCL to I2C clock pin (A5 on UNO)
// Connect SDA to I2C data pin (A4 on UNO)
Adafruit_HTU21DF htu = Adafruit_HTU21DF();
#include <SoftwareSerial.h>
float h;
float t;
const byte HC12RxdPin = 2; //HC12 "RXD" Pin on HC12
const byte HC12TxdPin = 3; //HC12 "TXD" Pin on HC12
const byte HC12SetPin = 4; //HC12 "SET" Pin on HC12
SoftwareSerial HC12(HC12TxdPin, HC12RxdPin); // RX nano to HC-12 TX Pin, TX nano to HC-12 RX Pin
String sensorNum = "02";
char incomingByte;
String readBuffer = "";
boolean HC12End = false;
void setup()
{
Serial.begin(9600); // Open serial port to computer
if (!htu.begin())
{
Serial.println("Couldn't find sensor!");
while (1);
}
/////////////////////////////////////////////////////////sezione HC12 start
HC12.begin(9600); // Open serial port to HC12
Serial.println("HC12 ready:");
pinMode(HC12SetPin, OUTPUT);
digitalWrite(HC12SetPin, HIGH);
///////////////////////////////////////////////////////////sezione HC12 fine
delay(500);
}
void loop()
{
while (HC12.available()) // If HC-12 has data
{
incomingByte = HC12.read(); // Store each icoming byte from HC-12
readBuffer += char(incomingByte); // Add each byte to ReadBuffer string variable
Serial.println(readBuffer); // Send the data to Serial monitor
// if (readBuffer == '\n')
// {
// HC12End = true;
// Serial.println("HC12End"); // Send the data to Serial monitor
// }
}
if (readBuffer == sensorNum)
{
// HC12End = false;
readBuffer = "";
t = htu.readTemperature();
h = htu.readHumidity();
Serial.print("Temp: "); Serial.print(t); Serial.print(" C");
Serial.print("\t");
Serial.print("Humidity: "); Serial.print(h); Serial.println(" \%");
delay(3000);
// check if returns are valid, if they are NaN (not a number) then something went wrong!
if (isnan(t) || isnan(h))
{
Serial.println("Failed to read from DHT");
delay(500);
}
else
{
Serial.println("Sending data");
// HC12.print("S ");
// HC12.print(sensorNum);
// HC12.print('\n');
HC12.print("h ");
HC12.println(h);
HC12.print('\n');
HC12.print("t ");
HC12.println(t);
HC12.print('\n');
HC12.print("00");
}
}
}
//void sendData(float h)
//{
// HC12.write(h);
// HC12.print("|");
// HC12.print('\n');
//
// ////sends the angle value with start marker "s" and end marker "e"
// // HC12.print("s" + angleString + "e");
//}