-
Python Listener Code
03/02/2017 at 23:23 • 0 comments#!/usr/bin/python import socket, requests from subprocess import call UDP_PORT = 12345 UDP_IP = "" sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock.bind((UDP_IP,UDP_PORT)) print "Listening on UDP port: "+str(UDP_PORT) while True: data, addr = sock.recvfrom(1024) print addr[0],data if data.find("->"): influxValueFind = data.find("->")+3 influxValue = data[influxValueFind:influxValueFind+5] influxLoc = data[data.find("(")+1:data.find(")")] r=requests.post('http://10.0.1.202:8086/write?db=mydb', 'temperature,host=ESP00000,location=%s value=%s'%(influxLoc,influxValue))
-
Arduino IDE
03/02/2017 at 23:05 • 0 comments#include <ESP8266WiFi.h> #include <WiFiUdp.h> #include <NTPClient.h> #include <OneWire.h> #include <DallasTemperature.h> #include <ArduinoOTA.h> // Temperature Stuff // Set GPIO for DS18B20 #define ONE_WIRE_BUS 2 // OneWire Instance OneWire oneWire(ONE_WIRE_BUS); // Use Dallas stuff DallasTemperature sensors(&oneWire); // Wifi Stuff const char *ssid = "*******"; // Your SSID const char *password = "*******"; // Your Password WiFiUDP ntpUDP; IPAddress ipMulti(10, 0, 1, 255); // My network happens to be 10.0.1.XXX, change to whatever your network is unsigned int portMulti = 12345; // NTP Client NTPClient timeClient(ntpUDP, -18000); // Timer instead of delay (delay messes up OTA) unsigned long previousMillis = 0; const long interval = 60000; void setup() { Serial.begin(115200); Serial.println("Initializing"); WiFi.mode(WIFI_STA); WiFi.disconnect(); delay(100); // Start temperature library sensors.begin(); // Start Wifi WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } // Start NTP timeClient.begin(); // All the OTA Stuff ArduinoOTA.onStart([]() { Serial.println("Start"); }); ArduinoOTA.onEnd([]() { Serial.println("\nEnd"); }); ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) { Serial.printf("Progress: %u%%\r", (progress / (total / 100))); }); ArduinoOTA.onError([](ota_error_t error) { Serial.printf("Error[%u]: ", error); if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed"); else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed"); else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed"); else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed"); else if (error == OTA_END_ERROR) Serial.println("End Failed"); }); ArduinoOTA.begin(); Serial.println("Ready"); Serial.print("IP address: "); Serial.println(WiFi.localIP()); } void loop() { // Timer code unsigned long currentMillis = millis(); if (currentMillis - previousMillis > interval) { previousMillis = currentMillis; getTimeandTemp(); } ArduinoOTA.handle(); } char sendudp(String sendit) { ntpUDP.beginPacket(ipMulti, portMulti); ntpUDP.println(sendit); ntpUDP.endPacket(); } void getTimeandTemp() { String str; timeClient.update(); sensors.requestTemperatures(); str = "(Downstairs) at: " + String(timeClient.getFormattedTime()) + "/" + String(timeClient.getEpochTime()) + " -> " + String(sensors.getTempFByIndex(0)) + "F";// Change the label (Downstairs) to whatever you like sendudp(str); }