I've created a simple transmitter with an HC-12 and an Arduino Nano (clone), where I send the values of DHT11 and LM35 sensors to a receiver unit made only of a Wemos D1 and another HC-12.
Setup is really simple and worked fine for a couple of days (transmitter in the cellar 2 floors down, receiver in my living room, it was POSTing data to my own server correctly), then I unplugged the receiver and modified the transmitter to add a JSON library; when I plugged them back again the receiver is not getting anything anymore. Transmitter seems to be working fine, at least it prints the number of bytes written in the Serial Monitor.
I tried:
- reverting back to the working version (no JSON), but still the same
- tested each of the HC12 modules with a simple test code and AT commands, they all responds and are all set to factory configuration, like it was before actually (AT+DEFAULT)
- made another receiver with an Arduino Uno and a lcd screen - a setup that I was successfully using before changing to the Wemos. Strangely, now this setup doesn't work too...
- Switched the modules and what I got? The transmitter still transmits, the receiver doesn't receive, nothing changed.
- Tried sending a simple string "Test", still no results.
Relevant code:
Transmitter:
#include <ArduinoJson.h>
#include <DHT_U.h> // just Adafruit Library
#include <DHT.h>
#include <SoftwareSerial.h>
#define DHTTYPE DHT11 // DHT 11
SoftwareSerial HC12(10, 11); // HC-12 TX Pin, HC-12 RX Pin
int DHTPIN = 4;
int LMPIN = 5;
DHT dht(DHTPIN, DHTTYPE);
StaticJsonDocument<80> doc;
void setup() {
analogReference(INTERNAL);
Serial.begin(9600); // Serial port to computer
HC12.begin(9600); // Serial port to HC12
dht.begin();
}
void loop() {
delay(5000);
float h = dht.readHumidity();
float t = dht.readTemperature();
int reading = analogRead(LMPIN);
float lmt = reading/9.31;
doc["dht_h"] = h;
doc["dht_t"] = t;
doc["ds_t"] = lmt;
char result[50];
serializeJson(doc, result);
Serial.println(result);
int written = HC12.print(result);
Serial.println(written); // it's 39 chars
}
Receiver (all wifi sections are removed):
#include <ESP8266WiFi.h>
#include <SoftwareSerial.h>
#define STASSID "....."
#define STAPSK "....."
SoftwareSerial HC12(D13, D15); // HC-12 TX Pin, HC-12 RX Pin
const char* ssid = STASSID;
const char* password = STAPSK;
const char* host = "...........";
const uint16_t port = 3000;
void setup() {
Serial.begin(9600);
HC12.begin(9600);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500); Serial.print(".");
}
}
void loop() {
String content = HC12.readString();
Serial.println(content);
if(content.length()) {
// This will send a string to the server
Serial.println("sending data to server");
if (client.connected()) {
//....
}
}
delay(1000);
}
I'm pretty stumped by the fact that it stopped working, and that both the HC-12 modules seem to be working fine (at least they respond to all the AT commands). I even tried purging arduino from the system and reinstalling (I now have the very latest IDE release) but nothing....
Am I missing something obvious? (I've a weak electronic background, I'm a web developer by trade). Is there a way to check if data is actually sent? How can I debug these modules?
server.on
handlers instead of the painfully low-levelclient.connected
stuff.