void Handle (DHT& dht, const Host& host, const Packet& pckt) { Key k = pckt.GetArg<Key>(DHT_GET_KEY); pf_log[W_DHT] << "Get received with key " << k; if(dht.GetStorage()->hasKey(k)) { pf_log[W_DHT] << "Answer with data " << dht.GetStorage()->getInfo(k)->GetStr(); Packet get_ack(DHTGetAckType, dht.GetMe(), host.GetKey()); get_ack.SetArg(DHT_GET_ACK_KEY, k); get_ack.SetArg(DHT_GET_ACK_DATA, dht.GetStorage()->getInfo(k)); if(!dht.GetChimera()->Send(host, get_ack)) pf_log[W_DHT] << "Send get ACK message failed!"; return; } pf_log[W_DHT] << "Data not in cache, try to relay to a closest host."; if(!dht.GetChimera()->Route(pckt)) { pf_log[W_DHT] << "I'm the owner and the key is unknow, answer with GET_NACK."; Packet get_nack(DHTGetNAckType, dht.GetMe(), host.GetKey()); get_nack.SetArg(DHT_GET_NACK_KEY, k); if(!dht.GetChimera()->Send(host, get_nack)) pf_log[W_DHT] << "Send get NACK message failed!"; return; } pf_log[W_DHT] << "GET relayed."; }
bool ReadTempHum() { bool bRet= dht.readData(); if(bRet) { digitalWrite(PIO3_3, !digitalRead(PIO3_3)); #if DBG_PRINT_DHT printf(" Temperature: %4.2f C \n", dht._lastTemperature ); printf(" Humidity: %4.2f\n",dht._lastHumidity); printf(" Dew point: %4.2f (FastCalc: %4.2f)\r\n", dht.CalcdewPointFast(dht._lastTemperature, dht._lastHumidity)); bRet= true; } else printf("Err %i \r\n",dht._lastError); #else }
void cmd_sensor(Stream * chp, int argc, char * argv[]) { float temperature, humidity; if (dht.readData() == ERROR_NONE) { temperature = dht.ReadTemperature(CELCIUS); humidity = dht.ReadHumidity(); chp->printf("Temperature %.2fC \r\n", temperature); chp->printf("Humidity %.2f%% \r\n", humidity); chp->printf("Dew point %.2f\r\n", dht.CalcdewPoint(temperature, humidity)); } }
// MQTT : Publish our message void publishMessage() { if (mqtt.getConnectionState() != eTCS_Connected) { startMqttClient(); // Auto reconnect return; } // Read DHT22 TempAndHumidity th; if(!dht.readTempAndHumidity(th)) { return; } // Make JSON data String message = "{\"temp\":"; message += th.temp; message += ", \"humi\":"; message += th.humid; message += "}"; // publish message Serial.println("Let's publish message now!"); mqtt.publish("home/thirdroom/temp_humi", message, true); // retained message displayTemp(message); }
void Handle (DHT& dht, const Host&, const Packet& pckt) { Key k = pckt.GetArg<Key>(DHT_GET_ACK_KEY); Data *data = pckt.GetArg<Data*>(DHT_GET_ACK_DATA); pf_log[W_DHT] << "Received data with key " << k; pf_log[W_DHT] << data->GetStr(); try { dht.GetStorage()->addInfo(k, data); } catch(Storage::WrongDataType e) { pf_log[W_DHT] << "Asked to store wrong data type, data not stored."; return; } pf_log[W_DHT] << "Data stored locally."; /* Send data to upper layer. */ dht.GetArbore()->DataCallback(k, data); }
/* * Read Data from DHT. On success the green LED will blink. */ bool ReadDHT() { if(dht.readData() /*&& dht1.readData() */ ) { digitalWrite(PIO3_3, !digitalRead(PIO3_3)); // dht._lastTemperature; // dht._lastHumidity; // float fDewPoint= dht.CalcdewPointFast( dht._lastTemperature, dht._lastHumidity); return true; } //else dht._lastError; return false; }
void Handle (DHT& dht, const Host&, const Packet& pckt) { Key k = pckt.GetArg<Key>(DHT_UNPUBLISH_KEY); pf_log[W_DHT] << "Got Unpublish message for key " << k; Data *data = pckt.GetArg<Data*>(DHT_UNPUBLISH_DATA); pf_log[W_DHT] << "Data: " << data->GetStr(); try { dht.GetStorage()->removeInfo(k, data); } catch(Storage::WrongDataType e) { pf_log[W_DHT] << "Asked to remove wrong data type, abord."; return; } /* Repeat unpublish on redondancy hosts */ Packet replicate(DHTRepeatUType, dht.GetMe()); replicate.SetArg(DHT_REPEAT_U_KEY, k); replicate.SetArg(DHT_REPEAT_U_DATA, data); dht.GetChimera()->SendToNeighbours(dht.REDONDANCY, replicate); }
void RestDhtApi::dht(WebServer &server, WebServer::ConnectionType type, char *url_tail, bool tail_complete) { URLPARAM_RESULT rc; char name[32]; char value[32]; //server.httpSuccess("application/json"); server.httpSuccess(); if (type != WebServer::GET) return; if (strlen(url_tail)) { DHT dht; while (strlen(url_tail)) { rc = server.nextURLparam(&url_tail, name, 32, value, 32); String param = String(name); if (param == "pin") { String vl = value; int v = atoi(vl.c_str()); dht.setup(v); dht.getMinimumSamplingPeriod(); double hum = dht.getHumidity(); double tempC = dht.getTemperature(); double tempF = dht.toFahrenheit(tempC); Serial.println(v); Serial.print(dht.getStatusString()); Serial.print(" - "); Serial.print(hum, 1); Serial.print("% - "); Serial.print(tempC, 1); Serial.print("C - "); Serial.print(tempF, 1); Serial.println("F"); } } } }
void Handle (DHT& dht, const Host&, const Packet& pckt) { Key k = pckt.GetArg<Key>(DHT_REPEAT_P_KEY); pf_log[W_DHT] << "Got Repeat publisg message for key " << k; Data *data = pckt.GetArg<Data*>(DHT_REPEAT_P_DATA); pf_log[W_DHT] << "Data: " << data->GetStr(); try { dht.GetStorage()->addInfo(k, data); } catch(Storage::WrongDataType e) { pf_log[W_DHT] << "Asked to store wrong data type, data not stored."; return; } }
void init() { Serial.begin(SERIAL_BAUD_RATE); // 115200 by default Serial.systemDebugOutput(true); // Debug output to serial display.begin(SSD1306_SWITCHCAPVCC); display.clearDisplay(); dht.begin(); WifiStation.config(WIFI_SSID, WIFI_PWD); WifiStation.enable(true); WifiAccessPoint.enable(false); // Run our method when station was connected to AP (or not connected) WifiStation.waitConnection(connectOk, 20, connectFail); // We recommend 20+ seconds for connection timeout at start }
void query() { hash0.init_global_DHT( &hash0, cores()*16*1024 ); { V1.data = readTuples<MaterializedTupleRef_V1_0_1_2>( "test1", FLAGS_nt); V1.numtuples = FLAGS_nt; auto l_V1 = V1; on_all_cores([=]{ V1 = l_V1; }); } { V2.data = readTuples<MaterializedTupleRef_V2_0_1_2>( "test2", FLAGS_nt); V2.numtuples = FLAGS_nt; auto l_V2 = V2; on_all_cores([=]{ V2 = l_V2; }); } CompletionEvent ce1; spawn(&ce1, [=] { forall<&loop1>( V1.data, V1.numtuples, [=](int64_t i, MaterializedTupleRef_V1_0_1_2& t_000) { hash0.insert_lookup_iter_left<&loop1>(t_000.get(0), t_000, [=](MaterializedTupleRef_V2_0_1_2& t_001) { LOG(INFO) << "V1(" << i <<") : (" << t_000 << ") -> " << t_001; }); }); // end scan over V1 }); CompletionEvent ce2; spawn(&ce2, [=] { forall<&loop2>( V2.data, V2.numtuples, [=](int64_t i, MaterializedTupleRef_V2_0_1_2& t_000) { hash0.insert_lookup_iter_right<&loop2>(t_000.get(0), t_000, [=](MaterializedTupleRef_V1_0_1_2& t_001) { LOG(INFO) << "V2(" << i << ") : (" << t_000 << ") -> " << t_001; }); }); // end scan over V2 }); ce1.wait(); ce2.wait(); }
/* * Initialize the application. */ void setup() { #if DBG_RTC rtc.Ds3231Init(); // Initialize Ds3231 // WriteInitTime(); // Comment in, if you want setup the RTC TIME/Calendar // SetRTCAlarm(); // Comment in, if you want to set the Alarm1|2 #endif #if DBG_LUX bh.BH1750Init(); // Initialize BH1750 #endif #if DBG_DHT dht.DHTInit(PIO2_2, DHT22); // Use the DHT22 sensor on PIN #endif /* if(I2C::Instance()->bI2CIsInitialized) { // I2CScan I2C::Instance()->I2CScan(); // check .I2CScan_State ans .I2CScan_uAdress } */ // LED Initialize pinMode(PIO2_6, OUTPUT); // Info LED (yellow) pinMode(PIO3_3, OUTPUT); // Run LED (green) pinMode(PIO2_0, OUTPUT); // Prog LED (red) // LED Set Initial Value (ON|OFF) digitalWrite(PIO3_3, 0); // Info LED (yellow) // Will be toggled with dht function (blink on read success) digitalWrite(PIO2_6, 0); // Run LED (green) // Will be toggled with LUX function (off if Lux == 0) digitalWrite(PIO2_0, 1); // Prog LED (red) // Will be toggled with rtc function enableInterrupt(TIMER_32_0_IRQn); // Enable the timer interrupt timer32_0.begin(); // Begin using the timer timer32_0.prescaler((SystemCoreClock / 1000) - 1); // Let the timer count milliseconds timer32_0.matchMode(MAT1, RESET | INTERRUPT); // On match of MAT1, generate an interrupt and reset the timer timer32_0.match(MAT1, READ_TIMER); // Match MAT1 when the timer reaches this value (in milliseconds) timer32_0.start(); // Start now the timer }
/* * Initialize the application. */ void setup() { dht.DHTInit(PIO2_2, DHT22); // Use the DHT22 sensor on PIN //dht1.DHTInit(PIO0_7, DHT22); // Use the DHT22 sensor on PIN // LED Initialize pinMode(PIO2_6, OUTPUT); // Info LED (yellow) pinMode(PIO3_3, OUTPUT); // Run LED (green) pinMode(PIO2_0, OUTPUT); // Prog LED (red) // LED Set Initial Value (ON|OFF) digitalWrite(PIO3_3, 0); // Info LED (yellow) digitalWrite(PIO2_6, 0); // Run LED (green) // Will be toggled with dht function (blink on read success) digitalWrite(PIO2_0, 1); // Prog LED (red) enableInterrupt(TIMER_32_0_IRQn); // Enable the timer interrupt timer32_0.begin(); // Begin using the timer timer32_0.prescaler((SystemCoreClock / 1000) - 1); // Let the timer count milliseconds timer32_0.matchMode(MAT1, RESET | INTERRUPT); // On match of MAT1, generate an interrupt and reset the timer timer32_0.match(MAT1, READ_TIMER); // Match MAT1 when the timer reaches this value (in milliseconds) timer32_0.start(); // Start now the timer }
int main(int argc, char** argv) { if(argc < 2) { std::cout << "Usage: " << argv[0] << " listen_port [boostrap_host:port]" << std::endl; return EXIT_FAILURE; } srand(time(NULL)); DHT* dht = new DHT(NULL, StrToTyp<uint16_t>(argv[1])); pf_log.SetLoggedFlags("DESYNCH WARNING ERR INFO DHT", false); Scheduler::StartSchedulers(5); if(argc > 2) { Host host = hosts_list.DecodeHost(argv[2]); pf_log[W_INFO] << "Connecting to " << host; dht->GetChimera()->Join(host); } std::string s; while(std::getline(std::cin, s)) { std::string command_str = stringtok(s, " "); Key k; if(command_str.length() == 0) continue; switch(command_str[0]) { case 's': case 'S': pf_log[W_DHT] << dht->GetStorage()->GetStr(); break; case 'l': case 'L': pf_log[W_DHT] << dht->GetChimera()->GetRouting()->GetLeafset()->GetStr(); break; case 'r': case 'R': pf_log[W_DHT] << dht->GetChimera()->GetRouting()->GetRoutingTable()->GetStr(); break; case 'p': case 'P': k.MakeHash(s); pf_log[W_DHT] << "Publish " << s << " with key " << k; dht->Publish(k, s); break; case 'u': case 'U': k = stringtok(s, " "); pf_log[W_DHT] << "Unublish " << s << " with key " << k; dht->Unpublish(k, s); break; case 'g': case 'G': k = s; pf_log[W_DHT] << "Request data with key " << k; if(!dht->RequestData(k)) if(dht->GetStorage()->hasKey(k)) pf_log[W_DHT] << dht->GetStorage()->getInfo(k)->GetStr(); break; case 'q': case 'Q': return EXIT_SUCCESS; default: pf_log[W_ERR] << "Command not recognized."; } } return EXIT_SUCCESS; }