Skip to content

Instantly share code, notes, and snippets.

@qistoph
Created November 9, 2019 11:40
Show Gist options
  • Save qistoph/bfdf8601969078be9284bcf6224351bf to your computer and use it in GitHub Desktop.
Save qistoph/bfdf8601969078be9284bcf6224351bf to your computer and use it in GitHub Desktop.
Premature termination JSON output
#include <OneWire.h>
OneWire ds(D7);
byte addr[8];
byte type_s;
unsigned long readTempAfter;
float celcius = 90;
bool findTemperatureSensor() {
if(!ds.search(addr)) {
ds.reset_search();
delay(250);
Serial.println("No DS18B20 found");
return false;
}
if(OneWire::crc8(addr, 7) != addr[7]) {
Serial.println("OneWire: CRC is not valid");
return false;
}
switch(addr[0]) {
case 0x10:
type_s = 1;
break;
case 0x28:
type_s = 0;
break;
case 0x22:
type_s = 0;
break;
default:
Serial.println("Device is not a DS18x20 family device.");
for(unsigned int i=0; i<sizeof(addr); ++i) {
Serial.print(addr[i], HEX);
Serial.print(", ");
}
Serial.println();
return false;
}
return true;
}
bool readTemperature() {
ds.reset();
ds.select(addr);
ds.write(0x44, 1); // Start conversation
readTempAfter = millis() + 1000;
return true;
}
float getTemperature() {
if(readTempAfter > millis()) {
Serial.print("Wait for ");
Serial.print((signed long)(readTempAfter - millis()));
Serial.println("ms");
while(millis() <= readTempAfter) {} // Wait for measurement
}
byte present = ds.reset();
ds.select(addr);
ds.write(0xBE); // Read scratchpad
byte data[12];
for(byte i=0; i<9; ++i) {
data[i] = ds.read();
}
int16_t raw = (data[1] << 8) | data[0];
if (type_s) {
raw = raw << 9; // 9 bit resolution default
if(data[7] == 0x10) {
raw = (raw & 0xFFF0) + 12 - data[6];
}
} else {
byte cfg = (data[4] & 0x60);
if(cfg == 0x00) raw = raw & ~7; // 9 bit resolution, 93.75 ms
else if(cfg == 0x20) raw = raw & ~ 3; // 10 bit resolution, 187.5 ms
else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
}
celcius = (float)raw / 16.0;
return celcius;
}
#include <Thing.h>
#include <WebThingAdapter.h>
#include <Adafruit_MCP23008.h>
const char* ssid = "C407";
const char* password = "";
const int motionPin = D5;
extern unsigned long readTempAfter; // Defined in Temp.ino
WebThingAdapter* adapter;
Adafruit_MCP23008 mcp;
const char* ledTypes[] = {"OnOffSwitch", "Light", NULL};
ThingDevice led("led", "Built-in LED", ledTypes);
ThingProperty ledOn("on", "", BOOLEAN, "OnOffProperty");
bool lastOn = false;
const char* motionTypes[] = {"MotionSensor", NULL};
ThingDevice motion("motion", "Motion sensor", motionTypes);
ThingProperty motionDetected("motion", "", BOOLEAN, "MotionProperty");
bool lastMovement = false;
const char* tempTypes[] = {"TemperatureSensor", NULL};
ThingDevice temperatureSensor("temperature", "Temperature sensor", tempTypes);
ThingProperty temperature("temperature", "", NUMBER, "TemperatureProperty");
float lastTemp = 0.0;
const char* relaisTypes[] = {"OnOffSwitch", NULL};
ThingDevice relais0("port0", "Relais port 0", relaisTypes);
ThingProperty relaisOn0("on", "", BOOLEAN, "OnOffProperty");
ThingDevice relais1("port1", "Relais port 1", relaisTypes);
ThingProperty relaisOn1("on", "", BOOLEAN, "OnOffProperty");
ThingDevice relais2("port2", "Relais port 2", relaisTypes);
ThingProperty relaisOn2("on", "", BOOLEAN, "OnOffProperty");
ThingDevice relais3("port3", "Relais port 3", relaisTypes);
ThingProperty relaisOn3("on", "", BOOLEAN, "OnOffProperty");
ThingDevice relais4("port4", "Relais port 4", relaisTypes);
ThingProperty relaisOn4("on", "", BOOLEAN, "OnOffProperty");
ThingDevice relais5("port5", "Relais port 5", relaisTypes);
ThingProperty relaisOn5("on", "", BOOLEAN, "OnOffProperty");
ThingDevice relais6("port6", "Relais port 6", relaisTypes);
ThingProperty relaisOn6("on", "", BOOLEAN, "OnOffProperty");
ThingDevice relais7("port7", "Relais port 7", relaisTypes);
ThingProperty relaisOn7("on", "", BOOLEAN, "OnOffProperty");
ThingProperty *relaisOns[] = {
&relaisOn0,
&relaisOn1,
&relaisOn2,
&relaisOn3,
&relaisOn4,
&relaisOn5,
&relaisOn6,
&relaisOn7,
};
bool lastRelaisOns[] = {
false,
false,
false,
false,
false,
false,
false,
false,
};
void setup() {
Serial.begin(115200);
Serial.println();
Serial.print("Connecting to \"");
Serial.print(ssid);
Serial.println("\"");
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.println("");
bool blink = true;
while(WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
digitalWrite(LED_BUILTIN, blink ? LOW : HIGH);
blink = !blink;
}
digitalWrite(LED_BUILTIN, HIGH);
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
adapter = new WebThingAdapter("w25", WiFi.localIP());
// Commenting out these two lines
led.addProperty(&ledOn);
adapter->addDevice(&led);
// or these two lines make the sketch work
motion.addProperty(&motionDetected);
adapter->addDevice(&motion);
temperatureSensor.addProperty(&temperature);
adapter->addDevice(&temperatureSensor);
mcp.begin(); // Use default address (0x20)
for(byte i=0; i<8; ++i) {
mcp.pinMode(i, OUTPUT);
mcp.digitalWrite(i, HIGH); // Active low, turn off
}
relais0.addProperty(&relaisOn0);
adapter->addDevice(&relais0);
relais1.addProperty(&relaisOn1);
adapter->addDevice(&relais1);
relais2.addProperty(&relaisOn2);
adapter->addDevice(&relais2);
relais3.addProperty(&relaisOn3);
adapter->addDevice(&relais3);
relais4.addProperty(&relaisOn4);
adapter->addDevice(&relais4);
relais5.addProperty(&relaisOn5);
adapter->addDevice(&relais5);
relais6.addProperty(&relaisOn6);
adapter->addDevice(&relais6);
relais7.addProperty(&relaisOn7);
adapter->addDevice(&relais7);
adapter->begin();
Serial.println("HTTP server started");
Serial.print("http://");
Serial.print(WiFi.localIP());
Serial.println("/");
findTemperatureSensor();
readTemperature();
}
void loop() {
adapter->update();
bool on = ledOn.getValue().boolean;
digitalWrite(LED_BUILTIN, on ? LOW : HIGH);
if(on != lastOn) {
Serial.print(led.id);
Serial.print(": ");
Serial.println(on);
}
lastOn = on;
ThingPropertyValue movement;
movement.boolean = digitalRead(motionPin);
if(movement.boolean != lastMovement) {
motionDetected.setValue(movement);
}
lastMovement = movement.boolean;
for(byte i=0; i<8; ++i) {
bool on = relaisOns[i]->getValue().boolean;
if(on != lastRelaisOns[i]) {
mcp.digitalWrite(i, on ? LOW : HIGH); // The relais board has active low gates
Serial.print("Relais ");
Serial.print(i);
Serial.print(": ");
Serial.println(on);
}
lastRelaisOns[i] = on;
}
if(millis() > readTempAfter) {
ThingPropertyValue temp;
temp.number = getTemperature();
readTemperature(); // Start next reading
if(temp.number != lastTemp) {
temperature.setValue(temp);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment