In Old meets new, the 1-Wire Weather Station on the SPARK Core. (part 1) I described the motivation of the project. In Old meets new, the 1-Wire Weather Station on the SPARK Core. (part 2) the first steps of programming the SPARK Core and interfacing it to the 1-Wire bus are described. I showed how the bus was scanned, and temperature values measured, but output was only sent to the serial USB port. Not really an The Internet of Things approach. I will now first look at the possibilities to present the weather data to the outside world. In later blogposts I will go into more detail about the hardware and software for a full functioning IoT weather station.
The old days, a local Webserver
In the old days of the TINI java controller the data was presented on a web page which was hosted on the device itself using a small HTTP server. This approach is also possible on the SPARK Core. Using the webserver library which was derived from the webduino library.
Here is the code to publish the device scan on a webpage. Each time the page is refreshed, a new device scan is carried out.
// This #include statement was automatically added by the Spark IDE. #include "WebServer/WebServer.h" #include "OneWire/OneWire.h" #include "OWSensor.h" #define NUM_SENSORS 10 #define PREFIX "" WebServer webserver(PREFIX, 80); OneWire one = OneWire(D3); uint8_t resp[9]; char myIpAddress[24]; char tempfStr[16]; double temperature = 0; int test = 0; //unsigned int lastTime = 0; OWSensor sensors[NUM_SENSORS]; int checkIndex = 0; void getTempBytes(uint8_t *rom) { // Get the temp one.reset(); one.write(0x55); one.write_bytes(rom,8); one.write(0x44); delay(10); //ask for the temperature from one.reset(); one.write(0x55); one.write_bytes(rom, 8); one.write(0xBE); one.read_bytes(resp, 9); } float getTemp(char unit) { byte MSB = resp[1]; byte LSB = resp[0]; float tempRead = ((MSB << 8) | LSB); //using two's compliment if (unit == 'F') { float TemperatureSum = tempRead / 16; //Multiply by 9, then divide by 5, then add 32 float fahrenheit = ((TemperatureSum * 9) / 5) + 32; if (fahrenheit > 7000) { fahrenheit = 7404 - fahrenheit; } return fahrenheit; } else { float celcius = tempRead * 0.0625; return celcius; } } void findDevices() { uint8_t addr[12]; int found = 0; while(one.search(addr)) { webserver.print("<br>Found device: "); char *owID = new char[24]; sprintf(owID, "%02x%02x%02x%02x%02x%02x%02x%02x%02x", addr[0], addr[1], addr[2] , addr[3] , addr[4] , addr[5], addr[6], addr[7] , addr[8] ); sensors[found].id = owID; for(int i=0;i<9;i++) { sensors[found].rom[i] = addr[i]; } sensors[found].updated = 0; webserver.print(owID); if (addr[0] == 0x22) { //ds1822 temp sensor getTempBytes(sensors[found].rom); temperature = getTemp('C'); sensors[found].value = temperature; webserver.print(" Temperature: " + String(temperature)); sensors[found].updated = millis(); } found++; } } void helloCmd(WebServer &server, WebServer::ConnectionType type, char *, bool) { server.httpSuccess(); if (type != WebServer::HEAD) { P(helloMsg) = "<h1>1-Wire Weather on SPARK Core</h1>"; server.printP(helloMsg); server.print("<pre>"); findDevices(); server.print("</pre>"); } } void setup() { IPAddress myIp = WiFi.localIP(); sprintf(myIpAddress, "%d.%d.%d.%d", myIp[0], myIp[1], myIp[2], myIp[3]); // setup webserver webserver.setDefaultCommand(&helloCmd); webserver.addCommand("index.html", &helloCmd); webserver.begin(); } void loop() { char buff[64]; int len = 64; /* process incoming connections one at a time forever */ webserver.processConnection(buff, &len); }
The following web page is produced when your browser connects to the SPARK Core:
Off-course this is a very simple example, but by adding some html and javascript you can make a fancy looking weather station webpage.
But.......
Is this really what we want? I don't think so. In the old TINI days there was not much as an alternative, other than sending your measurements to an weather server. But nowadays plenty of IoT dashboards are available to present your data in nice graphs.
This also solves another problem of the approach described above. When the SPARK Core acts as an webserver, it needs an official ip address in order to give it access from the outside world. You have to make arrangements for that in your internet router, otherwise it is only available from your local network.
So next blogpost will explore the possibilities of some IoT dashboards.
Stay tuned...........
(BTW, feel free to post questions or comments)
Top Comments