IoT_hobbyist
Published © GPL3+

Arduino - Send Temperature to Web via Serial

Using Serial. println(temp) to send real-time temperature to web. Optionally, we can see daily or hourly graph of temperature in real-time.

BeginnerFull instructions provided17,570

Things used in this project

Story

Read more

Schematics

Wiring between Arduino and Sensor

1. Stack PHPoC Wifi Shield 2 or PHPoC Shield 2 on Arduino
2. Wiring like below image

Real Wiring

Code

Arduino Code - Short Sampling Period

Arduino
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Phpoc.h>
#define SAMPLE_INTERVAL 1000 // in ms

// Data wire is plugged into port 8 on the Arduino
OneWire oneWire(8);
DallasTemperature sensors(&oneWire);

unsigned long lastSampleTime;

void setup() {
	Serial.begin(9600);
	while(!Serial)
		;

	sensors.begin();
	Phpoc.begin();

	lastSampleTime = millis();
}

void loop() {
	if((millis() - lastSampleTime) > SAMPLE_INTERVAL) {
		sensors.requestTemperatures(); 
		float temp = sensors.getTempCByIndex(0);

		Serial.println(temp);

		lastSampleTime = millis();
	}
}

Arduino Code - Long Sampling Period (daily, hourly...)

Arduino
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Phpoc.h>
#define UPDATE_INTERVAL 1000 // in ms
#define SAMPLE_INTERVAL 60*60*1000 // 1 hour

// Data wire is plugged into port 8 on the Arduino
OneWire oneWire(8);
DallasTemperature sensors(&oneWire);

unsigned long lastSampleTime;
unsigned long lastUpdateTime;

float temps[100];
int index;

void setup() {
	Serial.begin(9600);
	while(!Serial)
		;
	
	sensors.begin();
	Phpoc.begin();

	lastSampleTime = millis();
	lastUpdateTime = millis();
	index = 0;
}

void loop() {
	if((millis() - lastSampleTime) > SAMPLE_INTERVAL) {
		sensors.requestTemperatures(); 
		float temp = sensors.getTempCByIndex(0);

		temps[index] = temp;

		index = (index + 1) % 100;

		lastSampleTime = millis();
	}

	if((millis() - lastUpdateTime) > UPDATE_INTERVAL) {
		for(int i = 0; i < 100; i++) {
			Serial.println(temps[(index - i + 100)%100]);
		}

		lastUpdateTime = millis();
	}
}

Credits

IoT_hobbyist
4 projects • 28 followers
Contact

Comments

Please log in or sign up to comment.