Andrew Reed
/
CITY1082-i2c_master_wifi_mqtt
CITY3032-wifi-mqtt
Diff: main.cpp
- Revision:
- 3:62825c5f3cd7
- Parent:
- 2:3dabedbecf5e
diff -r 3dabedbecf5e -r 62825c5f3cd7 main.cpp --- a/main.cpp Tue Feb 04 09:30:49 2020 +0000 +++ b/main.cpp Sat Nov 13 09:06:17 2021 +0000 @@ -8,53 +8,53 @@ * This module sets the PSoC 6 up as the master on the I2C bus * and periodically collects a set of environment data from an arduino * running the code below: - */ -// + */ +// // //I2C SLAVE CODE // //I2C Communication between Two Arduino // //CircuitDigest // //Pramoth.T -// +// // #include<Wire.h> //Library for I2C Communication // // functions // #include <Adafruit_Sensor.h> // #include <DHT.h> // #include <DHT_U.h> -// -// #define DHTPIN 4 // Digital pin connected to the DHT sensor +// +// #define DHTPIN 4 // Digital pin connected to the DHT sensor // // Feather HUZZAH ESP8266 note: use pins 3, 4, 5, 12, 13 or 14 -- // // Pin 15 can work but DHT must be disconnected during program upload. -// +// // // Uncomment the type of sensor in use: // #define DHTTYPE DHT11 // DHT 11 // //#define DHTTYPE DHT22 // DHT 22 (AM2302) // //#define DHTTYPE DHT21 // DHT 21 (AM2301) -// +// // // See guide for details on sensor wiring and usage: // // https://learn.adafruit.com/dht/overview -// +// // DHT_Unified dht(DHTPIN, DHTTYPE); -// +// // uint32_t delayMS; // #include <PreciseLM35.h> // #define TESTING false -// +// // const int pinLM35 = A2; // PreciseLM35 lm35(pinLM35, DEFAULT); -// -// +// +// // unsigned char txDataPacket[10]; // unsigned char rxCommandPacket[8]; // unsigned char rxCommandNum; // unsigned char rxCommand = 0; // bool SW1Pressed = 0; // bool SW2Pressed = 0; -// +// // int blueLed = 13; // int SW1Pin = 2; // int SW2Pin = 3; -// +// // void setup() -// +// // { // pinMode(SW1Pin, INPUT); // pinMode(SW2Pin, INPUT); @@ -69,7 +69,7 @@ // Wire.onReceive(receiveEvent); //Function call when Slave receives // value from master // Wire.onRequest(requestEvent); //Function call when Master request // value from Slave // } -// +// // void loop(void) // { // unsigned char SW1Debounced; @@ -77,8 +77,8 @@ // int temperature = 20; // while (1) { // if (rxCommandNum || TESTING) { -// -// +// +// // Serial.println("Slave Received Command From Master:"); //Prints in // // Serial Monitor // Serial.print(rxCommand); // Serial.print(" - "); @@ -129,86 +129,161 @@ // if (TESTING) delay(2000); // } // } -// +// // void receiveEvent (int howMany) //This Function is called // when Slave receives value from master // { for (int i = 0; i < howMany; i++) { // rxCommandPacket[i] = Wire.read(); //Used to read value // received from master and store in variable SlaveReceived // } // rxCommandNum = howMany; // rxCommand = rxCommandPacket[0]; -// } * +// } +// void requestEvent() //This Function is called when Master wants data from slave +// { +// Wire.write(txDataPacket, 10); // sends eight bytes of data to master +// SW1Pressed = 0; // Clear key presses on send to master +// SW2Pressed = 0; +// } #include "mbed.h" #include "platform/mbed_thread.h" -#include "TextLCD.h" #include "GUI.h" #include "cy8ckit_028_tft.h" - #define UNO_CMD (0x10) // Command byte to Uno #define UNO_CMD_CONF (0x01) // Configuration data #define UNO_ADDR (0x10) // LM75 address //I2C i2c(P8_1, P8_0); I2C i2c(I2C_SDA, I2C_SCL); -TextLCD_I2C lcd(&i2c, 0x7e, TextLCD::LCD16x2 /*, TextLCD::WS0010*/); // I2C exp: I2C bus, PCF8574 Slaveaddress, LCD Type -TextLCD_I2C lcd2(&i2c, 0x4e, TextLCD::LCD16x2 /*, TextLCD::WS0010*/); // I2C exp: I2C bus, PCF8574 Slaveaddress, LCD Type DigitalOut myled(LED1); +#include <MQTTClientMbedOs.h> + +//NetworkInterface* network; //Serial pc(SERIAL_TX, SERIAL_RX); Serial pc(USBTX, USBRX); +#define APP_INFO( x ) pc.printf x + volatile char TempCelsiusDisplay[] = "+abc.d C"; +int lastLightDisplay = 101; +int lthresh = 50; +int arrivedcount = 0; + +void messageArrived(MQTT::MessageData& md) +{ +// char buf[80]; + MQTT::Message &message = md.message; + APP_INFO(("Message arrived: qos %d, retained %d, dup %d, packetid %d\r\n", message.qos, message.retained, message.dup, message.id)); + APP_INFO(("Payload %.*s\r\n", message.payloadlen, (char*)message.payload)); +// lthresh = stoi((string)message.payload); +// } + arrivedcount++; + APP_INFO(("Arrived = %d\r\n",arrivedcount)); +} int main() { char data_write[8]; char data_read[10]; char buffer[120]; + int rc; /* Initialise display */ GUI_Init(); GUI_Clear(); - lcd.cls(); - lcd.setBacklight(TextLCD::LightOn); - lcd.setCursor(TextLCD::CurOff_BlkOff); - lcd2.cls(); - lcd2.setBacklight(TextLCD::LightOn); - lcd2.setCursor(TextLCD::CurOff_BlkOff); + APP_INFO(("Connecting to the network using Wifi...\r\n")); +// network = NetworkInterface::get_default_instance(); + NetworkInterface *network = NetworkInterface::get_default_instance(); + + nsapi_error_t net_status = -1; + for (int tries = 0; tries < 10; tries++) { + net_status = network->connect(); + if (net_status == NSAPI_ERROR_OK) { + break; + } else { + APP_INFO(("Unable to connect to network. Retrying...\r\n")); + } + } + + if (net_status != NSAPI_ERROR_OK) { + APP_INFO(("ERROR: Connecting to the network failed (%d)!\r\n", net_status)); +// return -1; + } - /* Configure the Temperature sensor device STLM75: - - Thermostat mode Interrupt - - Fault tolerance: 0 - */ + APP_INFO(("Connected to the network successfully. IP address: %s\n", network->get_ip_address())); + MQTTPacket_connectData data = MQTTPacket_connectData_initializer; + data.clientID.cstring = (char *)"my_thing"; + data.keepAliveInterval = 20; + data.cleansession = 1; + data.username.cstring = (char *)""; + data.password.cstring = (char *)""; +// char *host = "10.0.0.2"; + char *host = (char *)"192.168.1.174"; + int port = 1883; + TCPSocket socket; + MQTTClient client(&socket); + socket.open(network); + socket.connect(host, port); + client.connect(data); + MQTT::Message message; + sprintf(buffer, "Hello World! from My Thang\r\n"); + message.qos = MQTT::QOS0; + message.retained = false; + message.dup = false; + message.payload = (void*)buffer; + message.payloadlen = strlen(buffer)+1; + + client.publish( "mytopic/announce", message); + rc = client.subscribe("mytopic/lthresh", MQTT::QOS0, messageArrived); + if (rc) APP_INFO(("Subscription Error %d", rc)); + else APP_INFO(("Subscribed to mytopic/lthresh")); + + sprintf(buffer, "%d",lthresh); + message.payload = (void*)buffer; + message.payloadlen = strlen(buffer)+1; + client.publish("mytopic/lthresh", message); data_write[0] = UNO_CMD; data_write[1] = 0x08; GUI_SetFont(GUI_FONT_10_1); GUI_SetTextAlign(GUI_TA_LEFT); int status = i2c.write(UNO_ADDR, data_write, 2, 0); - if (status != 0) { // Error - GUI_DispStringAt("I2C Error", 0, 220); - while (1) { - myled = !myled; - ThisThread::sleep_for(200); - } - } - GUI_DispStringAt("I2C Connected", 0, 220); +// if (status != 0) { // Error +// GUI_DispStringAt("I2C Error", 0, 220); +// pc.printf("I2C connect error\n"); +// while (1) { +// myled = !myled; +// ThisThread::sleep_for(200); +// } +// } + pc.printf("I2C connected"); GUI_SetFont(GUI_FONT_20B_1); GUI_DispStringAt("Data from Arduino", 0, 0); + int lightDisplay; + int potValue; + int humidity; while (1) { + // Read Arduino data data_write[0] = UNO_CMD; i2c.write(UNO_ADDR, data_write, 2, 1); // no stop - if (i2c.read(UNO_ADDR, data_read, 10, 0)) { +// if (i2c.read(UNO_ADDR, data_read, 10, 0)) { +// GUI_SetFont(GUI_FONT_10_1); +// GUI_DispStringAt("I2C receive error ", 0, 220); +// pc.printf("I2C Rx Error\n"); +// } else + { + i2c.read(UNO_ADDR, data_read, 10, 0); + for (int i; i < 10; i++) pc.printf("%2x - ", data_read[i]); + pc.printf("\n"); GUI_SetFont(GUI_FONT_10_1); - GUI_DispStringAt("I2C receive error ", 0, 220); - } else { - GUI_SetFont(GUI_FONT_10_1); - GUI_DispStringAt("I2C Data Received ", 0, 220); + sprintf(buffer, "I2c Data Received, IP address is: %s arrived %d", network->get_ip_address(), arrivedcount); + GUI_DispStringAt(buffer, 0, 220); + pc.printf("I2C Rx data recevied\n"); // read eight bytes // pot value - msb-0, lsb-1 @@ -238,32 +313,33 @@ unsigned char SW2State = data_read[7]; //light level - int lightDisplay = (int)((int)data_read[3] << 8) | data_read[2]; + lightDisplay = (int)((int)data_read[3] << 8) | data_read[2]; //potentiometer value - int potValue = (int)((int)data_read[1] << 8) | data_read[0]; - - int humidity = (int)((int)data_read[9] << 8) | data_read[8]; + potValue = (int)((int)data_read[1] << 8) | data_read[0]; + + // humidity value + humidity = (int)((int)data_read[9] << 8) | data_read[8]; // Display result pc.printf("temp = %s, Light is %3d%%, Hum is %3d%%%s%s\n", TempCelsiusDisplay, lightDisplay, humidity, SW1State?", SW1 pressed":"", SW2State?", SW2 pressed":""); - lcd.locate(0,0); - lcd.printf("Temp Lght Hum 12"); - lcd.locate(0,1); - lcd.printf(" %2dC %2d%c %2d%c %s%s", tempval/10, lightDisplay, 0x25, humidity, 0x25, SW1State?"1":"_", SW2State?"2":"_"); - lcd2.locate(0,0); - lcd2.printf("Temp Lght Pot 12"); - lcd2.locate(0,1); - lcd2.printf(" %2dC %2d%c %2d%c %s%s", tempval/10, lightDisplay, 0x25, potValue, 0x25, SW1State?"1":"_", SW2State?"2":"_"); - sprintf(buffer,"Temp is %2dC \nLight Level is %2d%c \nPot Value is %2d%c \nHumidity is %2d%c \n%s \n%s ", tempval/10, lightDisplay, 0x25, potValue, 0x25, humidity, 0x25, SW1State?"SW1 Pressed ":"SW1 Released", SW2State?"SW2 Pressed ":"SW2 Released"); GUI_SetFont(GUI_FONT_20_1); GUI_DispStringAt(buffer, 0, 40); } + sprintf(buffer, "Light Level is %d\r\n", lightDisplay); + message.payload = (void*)buffer; + message.payloadlen = strlen(buffer)+1; + +// if (lightDisplay != lastLightDisplay) { + client.publish( "mytopic/light", message); + lastLightDisplay = lightDisplay; +// } + myled = !myled; data_write[1] = data_write[1] ^ 0x01; - thread_sleep_for(250); + thread_sleep_for(1000); } }