Project in progress
HC-12 433 MHz Data Logger Connected to The Thingsboard

HC-12 433 MHz Data Logger Connected to The Thingsboard © MIT

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

  • 250 views
  • 0 comments
  • 0 respects

Components and supplies

About this project

using MQTT protocol IoT with a Wemos as the gateway of HC-12 MHz radio long-range

is sent to the cloud. It was made by programming with Arduino IDE, PubSubClient, and DHT library. We can send the data from the gateway to the ThingsBoard cloud in real-time. The data can be visualized in ThingsBoard.

Arduino nano as an HC-12 transmitter sends to sensor data of DHT22 to the wemos IoT gateway. IoT Gateway collects all data which uses 433 MHz radio frequency. Then data from gateway are sent to thingsboard using MQTT.

Code

nano transmitterArduino
#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 gatewayArduino
#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;
}

Schematics

nano transmitter
Hc12 iot bb yuwb9dyno4
wemos gateway
Gateway hc12 xtiq3lhyzb

Comments

Similar projects you might like

Publish Your Arduino Data to the Cloud

by Jaume Miralles

  • 11,367 views
  • 14 comments
  • 54 respects

Get Data from the Cloud to Your Arduino

by Jaume Miralles

  • 6,885 views
  • 5 comments
  • 26 respects

Cayenne and SparkFun IoT 433 Mhz Sockets

Project tutorial by Giovanni Gentile

  • 2,790 views
  • 0 comments
  • 13 respects

OH HAI! on Windows 10 IoT Core

Project in progress by BuddyC

  • 14,911 views
  • 3 comments
  • 51 respects

Arduino Controlled Smart Hydroponic Modular System

Project in progress by Luis Antonio Martin Nuez

  • 28,642 views
  • 7 comments
  • 124 respects

IoT Thermometer Using Python

Project tutorial by Luigi Francesco Cerfeda

  • 11,421 views
  • 4 comments
  • 29 respects
Add projectSign up / Login