Dust Sesnsor PMS5003
Dependencies: NetServices ThingSpeakEthernet mbed
Revision 6:ebbde59c5a1d, committed 2015-11-20
- Comitter:
- tsoic
- Date:
- Fri Nov 20 08:28:11 2015 +0000
- Parent:
- 5:1e77bdd1a639
- Child:
- 7:1da0a084cd69
- Commit message:
- Uploading sensor data ( Voltage divider, MAX4172, INA219) over ethernet to Thing Speak service.
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/C12832.lib Fri Nov 20 08:28:11 2015 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/users/chris/code/C12832/#7de323fa46fe
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/I2CR.lib Fri Nov 20 08:28:11 2015 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/ShockSoc/code/I2CR/#375ecd0ed73d
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/INA219.lib Fri Nov 20 08:28:11 2015 +0000 @@ -0,0 +1,1 @@ +https://developer.mbed.org/users/tsoic/code/INA219/#cdfbda214bee
--- a/RPCInterface.lib Wed Dec 15 18:03:19 2010 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -http://mbed.org/users/MichaelW/code/RPCInterface/#f15d3610ddbe
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Sensor.cpp Fri Nov 20 08:28:11 2015 +0000 @@ -0,0 +1,20 @@ +#include "Sensor.h" +#include "mbed.h" + +Sensor::Sensor(PinName sensorPin, float Koef) : sensorInput(sensorPin), K(Koef) { +//* Implementacija kontruktora kako bi spojio dani ulazni pin i koeficijent množenja sa privatnim varijablama klase Sensor +} + +void Sensor::read() { + //* Očitvanje vrijednosti analognom ulaza 10 puta i spremanje vrijednosti u float polje readVal[] + readVal[0] = sensorInput; + +} + +float Sensor::calc() { + //* Potrebno je postaviti 0 u lokalnu varijablu sum 0kako se u njoj nebi pojavili neki neočekivani brojevi + + realVal = readVal[0]; //* Računanje srednje vrijednosti + return realVal *= K; //* Vračanje stvarne vrijednosti za daljnu obradu +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Sensor.h Fri Nov 20 08:28:11 2015 +0000 @@ -0,0 +1,20 @@ +#ifndef _SENSOR_H +#define SENSOR_H +#include "mbed.h" + +class Sensor { + + public: + + Sensor(PinName, float); //* Konsturktor klase Sensor koji uzima kao argument pin na kojem je spojen senzor može biti od p15-p20 *// + //* i varijablu float kao koeficijent množenja vrijednosti dobivene ADC pretvorbom + void read(); //* Funkcija read() bez povratnog argumenta ocitava vrijednost 10 puta i spremaju u float polje + float calc(); //* Fukcija calac() raćuna srednju vrijednost readVal polja te vraća float varijablu sa stvarnom vrijednosti + //* mjernog signala + private: + + AnalogIn sensorInput; + float readVal[], realVal, K; +}; + +#endif \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ThingSpeak.cpp Fri Nov 20 08:28:11 2015 +0000 @@ -0,0 +1,48 @@ +#include "mbed.h" +#include "EthernetNetIf.h" +#include "HTTPClient.h" +#include "ThingSpeak.h" + +Serial pc(USBTX, USBRX); + +ThingSpeak::ThingSpeak(char* Key, const int Qt) : thingSpeakKey(Key) { + thingSpeakUrl = "https://api.thingspeak.com/update"; + urlBuffer[0] = 0; + fieldBuffer[0] = 0; +} + +void ThingSpeak::connect() { + pc.printf("Setting up Ethernet...\r\n"); + EthernetErr ethErr = eth.setup(); + if(ethErr){ + pc.printf("Error %d in ethernet setup.\r\n", ethErr); + //* connect(); + } + pc.printf("Ethernet setup OK\r\n"); +} + +void ThingSpeak::getIP() { + +} + +void ThingSpeak::setField(float field, int i) { + char fieldi; + char fieldShortBuff[8]; + sprintf(fieldShortBuff, "%f", field); + sprintf(&fieldi, "%i", i); + strncat(fieldBuffer, "&field", 6 ); + strncat(fieldBuffer, &fieldi , 1); + strncat(fieldBuffer, "=", 1); + strncat(fieldBuffer, fieldShortBuff , 4); +} + +void ThingSpeak::putUp() { + sprintf(urlBuffer, "%s?key=%s%s", thingSpeakUrl, thingSpeakKey, fieldBuffer); + pc.printf("URL Buffer request: %s ", urlBuffer); + res = http.get(urlBuffer, &resp); + if (res == HTTP_OK) + pc.printf(" Result :\"%s\"\r\n", resp.gets()); + else + pc.printf(" Error %d\r\n", res); + fieldBuffer[0] = 0; +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ThingSpeak.h Fri Nov 20 08:28:11 2015 +0000 @@ -0,0 +1,32 @@ +#ifndef THINGSPEAK_H +#define THINGSPEAK_H +#define HOSTNAME "mbed" +#include "mbed.h" +#include "EthernetNetIf.h" +#include "HTTPClient.h" + + +class ThingSpeak { + + public: + + ThingSpeak(char*, const int); + void connect(); + void getIP(); + void putUp(); + void setField(float, int); + private: + + char* thingSpeakUrl; + char* thingSpeakKey; + char urlBuffer[511]; + char fieldBuffer[255]; + EthernetNetIf eth; + EthernetErr ethErr; + HTTPClient http; + IpAddr ethIp; + HTTPText resp; + HTTPResult res; +}; + +#endif \ No newline at end of file
--- a/main.cpp Wed Dec 15 18:03:19 2010 +0000 +++ b/main.cpp Fri Nov 20 08:28:11 2015 +0000 @@ -1,96 +1,59 @@ #include "mbed.h" -#include "EthernetNetIf.h" -#include "HTTPClient.h" -#include "NTPClient.h" -#include "HTTPServer.h" -#include "RPCFunction.h" +#include "iostream" +#include "ThingSpeak.h" +#include "Sensor.h" +#include "C12832.h" +#include "INA219.h" -#define HOSTNAME "mbedSE" -EthernetNetIf eth(HOSTNAME); -HTTPClient http; -NTPClient ntp; -HTTPServer svr; -DigitalOut led1(LED1, "led1"); -DigitalOut led2(LED2, "led2"); -DigitalOut led3(LED3, "led3"); -DigitalOut led4(LED4, "led4"); + ThingSpeak ts("074MPWFODR7JHD1K", 6); + C12832 lcd(p5, p7, p6, p8, p11); + Sensor current(p19,4); + Sensor voltage(p20,24); + -void DoEcho(char* input, char* output); -RPCFunction Echo(&DoEcho, "echo"); - -void DoEcho(char* input, char* output) { - printf("%s\n", input); - strcpy(output, input); -} int main() { - - printf("\n\n/* Segundo Equipo NetServices library demonstration */\n"); - printf("Try starting the program with the network disconnected and then connect after a few timeouts reported\n\n"); - EthernetErr ethErr; - int count = 0; - do { - printf("Setting up %d...\n", ++count); - ethErr = eth.setup(); - if (ethErr) printf("Timeout\n", ethErr); - } while (ethErr != ETH_OK); - - printf("Connected OK\n"); - const char* hwAddr = eth.getHwAddr(); - printf("HW address : %02x:%02x:%02x:%02x:%02x:%02x\n", - hwAddr[0], hwAddr[1], hwAddr[2], - hwAddr[3], hwAddr[4], hwAddr[5]); - - IpAddr ethIp = eth.getIp(); - printf("IP address : %d.%d.%d.%d\n", ethIp[0], ethIp[1], ethIp[2], ethIp[3]); - printf("Check router DHCP table for name : %s\n", eth.getHostname()); - - printf("\nHTTPClient get...\n"); - HTTPText txt; - HTTPResult r = http.get("http://mbed.org/media/uploads/donatien/hello.txt", &txt); - if (r==HTTP_OK) { - printf("Result ok : %s\n", txt.gets()); - } else { - printf("Error %d\n", r); - } - - time_t ctTime; - ctTime = time(NULL); - printf("\nCurrent time is (UTC): %d %s\n", ctTime, ctime(&ctTime)); - printf("NTP setTime...\n"); - Host server(IpAddr(), 123, "pool.ntp.org"); - printf("Result : %d\n", ntp.setTime(server)); - - ctTime = time(NULL); - printf("\nTime is now (UTC): %d %s\n", ctTime, ctime(&ctTime)); - - printf("NTP setTime to a deliberately incorrect server...\n"); - server.setName("www.google.com"); - printf("Result : %d\n", ntp.setTime(server)); - - ctTime = time(NULL); - printf("\nTime should still be correct (UTC): %d %s\n", ctTime, ctime(&ctTime)); + INA219 ina219; + float U, I, ina[3]; + int i; + + lcd.cls(); + lcd.locate(0,3); + ts.connect(); + lcd.printf("Ethernet Connected ! \n"); + ts.getIP(); + + wait(1); - char ip[16]; - sprintf(ip, "%d.%d.%d.%d", ethIp[0], ethIp[1], ethIp[2], ethIp[3]); - -#define PORT 80 - svr.addHandler<RPCHandler>("/rpc"); - svr.bind(PORT); - printf("Server listening (port %d)\n", PORT); - printf("- LED1 should flash\n"); - printf("- type into browser to test url decode : %s/rpc/echo/run \"quoted string with <>\" or http://192.168.1.103/rpc/echo/run%%20%%22quoted%%20string%%20with%%20%%3C%%3E%%22\n", ip); - - Timer tm; - tm.start(); - - while (true) { - Net::poll(); - if (tm.read() > 0.5) { - led1 = !led1; - tm.start(); - } + while(1){ + + voltage.read(); + current.read(); + U = voltage.calc(); + I = current.calc(); + ina[0] = ina219.readRawReg(0x04); //* Current *// + ina[1] = ina219.readRawReg(0x02); //* Bus Voltage *// + ina[2] = ina219.readRawReg(0x03); //* Power *// + + lcd.cls(); + lcd.locate(0,3); + lcd.printf("V = %.2f V \n",U); + lcd.printf("I = %.2f A \n",I); + lcd.printf("P = %.2f W \n", U*I); + lcd.locate(0,14); + lcd.printf("V = %.2f V \n",ina[1]); + lcd.printf("I = %.2f A \n",ina[0]); + lcd.printf("P = %.2f W \n",ina[2]); + + i = 1; + ts.setField(U, i++); + ts.setField(I, i++); + for(int j = 0 ; j<= 3; j++) + ts.setField(ina[j], i++); + + ts.putUp(); + wait(15); } } \ No newline at end of file
--- a/mbed.bld Wed Dec 15 18:03:19 2010 +0000 +++ b/mbed.bld Fri Nov 20 08:28:11 2015 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/mbed_official/code/mbed/builds/e2ac27c8e93e +http://mbed.org/users/mbed_official/code/mbed/builds/63bcd7ba4912