Ahmad Radhy
Published © MIT

HC-12 433 MHz Data Logger Connected to The Thingsboard

Internet of Things based on HC-12 to collect real-time data

IntermediateWork in progressOver 1 day420
HC-12 433 MHz Data Logger Connected to The Thingsboard

Things used in this project

Hardware components

Arduino Nano R3
Arduino Nano R3
×1
Wemos D1 Mini
Espressif Wemos D1 Mini
×1
DHT22 Temperature Sensor
DHT22 Temperature Sensor
×1

Story

Read more

Schematics

nano transmitter

wemos gateway

Code

nano transmitter

Arduino
#include "Arduino.h"
#include <SoftwareSerial.h>
#include <ArduinoJson.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>

#define DHTPIN 5
#define DHTTYPE DHT22
boolean state = false;

#define RX 2
#define TX 3

DHT dht(DHTPIN, DHTTYPE);
SoftwareSerial hc12(RX, TX);

long lastData = 0;
float t;
float h;

void setup()
{
	pinMode(7, OUTPUT);
	pinMode(LED_BUILTIN, OUTPUT);
	Serial.begin(9600);
	hc12.begin(9600);

	if (hc12.isListening()) {

		Serial.println("OK");
		}

	else{

		Serial.println("Not OK");

		}

	digitalWrite(7, LOW);
	hc12.print("AT+DEFAULT");
	delay(100);
	digitalWrite(7, HIGH);

	dht.begin();

}


void loop()
{

	digitalWrite(LED_BUILTIN, state);

	t = dht.readTemperature();
	h = dht.readHumidity();

	if(isnan(t) || isnan(h)){
		Serial.println("Failed to read from DHT");
		return;
	}


	const size_t capacity = JSON_OBJECT_SIZE(2);
	DynamicJsonDocument doc(capacity);

	doc["temperature"] = t;
	doc["humidity"] = h;

	char attributes[250];
	serializeJson(doc, attributes);

	long now = millis();

	if (now - lastData > 3000) {

		lastData = now;
		hc12.println(attributes);
		state = not state;
	}

}

wemos gateway

Arduino
#include "Arduino.h"
#include <SoftwareSerial.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>

void initialize_HC12();
void setup_wifi();
void reconnect();

#define RX D2
#define TX D1

SoftwareSerial HC12(RX, TX);

#define TOKEN "xxxxxxxxxx"

const char* ssid = "";
const char* password = "";

const char mqtt_server[] = "demo.thingsboard.io";
const char publishTopic[] = "v1/devices/me/telemetry";

WiFiClient espClient;
PubSubClient client(espClient);

long lastData = 0;
//String payload;
//char attributes[1000];
boolean state = false;

void initialize_HC12(){


	pinMode(D3, OUTPUT);

	HC12.begin(9600);

	if (HC12.isListening()) {


		Serial.println("OK");
	}

	else{
		Serial.println("Not OK");
		}

	//Test HC-12
	digitalWrite(D3, LOW);
	HC12.write("AT+DEFAULT");
	delay(100);
	digitalWrite(D3, HIGH);

	Serial.println();
	Serial.println("Initialization done.");

}

void setup_wifi(){

	delay(10);
	Serial.println();
	Serial.println("Connecting to");
	Serial.println(ssid);
	WiFi.begin(ssid, password);

	while( WiFi.status() != WL_CONNECTED){

		delay(500);
		Serial.print(".");
	}

	randomSeed(micros());
	Serial.println("");
	Serial.println("WiFi connected");
	Serial.println("IP address: ");
	Serial.println(WiFi.localIP());
}

void reconnect(){

	while(!client.connected()){

		if (client.connect("deviceID", TOKEN, NULL)) {

			Serial.println("Connected to server");

		}

		else{

			Serial.print("Failed, rc=");
			Serial.print(client.state());
			Serial.println("try again in 5 seconds");
			delay(5000);
		}
	}


}

void setup()
{
	Serial.begin(9600);
	pinMode(LED_BUILTIN, OUTPUT);
	initialize_HC12();
	setup_wifi();
	client.setServer(mqtt_server, 1883);

}


void loop()
{

	if (!client.connected()) {

		reconnect();
	}

	client.loop();

	String payload;
	char attributes[1000];

	if(HC12.available() > 0){

		while(HC12.available() > 0){

			payload = HC12.readString();
			payload.toCharArray(attributes, 1000);
			Serial.print(attributes);
			client.publish(publishTopic, attributes);
		}

		/*payload = HC12.readString();
		payload.toCharArray(attributes, 1000);
		Serial.print(attributes);
	    client.publish(publishTopic, attributes);
	    delay(3000);*/
	}

	delay(3000);
	digitalWrite(LED_BUILTIN, state);
	state = not state;
}

Credits

Ahmad Radhy

Ahmad Radhy

13 projects • 11 followers
I'm now taking master degree in Physics Department Institut Teknologi Bandung (ITB).

Comments