Skip to content

Instantly share code, notes, and snippets.

@itpcc
Created May 29, 2016 10:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save itpcc/0c57691deb489d2037bf273890479bc5 to your computer and use it in GitHub Desktop.
Save itpcc/0c57691deb489d2037bf273890479bc5 to your computer and use it in GitHub Desktop.
#include <AntoIO.h>
#include <dht11.h>
#define DHT11PIN 12
#define GPIO15 15
dht11 DHT11;
// change ssid and password to yours
const char* ssid = "aisfibre2704_2.4G";
const char* password = "6FF56B76";
// use demo token or change to your token
const char* token = "jb6OH5RkB2DTysvKcxNfQwdFgh13pr4u9oGLPYXC";
// use demo thing or change to your thing
const char* thing = "test_1";
// use demo username or change to your username
const char *user = "itpcc";
bool bIsConnected = false;
int32_t tempInt = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
delay(10);
Serial.println("ok");
Serial.println("DHT11 TEST PROGRAM ");
Serial.print( "LIBRARY VERSION: " );
Serial.println( DHT11LIB_VERSION );
//port output
pinMode(GPIO15,OUTPUT);
// register callback functions
Anto.onConnected(connectedCB);
Anto.onDisconnected(disconnectedCB);
Anto.onMsgArrv(msgArrvCB);
Anto.onPublished(publishedCB);
// Connect to a WiFi network and MQTT broker
if (!Anto.begin(ssid, password, token, user, thing)) {
Serial.println("Connection failed!!");
while (1);
}
Serial.println("WiFi connected & Connecting to MQTT broker");
}
void loop(){
// put your main code here, to run repeatedly:
// Wait a few seconds between measurements.
delay(2000);
Serial.print("Read sensor: ");
switch (DHT11.read(DHT11PIN)){
case 0:
Serial.print("OK-> ");
Serial.print("Hud: ");
Serial.print((float)DHT11.humidity, 2);
Serial.print(" Temp: ");
Serial.print((float)DHT11.temperature, 2);
tempInt = (float)DHT11.temperature*100.0;
Serial.print(" -->"); Serial.print(tempInt);
Serial.print(Anto.requestHttp("MYSERVER_DOMAIN", "/DHT11/"+String(tempInt)+"/?millis="+String(millis())));
Serial.println("");
break;
case -1: Serial.println("Checksum error"); break;
case -2: Serial.println("Time out error"); break;
default: Serial.println("Unknown error"); break;
}
}
/*
* connectedCB(): a callback function called when the connection to the MQTT broker is establised.
*/
void connectedCB(){
// If the connection is establised, subscribe channels.
bIsConnected = true;
Serial.println("Connected to MQTT Broker");
Anto.sub("LEDtest1");
Anto.sub("DHT11_temperature");
}
/*
* disconnectedCB(): a callback function called when the connection to the MQTT broker is broken.
*/
void disconnectedCB(){
bIsConnected = false;
Serial.println("Disconnected to MQTT Broker");
}
/*
* msgArrvCB(): a callback function called when there a message from the subscribed channel.
*/
void msgArrvCB(String& topic, String& msg){
uint8_t index = topic.indexOf('/');
Serial.println("");
Serial.print(topic);
Serial.print(" -> :");
index = topic.indexOf('/', index + 1);
index = topic.indexOf('/', index + 1);
topic.remove(0, index + 1);
Serial.print(topic);
Serial.print(": ");
Serial.print(msg);
if(topic.equals("LEDtest1")){
if(msg.toInt() == 1){
Serial.println(" ===> LIGHT ON");
digitalWrite(GPIO15,HIGH);
}else{
Serial.println(" ---> LIGHT OFF");
digitalWrite(GPIO15,LOW);
}
}else{
Serial.println("");
}
}
/*
* publishedCB(): a callback function called when the message is published.
*/
void publishedCB(void){
Serial.println("published");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment